[Android] Update MediaLib at every external device (un)mount

Geoffrey Métais git at videolan.org
Fri Jun 12 17:03:32 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun 12 16:57:28 2015 +0200| [44c3f743cbe16149cb704ec04ee8384340c15f4b] | committer: Geoffrey Métais

Update MediaLib at every external device (un)mount

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

 .../org/videolan/vlc/gui/tv/MainTvActivity.java    |   45 ++++++++++++++++++--
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index 72fd377..0a64ed2 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -39,6 +39,7 @@ import org.videolan.vlc.interfaces.IVideoBrowser;
 import org.videolan.vlc.gui.video.VideoListHandler;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.VLCInstance;
+import org.videolan.vlc.util.WeakHandler;
 
 import android.app.Activity;
 import android.app.FragmentManager;
@@ -53,6 +54,7 @@ import android.net.NetworkInfo;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Message;
 import android.support.v17.leanback.app.BackgroundManager;
 import android.support.v17.leanback.app.BrowseFragment;
 import android.support.v17.leanback.widget.ArrayObjectAdapter;
@@ -164,8 +166,14 @@ public class MainTvActivity extends Activity implements IVideoBrowser, OnItemVie
         if (mMediaLibrary.isWorking()) //Display UI while MediaLib is scanning
             updateList();
         //Handle network connection state
-        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
-        registerReceiver(networkReceiver, filter);
+        IntentFilter networkfilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
+
+        IntentFilter storageFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
+        storageFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
+        storageFilter.addDataScheme("file");
+
+        registerReceiver(externalDevicesReceiver, networkfilter);
+        registerReceiver(externalDevicesReceiver, storageFilter);
     }
 
     protected void onPause() {
@@ -176,7 +184,7 @@ public class MainTvActivity extends Activity implements IVideoBrowser, OnItemVie
         if (sThumbnailer != null)
             sThumbnailer.setVideoBrowser(null);
         mBarrier.reset();
-        unregisterReceiver(networkReceiver);
+        unregisterReceiver(externalDevicesReceiver);
     }
 
     @Override
@@ -420,7 +428,7 @@ public class MainTvActivity extends Activity implements IVideoBrowser, OnItemVie
         return sThumbnailer;
     }
 
-    private final BroadcastReceiver networkReceiver = new BroadcastReceiver() {
+    private final BroadcastReceiver externalDevicesReceiver = new BroadcastReceiver() {
         boolean connected = true;
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -441,7 +449,36 @@ public class MainTvActivity extends Activity implements IVideoBrowser, OnItemVie
                     updateList();
                 }
 
+            } else if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED)) {
+                mStorageHandlerHandler.sendEmptyMessage(ACTION_MEDIA_MOUNTED);
+            } else if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED)) {
+                mStorageHandlerHandler.sendEmptyMessageDelayed(ACTION_MEDIA_UNMOUNTED, 100); //Delay to cancel it in case of MOUNT
             }
         }
     };
+
+    Handler mStorageHandlerHandler = new FileBrowserFragmentHandler(this);
+
+    private static final int ACTION_MEDIA_MOUNTED = 1337;
+    private static final int ACTION_MEDIA_UNMOUNTED = 1338;
+
+    private static class FileBrowserFragmentHandler extends WeakHandler<MainTvActivity> {
+
+        public FileBrowserFragmentHandler(MainTvActivity owner) {
+            super(owner);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            super.handleMessage(msg);
+
+            switch (msg.what){
+                case ACTION_MEDIA_MOUNTED:
+                    removeMessages(ACTION_MEDIA_UNMOUNTED);
+                case ACTION_MEDIA_UNMOUNTED:
+                    getOwner().mMediaLibrary.loadMediaItems(true);
+                    break;
+            }
+        }
+    }
 }



More information about the Android mailing list