[Android] MediaLibrary: allow an ongoing scan to be restarted

Ludovic Fauvet git at videolan.org
Mon Oct 1 16:03:53 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Mon Oct  1 15:44:56 2012 +0200| [2ccbed5925711025aadeafecb589b085d20c0c64] | committer: Ludovic Fauvet

MediaLibrary: allow an ongoing scan to be restarted

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=2ccbed5925711025aadeafecb589b085d20c0c64
---

 vlc-android/src/org/videolan/vlc/MediaLibrary.java |   40 +++++++++++++++++++-
 .../src/org/videolan/vlc/gui/MainActivity.java     |    4 +-
 2 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index 5e9fda8..775b62b 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -38,6 +38,7 @@ import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Environment;
 import android.os.Handler;
+import android.os.Message;
 import android.preference.PreferenceManager;
 import android.util.Log;
 
@@ -50,6 +51,8 @@ public class MediaLibrary {
     private final ArrayList<Media> mItemList;
     private final ArrayList<Handler> mUpdateHandler;
     private boolean isStopping = false;
+    private boolean mRestart = false;
+    private Context mRestartContext;
     protected Thread mLoadingThread;
 
     private MediaLibrary(Context context) {
@@ -58,6 +61,17 @@ public class MediaLibrary {
         mUpdateHandler = new ArrayList<Handler>();
     }
 
+    public void loadMediaItems(Context context, boolean restart) {
+        if (restart && isWorking()) {
+            /* do a clean restart if a scan is ongoing */
+            mRestart = true;
+            isStopping = true;
+            mRestartContext = context;
+        } else {
+            loadMediaItems(context);
+        }
+    }
+
     public void loadMediaItems(Context context) {
         if (mLoadingThread == null || mLoadingThread.getState() == State.TERMINATED) {
             isStopping = false;
@@ -246,7 +260,7 @@ public class MediaLibrary {
                     }
 
                     // Filter the extensions and the folders
-                    try { 
+                    try {
                         if ((f = dir.listFiles(mediaFileFilter)) != null) {
                             for (File file : f) {
                                 if (file.isFile()) {
@@ -318,11 +332,33 @@ public class MediaLibrary {
 
                 VideoListFragment.actionScanStop(mContext);
 
-                mContext = null;
+                if (mRestart) {
+                    Log.d(TAG, "Restarting scan");
+                    mRestart = false;
+                    restartHandler.sendEmptyMessage(1);
+                } else {
+                    mRestartContext = null;
+                    mContext = null;
+                }
             }
         }
     };
 
+    private Handler restartHandler = new Handler() {
+        @Override
+        public void handleMessage(final Message msgs) {
+            restartHandler.postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    if (mRestartContext != null)
+                        loadMediaItems(mRestartContext);
+                    else
+                        Log.e(TAG, "Context lost in a black hole");
+                }
+            }, 200);
+        }
+    };
+
     /**
      * Filters all irrelevant files
      */
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 64f754a..8172dab 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -309,7 +309,7 @@ public class MainActivity extends SherlockFragmentActivity {
 
         /* Load media items from database and storage */
         if (mScanNeeded)
-            MediaLibrary.getInstance(this).loadMediaItems(this);
+            MediaLibrary.getInstance(this).loadMediaItems(this, true);
 
         super.onResume();
     }
@@ -443,7 +443,7 @@ public class MainActivity extends SherlockFragmentActivity {
                 else if(mCurrentFragment.equals("history"))
                     ((HistoryFragment) getFragment(mCurrentFragment)).refresh();
                 else
-                    MediaLibrary.getInstance(this).loadMediaItems(this);
+                    MediaLibrary.getInstance(this).loadMediaItems(this, true);
                 break;
             // Open MRL
             case R.id.ml_menu_open_mrl:



More information about the Android mailing list