1
1
package org .mozilla .firefoxaccounts ;
2
2
3
3
import android .app .Activity ;
4
- import android .content .BroadcastReceiver ;
5
- import android .content .Context ;
6
- import android .content .Intent ;
7
- import android .content .IntentFilter ;
8
4
import android .os .Bundle ;
9
- import android .support .v4 .content .LocalBroadcastManager ;
10
5
import android .util .Log ;
11
6
import android .view .View ;
12
7
import android .webkit .CookieManager ;
13
8
import android .webkit .CookieSyncManager ;
14
9
15
- import org .json .JSONException ;
16
10
import org .json .JSONObject ;
17
- import org .mozilla .accounts .fxa .FxAGlobals ;
18
- import org .mozilla .accounts .fxa .Intents ;
19
11
import org .mozilla .accounts .fxa .LoggerUtil ;
12
+ import org .mozilla .accounts .fxa .IFxaCallback ;
13
+
14
+ import org .mozilla .accounts .fxa .FxAGlobals ;
20
15
import org .mozilla .accounts .fxa .dialog .DevOAuthDialog ;
21
16
import org .mozilla .accounts .fxa .tasks .dev .DevDestroyOAuthTask ;
22
17
import org .mozilla .accounts .fxa .tasks .dev .DevRetrieveProfileTask ;
23
18
import org .mozilla .accounts .fxa .tasks .dev .DevSetDisplayNameTask ;
24
19
import org .mozilla .accounts .fxa .tasks .dev .DevVerifyOAuthTask ;
25
20
26
- public class MainApp extends Activity {
21
+ public class MainApp
22
+ extends Activity
23
+ implements IFxaCallback {
27
24
private static final String LOG_TAG = LoggerUtil .makeLogTag (MainApp .class );
28
25
29
26
// These secrets are provisioned from the FxA dashboard
@@ -32,74 +29,8 @@ public class MainApp extends Activity {
32
29
// And finally the callback endpoint on our web application
33
30
// Example server endpoint code is available under the `sample_endpoint` subdirectory.
34
31
public final String FXA_APP_CALLBACK = "http://ec2-52-1-93-147.compute-1.amazonaws.com/fxa/callback" ;
35
-
36
32
static String BEARER_TOKEN = null ;
37
33
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
-
103
34
@ Override
104
35
public void onCreate (Bundle savedInstanceState ) {
105
36
super .onCreate (savedInstanceState );
@@ -108,14 +39,6 @@ public void onCreate(Bundle savedInstanceState) {
108
39
FxAGlobals .initFxaLogin (this , app_name );
109
40
110
41
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 );
119
42
}
120
43
121
44
/*
@@ -137,4 +60,62 @@ public void onClick_fxaLogin(View v) {
137
60
scopes ,
138
61
FXA_APP_KEY ).show ();
139
62
}
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
+ }
140
121
}
0 commit comments