[Android] Factorize actionMode code and prevent NPE

Geoffrey Métais git at videolan.org
Fri Jan 20 15:21:41 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jan 20 15:21:10 2017 +0100| [9ace9c3fc7a9aa7c4c690cb409b2564a5c112fdf] | committer: Geoffrey Métais

Factorize actionMode code and prevent NPE

> https://code.videolan.org/videolan/vlc-android/commit/9ace9c3fc7a9aa7c4c690cb409b2564a5c112fdf
---

 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    | 25 ++++++----------------
 .../vlc/gui/audio/AudioBrowserFragment.java        | 17 +--------------
 .../videolan/vlc/gui/audio/BaseAudioBrowser.java   | 20 ++++++++++-------
 3 files changed, 19 insertions(+), 43 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
index 35ec13a..f3219f1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -286,7 +286,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mFastScroller.setRecyclerView((RecyclerView) mLists[mViewPager.getCurrentItem()]);
+                        mFastScroller.setRecyclerView(mLists[mViewPager.getCurrentItem()]);
                         mSwipeRefreshLayout.setRefreshing(false);
                     }
                 });
@@ -333,7 +333,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
     @Override
     public void onCtxClick(View anchor, final int position, final MediaLibraryItem mediaItem) {
         if (mActionMode == null)
-            ((ContextMenuRecyclerView)mLists[mViewPager.getCurrentItem()]).openContextMenu(position);
+            mLists[mViewPager.getCurrentItem()].openContextMenu(position);
     }
 
     @Override
@@ -341,18 +341,18 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
 
     @Override
     public void onTabSelected(TabLayout.Tab tab) {
-        mFastScroller.setRecyclerView((RecyclerView) mLists[tab.getPosition()]);
+        mFastScroller.setRecyclerView(mLists[tab.getPosition()]);
     }
 
     @Override
     public void onTabUnselected(TabLayout.Tab tab) {
         stopActionMode();
-        onDestroyActionMode(tab.getPosition());
+        onDestroyActionMode((AudioBrowserAdapter) mLists[tab.getPosition()].getAdapter());
     }
 
     @Override
     public void onTabReselected(TabLayout.Tab tab) {
-        ((RecyclerView)mLists[tab.getPosition()]).smoothScrollToPosition(0);
+        mLists[tab.getPosition()].smoothScrollToPosition(0);
     }
 
     protected AudioBrowserAdapter getCurrentAdapter() {
@@ -360,23 +360,10 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
     }
 
     private ContextMenuRecyclerView getCurrentRV() {
-        return (ContextMenuRecyclerView)mLists[mViewPager.getCurrentItem()];
+        return mLists[mViewPager.getCurrentItem()];
     }
 
     protected boolean songModeSelected() {
         return mViewPager.getCurrentItem() == MODE_SONG;
     }
-
-    public void onDestroyActionMode(int position) {
-        mActionMode = null;
-        AudioBrowserAdapter adapter = (AudioBrowserAdapter) ((ContextMenuRecyclerView)mLists[position]).getAdapter();
-        MediaLibraryItem[] items = adapter.getAll();
-        for (int i = 0; i < items.length; ++i) {
-            if (items[i].hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
-                items[i].removeStateFlags(MediaLibraryItem.FLAG_SELECTED);
-                adapter.notifyItemChanged(i, items[i]);
-            }
-        }
-        adapter.resetSelectionCount();
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index a132a44..1fec434 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -495,7 +495,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
     @Override
     public void onTabUnselected(TabLayout.Tab tab) {
         stopActionMode();
-        onDestroyActionMode(tab.getPosition());
+        onDestroyActionMode((AudioBrowserAdapter) mLists[tab.getPosition()].getAdapter());
         mMainActivity.closeSearchView();
         mAdapters[tab.getPosition()].restoreList();
     }
@@ -739,19 +739,4 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
     public void onReloadCompleted(String entryPoint) {
         mHandler.sendEmptyMessage(UNSET_REFRESHING);
     }
-
-    public void onDestroyActionMode(int position) {
-        mActionMode = null;
-        AudioBrowserAdapter adapter = mAdapters[position];
-        MediaLibraryItem[] items = adapter.getAll();
-        if (items == null)
-            return;
-        for (int i = 0; i < items.length; ++i) {
-            if (items[i].hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
-                items[i].removeStateFlags(MediaLibraryItem.FLAG_SELECTED);
-                adapter.notifyItemChanged(i, items[i]);
-            }
-        }
-        adapter.resetSelectionCount();
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.java b/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.java
index 4a9dd5f..09f860f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/BaseAudioBrowser.java
@@ -103,18 +103,22 @@ public abstract class BaseAudioBrowser extends MediaBrowserFragment implements I
         return true;
     }
 
-    @Override
-    public void onDestroyActionMode(ActionMode mode) {
+    public void onDestroyActionMode(ActionMode actionMode) {
+        onDestroyActionMode(getCurrentAdapter());
+    }
+
+    public void onDestroyActionMode(AudioBrowserAdapter adapter) {
         mActionMode = null;
-        final AudioBrowserAdapter adapter = getCurrentAdapter();
         MediaLibraryItem[] items = adapter.getAll();
-        for (int i = 0; i < items.length; ++i) {
-            if (items[i].hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
-                items[i].removeStateFlags(MediaLibraryItem.FLAG_SELECTED);
-                adapter.notifyItemChanged(i, items[i]);
+        if (items != null) {
+            for (int i = 0; i < items.length; ++i) {
+                if (items[i].hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
+                    items[i].removeStateFlags(MediaLibraryItem.FLAG_SELECTED);
+                    adapter.notifyItemChanged(i, items[i]);
+                }
             }
         }
-        getCurrentAdapter().resetSelectionCount();
+        adapter.resetSelectionCount();
     }
 
     @Override



More information about the Android mailing list