Moved clicking action to main thread
This commit is contained in:
parent
1bc9285aa5
commit
664a6ccf8c
83
src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java
Normal file
83
src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -8,10 +8,11 @@ import com.actionbarsherlock.view.Menu;
|
|||||||
import com.actionbarsherlock.view.MenuInflater;
|
import com.actionbarsherlock.view.MenuInflater;
|
||||||
import com.actionbarsherlock.view.MenuItem;
|
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.FeedItem;
|
||||||
import de.danoeh.antennapod.feed.FeedManager;
|
import de.danoeh.antennapod.feed.FeedManager;
|
||||||
import de.danoeh.antennapod.storage.DownloadRequester;
|
import de.danoeh.antennapod.storage.DownloadRequester;
|
||||||
import de.danoeh.antennapod.R;
|
|
||||||
|
|
||||||
/** Handles interactions with the FeedItemMenu. */
|
/** Handles interactions with the FeedItemMenu. */
|
||||||
public class FeedItemMenuHandler {
|
public class FeedItemMenuHandler {
|
||||||
@ -102,7 +103,7 @@ public class FeedItemMenuHandler {
|
|||||||
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
||||||
break;
|
break;
|
||||||
case R.id.support_item:
|
case R.id.support_item:
|
||||||
FlattrUtils.clickUrl(context, selectedItem.getPaymentLink());
|
new FlattrClickWorker(context, selectedItem.getPaymentLink()).execute();
|
||||||
break;
|
break;
|
||||||
case R.id.share_link_item:
|
case R.id.share_link_item:
|
||||||
ShareUtils.shareFeedItemLink(context, selectedItem);
|
ShareUtils.shareFeedItemLink(context, selectedItem);
|
||||||
|
@ -11,6 +11,7 @@ import com.actionbarsherlock.view.MenuInflater;
|
|||||||
import com.actionbarsherlock.view.MenuItem;
|
import com.actionbarsherlock.view.MenuItem;
|
||||||
|
|
||||||
import de.danoeh.antennapod.activity.FeedInfoActivity;
|
import de.danoeh.antennapod.activity.FeedInfoActivity;
|
||||||
|
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
|
||||||
import de.danoeh.antennapod.feed.Feed;
|
import de.danoeh.antennapod.feed.Feed;
|
||||||
import de.danoeh.antennapod.feed.FeedItem;
|
import de.danoeh.antennapod.feed.FeedItem;
|
||||||
import de.danoeh.antennapod.feed.FeedManager;
|
import de.danoeh.antennapod.feed.FeedManager;
|
||||||
@ -68,7 +69,7 @@ public class FeedMenuHandler {
|
|||||||
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
||||||
break;
|
break;
|
||||||
case R.id.support_item:
|
case R.id.support_item:
|
||||||
FlattrUtils.clickUrl(context, selectedFeed.getPaymentLink());
|
new FlattrClickWorker(context, selectedFeed.getPaymentLink()).execute();
|
||||||
break;
|
break;
|
||||||
case R.id.share_link_item:
|
case R.id.share_link_item:
|
||||||
ShareUtils.shareFeedlink(context, selectedFeed);
|
ShareUtils.shareFeedlink(context, selectedFeed);
|
||||||
|
@ -17,6 +17,7 @@ import android.content.DialogInterface.OnClickListener;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -85,22 +86,14 @@ public class FlattrUtils {
|
|||||||
storeToken(null);
|
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();
|
FlattrFactory factory = FlattrFactory.getInstance();
|
||||||
AccessToken token = retrieveToken();
|
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
FlattrService fs = factory.createFlattrService(retrieveToken());
|
FlattrService fs = factory.createFlattrService(retrieveToken());
|
||||||
try {
|
fs.click(url);
|
||||||
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 {
|
} else {
|
||||||
showNoTokenDialog(context, url);
|
Log.e(TAG, "clickUrl was called with null access token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,17 +109,16 @@ public class FlattrUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void revokeAccessToken(Context context) {
|
public static void revokeAccessToken(Context context) {
|
||||||
Log.d(TAG, "Revoking access token");
|
Log.d(TAG, "Revoking access token");
|
||||||
deleteToken();
|
deleteToken();
|
||||||
showRevokeDialog(context);
|
showRevokeDialog(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------ DIALOGS
|
||||||
//------------------------------------------------ DIALOGS
|
|
||||||
|
public static void showRevokeDialog(final Context context) {
|
||||||
private static void showRevokeDialog(final Context context) {
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
builder.setTitle(R.string.access_revoked_title);
|
builder.setTitle(R.string.access_revoked_title);
|
||||||
builder.setMessage(R.string.access_revoked_info);
|
builder.setMessage(R.string.access_revoked_info);
|
||||||
@ -140,7 +132,7 @@ public class FlattrUtils {
|
|||||||
builder.create().show();
|
builder.create().show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void showNoTokenDialog(final Context context,
|
public static void showNoTokenDialog(final Context context,
|
||||||
final String url) {
|
final String url) {
|
||||||
Log.d(TAG, "Creating showNoTokenDialog");
|
Log.d(TAG, "Creating showNoTokenDialog");
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
@ -170,7 +162,7 @@ public class FlattrUtils {
|
|||||||
builder.create().show();
|
builder.create().show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void showForbiddenDialog(final Context context,
|
public static void showForbiddenDialog(final Context context,
|
||||||
final String url) {
|
final String url) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
builder.setTitle(R.string.action_forbidden_title);
|
builder.setTitle(R.string.action_forbidden_title);
|
||||||
@ -199,7 +191,7 @@ public class FlattrUtils {
|
|||||||
builder.create().show();
|
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);
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
builder.setTitle(R.string.error_label);
|
builder.setTitle(R.string.error_label);
|
||||||
builder.setMessage(msg);
|
builder.setMessage(msg);
|
||||||
@ -212,4 +204,5 @@ public class FlattrUtils {
|
|||||||
});
|
});
|
||||||
builder.create().show();
|
builder.create().show();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user