[Android] Fill playlist later & prevent race conditions

Geoffrey Métais git at videolan.org
Thu Jan 5 10:16:33 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan  5 09:51:23 2017 +0100| [6c452adaf773a12573a7d1e8940a46d429ef1703] | committer: Geoffrey Métais

Fill playlist later & prevent race conditions

> https://code.videolan.org/videolan/vlc-android/commit/6c452adaf773a12573a7d1e8940a46d429ef1703
---

 .../org/videolan/vlc/gui/audio/AudioPlayer.java    | 36 +++++++++++++++-------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
index 3db727d..28bf08d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
@@ -28,6 +28,7 @@ import android.content.SharedPreferences;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Message;
 import android.preference.PreferenceManager;
 import android.support.annotation.Nullable;
 import android.support.annotation.RequiresPermission;
@@ -78,7 +79,6 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
     private boolean mPreviewingSeek = false;
 
     private PlaylistAdapter mPlaylistAdapter;
-    private Handler mHandler = new Handler();
 
     private boolean mAdvFuncVisible;
     private boolean mPlaylistSwitchVisible;
@@ -92,24 +92,19 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
     private static final String PREF_AUDIOPLAYER_TIPS_SHOWN = "audioplayer_tips_shown";
 
     @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        mPlaylistAdapter = new PlaylistAdapter(this);
-    }
-
-    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         mBinding = AudioPlayerBinding.inflate(inflater);
-        mBinding.songsList.setLayoutManager(new LinearLayoutManager(mBinding.getRoot().getContext()));
-        mBinding.songsList.setAdapter(mPlaylistAdapter);
-        mBinding.audioMediaSwitcher.setAudioMediaSwitcherListener(mHeaderMediaSwitcherListener);
-        mBinding.coverMediaSwitcher.setAudioMediaSwitcherListener(mCoverMediaSwitcherListener);
         return mBinding.getRoot();
     }
 
     @Override
     public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
+        mPlaylistAdapter = new PlaylistAdapter(this);
+        mBinding.songsList.setLayoutManager(new LinearLayoutManager(mBinding.getRoot().getContext()));
+        mBinding.songsList.setAdapter(mPlaylistAdapter);
+        mBinding.audioMediaSwitcher.setAudioMediaSwitcherListener(mHeaderMediaSwitcherListener);
+        mBinding.coverMediaSwitcher.setAudioMediaSwitcherListener(mCoverMediaSwitcherListener);
         mBinding.playlistSearchText.getEditText().addTextChangedListener(this);
 
         ItemTouchHelper.Callback callback =  new SwipeDragItemTouchHelperCallback(mPlaylistAdapter);
@@ -175,6 +170,11 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
     }
 
     public void update() {
+        mHandler.removeMessages(UPDATE);
+        mHandler.sendEmptyMessageDelayed(UPDATE, 50);
+    }
+
+    public void doUpdate() {
         if (mService == null || getActivity() == null)
             return;
 
@@ -660,4 +660,18 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
         if (getFragmentManager() != null)
             super.setUserVisibleHint(isVisibleToUser);
     }
+
+    private static final int UPDATE = 0;
+    private Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case UPDATE:
+                    doUpdate();
+                    break;
+                default:
+                    super.handleMessage(msg);
+            }
+        }
+    };
 }



More information about the Android mailing list