[Android] [PATCH 7/8] extract database access to AsyncTask and avoid races on mLocation

Yu Lin yu.lin.86 at gmail.com
Tue Mar 4 23:33:45 CET 2014


---
 .../vlc/gui/video/VideoPlayerActivity.java         |   57 +++++++++++++++-----
 1 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 5da0e05..4a1d31b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -74,6 +74,7 @@ import android.graphics.PixelFormat;
 import android.media.AudioManager;
 import android.media.AudioManager.OnAudioFocusChangeListener;
 import android.media.MediaRouter;
+import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
@@ -1282,12 +1283,27 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
                     }
                     if(trackID < 0) return;
 
-                    MediaDatabase.getInstance(VideoPlayerActivity.this).updateMedia(
-                            mLocation,
-                            MediaDatabase.mediaColumn.MEDIA_AUDIOTRACK,
-                            trackID);
-                    mLibVLC.setAudioTrack(trackID);
-                    dialog.dismiss();
+                    new AsyncTask<Object, Void, Void>() {
+                        DialogInterface dialog;
+                        int trackID;
+
+                        protected Void doInBackground(Object... args) {
+                            trackID = (Integer) args[0];
+                            dialog = (DialogInterface) args[1];
+                            synchronized (VideoPlayerActivity.this) {
+                                MediaDatabase.getInstance(VideoPlayerActivity.this).updateMedia(
+                                        mLocation,
+                                        MediaDatabase.mediaColumn.MEDIA_AUDIOTRACK,
+                                        trackID);
+                            }
+                            return null;
+                        }
+
+                        protected void onPostExecute(Void v) {
+                            mLibVLC.setAudioTrack(trackID);
+                            dialog.dismiss();
+                        }
+                    }.execute(trackID, dialog);
                 }
             })
             .create();
@@ -1329,12 +1345,27 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
                     }
                     if(trackID < -1) return;
 
-                    MediaDatabase.getInstance(VideoPlayerActivity.this).updateMedia(
-                            mLocation,
-                            MediaDatabase.mediaColumn.MEDIA_SPUTRACK,
-                            trackID);
-                    mLibVLC.setSpuTrack(trackID);
-                    dialog.dismiss();
+                    new AsyncTask<Object, Void, Void>() {
+                        DialogInterface dialog;
+                        int trackID;
+
+                        protected Void doInBackground(Object... args) {
+                            trackID = (Integer) args[0];
+                            dialog = (DialogInterface) args[1];
+                            synchronized (VideoPlayerActivity.this) {
+                                MediaDatabase.getInstance(VideoPlayerActivity.this).updateMedia(
+                                        mLocation,
+                                        MediaDatabase.mediaColumn.MEDIA_SPUTRACK,
+                                        trackID);
+                            }
+                            return null;
+                        }
+
+                        protected void onPostExecute(Void v) {
+                            mLibVLC.setSpuTrack(trackID);
+                            dialog.dismiss();
+                        }
+                    }.execute(trackID, dialog);
                 }
             })
             .create();
@@ -1672,7 +1703,7 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
      * - position (long) - position of the video to start with (in ms)
      */
     @SuppressWarnings({ "deprecation", "unchecked" })
-    private void load() {
+    private synchronized void load() {
         mLocation = null;
         String title = getResources().getString(R.string.title);
         boolean dontParse = false;
-- 
1.7.4.4



More information about the Android mailing list