[Android] Scanning service stop triggered by ML idling

Geoffrey Métais git at videolan.org
Wed May 17 12:04:19 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon May 15 16:17:13 2017 +0200| [42107a85b364a42697e516ce6f9388aecbc2519d] | committer: Geoffrey Métais

Scanning service stop triggered by ML idling

> https://code.videolan.org/videolan/vlc-android/commit/42107a85b364a42697e516ce6f9388aecbc2519d
---

 .../org/videolan/medialibrary/Medialibrary.java    |  5 ++++
 .../src/org/videolan/vlc/MediaParsingService.java  | 32 +++++++++++-----------
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 82b855d..75c86db 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -2,11 +2,13 @@ package org.videolan.medialibrary;
 
 import android.Manifest;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Environment;
 import android.support.v4.content.ContextCompat;
+import android.support.v4.content.LocalBroadcastManager;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -41,6 +43,8 @@ public class Medialibrary {
     public static final int FLAG_MEDIA_ADDED_VIDEO          = 1 << 5;
 
     private static final String extDirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
+    public static final String ACTION_IDLE = "action_idle";
+    public static final String STATE_IDLE = "state_idle";
 
     private static final MediaWrapper[] EMPTY_COLLECTION = {};
     public static final String VLC_MEDIA_DB_NAME = "/vlc_media.db";
@@ -365,6 +369,7 @@ public class Medialibrary {
     }
 
     public void onBackgroundTasksIdleChanged(boolean isIdle) {
+        LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent(ACTION_IDLE).putExtra(STATE_IDLE, isIdle));
         mIsWorking = !isIdle;
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index c487c95..ad3f8d5 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -53,7 +53,6 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
     public static final long NOTIFICATION_DELAY = 1000L;
     private PowerManager.WakeLock mWakeLock;
 
-    private volatile boolean initOngoing = false;
     private final IBinder mBinder = new LocalBinder();
     private Medialibrary mMedialibrary;
     private int mParsing = 0, mReload = 0;
@@ -64,26 +63,33 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
             new LinkedBlockingQueue<Runnable>(), VLCApplication.THREAD_FACTORY);
 
     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        boolean paused = false;
         @Override
         public void onReceive(Context context, Intent intent) {
             switch (intent.getAction()) {
                 case ACTION_PAUSE_SCAN:
                     if (mWakeLock.isHeld())
                         mWakeLock.release();
+                    paused = true;
                     mMedialibrary.pauseBackgroundOperations();
                     break;
                 case ACTION_RESUME_SCAN:
                     if (!mWakeLock.isHeld())
                         mWakeLock.acquire();
                     mMedialibrary.resumeBackgroundOperations();
+                    paused = false;
+                    break;
+                case Medialibrary.ACTION_IDLE:
+                    if (intent.getBooleanExtra(Medialibrary.STATE_IDLE, true) && !paused) {
+                        stopSelf();
+                    } else {
+                        synchronized (this) {
+                            mLastNotificationTime = 0L;
+                        }
+                        showNotification();
+                    }
                     break;
-                default:
-                    return;
-            }
-            synchronized (this) {
-                mLastNotificationTime = 0L;
             }
-            showNotification();
         }
     };
 
@@ -96,6 +102,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
         filter.addAction(ACTION_PAUSE_SCAN);
         filter.addAction(ACTION_RESUME_SCAN);
         registerReceiver(mReceiver, filter);
+        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(Medialibrary.ACTION_IDLE));
         PowerManager pm = (PowerManager) VLCApplication.getAppContext().getSystemService(Context.POWER_SERVICE);
         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
         mWakeLock.acquire();
@@ -186,7 +193,6 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
             mThreadPool.execute(new Runnable() {
                 @Override
                 public void run() {
-                    initOngoing = true;
                     boolean shouldInit = !(new File(MediaParsingService.this.getCacheDir()+Medialibrary.VLC_MEDIA_DB_NAME).exists());
                     mMedialibrary.setup();
                     if (mMedialibrary.init(MediaParsingService.this)) {
@@ -216,7 +222,6 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
                         } else if (upgrade)
                             mMedialibrary.forceParserRetry();
                     }
-                    initOngoing = false;
                 }
             });
     }
@@ -295,17 +300,13 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
     @Override
     public void onDiscoveryCompleted(String entryPoint) {
         if (BuildConfig.DEBUG) Log.d(TAG, "onDiscoveryCompleted: "+entryPoint);
-        if (!initOngoing && !mMedialibrary.isWorking())
-            stopSelf();
     }
 
     @Override
     public void onParsingStatsUpdated(int percent) {
         if (BuildConfig.DEBUG) Log.d(TAG, "onParsingStatsUpdated: "+percent);
         mParsing = percent;
-        if (mParsing == 100)
-            stopSelf();
-        else
+        if (mParsing != 100)
             showNotification();
     }
 
@@ -321,8 +322,6 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
         if (BuildConfig.DEBUG) Log.d(TAG, "onReloadCompleted "+entryPoint);
         if (TextUtils.isEmpty(entryPoint))
             --mReload;
-        if (!initOngoing && !mMedialibrary.isWorking())
-            stopSelf();
     }
 
     @Override
@@ -331,6 +330,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
         hideNotification();
         mMedialibrary.removeDeviceDiscoveryCb(this);
         unregisterReceiver(mReceiver);
+        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
         if (mWakeLock.isHeld())
             mWakeLock.release();
         super.onDestroy();



More information about the Android mailing list