[Android] Util: run PlaybackService commands from Main thread

Thomas Guillem git at videolan.org
Thu Jun 11 19:03:47 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Thu Jun 11 17:35:24 2015 +0200| [f91b07497b4224b990a4dee24bd520ff86c8cf52] | committer: Thomas Guillem

Util: run PlaybackService commands from Main thread

Remove VLCCallbackTask and replace it with DialogCallback

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

 .../src/org/videolan/vlc/VLCCallbackTask.java      |   78 --------------------
 vlc-android/src/org/videolan/vlc/util/Util.java    |   61 +++++++--------
 2 files changed, 26 insertions(+), 113 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java b/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
deleted file mode 100644
index a425c53..0000000
--- a/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*****************************************************************************
- * VLCCallbackTask.java
- *****************************************************************************
- * Copyright © 2012 VLC authors and VideoLAN
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
-package org.videolan.vlc;
-
-import android.app.ProgressDialog;
-import android.content.Context;
-import android.os.AsyncTask;
-
-/**
- * A small callback helper class to make running callbacks in threads easier
- */
-public abstract class VLCCallbackTask extends AsyncTask<Void, Void, Void> {
-
-    private Context context;
-    private ProgressDialog dialog;
-
-    /**
-     * Runs a callback in a background thread
-     */
-    public VLCCallbackTask() {
-    }
-
-    /**
-     * Runs a callback in a background thread, and display a ProgressDialog until it's finished
-     */
-    public VLCCallbackTask(Context context) {
-        this.context = context;
-    }
-
-    @Override
-    /* Runs on the UI thread */
-    protected void onPreExecute() {
-        if (context != null) {
-            dialog = ProgressDialog.show(
-                    context,
-                    context.getApplicationContext().getString(R.string.loading) + "…",
-                    context.getApplicationContext().getString(R.string.please_wait), true);
-            dialog.setCancelable(true);
-        }
-        super.onPreExecute();
-    }
-
-    public abstract void run();
-
-    @Override
-    /* Runs on a background thread */
-    protected Void doInBackground(Void... params) {
-        run();
-        return null;
-    }
-
-    @Override
-    /* Runs on the UI thread */
-    protected void onPostExecute(Void result) {
-        if (dialog != null)
-            dialog.dismiss();
-        dialog = null;
-        context = null;
-        super.onPostExecute(result);
-    }
-}
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 1efc016..53b4cdd 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -21,6 +21,7 @@
 package org.videolan.vlc.util;
 
 import android.annotation.TargetApi;
+import android.app.ProgressDialog;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
@@ -46,7 +47,6 @@ import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.PlaybackServiceClient;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
-import org.videolan.vlc.VLCCallbackTask;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 
 import java.io.BufferedReader;
@@ -206,6 +206,27 @@ public class Util {
         VLCApplication.getAppContext().sendBroadcast(intent);
     }
 
+    private static class DialogCallback implements PlaybackServiceClient.ResultCallback<Void> {
+        private final ProgressDialog dialog;
+
+        private DialogCallback(Context context) {
+            this.dialog = ProgressDialog.show(
+                    context,
+                    context.getApplicationContext().getString(R.string.loading) + "…",
+                    context.getApplicationContext().getString(R.string.please_wait), true);
+            dialog.setCancelable(true);
+        }
+
+        @Override
+        public void onResult(PlaybackServiceClient client, Void result) {
+            dialog.dismiss();
+        }
+
+        @Override
+        public void onError(PlaybackServiceClient client) {
+            dialog.dismiss();
+        }
+    }
 
     public static void openMedia(final Context context, final MediaWrapper media){
         if (media == null)
@@ -213,46 +234,16 @@ public class Util {
         if (media.getType() == MediaWrapper.TYPE_VIDEO)
             VideoPlayerActivity.start(context, media.getUri(), media.getTitle());
         else if (media.getType() == MediaWrapper.TYPE_AUDIO) {
-            VLCCallbackTask task = new VLCCallbackTask(context) {
-                @Override
-                public void run() {
-                    PlaybackServiceClient.load(context, null, media);
-                }
-            };
-            task.execute();
+            PlaybackServiceClient.load(context, new DialogCallback(context), media);
         }
     }
 
-    public static  void openList(final Context context, final List<MediaWrapper> list, final int position){
-        VLCCallbackTask task = new VLCCallbackTask(context){
-            @Override
-            public void run() {
-                      /* Use the audio player by default. If a video track is
-                       * detected, then it will automatically switch to the video
-                       * player. This allows us to support more types of streams
-                       * (for example, RTSP and TS streaming) where ES can be
-                       * dynamically adapted rather than a simple scan.
-                       */
-                PlaybackServiceClient.load(context, null, list, position);
-            }
-        };
-        task.execute();
+    public static void openList(final Context context, final List<MediaWrapper> list, final int position){
+        PlaybackServiceClient.load(context, new DialogCallback(context), list, position);
     }
 
     public static void openStream(final Context context, final String uri){
-        VLCCallbackTask task = new VLCCallbackTask(context){
-            @Override
-            public void run() {
-                      /* Use the audio player by default. If a video track is
-                       * detected, then it will automatically switch to the video
-                       * player. This allows us to support more types of streams
-                       * (for example, RTSP and TS streaming) where ES can be
-                       * dynamically adapted rather than a simple scan.
-                       */
-                PlaybackServiceClient.loadLocation(context, null, uri);
-            }
-        };
-        task.execute();
+        PlaybackServiceClient.loadLocation(context, new DialogCallback(context), uri);
     }
 
     private static String getMediaString(Context ctx, int id) {



More information about the Android mailing list