[Android] VlcCallbackTask: use AsyncTask and optionnaly show a ProgressDialog
Sébastien Toque
git at videolan.org
Mon Aug 20 22:43:32 CEST 2012
vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Mon Aug 20 22:43:17 2012 +0200| [34a28d300fa064398fb8adb3e0b964b4c9d8b7cc] | committer: Sébastien Toque
VlcCallbackTask: use AsyncTask and optionnaly show a ProgressDialog
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=34a28d300fa064398fb8adb3e0b964b4c9d8b7cc
---
.../src/org/videolan/vlc/VLCCallbackTask.java | 68 ++++++++++++--------
.../src/org/videolan/vlc/gui/MainActivity.java | 56 ++++++----------
2 files changed, 62 insertions(+), 62 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java b/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
index 304f97f..92623b7 100644
--- a/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
+++ b/vlc-android/src/org/videolan/vlc/VLCCallbackTask.java
@@ -19,46 +19,60 @@
*****************************************************************************/
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 class VLCCallbackTask implements Runnable {
- private final CallbackListener callback;
- private final Object user;
+public abstract class VLCCallbackTask extends AsyncTask<Void, Void, Void> {
+
+ private Context context;
+ private ProgressDialog dialog;
/**
- * The callback interface. Implement callback() to run as the main thread,
- * and implement callback_object() to run when the main thread completes.
+ * Runs a callback in a background thread
*/
- public interface CallbackListener {
- public abstract void callback();
- public abstract void callback_object(Object o);
+ public VLCCallbackTask() {
}
/**
- * @param _callback The CallbackListener as described above
- * @param _user Any user object you want to pass
+ * Runs a callback in a background thread, and display a ProgressDialog until it's finished
*/
- public VLCCallbackTask(CallbackListener _callback, Object _user) {
- this.callback = _callback;
- this.user = _user;
+ public VLCCallbackTask(Context context) {
+ this.context = context;
}
- /**
- * A version of VLCCallbackTask if you are not using the user parameter
- *
- * @param _callback The CallbackListener as described above
- */
- public VLCCallbackTask(CallbackListener _callback) {
- this.callback = _callback;
- this.user = null;
+ @Override
+ /* Runs on the UI thread */
+ protected void onPreExecute() {
+ if (context != null) {
+ dialog = ProgressDialog.show(
+ context,
+ context.getApplicationContext().getString(R.string.loading),
+ "Please wait...", true);
+ dialog.setCancelable(true);
+ }
+ super.onPreExecute();
}
- /* (non-Javadoc)
- * @see java.lang.Runnable#run()
- */
- public void run() {
- callback.callback();
- callback.callback_object(user);
+ 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/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 86b6be9..0dc2072 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -37,7 +37,6 @@ import org.videolan.vlc.widget.AudioMiniPlayer;
import android.app.AlertDialog;
import android.app.Dialog;
-import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
@@ -464,40 +463,27 @@ public class MainActivity extends SherlockFragmentActivity {
b.setPositiveButton(R.string.open, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int button) {
- ProgressDialog pd = ProgressDialog.show(
- MainActivity.this,
- getApplicationContext().getString(R.string.loading),
- "Please wait...", true);
- pd.setCancelable(true);
-
- VLCCallbackTask t = new VLCCallbackTask(
- /* Task to run */
- new VLCCallbackTask.CallbackListener() {
- @Override
- public void callback() {
- AudioServiceController c = AudioServiceController.getInstance();
- String s = input.getText().toString();
-
- /* 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.
- */
- ArrayList<String> media = new ArrayList<String>();
- media.add(s);
- c.append(media);
- }
-
- @Override
- public void callback_object(Object o) {
- ProgressDialog pd = (ProgressDialog)o;
- pd.dismiss();
- }
- }, pd);
-
- /* Start this in a new friend as to not block the UI thread */
- new Thread(t).start();
+
+ /* Start this in a new thread as to not block the UI thread */
+ VLCCallbackTask task = new VLCCallbackTask(MainActivity.this)
+ {
+ @Override
+ public void run() {
+ AudioServiceController c = AudioServiceController.getInstance();
+ String s = input.getText().toString();
+
+ /* 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.
+ */
+ ArrayList<String> media = new ArrayList<String>();
+ media.add(s);
+ c.append(media);
+ }
+ };
+ task.execute();
}
}
);
More information about the Android
mailing list