16
16
package com .github .mobile .accounts ;
17
17
18
18
import static android .accounts .AccountManager .KEY_ACCOUNT_NAME ;
19
+ import static android .content .DialogInterface .BUTTON_POSITIVE ;
19
20
import static android .util .Log .DEBUG ;
20
21
import static com .github .mobile .accounts .AccountConstants .ACCOUNT_TYPE ;
21
22
import android .accounts .Account ;
25
26
import android .accounts .AuthenticatorException ;
26
27
import android .accounts .OperationCanceledException ;
27
28
import android .app .Activity ;
29
+ import android .app .AlertDialog ;
28
30
import android .content .Context ;
31
+ import android .content .DialogInterface ;
32
+ import android .content .DialogInterface .OnCancelListener ;
33
+ import android .content .DialogInterface .OnClickListener ;
29
34
import android .os .Bundle ;
30
35
import android .util .Log ;
31
36
37
+ import com .github .mobile .R .string ;
38
+ import com .github .mobile .ui .LightAlertDialog ;
39
+
32
40
import java .io .IOException ;
41
+ import java .util .ArrayList ;
42
+ import java .util .List ;
33
43
34
44
import org .eclipse .egit .github .core .User ;
35
45
@@ -40,6 +50,11 @@ public class AccountUtils {
40
50
41
51
private static final String TAG = "AccountUtils" ;
42
52
53
+ private static class AuthenticatorConflictException extends IOException {
54
+
55
+ private static final long serialVersionUID = 641279204734869183L ;
56
+ }
57
+
43
58
/**
44
59
* Is the given user the owner of the default account?
45
60
*
@@ -87,7 +102,28 @@ private static Account[] getAccounts(final AccountManager manager)
87
102
final AccountManagerFuture <Account []> future = manager
88
103
.getAccountsByTypeAndFeatures (ACCOUNT_TYPE , null , null , null );
89
104
final Account [] accounts = future .getResult ();
90
- return accounts != null ? accounts : new Account [0 ];
105
+ if (accounts != null && accounts .length > 0 )
106
+ return getPasswordAccessibleAccounts (manager , accounts );
107
+ else
108
+ return new Account [0 ];
109
+ }
110
+
111
+ private static Account [] getPasswordAccessibleAccounts (
112
+ final AccountManager manager , final Account [] candidates )
113
+ throws AuthenticatorConflictException {
114
+ final List <Account > accessible = new ArrayList <Account >(
115
+ candidates .length );
116
+ boolean exceptionThrown = false ;
117
+ for (Account account : candidates )
118
+ try {
119
+ manager .getPassword (account );
120
+ accessible .add (account );
121
+ } catch (SecurityException ignored ) {
122
+ exceptionThrown = true ;
123
+ }
124
+ if (accessible .isEmpty () && exceptionThrown )
125
+ throw new AuthenticatorConflictException ();
126
+ return accessible .toArray (new Account [accessible .size ()]);
91
127
}
92
128
93
129
/**
@@ -96,9 +132,11 @@ private static Account[] getAccounts(final AccountManager manager)
96
132
* @param manager
97
133
* @param activity
98
134
* @return account
135
+ * @throws IOException
136
+ * @throws AccountsException
99
137
*/
100
138
public static Account getAccount (final AccountManager manager ,
101
- final Activity activity ) {
139
+ final Activity activity ) throws IOException , AccountsException {
102
140
final boolean loggable = Log .isLoggable (TAG , DEBUG );
103
141
if (loggable )
104
142
Log .d (TAG , "Getting account" );
@@ -123,18 +161,55 @@ public static Account getAccount(final AccountManager manager,
123
161
} catch (OperationCanceledException e ) {
124
162
Log .d (TAG , "Excepting retrieving account" , e );
125
163
activity .finish ();
126
- throw new RuntimeException ( e ) ;
164
+ throw e ;
127
165
} catch (AccountsException e ) {
128
166
Log .d (TAG , "Excepting retrieving account" , e );
129
- throw new RuntimeException (e );
167
+ throw e ;
168
+ } catch (AuthenticatorConflictException e ) {
169
+ activity .runOnUiThread (new Runnable () {
170
+
171
+ public void run () {
172
+ showConflictMessage (activity );
173
+ }
174
+ });
175
+ throw e ;
130
176
} catch (IOException e ) {
131
177
Log .d (TAG , "Excepting retrieving account" , e );
132
- throw new RuntimeException ( e ) ;
178
+ throw e ;
133
179
}
134
180
135
181
if (loggable )
136
182
Log .d (TAG , "Returning account " + accounts [0 ].name );
137
183
138
184
return accounts [0 ];
139
185
}
186
+
187
+ /**
188
+ * Show conflict message about previously registered authenticator from
189
+ * another application
190
+ *
191
+ * @param activity
192
+ */
193
+ private static void showConflictMessage (final Activity activity ) {
194
+ AlertDialog dialog = LightAlertDialog .create (activity );
195
+ dialog .setTitle (activity .getString (string .authenticator_conflict_title ));
196
+ dialog .setMessage (activity
197
+ .getString (string .authenticator_conflict_message ));
198
+ dialog .setOnCancelListener (new OnCancelListener () {
199
+
200
+ @ Override
201
+ public void onCancel (DialogInterface dialog ) {
202
+ activity .finish ();
203
+ }
204
+ });
205
+ dialog .setButton (BUTTON_POSITIVE ,
206
+ activity .getString (android .R .string .ok ), new OnClickListener () {
207
+
208
+ @ Override
209
+ public void onClick (DialogInterface dialog , int which ) {
210
+ activity .finish ();
211
+ }
212
+ });
213
+ dialog .show ();
214
+ }
140
215
}
0 commit comments