From 8957642cee0a07ebfc415a32eb37f5afa084d879 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 18 Jul 2012 13:20:00 +0200 Subject: [PATCH] Moved callback handler to background thread --- res/values/strings.xml | 1 + .../activity/FlattrAuthActivity.java | 18 ++-- .../asynctask/FlattrTokenFetcher.java | 82 +++++++++++++++++++ .../danoeh/antennapod/util/FlattrUtils.java | 17 ++-- 4 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 667078eda..60cae4c1d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -123,5 +123,6 @@ Published:\u0020 Length:\u0020 Size:\u0020 + Processing \ No newline at end of file diff --git a/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java b/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java index 54ff8db96..57adc94c1 100644 --- a/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java +++ b/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java @@ -25,10 +25,13 @@ public class FlattrAuthActivity extends SherlockActivity { private TextView txtvExplanation; private Button butAuthenticate; private Button butReturn; + + private static FlattrAuthActivity singleton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + singleton = this; Log.d(TAG, "Activity created"); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.flattr_auth); @@ -55,6 +58,10 @@ public class FlattrAuthActivity extends SherlockActivity { } }); } + + public static FlattrAuthActivity getInstance() { + return singleton; + } @Override protected void onResume() { @@ -63,18 +70,11 @@ public class FlattrAuthActivity extends SherlockActivity { Uri uri = getIntent().getData(); if (uri != null) { Log.d(TAG, "Received uri"); - try { - if (FlattrUtils.handleCallback(uri) != null) { - handleAuthenticationSuccess(); - Log.d(TAG, "Authentication seemed to be successful"); - } - } catch (FlattrException e) { - e.printStackTrace(); - } + FlattrUtils.handleCallback(this, uri); } } - private void handleAuthenticationSuccess() { + public void handleAuthenticationSuccess() { txtvExplanation.setText(R.string.flattr_auth_success); butAuthenticate.setEnabled(false); butReturn.setVisibility(View.VISIBLE); diff --git a/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java b/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java new file mode 100644 index 000000000..3b514fd3e --- /dev/null +++ b/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java @@ -0,0 +1,82 @@ +package de.danoeh.antennapod.asynctask; + +import org.shredzone.flattr4j.exception.FlattrException; +import org.shredzone.flattr4j.oauth.AccessToken; +import org.shredzone.flattr4j.oauth.AndroidAuthenticator; + +import android.app.ProgressDialog; +import android.content.Context; +import android.net.Uri; +import android.os.AsyncTask; +import android.util.Log; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.FlattrAuthActivity; +import de.danoeh.antennapod.util.FlattrUtils; + +/** Fetches the access token in the background in order to avoid networkOnMainThread exception. */ +public class FlattrTokenFetcher extends AsyncTask { + private static final String TAG = "FlattrTokenFetcher"; + Context context; + AndroidAuthenticator auth; + AccessToken token; + Uri uri; + ProgressDialog dialog; + FlattrException exception; + + public FlattrTokenFetcher(Context context, AndroidAuthenticator auth, Uri uri) { + super(); + this.context = context; + this.auth = auth; + this.uri = uri; + } + + @Override + protected void onPostExecute(AccessToken result) { + super.onPostExecute(result); + dialog.dismiss(); + if (exception == null) { + FlattrAuthActivity instance = FlattrAuthActivity.getInstance(); + if (instance != null) { + instance.handleAuthenticationSuccess(); + } else { + Log.e(TAG, "FlattrAuthActivity instance was null"); + } + } else { + FlattrUtils.showErrorDialog(context, exception.getMessage()); + } + } + + + + @Override + protected void onPreExecute() { + super.onPreExecute(); + dialog = new ProgressDialog(context); + dialog.setMessage(context.getString(R.string.processing_label)); + dialog.setIndeterminate(true); + dialog.setCancelable(false); + dialog.show(); + } + + + + @Override + protected AccessToken doInBackground(Void... params) { + try { + token = auth.fetchAccessToken(uri); + } catch (FlattrException e) { + e.printStackTrace(); + exception = e; + return null; + } + if (token != null) { + Log.d(TAG, "Successfully got token"); + FlattrUtils.storeToken(token); + return token; + } else { + Log.w(TAG, "Flattr token was null"); + return null; + } + } + +} \ No newline at end of file diff --git a/src/de/danoeh/antennapod/util/FlattrUtils.java b/src/de/danoeh/antennapod/util/FlattrUtils.java index ccc9347f3..65ba14d70 100644 --- a/src/de/danoeh/antennapod/util/FlattrUtils.java +++ b/src/de/danoeh/antennapod/util/FlattrUtils.java @@ -11,6 +11,7 @@ import org.shredzone.flattr4j.oauth.Scope; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; @@ -23,6 +24,7 @@ import android.util.Log; import android.widget.Toast; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.activity.FlattrAuthActivity; +import de.danoeh.antennapod.asynctask.FlattrTokenFetcher; import de.danoeh.antennapod.R; /** Utility methods for doing something with flattr. */ @@ -69,7 +71,7 @@ public class FlattrUtils { } /** Stores the token as a preference */ - private static void storeToken(AccessToken token) { + public static void storeToken(AccessToken token) { Log.d(TAG, "Storing token"); SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(PodcastApp.getInstance()).edit(); @@ -97,17 +99,9 @@ public class FlattrUtils { } } - public static AccessToken handleCallback(Uri uri) throws FlattrException { + public static void handleCallback(Context context, Uri uri) { AndroidAuthenticator auth = createAuthenticator(); - AccessToken token = auth.fetchAccessToken(uri); - if (token != null) { - Log.d(TAG, "Successfully got token"); - storeToken(token); - return token; - } else { - Log.w(TAG, "Flattr token was null"); - return null; - } + new FlattrTokenFetcher(context, auth, uri).execute(); } public static void revokeAccessToken(Context context) { @@ -204,5 +198,6 @@ public class FlattrUtils { }); builder.create().show(); } + }