Prevent flattr login if API keys are missing

closes #474
This commit is contained in:
daniel oeh 2014-08-06 19:37:18 +02:00
parent 04cb23acff
commit ee807628bb
3 changed files with 249 additions and 216 deletions

View File

@ -45,6 +45,7 @@ public class PreferenceActivity extends android.preference.PreferenceActivity {
private static final String TAG = "PreferenceActivity"; private static final String TAG = "PreferenceActivity";
private static final String PREF_FLATTR_THIS_APP = "prefFlattrThisApp"; private static final String PREF_FLATTR_THIS_APP = "prefFlattrThisApp";
private static final String PREF_FLATTR_SETTINGS = "prefFlattrSettings";
private static final String PREF_FLATTR_AUTH = "pref_flattr_authenticate"; private static final String PREF_FLATTR_AUTH = "pref_flattr_authenticate";
private static final String PREF_FLATTR_REVOKE = "prefRevokeAccess"; private static final String PREF_FLATTR_REVOKE = "prefRevokeAccess";
private static final String PREF_AUTO_FLATTR_PREFS = "prefAutoFlattrPrefs"; private static final String PREF_AUTO_FLATTR_PREFS = "prefAutoFlattrPrefs";
@ -332,6 +333,7 @@ public class PreferenceActivity extends android.preference.PreferenceActivity {
boolean hasFlattrToken = FlattrUtils.hasToken(); boolean hasFlattrToken = FlattrUtils.hasToken();
findPreference(PREF_FLATTR_SETTINGS).setEnabled(FlattrUtils.hasAPICredentials());
findPreference(PREF_FLATTR_AUTH).setEnabled(!hasFlattrToken); findPreference(PREF_FLATTR_AUTH).setEnabled(!hasFlattrToken);
findPreference(PREF_FLATTR_REVOKE).setEnabled(hasFlattrToken); findPreference(PREF_FLATTR_REVOKE).setEnabled(hasFlattrToken);
findPreference(PREF_AUTO_FLATTR_PREFS).setEnabled(hasFlattrToken); findPreference(PREF_AUTO_FLATTR_PREFS).setEnabled(hasFlattrToken);

View File

@ -872,7 +872,7 @@ public final class DBTasks {
item.getFlattrStatus().setFlattrQueue(); item.getFlattrStatus().setFlattrQueue();
DBWriter.setFlattredStatus(context, item, true); DBWriter.setFlattredStatus(context, item, true);
} else { } else {
FlattrUtils.showNoTokenDialog(context, item.getPaymentLink()); FlattrUtils.showNoTokenDialogOrRedirect(context, item.getPaymentLink());
} }
} }
@ -888,7 +888,7 @@ public final class DBTasks {
feed.getFlattrStatus().setFlattrQueue(); feed.getFlattrStatus().setFlattrQueue();
DBWriter.setFlattredStatus(context, feed, true); DBWriter.setFlattredStatus(context, feed, true);
} else { } else {
FlattrUtils.showNoTokenDialog(context, feed.getPaymentLink()); FlattrUtils.showNoTokenDialogOrRedirect(context, feed.getPaymentLink());
} }
} }

View File

@ -9,12 +9,8 @@ import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.PodcastApp; import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;
import de.danoeh.antennapod.storage.DBWriter;
import org.shredzone.flattr4j.FlattrService; import org.shredzone.flattr4j.FlattrService;
import org.shredzone.flattr4j.exception.FlattrException; import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.model.Flattr; import org.shredzone.flattr4j.model.Flattr;
@ -23,9 +19,23 @@ import org.shredzone.flattr4j.oauth.AccessToken;
import org.shredzone.flattr4j.oauth.AndroidAuthenticator; import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
import org.shredzone.flattr4j.oauth.Scope; import org.shredzone.flattr4j.oauth.Scope;
import java.util.*; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.TimeZone;
/** Utility methods for doing something with flattr. */ import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;
import de.danoeh.antennapod.storage.DBWriter;
/**
* Utility methods for doing something with flattr.
*/
public class FlattrUtils { public class FlattrUtils {
private static final String TAG = "FlattrUtils"; private static final String TAG = "FlattrUtils";
@ -75,6 +85,14 @@ public class FlattrUtils {
} }
/**
* Returns true if FLATTR_APP_KEY and FLATTR_APP_SECRET in BuildConfig are not null and not empty
*/
public static boolean hasAPICredentials() {
return StringUtils.isNotEmpty(BuildConfig.FLATTR_APP_KEY)
&& StringUtils.isNoneEmpty(BuildConfig.FLATTR_APP_SECRET);
}
public static boolean hasToken() { public static boolean hasToken() {
return retrieveToken() != null; return retrieveToken() != null;
} }
@ -138,13 +156,13 @@ public class FlattrUtils {
Date firstOfMonthDate = firstOfMonth.getTime(); Date firstOfMonthDate = firstOfMonth.getTime();
// subscriptions some times get flattrd slightly before midnight - give it an hour leeway // subscriptions some times get flattrd slightly before midnight - give it an hour leeway
firstOfMonthDate = new Date(firstOfMonthDate.getTime() - 60*60*1000); firstOfMonthDate = new Date(firstOfMonthDate.getTime() - 60 * 60 * 1000);
final int FLATTR_COUNT = 30; final int FLATTR_COUNT = 30;
final int FLATTR_MAXPAGE = 5; final int FLATTR_MAXPAGE = 5;
for (int page = 0; page < FLATTR_MAXPAGE; page++) { for (int page = 0; page < FLATTR_MAXPAGE; page++) {
for (Flattr fl: fs.getMyFlattrs(FLATTR_COUNT, page)) { for (Flattr fl : fs.getMyFlattrs(FLATTR_COUNT, page)) {
if (fl.getCreated().after(firstOfMonthDate)) if (fl.getCreated().after(firstOfMonthDate))
myFlattrs.add(fl); myFlattrs.add(fl);
else else
@ -155,7 +173,7 @@ public class FlattrUtils {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(TAG, "Got my flattrs list of length " + Integer.toString(myFlattrs.size()) + " comparison date" + firstOfMonthDate); Log.d(TAG, "Got my flattrs list of length " + Integer.toString(myFlattrs.size()) + " comparison date" + firstOfMonthDate);
for (Flattr fl: myFlattrs) { for (Flattr fl : myFlattrs) {
Thing thing = fl.getThing(); Thing thing = fl.getThing();
Log.d(TAG, "Flattr thing: " + fl.getThingId() + " name: " + thing.getTitle() + " url: " + thing.getUrl() + " on: " + fl.getCreated()); Log.d(TAG, "Flattr thing: " + fl.getThingId() + " name: " + thing.getTitle() + " url: " + thing.getUrl() + " on: " + fl.getCreated());
} }
@ -198,9 +216,13 @@ public class FlattrUtils {
builder.create().show(); builder.create().show();
} }
public static void showNoTokenDialog(final Context context, final String url) { /**
if (BuildConfig.DEBUG) * Opens a dialog that ask the user to either connect the app with flattr or to be redirected to
Log.d(TAG, "Creating showNoTokenDialog"); * the thing's website.
* If no API credentials are available, the user will immediately be redirected to the thing's website.
* */
public static void showNoTokenDialogOrRedirect(final Context context, final String url) {
if (hasAPICredentials()) {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.no_flattr_token_title); builder.setTitle(R.string.no_flattr_token_title);
builder.setMessage(R.string.no_flattr_token_msg); builder.setMessage(R.string.no_flattr_token_msg);
@ -213,7 +235,9 @@ public class FlattrUtils {
FlattrAuthActivity.class)); FlattrAuthActivity.class));
} }
}); }
);
builder.setNegativeButton(R.string.visit_website_label, builder.setNegativeButton(R.string.visit_website_label,
new OnClickListener() { new OnClickListener() {
@ -224,8 +248,13 @@ public class FlattrUtils {
uri)); uri));
} }
}); }
);
builder.create().show(); builder.create().show();
} else {
Uri uri = Uri.parse(url);
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} }
public static void showForbiddenDialog(final Context context, public static void showForbiddenDialog(final Context context,
@ -242,7 +271,8 @@ public class FlattrUtils {
FlattrAuthActivity.class)); FlattrAuthActivity.class));
} }
}); }
);
builder.setNegativeButton(R.string.visit_website_label, builder.setNegativeButton(R.string.visit_website_label,
new OnClickListener() { new OnClickListener() {
@ -253,7 +283,8 @@ public class FlattrUtils {
uri)); uri));
} }
}); }
);
builder.create().show(); builder.create().show();
} }