Commit a2a5f2ad authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Medialibrary: Remove obsolete broadcasting

parent b0b6400f
......@@ -29,9 +29,6 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.videolan.libvlc.LibVLC;
import org.videolan.medialibrary.interfaces.AbstractMedialibrary;
......
......@@ -61,9 +61,6 @@ abstract public class AbstractMedialibrary {
public static final int ML_INIT_FAILED = 2;
public static final int ML_INIT_DB_RESET = 3;
public static final String ACTION_IDLE = "action_idle";
public static final String STATE_IDLE = "state_idle";
public static final AbstractMediaWrapper[] EMPTY_COLLECTION = {};
public static final String VLC_MEDIA_DB_NAME = "/vlc_media.db";
public static final String THUMBS_FOLDER_NAME = "/thumbs";
......@@ -384,7 +381,6 @@ abstract public class AbstractMedialibrary {
public void onBackgroundTasksIdleChanged(boolean isIdle) {
mIsWorking = !isIdle;
sRunning.postValue(mIsWorking);
LocalBroadcastManager.getInstance(sContext).sendBroadcast(new Intent(ACTION_IDLE).putExtra(STATE_IDLE, isIdle));
if (isIdle) {
synchronized (onMedialibraryReadyListeners) {
for (OnMedialibraryReadyListener listener : onMedialibraryReadyListeners) listener.onMedialibraryIdle();
......
......@@ -38,7 +38,7 @@ import android.text.TextUtils
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.*
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
......@@ -56,8 +56,10 @@ private const val NOTIFICATION_DELAY = 1000L
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, LifecycleOwner {
override val coroutineContext = Dispatchers.Main.immediate
private val dispatcher = ServiceLifecycleDispatcher(this)
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var localBroadcastManager: LocalBroadcastManager
......@@ -89,6 +91,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
@SuppressLint("WakelockTimeout")
override fun onCreate() {
dispatcher.onServicePreSuperOnCreate()
super.onCreate()
localBroadcastManager = LocalBroadcastManager.getInstance(this)
medialibrary = AbstractMedialibrary.getInstance()
......@@ -97,15 +100,20 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
filter.addAction(ACTION_PAUSE_SCAN)
filter.addAction(ACTION_RESUME_SCAN)
registerReceiver(receiver, filter)
localBroadcastManager.registerReceiver(receiver, IntentFilter(AbstractMedialibrary.ACTION_IDLE))
val pm = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VLC:MediaParsigService")
wakeLock.acquire()
if (lastNotificationTime == 5L) stopSelf()
AbstractMedialibrary.getState().observe(this, Observer<Boolean> { running ->
if (!running && !scanPaused) {
exitCommand()
}
})
}
override fun onBind(intent: Intent): IBinder? {
dispatcher.onServicePreSuperOnBind()
return binder
}
......@@ -119,6 +127,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
if (AndroidUtil.isOOrLater) forceForeground()
else if (lastNotificationTime <= 0L) lastNotificationTime = System.currentTimeMillis()
dispatcher.onServicePreSuperOnStart()
when (intent.action) {
ACTION_INIT -> {
val upgrade = intent.getBooleanExtra(EXTRA_UPGRADE, false)
......@@ -341,6 +350,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
}
override fun onDestroy() {
dispatcher.onServicePreSuperOnDestroy()
notificationActor.offer(Hide)
medialibrary.removeDeviceDiscoveryCb(this)
unregisterReceiver(receiver)
......@@ -403,12 +413,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
medialibrary.resumeBackgroundOperations()
scanPaused = false
}
AbstractMedialibrary.ACTION_IDLE -> if (intent.getBooleanExtra(AbstractMedialibrary.STATE_IDLE, true)) {
if (!scanPaused) {
exitCommand()
return
}
}
}
}
}
......@@ -421,6 +425,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
return false
}
override fun getLifecycle(): Lifecycle = dispatcher.lifecycle
companion object {
val progress = MutableLiveData<ScanProgress>()
val newStorages = MutableLiveData<MutableList<String>>()
......
......@@ -41,11 +41,8 @@ import androidx.annotation.MainThread
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.*
import androidx.lifecycle.Observer
import androidx.lifecycle.ServiceLifecycleDispatcher
import androidx.media.MediaBrowserServiceCompat
import androidx.media.session.MediaButtonReceiver
import kotlinx.coroutines.*
......@@ -1241,7 +1238,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
@MainThread
fun setVideoAspectRatio(aspect: String?) = playlistManager.player.setVideoAspectRatio(aspect)
override fun getLifecycle() = dispatcher.lifecycle!!
override fun getLifecycle(): Lifecycle = dispatcher.lifecycle
/*
* Browsing
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment