[Android] Detect USB/SD insertion or removal

Geoffrey Métais git at videolan.org
Mon Jun 15 17:12:53 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jun 15 17:12:25 2015 +0200| [42b8085aa61688ed1940a556076ed587cd44910e] | committer: Geoffrey Métais

Detect USB/SD insertion or removal

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

 .../vlc/gui/browser/BaseBrowserFragment.java       |   25 ++++++++--
 .../vlc/gui/browser/FileBrowserFragment.java       |   51 +++++++++++++++++---
 2 files changed, 64 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
index e4a34e4..9667ebf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -147,7 +147,12 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
 
     public void onStop(){
         super.onStop();
-        mMediaBrowser.release();
+        releaseBrowser();
+    }
+
+    private void releaseBrowser() {
+        if (mMediaBrowser != null)
+            mMediaBrowser.release();
     }
 
     public void onSaveInstanceState(Bundle outState){
@@ -218,6 +223,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
 
     @Override
     public void onBrowseEnd() {
+        releaseBrowser();
         mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
         if (mReadyToDisplay)
             display();
@@ -260,9 +266,13 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
     }
 
     protected void update(){
+        update(false);
+    }
+
+    protected void update(boolean force){
         if (mReadyToDisplay) {
             updateEmptyView();
-            if (mAdapter.isEmpty()) {
+            if (force || mAdapter.isEmpty()) {
                 refresh();
             } else {
                 updateDisplay();
@@ -285,7 +295,10 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
     @Override
     public void refresh() {
         mAdapter.clear();
-        mMediaBrowser.changeEventListener(this);
+        if (mMediaBrowser == null)
+            mMediaBrowser = new MediaBrowser(VLCInstance.get(), this);
+        else
+            mMediaBrowser.changeEventListener(this);
         mCurrentParsedPosition = 0;
         if (mRoot)
             browseRoot();
@@ -475,7 +488,10 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         if (mCurrentParsedPosition == -1 || mAdapter.isEmpty())
             return;
         mMediaLists.clear();
-        mMediaBrowser.changeEventListener(mFoldersBrowserListener);
+        if (mMediaBrowser == null)
+            mMediaBrowser = new MediaBrowser(VLCInstance.get(), mFoldersBrowserListener);
+        else
+            mMediaBrowser.changeEventListener(mFoldersBrowserListener);
         mCurrentParsedPosition = 0;
         Object item;
         MediaWrapper mw;
@@ -545,6 +561,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
                     mMediaBrowser.browse(mw.getUri());
                 } else {
                     mCurrentParsedPosition = -1;
+                    releaseBrowser();
                 }
             }
             directories .clear();
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 8a7434f..2d8de86 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -30,6 +30,8 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.support.v4.app.Fragment;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.widget.AppCompatEditText;
@@ -39,11 +41,13 @@ import android.view.MenuItem;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.MediaDatabase;
+import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.CustomDirectories;
 import org.videolan.vlc.util.Util;
+import org.videolan.vlc.util.WeakHandler;
 
 import java.io.File;
 
@@ -127,8 +131,7 @@ public class FileBrowserFragment extends BaseBrowserFragment {
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
         filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
-        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
-        filter.addAction(Intent.ACTION_MEDIA_EJECT);
+        filter.addDataScheme("file");
         getActivity().registerReceiver(storageReceiver, filter);
         if (mReadyToDisplay)
             update();
@@ -149,6 +152,12 @@ public class FileBrowserFragment extends BaseBrowserFragment {
             mAlertDialog.dismiss();
     }
 
+    private void stopBackgroundTasks() {
+        MediaLibrary ml = MediaLibrary.getInstance();
+        if (ml.isWorking())
+            ml.stop();
+    }
+
     public void showAddDirectoryDialog() {
         final Context context = getActivity();
         AlertDialog.Builder builder = new AlertDialog.Builder(context);
@@ -206,13 +215,39 @@ public class FileBrowserFragment extends BaseBrowserFragment {
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
 
-            if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED) ||
-                    action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED) ||
-                    action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED) ||
-                    action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) {
-                if (mReadyToDisplay)
-                    update();
+            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);
             }
         }
     };
+
+    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<FileBrowserFragment> {
+
+        public FileBrowserFragmentHandler(FileBrowserFragment owner) {
+            super(owner);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            super.handleMessage(msg);
+
+            switch (msg.what){
+                case ACTION_MEDIA_MOUNTED:
+                    removeMessages(ACTION_MEDIA_UNMOUNTED);
+                    getOwner().update(true);
+                    break;
+                case ACTION_MEDIA_UNMOUNTED:
+                    getOwner().stopBackgroundTasks();
+                    getOwner().update(true);
+                    break;
+            }
+        }
+    }
 }



More information about the Android mailing list