8000 refactored API to have a smaller footprint by using a callback · crankycoder/fxa-android@ba0bc0c · GitHub
[go: up one dir, main page]

Skip to content

Commit ba0bc0c

Browse files
committed
refactored API to have a smaller footprint by using a callback
interface
1 parent 4863219 commit ba0bc0c

File tree

3 files changed

+231
-83
lines changed

3 files changed

+231
-83
lines changed
Lines changed: 64 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
package org.mozilla.firefoxaccounts;
22

33
import android.app.Activity;
4-
import android.content.BroadcastReceiver;
5-
import android.content.Context;
6-
import android.content.Intent;
7-
import android.content.IntentFilter;
84
import android.os.Bundle;
9-
import android.support.v4.content.LocalBroadcastManager;
105
import android.util.Log;
116
import android.view.View;
127
import android.webkit.CookieManager;
138
import android.webkit.CookieSyncManager;
149

15-
import org.json.JSONException;
1610
import org.json.JSONObject;
17-
import org.mozilla.accounts.fxa.FxAGlobals;
18-
import org.mozilla.accounts.fxa.Intents;
1911
import org.mozilla.accounts.fxa.LoggerUtil;
12+
import org.mozilla.accounts.fxa.IFxaCallback;
13+
14+
import org.mozilla.accounts.fxa.FxAGlobals;
2015
import org.mozilla.accounts.fxa.dialog.DevOAuthDialog;
2116
import org.mozilla.accounts.fxa.tasks.dev.DevDestroyOAuthTask;
2217
import org.mozilla.accounts.fxa.tasks.dev.DevRetrieveProfileTask;
2318
import org.mozilla.accounts.fxa.tasks.dev.DevSetDisplayNameTask;
2419
import org.mozilla.accounts.fxa.tasks.dev.DevVerifyOAuthTask;
2520

26-
public class MainApp extends Activity {
21+
public class MainApp
22+
extends Activity
23+
implements IFxaCallback {
2724
private static final String LOG_TAG = LoggerUtil.makeLogTag(MainApp.class);
2825

2926
// These secrets are provisioned from the FxA dashboard
@@ -32,74 +29,8 @@ public class MainApp extends Activity {
3229
// And finally the callback endpoint on our web application
3330
// Example server endpoint code is available under the `sample_endpoint` subdirectory.
3431
public final String FXA_APP_CALLBACK = "http://ec2-52-1-93-147.compute-1.amazonaws.com/fxa/callback";
35-
3632
static String BEARER_TOKEN = null;
3733

38-
private final BroadcastReceiver callbackReceiver = new BroadcastReceiver() {
39-
@Override
40-
public void onReceive(Context context, Intent intent) {
41-
if (intent.getAction().equals(Intents.ORG_MOZILLA_ACCOUNTS_FXA_BEARER_TOKEN)) {
42-
processBearerToken(intent);
43-
} else if (intent.getAction().equals(Intents.PROFILE_READ)) {
44-
processProfile(intent);
45-
} else if (intent.getAction().equals(Intents.OAUTH_VERIFY)) {
46-
Log.i(LOG_TAG, "OAuth verification success!");
47-
DevSetDisplayNameTask task = new DevSetDisplayNameTask(getApplicationContext());
48-
task.execute( 6D47 BEARER_TOKEN, "FxA_testing");
49-
} else if (intent.getAction().equals(Intents.OAUTH_VERIFY_FAIL)) {
50-
Log.i(LOG_TAG, "OAuth verification failure!");
51-
} else if (intent.getAction().equals(Intents.OAUTH_DESTROY)) {
52-
Log.i(LOG_TAG, "OAuth destruction of bearer token succeeded!");
53-
} else if (intent.getAction().equals(Intents.OAUTH_DESTROY_FAIL)) {
54-
Log.i(LOG_TAG, "OAuth destruction of bearer token failed");
55-
} else if (intent.getAction().equals(Intents.DISPLAY_NAME_WRITE)) {
56-
Log.i(LOG_TAG, "Display name was updated!");
57-
Log.i(LOG_TAG, "Display name update response: " + intent.getStringExtra("json"));
58-
DevDestroyOAuthTask task = new DevDestroyOAuthTask(getApplicationContext());
59-
task.execute(BEARER_TOKEN);
60-
} else if (intent.getAction().equals(Intents.DISPLAY_NAME_WRITE_FAILURE)) {
61-
Log.i(LOG_TAG, "Display name was update failed!");
62-
} else {
63-
Log.w(LOG_TAG, "Unexpected intent: " + intent);
64-
}
65-
}
66-
67-
private void processProfile(Intent intent) {
68-
String jsonBlob = intent.getStringExtra("json");
69-
Log.i(LOG_TAG, "Profile response body: " + jsonBlob);
70-
71-
// Fire off the verify OAuth task
72-
DevVerifyOAuthTask task = new DevVerifyOAuthTask(getApplicationContext());
73-
task.execute(BEARER_TOKEN);
74-
}
75-
76-
private void processBearerToken(Intent intent) {
77-
/*
78-
Sample JSON that you might get back
79-
{"access_token":"fadf25f84838877d6eb03563f501abfac62c0a01aaf98b34eec1b28e888b02a2",
80-
"token_type":"bearer",
81-
"scope":"profile:email",
82-
"auth_at":1432917700}
83-
*/
84-
85-
String jsonBlob = intent.getStringExtra("json");
86-
if (jsonBlob == null) {
87-
Log.w(LOG_TAG, "error extracting json data");
88-
return;
89-
}
90-
JSONObject authJSON = null;
91-
try {
92-
authJSON = new JSONObject(jsonBlob);
93-
Log.i(LOG_TAG, "Login yielded this JSON blob: " + authJSON);
94-
BEARER_TOKEN = authJSON.getString("access_token");
95-
DevRetrieveProfileTask task = new DevRetrieveProfileTask(getApplicationContext());
96-
task.execute(BEARER_TOKEN);
97-
} catch (JSONException e) {
98-
Log.e(LOG_TAG, "Error fetching bearer token. JSON = [" + authJSON + "]", e);
99-
}
100-
}
101-
};
102-
10334
@Override
10435
public void onCreate(Bundle savedInstanceState) {
10536
super.onCreate(savedInstanceState);
@@ -108,14 +39,6 @@ public void onCreate(Bundle savedInstanceState) {
10839
FxAGlobals.initFxaLogin(this, app_name);
10940

11041
setContentView(R.layout.appmainexample);
111-
112-
IntentFilter intentFilter = new IntentFilter();
113-
Intents.registerFxaIntents(intentFilter);
114-
115-
116-
LocalBroadcastManager
117-
.getInstance(getApplicationContext())
118-
.registerReceiver(callbackReceiver, intentFilter);
11942
}
12043

12144
/*
@@ -137,4 +60,62 @@ public void onClick_fxaLogin(View v) {
13760
scopes,
13861
FXA_APP_KEY).show();
13962
}
63+
64+
65+
/*
66+
* These are callbacks that the FxA library will callback on when network activity is complete.
67+
*/
68+
@Override
69+
public void acceptFxaBearerToken(String token) {
70+
//Note that we have to save the token right away
71+
BEARER_TOKEN = token;
72+
73+
DevRetrieveProfileTask task = new DevRetrieveProfileTask(getApplicationContext());
74+
task.execute(BEARER_TOKEN);
75+
}
76+
77+
@Override
78+
public void acceptProfile(JSONObject profileJson) {
79+
// Fire off the verify OAuth task
80+
DevVerifyOAuthTask task = new DevVerifyOAuthTask(getApplicationContext());
81+
task.execute(BEARER_TOKEN);
82+
}
83+
84+
@Override
85+
public void profileReadFailure() {
86+
// do nothing for the demo.
87+
}
88+
89+
@Override
90+
public void acceptOAuthVerified() {
91+
Log.i(LOG_TAG, "OAuth verification success!");
92+
DevSetDisplayNameTask task = new DevSetDisplayNameTask(getApplicationContext());
93+
task.execute(BEARER_TOKEN, "FxA_testing");
94+
}
95+
96+
@Override
97+
public void oauthVerificationFailure() {
98+
Log.i(LOG_TAG, "OAuth verification failure!");
99+
}
100+
101+
@Override
102+
public void acceptOAuthDestroy() {
103+
Log.i(LOG_TAG, "OAuth destruction of bearer token succeeded!");
104+
}
105+
106+
@Override
107+
public void oauthDestroyFailure() {
108+
Log.i(LOG_TAG, "OAuth destruction of bearer token failed");
109+
}
110+
111+
@Override
112+
public void acceptDisplayNameWrite() {
113+
DevDestroyOAuthTask task = new DevDestroyOAuthTask(getApplicationContext());
114+
task.execute(BEARER_TOKEN);
115+
}
116+
117+
@Override
118+
public void acceptDisplayNameWriteFailure() {
119+
Log.i(LOG_TAG, "Display name was update failed!");
120+
}
140121
}

libraries/fxa/src/main/java/org/mozilla/accounts/fxa/FxAGlobals.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,159 @@
44

55
package org.mozilla.accounts.fxa;
66

7+
import android.content.BroadcastReceiver;
78
import android.content.Context;
9+
import android.content.Intent;
10+
import android.content.IntentFilter;
11+
import android.support.v4.content.LocalBroadcastManager;
12+
import android.text.TextUtils;
13+
import android.util.Log;
14+
15+
import org.json.JSONObject;
816

917
public class FxAGlobals {
1018

19+
private static final String LOG_TAG = LoggerUtil.makeLogTag(FxAGlobals.class);
20+
1121
public static String appVersionName;
1222
public static int appVersionCode;
1323
public static String appName;
1424

25+
26+
// Collapse these into one interface
27+
static IFxaCallback callback;
28+
29+
public final static BroadcastReceiver fxaCallbackReceiver = new BroadcastReceiver() {
30+
@Override
31+
public void onReceive(Context context, Intent intent) {
32+
if (intent.getAction().equals(Intents.ORG_MOZILLA_ACCOUNTS_FXA_BEARER_TOKEN)) {
33+
processBearerToken(intent);
34+
} else if (intent.getAction().equals(Intents.PROFILE_READ)) {
35+
processProfile(context, intent);
36+
} else if (intent.getAction().equals(Intents.PROFILE_READ_FAILURE)) {
37+
// bad profile reads automatically logout the user
38+
processProfileReadFail();
39+
} else if (intent.getAction().equals(Intents.OAUTH_DESTROY)) {
40+
processOAuthDestroy();
41+
} else if (intent.getAction().equals(Intents.OAUTH_DESTROY_FAIL)) {
42+
// I don't care. Clear the login state even if fxa logout 'fails'
43+
processOAuthDestroyFailure();
44+
} else if (intent.getAction().equals(Intents.DISPLAY_NAME_WRITE)) {
45+
processDisplayNameWrite();
46+
} else if (intent.getAction().equals(Intents.DISPLAY_NAME_WRITE_FAILURE)) {
47+
processDisplayNameWriteFailure();
48+
} else if (intent.getAction().equals(Intents.OAUTH_VERIFY)) {
49+
// Do nothing
50+
processOAuthVerify();
51+
} else if (intent.getAction().equals(Intents.OAUTH_VERIFY_FAIL)) {
52+
processOAuthVerifyFailure();
53+
}
54+
}
55+
};
56+
57+
1558
public static void initFxaLogin(Context ctx, String app_name) {
1659
// Clobber the FxAGlobals so that the user-agent for the FxA client will be sensible
1760
FxAGlobals.appVersionName = BuildConfig.VERSION_NAME;
1861
FxAGlobals.appVersionCode = BuildConfig.VERSION_CODE;
1962
FxAGlobals.appName = app_name;
63+
64+
65+
IntentFilter intentFilter = new IntentFilter();
66+
Intents.registerFxaIntents(intentFilter);
67+
68+
69+
LocalBroadcastManager
70+
.getInstance(ctx.getApplicationContext())
71+
.registerReceiver(fxaCallbackReceiver, intentFilter);
72+
}
73+
74+
public static void register(IFxaCallback client) {
75+
callback = client;
76+
}
77+
78+
79+
private static void processProfile(Context ctx, Intent intent) {
80+
if (callback == null) {
81+
return;
82+
}
83+
84+
try {
85+
String jsonBlob = intent.getStringExtra("json");
86+
callback.acceptProfile(new JSONObject(jsonBlob));
87+
} catch (Exception e) {
88+
Log.e(LOG_TAG, "Error processing JSON", e);
89+
}
90+
}
91+
92+
93+
private static void processProfileReadFail() {
94+
if (callback == null) {
95+
return;
96+
}
97+
callback.profileReadFailure();
98+
}
99+
100+
private static void processBearerToken(Intent intent) {
101+
if (callback == null) {
102+
return;
103+
}
104+
105+
String jsonBlob = intent.getStringExtra("json");
106+
if (TextUtils.isEmpty(jsonBlob)) {
107+
Log.w(LOG_TAG, "error extracting json data");
108+
return;
109+
}
110+
JSONObject authJSON = null;
111+
try {
112+
authJSON = new JSONObject(jsonBlob);
113+
callback.acceptFxaBearerToken(authJSON.getString("access_token"));
114+
} catch (Exception e) {
115+
Log.e(LOG_TAG, "Error fetching bearer token.", e);
116+
return;
117+
}
118+
}
119+
120+
private static void processOAuthVerifyFailure() {
121+
if (callback == null) {
122+
return;
123+
}
124+
callback.oauthVerificationFailure();
125+
}
126+
127+
private static void processOAuthVerify() {
128+
if (callback == null) {
129+
return;
130+
}
131+
callback.acceptOAuthVerified();
132+
}
133+
134+
private static void processOAuthDestroyFailure() {
135+
if (callback == null) {
136+
return;
137+
}
138+
callback.acceptOAuthDestroy();
139+
}
140+
141+
private static void processOAuthDestroy() {
142+
if (callback == null) {
143+
return;
144+
}
145+
callback.oauthDestroyFailure();
146+
}
147+
148+
private static void processDisplayNameWriteFailure() {
149+
if (callback == null) {
150+
return;
151+
}
152+
callback.acceptDisplayNameWrite();
153+
}
154+
155+
private static void processDisplayNameWrite() {
156+
if (callback == null) {
157+
return;
158+
}
159+
callback.acceptDisplayNameWriteFailure();
20160
}
21161
}
22162

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.accounts.fxa;
6+
7+
import org.json.JSONObject;
8+
9+
public interface IFxaCallback {
10+
void acceptFxaBearerToken(String token);
11+
12+
void acceptDisplayNameWrite();
13+
14+
void acceptDisplayNameWriteFailure();
15+
16+
void acceptOAuthDestroy();
17+
18+
void oauthDestroyFailure();
19+
20+
void acceptOAuthVerified();
21+
22+
void oauthVerificationFailure();
23+
24+
void acceptProfile(JSONObject profileJson);
25+
26+
void profileReadFailure();
27+
}

0 commit comments

Comments
 (0)
0