summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/flattr/FlattrUtils.java
blob: b1ae64b06a2835875a6d463732af6bb1bd25c9c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package de.danoeh.antennapod.util.flattr;

import java.util.EnumSet;

import org.shredzone.flattr4j.FlattrService;
import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.model.Thing;
import org.shredzone.flattr4j.oauth.AccessToken;
import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
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;
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;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;

/** Utility methods for doing something with flattr. */

public class FlattrUtils {
	private static final String TAG = "FlattrUtils";

	private static final String HOST_NAME = "de.danoeh.antennapod";
	private static final String APP_KEY = "oJ5B1Z90x0gpHbu84F81fWgZWjRpKj4Y";
	private static final String APP_SECRET = "S0qKPpAcfGrNHMLCiKtnwAciJUyj3t8MjomZEN2KkjnSWZk4zEiW4k4D5sNZMLBV";

	private static final String PREF_ACCESS_TOKEN = "de.danoeh.antennapod.preference.flattrAccessToken";

	// Flattr URL for this app.
	public static final String APP_URL = "http://antennapod.com";
	// Human-readable flattr-page.
	public static final String APP_LINK = "https://flattr.com/thing/745609/";
	public static final String APP_THING_ID = "745609";

	private static volatile AccessToken cachedToken;

	private static AndroidAuthenticator createAuthenticator() {
		return new AndroidAuthenticator(HOST_NAME, APP_KEY, APP_SECRET);
	}

	public static void startAuthProcess(Context context) throws FlattrException {
		AndroidAuthenticator auth = createAuthenticator();
		auth.setScope(EnumSet.of(Scope.FLATTR));
		Intent intent = auth.createAuthenticateIntent();
		context.startActivity(intent);
	}

	private static AccessToken retrieveToken() {
		if (cachedToken == null) {
			if (AppConfig.DEBUG) Log.d(TAG, "Retrieving access token");
			String token = PreferenceManager.getDefaultSharedPreferences(
					PodcastApp.getInstance())
					.getString(PREF_ACCESS_TOKEN, null);
			if (token != null) {
				if (AppConfig.DEBUG) Log.d(TAG, "Found access token. Caching.");
				cachedToken = new AccessToken(token);
			} else {
				if (AppConfig.DEBUG) Log.d(TAG, "No access token found");
				return null;
			}
		}
		return cachedToken;

	}

	public static boolean hasToken() {
		return retrieveToken() != null;
	}

	public static void storeToken(AccessToken token) {
		if (AppConfig.DEBUG) Log.d(TAG, "Storing token");
		SharedPreferences.Editor editor = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance()).edit();
		if (token != null) {
			editor.putString(PREF_ACCESS_TOKEN, token.getToken());
		} else {
			editor.putString(PREF_ACCESS_TOKEN, null);
		}
		editor.commit();
		cachedToken = token;
	}

	public static void deleteToken() {
		if (AppConfig.DEBUG) Log.d(TAG, "Deleting flattr token");
		storeToken(null);
	}

	public static Thing getAppThing(Context context) {
		FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
		try {
			Thing thing = fs.getThing(Thing.withId(APP_THING_ID));
			return thing;
		} catch (FlattrException e) {
			e.printStackTrace();
			showErrorDialog(context, e.getMessage());
			return null;
		}
	}

	public static void clickUrl(Context context, String url)
			throws FlattrException {
		if (hasToken()) {
			FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
			fs.click(url);
		} else {
			Log.e(TAG, "clickUrl was called with null access token");
		}
	}

	public static void handleCallback(Context context, Uri uri) {
		AndroidAuthenticator auth = createAuthenticator();
		new FlattrTokenFetcher(context, auth, uri).executeAsync();
	}

	public static void revokeAccessToken(Context context) {
		if (AppConfig.DEBUG) Log.d(TAG, "Revoking access token");
		deleteToken();
		FlattrServiceCreator.deleteFlattrService();
		showRevokeDialog(context);
	}

	// ------------------------------------------------ DIALOGS

	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);
		builder.setNeutralButton(android.R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create().show();
	}

	public static void showNoTokenDialog(final Context context, final String url) {
		if (AppConfig.DEBUG) Log.d(TAG, "Creating showNoTokenDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.no_flattr_token_title);
		builder.setMessage(R.string.no_flattr_token_msg);
		builder.setPositiveButton(R.string.authenticate_now_label,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						context.startActivity(new Intent(context,
								FlattrAuthActivity.class));
					}

				});
		builder.setNegativeButton(R.string.visit_website_label,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Uri uri = Uri.parse(url);
						context.startActivity(new Intent(Intent.ACTION_VIEW,
								uri));
					}

				});
		builder.create().show();
	}

	public static void showForbiddenDialog(final Context context,
			final String url) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.action_forbidden_title);
		builder.setMessage(R.string.action_forbidden_msg);
		builder.setPositiveButton(R.string.authenticate_now_label,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						context.startActivity(new Intent(context,
								FlattrAuthActivity.class));
					}

				});
		builder.setNegativeButton(R.string.visit_website_label,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Uri uri = Uri.parse(url);
						context.startActivity(new Intent(Intent.ACTION_VIEW,
								uri));
					}

				});
		builder.create().show();
	}

	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);
		builder.setNeutralButton(android.R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create().show();
	}
	


}