Skip to content

Instantly share code, notes, and snippets.

View NielsMasdorp's full-sized avatar
🏠
Working from home

Niels Masdorp NielsMasdorp

🏠
Working from home
View GitHub Profile
typealias Province = String
object Netherlands {
private val PathParserMap = mapOf(
"Drenthe" to listOf(
PathParser().parsePathString(
"m161.06 30.326-0.77463 0.38746-0.38732 0.38746v0.38747l-1.5493 0.77492-0.38732 0.38746v0.38746l-0.38731 0.38746-0.38732 0.77492-0.38732 0.38746v0.38747l-0.77463 1.9373v0.38746 1.1624h-0.38731l-0.38732 1.5498h-0.38732l-1.1619-0.38746-0.38732 0.77492 1.162 0.77493h1.162l0.77463 0.38746-0.38732 1.1624v1.1624 0.38747l2.3239 3.4872 0.38731 0.77492 0.38732 0.77492 0.38732 0.38746-1.162 1.9373v0.38746l-1.9366 1.9373-1.5493 1.1624-1.5493-1.5498h-1.162l-1.162-0.38747-0.77463 0.38747v0.38746 0.38746l-0.77463 0.38746-1.162 1.5498-0.38731 0.38747-0.38732 0.38746-1.5493 0.77492-0.77463 0.77492-1.162 0.77492 0.38731 0.77493v0.38746l1.5493 1.5498 0.77463 0.77492 0.77463 0.77493 0.38732 0.77492h0.38731l-0.38731 0.77492-1.162 1.1624v0.38746h-0.38732l-0.38731 0.38746-1.162 0.38746-0.38732 0.38746h-0.38731v0.38746l1.162 2.3248v0.38746 0.38746l0.77463 1.1
@NielsMasdorp
NielsMasdorp / Media3Composite.kt
Last active September 6, 2022 10:14
Composite player implementation in media3
@UnstableApi
class StreamService : MediaSessionService(),
Listener, MediaSession.SessionCallback, SessionAvailabilityListener {
private lateinit var player: CompositePlayer
private lateinit var mediaSession: MediaSession
override fun onCreate() {
super.onCreate()
initialize()
@NielsMasdorp
NielsMasdorp / keyboard.md
Created May 1, 2020 10:05
Keyboard visibility events
/**
 * Fires events when keyboard visibility changes
 */
fun Activity.onKeyboardVisibilityChange(action: (visible: Boolean) -> Unit) {
    findViewById<View>(Window.ID_ANDROID_CONTENT)?.apply {
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            val rectangle = Rect(0, 0, 0, 0)
            val heightThreshold = resources.displayMetrics.density * KEYBOARD_VISIBILITY_THRESHOLD_DP
 var wasOpen = false
@NielsMasdorp
NielsMasdorp / excludetablets.md
Created April 16, 2019 11:03
Exclude tablets in Google Play
@NielsMasdorp
NielsMasdorp / ElevationAppBarLayout.md
Created November 22, 2018 12:05
AppBarLayout that sets elevation when scrolling view is scrolling and no elevation when scrolling view is at the top
/**
 * [AppBarLayout] that monitors behavior of a scrolling view in the same layout and changes elevation on its Toolbar
 *
 * Usage:
 *
 * <ElevationAppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp"
@NielsMasdorp
NielsMasdorp / kiosk.md
Created October 17, 2018 09:30
Use Kiosk mode in Android to lock a single app

Add a admin receiver to your project

class AdminReceiver: DeviceAdminReceiver()

Add the receiver to the AndroidManifest.xml

<receiver
  android:name=".AdminReceiver"
  android:label="@string/app_name"
@NielsMasdorp
NielsMasdorp / layoutcontainer.md
Created October 15, 2018 13:32
Use Kotlin Android extensions to access views inside `RecyclerView` `ViewHolder`s
class SomeViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {

    fun bind(data: Data) {
        myFirstTextView.text = data.foo
    }
}
@NielsMasdorp
NielsMasdorp / delayedaction.md
Created September 26, 2018 13:36
RxJava2: Have an action (like a loading spinner) be invoked when an observable takes a long time and not instantaneously
/**
 * Invoke some action when the observable does not emit an item for longer than a given time out
 * @param timeout time out
 * @param timeUnit the unit for the time out
 * @param scheduler the scheduler on which the action must be ran
 * @param action the action to invoke
 */
fun <T> Observable<T>.startAfterDelay(timeout: Long, timeUnit: TimeUnit, scheduler: Scheduler, action: () -> Unit): Observable<T> {
 return this.publish { o -&gt;
@NielsMasdorp
NielsMasdorp / touchdelegate.md
Created September 11, 2018 15:40
Increase touchable area of view without increasing its size
private fun changeTouchableAreaOfView(view: View, extraSpace: Int) {
    val parent = view.parent as View
    parent.post {
        val touchableArea = Rect()
        button.getHitRect(touchableArea)
        touchableArea.top -= extraSpace
        touchableArea.bottom += extraSpace
        touchableArea.left -= extraSpace
 touchableArea.right += extraSpace
@NielsMasdorp
NielsMasdorp / highorderviews.md
Created June 27, 2018 09:47
Example of using Kotlin higher order functions to create views in a ViewPager
class FooFragment : DialogFragment() {

    private val viewPagerAdapter by lazy { BarViewPager() }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_foo, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {