Moved callback handler to background thread
This commit is contained in:
parent
664a6ccf8c
commit
8957642cee
|
@ -123,5 +123,6 @@
|
|||
<string name="published_prefix">Published:\u0020</string>
|
||||
<string name="length_prefix">Length:\u0020</string>
|
||||
<string name="size_prefix">Size:\u0020</string>
|
||||
<string name="processing_label">Processing</string>
|
||||
|
||||
</resources>
|
|
@ -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);
|
||||
|
|
|
@ -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<Void, Void, AccessToken> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue