24
24
public class Main1 extends Activity {
25
25
26
26
private String TAG = this .getClass ().getSimpleName ();
27
- private android .os .Handler mHandler = new android .os .Handler ();
28
27
private AccountManager mAccountManager ;
29
28
30
29
@ Override
@@ -57,7 +56,6 @@ public void onClick(View v) {
57
56
findViewById (R .id .btnInvalidateAuthToken ).setOnClickListener (new View .OnClickListener () {
58
57
@ Override
59
58
public void onClick (View v ) {
60
- Log .d (TAG , "Dsds" );
61
59
showAccountPicker (AUTHTOKEN_TYPE_FULL_ACCESS , true );
62
60
}
63
61
});
@@ -75,22 +73,12 @@ private void addNewAccount(String accountType, String authTokenType) {
75
73
public void run (AccountManagerFuture <Bundle > future ) {
76
74
try {
77
75
Bundle bnd = future .getResult ();
78
- runOnUiThread (new Runnable () {
79
- @ Override
80
- public void run () {
81
- //To change body of implemented methods use File | Settings | File Templates.
82
- Toast .makeText (getBaseContext (), "Account was created" , Toast .LENGTH_SHORT ).show ();
83
- }
84
- });
85
-
86
- Log .d ("udini" , "AddNewAccount Bundle is " + bnd );
76
+ showMessage ("Account was created" );
77
+ Log .d ("udinic" , "AddNewAccount Bundle is " + bnd );
87
78
88
- } catch (OperationCanceledException e ) {
89
- e .printStackTrace ();
90
- } catch (IOException e ) {
91
- e .printStackTrace ();
92
- } catch (AuthenticatorException e ) {
79
+ } catch (Exception e ) {
93
80
e .printStackTrace ();
81
+ showMessage (e .getMessage ());
94
82
}
95
83
}
96
84
}, null );
@@ -131,7 +119,7 @@ public void onClick(DialogInterface dialog, int which) {
131
119
* @param authTokenType
132
120
*/
133
121
private void getExistingAccountAuthToken (Account account , String authTokenType ) {
134
- final AccountManagerFuture <Bundle > future = mAccountManager .getAuthToken (account , authTokenType , null , this , null ,null );
122
+ final AccountManagerFuture <Bundle > future = mAccountManager .getAuthToken (account , authTokenType , null , this , null , null );
135
123
136
124
new Thread (new Runnable () {
137
125
@ Override
@@ -140,26 +128,21 @@ public void run() {
140
128
Bundle bnd = future .getResult ();
141
129
142
130
final String authtoken = bnd .getString (AccountManager .KEY_AUTHTOKEN );
143
- runOnUiThread (new Runnable () {
144
- @ Override
145
- public void run () {
146
- //To change body of implemented methods use File | Settings | File Templates.
147
- Toast .makeText (getBaseContext (), ((authtoken != null ) ? "SUCCESS!" : "FAIL" ), Toast .LENGTH_SHORT ).show ();
148
- }
149
- });
150
- Log .d ("udini" , "GetToken Bundle is " + bnd );
151
- } catch (OperationCanceledException e ) {
152
- e .printStackTrace ();
153
- } catch (IOException e ) {
154
- e .printStackTrace ();
155
- } catch (AuthenticatorException e ) {
131
+ showMessage ((authtoken != null ) ? "SUCCESS!\n token: " + authtoken : "FAIL" );
132
+ Log .d ("udinic" , "GetToken Bundle is " + bnd );
133
+ } catch (Exception e ) {
156
134
e .printStackTrace ();
135
+ showMessage (e .getMessage ());
157
136
}
158
-
159
137
}
160
138
}).start ();
161
139
}
162
140
141
+ /**
142
+ * Invalidates the auth token for the account
143
+ * @param account
144
+ * @param authTokenType
145
+ */
163
146
private void invalidateAuthToken (final Account account , String authTokenType ) {
164
147
final AccountManagerFuture <Bundle > future = mAccountManager .getAuthToken (account , authTokenType , null , this , null ,null );
165
148
@@ -171,21 +154,11 @@ public void run() {
171
154
172
155
final String authtoken = bnd .getString (AccountManager .KEY_AUTHTOKEN );
173
156
mAccountManager .invalidateAuthToken (account .type , authtoken );
174
- runOnUiThread (new Runnable () {
175
- @ Override
176
- public void run () {
177
- Toast .makeText (getBaseContext (), account .name + " invalidated" , Toast .LENGTH_SHORT ).show ();
178
- }
179
- });
180
- Log .d ("udini" , "GetToken Bundle is " + bnd );
181
- } catch (OperationCanceledException e ) {
182
- e .printStackTrace ();
183
- } catch (IOException e ) {
184
- e .printStackTrace ();
185
- } catch (AuthenticatorException e ) {
157
+ showMessage (account .name + " invalidated" );
158
+ } catch (Exception e ) {
186
159
e .printStackTrace ();
160
+ showMessage (e .getMessage ());
187
161
}
188
-
189
162
}
190
163
}).start ();
191
164
}
@@ -207,24 +180,27 @@ public void run(AccountManagerFuture<Bundle> future) {
207
180
try {
208
181
bnd = future .getResult ();
209
182
final String authtoken = bnd .getString (AccountManager .KEY_AUTHTOKEN );
210
- runOnUiThread (new Runnable () {
211
- @ Override
212
- public void run () {
213
- //To change body of implemented methods use File | Settings | File Templates.
214
- Toast .makeText (getBaseContext (), ((authtoken != null ) ? "SUCCESS!" : "FAIL" ), Toast .LENGTH_SHORT ).show ();
215
- }
216
- });
217
- Log .d ("udini" , "GetTokenForAccount Bundle is " + bnd );
218
-
219
- } catch (OperationCanceledException e ) {
220
- e .printStackTrace ();
221
- } catch (IOException e ) {
222
- e .printStackTrace ();
223
- } catch (AuthenticatorException e ) {
183
+ showMessage (((authtoken != null ) ? "SUCCESS!\n token: " + authtoken : "FAIL" ));
184
+ Log .d ("udinic" , "GetTokenForAccount Bundle is " + bnd );
185
+
186
+ } catch (Exception e ) {
224
187
e .printStackTrace ();
188
+ showMessage (e .getMessage ());
225
189
}
226
190
}
227
191
}
228
192
, null );
229
193
}
194
+
195
+ private void showMessage (final String msg ) {
196
+ if (msg == null || msg .trim ().equals ("" ))
197
+ return ;
198
+
199
+ runOnUiThread (new Runnable () {
200
+ @ Override
201
+ public void run () {
202
+ Toast .makeText (getBaseContext (), msg , Toast .LENGTH_SHORT ).show ();
203
+ }
204
+ });
205
+ }
230
206
}
0 commit comments