[Android] Harden mediabrowser threading management

Geoffrey Métais git at videolan.org
Tue Sep 26 17:56:56 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Sep 26 17:54:02 2017 +0200| [7ba11386a37f8896f66821f066fca07ef30b510f] | committer: Geoffrey Métais

Harden mediabrowser threading management

> https://code.videolan.org/videolan/vlc-android/commit/7ba11386a37f8896f66821f066fca07ef30b510f
---

 .../vlc/gui/browser/BaseBrowserFragment.java       | 58 ++++++++++++++--------
 1 file changed, 38 insertions(+), 20 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 008480c4f..bf5788ad3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -29,6 +29,7 @@ import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.HandlerThread;
+import android.os.Looper;
 import android.os.Message;
 import android.os.Process;
 import android.support.annotation.NonNull;
@@ -117,7 +118,10 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
     private Handler mBrowserHandler;
 
     protected void runOnBrowserThread(Runnable runnable) {
-        mBrowserHandler.post(runnable);
+        if (Looper.myLooper() == mBrowserHandler.getLooper())
+            runnable.run();
+        else
+            mBrowserHandler.post(runnable);
     }
 
     public BaseBrowserFragment() {
@@ -217,13 +221,8 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
     @Override
     public void onHiddenChanged(boolean hidden) {
         super.onHiddenChanged(hidden);
-        if (hidden || mRoot) {
-            runOnBrowserThread(new Runnable() {
-                @Override
-                public void run() {
-                    releaseBrowser();
-                }
-            });
+        if (hidden) {
+            releaseBrowser();
         } else if (mFabPlay != null) {
             mFabPlay.setImageResource(R.drawable.ic_fab_play);
             updateFab();
@@ -231,10 +230,15 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
     }
 
     private void releaseBrowser() {
-        if (mMediaBrowser != null) {
-            mMediaBrowser.release();
-            mMediaBrowser = null;
-        }
+        runOnBrowserThread(new Runnable() {
+            @Override
+            public void run() {
+                if (mMediaBrowser != null) {
+                    mMediaBrowser.release();
+                    mMediaBrowser = null;
+                }
+            }
+        });
     }
 
     public void onSaveInstanceState(Bundle outState){
@@ -409,16 +413,30 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
                         }
                     });
                 else
-                    mMediaBrowser.browse(mCurrentMedia != null ? mCurrentMedia.getUri() : Uri.parse(mMrl), getBrowserFlags());
+                    browse(mCurrentMedia != null ? mCurrentMedia.getUri() : Uri.parse(mMrl), getBrowserFlags());
             }
         });
     }
 
-    protected void initMediaBrowser(MediaBrowser.EventListener listener) {
-        if (mMediaBrowser == null)
-            mMediaBrowser = new MediaBrowser(VLCInstance.get(), listener, mBrowserHandler);
-        else
-            mMediaBrowser.changeEventListener(listener);
+    private void browse(final Uri uri, final int flags) {
+        runOnBrowserThread(new Runnable() {
+            @Override
+            public void run() {
+                mMediaBrowser.browse(uri, flags);
+            }
+        });
+    }
+
+    protected void initMediaBrowser(final MediaBrowser.EventListener listener) {
+        runOnBrowserThread(new Runnable() {
+            @Override
+            public void run() {
+                if (mMediaBrowser == null)
+                    mMediaBrowser = new MediaBrowser(VLCInstance.get(), listener, mBrowserHandler);
+                else
+                    mMediaBrowser.changeEventListener(listener);
+            }
+        });
     }
 
     @Override
@@ -646,7 +664,7 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
                         if (mw != null) {
                             if (mw.getType() == MediaWrapper.TYPE_DIR || mw.getType() == MediaWrapper.TYPE_PLAYLIST) {
                                 final Uri uri = mw.getUri();
-                                mMediaBrowser.browse(uri, mShowHiddenFiles ? MediaBrowser.Flag.ShowHiddenFiles : 0);
+                                browse(uri, mShowHiddenFiles ? MediaBrowser.Flag.ShowHiddenFiles : 0);
                                 return;
                             }
                         }
@@ -713,7 +731,7 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
 
                 if (mw != null) {
                     if (mCurrentParsedPosition < currentMediaList.size()) {
-                        mMediaBrowser.browse(mw.getUri(), 0);
+                        browse(mw.getUri(), 0);
                     } else {
                         mCurrentParsedPosition = -1;
                         currentMediaList = new ArrayList<>();



More information about the Android mailing list