PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/service/MediaStoreObserver.kt

43 lines
1.5 KiB
Kotlin

/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.service
import android.database.ContentObserver
import android.os.Handler
class MediaStoreObserver(
private val musicService: MusicService,
private val mHandler: Handler
) : ContentObserver(mHandler), Runnable {
override fun onChange(selfChange: Boolean) {
// if a change is detected, remove any scheduled callback
// then post a new one. This is intended to prevent closely
// spaced events from generating multiple refresh calls
mHandler.removeCallbacks(this)
mHandler.postDelayed(this, REFRESH_DELAY)
}
override fun run() {
// actually call refresh when the delayed callback fires
// do not send a sticky broadcast here
musicService.handleAndSendChangeInternal(MusicService.MEDIA_STORE_CHANGED)
}
companion object {
// milliseconds to delay before calling refresh to aggregate events
private const val REFRESH_DELAY: Long = 500
}
}