Moved clicking action to main thread

This commit is contained in:
daniel oeh 2012-07-18 12:38:49 +02:00
parent 1bc9285aa5
commit 664a6ccf8c
4 changed files with 102 additions and 24 deletions

View File

@ -0,0 +1,83 @@
package de.danoeh.antennapod.asynctask;
import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.oauth.AccessToken;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.util.FlattrUtils;
/** Performs a click action in a background thread. */
public class FlattrClickWorker extends AsyncTask<Void, Void, Void> {
protected static final String TAG = "FlattrClickWorker";
protected Context context;
protected String url;
protected String errorMsg;
protected int exitCode;
protected final static int SUCCESS = 0;
protected final static int NO_TOKEN = 1;
protected final static int FLATTR_ERROR = 2;
public FlattrClickWorker(Context context, String url) {
super();
this.context = context;
this.url = url;
exitCode = SUCCESS;
errorMsg = "";
}
protected void onNoAccessToken() {
Log.w(TAG, "No access token was available");
FlattrUtils.showNoTokenDialog(context, url);
}
protected void onFlattrError() {
FlattrUtils.showErrorDialog(context, errorMsg);
}
protected void onSuccess() {
Toast toast = Toast.makeText(context.getApplicationContext(),
R.string.flattr_click_success, Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onPostExecute(Void result) {
Log.d(TAG, "Exit code was " + exitCode);
switch (exitCode) {
case NO_TOKEN:
onNoAccessToken();
break;
case FLATTR_ERROR:
onFlattrError();
break;
case SUCCESS:
onSuccess();
break;
}
}
@Override
protected Void doInBackground(Void... params) {
Log.d(TAG, "Starting background work");
AccessToken token = FlattrUtils.retrieveToken();
if (token != null) {
try {
FlattrUtils.clickUrl(token, context, url);
} catch (FlattrException e) {
e.printStackTrace();
exitCode = FLATTR_ERROR;
errorMsg = e.getMessage();
}
} else {
exitCode = NO_TOKEN;
}
return null;
}
}

View File

@ -8,10 +8,11 @@ import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.R;
/** Handles interactions with the FeedItemMenu. */
public class FeedItemMenuHandler {
@ -102,7 +103,7 @@ public class FeedItemMenuHandler {
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
break;
case R.id.support_item:
FlattrUtils.clickUrl(context, selectedItem.getPaymentLink());
new FlattrClickWorker(context, selectedItem.getPaymentLink()).execute();
break;
case R.id.share_link_item:
ShareUtils.shareFeedItemLink(context, selectedItem);

View File

@ -11,6 +11,7 @@ import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import de.danoeh.antennapod.activity.FeedInfoActivity;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
@ -68,7 +69,7 @@ public class FeedMenuHandler {
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
break;
case R.id.support_item:
FlattrUtils.clickUrl(context, selectedFeed.getPaymentLink());
new FlattrClickWorker(context, selectedFeed.getPaymentLink()).execute();
break;
case R.id.share_link_item:
ShareUtils.shareFeedlink(context, selectedFeed);

View File

@ -17,6 +17,7 @@ import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
@ -85,22 +86,14 @@ public class FlattrUtils {
storeToken(null);
}
public static void clickUrl(Context context, String url) {
public static void clickUrl(AccessToken token, Context context, String url)
throws FlattrException {
FlattrFactory factory = FlattrFactory.getInstance();
AccessToken token = retrieveToken();
if (token != null) {
FlattrService fs = factory.createFlattrService(retrieveToken());
try {
fs.click(url);
Toast toast = Toast.makeText(context.getApplicationContext(),
R.string.flattr_click_success, Toast.LENGTH_LONG);
toast.show();
} catch (FlattrException e) {
e.printStackTrace();
showErrorDialog(context, e.getMessage());
}
} else {
showNoTokenDialog(context, url);
Log.e(TAG, "clickUrl was called with null access token");
}
}
@ -123,10 +116,9 @@ public class FlattrUtils {
showRevokeDialog(context);
}
// ------------------------------------------------ DIALOGS
//------------------------------------------------ DIALOGS
private static void showRevokeDialog(final Context context) {
public static void showRevokeDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.access_revoked_title);
builder.setMessage(R.string.access_revoked_info);
@ -140,7 +132,7 @@ public class FlattrUtils {
builder.create().show();
}
private static void showNoTokenDialog(final Context context,
public static void showNoTokenDialog(final Context context,
final String url) {
Log.d(TAG, "Creating showNoTokenDialog");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
@ -170,7 +162,7 @@ public class FlattrUtils {
builder.create().show();
}
private static void showForbiddenDialog(final Context context,
public static void showForbiddenDialog(final Context context,
final String url) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_forbidden_title);
@ -199,7 +191,7 @@ public class FlattrUtils {
builder.create().show();
}
private static void showErrorDialog(final Context context, final String msg) {
public static void showErrorDialog(final Context context, final String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.error_label);
builder.setMessage(msg);
@ -212,4 +204,5 @@ public class FlattrUtils {
});
builder.create().show();
}
}