[Android] [PATCH 6/6] Util: run PlaybackService commands on Main thread

Jean-Baptiste Kempf jb at videolan.org
Thu Jun 11 17:59:34 CEST 2015


You should use @MainThread then or @UiThread if this is required.


On 11 Jun, Thomas Guillem wrote :
> Remove VLCCallbackTask and replace it with DialogCallback
> ---
>  .../src/org/videolan/vlc/VLCCallbackTask.java      | 78 ----------------------
>  vlc-android/src/org/videolan/vlc/util/Util.java    | 61 ++++++++---------
>  2 files changed, 26 insertions(+), 113 deletions(-)
>  delete mode 100644 vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
> 
> 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 f97e0d7..5c2f44e 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.AsyncCallback<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.Async.load(context, null, media);
> -                }
> -            };
> -            task.execute();
> +            PlaybackServiceClient.Async.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.Async.load(context, null, list, position);
> -            }
> -        };
> -        task.execute();
> +    public static void openList(final Context context, final List<MediaWrapper> list, final int position){
> +        PlaybackServiceClient.Async.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.Async.loadLocation(context, null, uri);
> -            }
> -        };
> -        task.execute();
> +        PlaybackServiceClient.Async.loadLocation(context, new DialogCallback(context), uri);
>      }
>  
>      private static String getMediaString(Context ctx, int id) {
> -- 
> 2.1.4
> 
> _______________________________________________
> Android mailing list
> Android at videolan.org
> https://mailman.videolan.org/listinfo/android

-- 
With my kindest regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device


More information about the Android mailing list