@@ -119,8 +119,6 @@ public void onCreate(Bundle savedInstanceState) {
119119 + savedInstanceState + ")" );
120120 }
121121
122- setContentView (R .layout .choose_type_and_account );
123-
124122 if (savedInstanceState != null ) {
125123 mPendingRequest = savedInstanceState .getInt (KEY_INSTANCE_STATE_PENDING_REQUEST );
126124 mExistingAccounts =
@@ -164,14 +162,29 @@ public void onCreate(Bundle savedInstanceState) {
164162 }
165163 }
166164
167- // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
168- Set <String > setOfAllowableAccountTypes = null ;
169- final String [] validAccountTypes =
165+ // An account type is relevant iff it is allowed by the caller and supported by the account
166+ // manager.
167+ Set <String > setOfRelevantAccountTypes = null ;
168+ final String [] allowedAccountTypes =
170169 intent .getStringArrayExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY );
171- if (validAccountTypes != null ) {
172- setOfAllowableAccountTypes = new HashSet <String >(validAccountTypes .length );
173- for (String type : validAccountTypes ) {
174- setOfAllowableAccountTypes .add (type );
170+ if (allowedAccountTypes != null ) {
171+
172+ setOfRelevantAccountTypes = new HashSet <String >(allowedAccountTypes .length );
173+ Set <String > setOfAllowedAccountTypes = new HashSet <String >(allowedAccountTypes .length );
174+ for (String type : allowedAccountTypes ) {
175+ setOfAllowedAccountTypes .add (type );
176+ }
177+
178+ AuthenticatorDescription [] descs = AccountManager .get (this ).getAuthenticatorTypes ();
179+ Set <String > supportedAccountTypes = new HashSet <String >(descs .length );
180+ for (AuthenticatorDescription desc : descs ) {
181+ supportedAccountTypes .add (desc .type );
182+ }
183+
184+ for (String acctType : setOfAllowedAccountTypes ) {
185+ if (supportedAccountTypes .contains (acctType )) {
186+ setOfRelevantAccountTypes .add (acctType );
187+ }
175188 }
176189 }
177190
@@ -185,22 +198,46 @@ public void onCreate(Bundle savedInstanceState) {
185198 && !setOfAllowableAccounts .contains (account )) {
186199 continue ;
187200 }
188- if (setOfAllowableAccountTypes != null
189- && !setOfAllowableAccountTypes .contains (account .type )) {
201+ if (setOfRelevantAccountTypes != null
202+ && !setOfRelevantAccountTypes .contains (account .type )) {
190203 continue ;
191204 }
192205 mAccountInfos .add (new AccountInfo (account ,
193206 getDrawableForType (typeToAuthDescription , account .type ),
194207 account .equals (selectedAccount )));
195208 }
196209
210+ if (mPendingRequest == REQUEST_NULL ) {
211+ // If there are no relevant accounts and only one relevant account typoe go directly to
212+ // add account. Otherwise let the user choose.
213+ if (mAccountInfos .isEmpty ()) {
214+ if (setOfRelevantAccountTypes .size () == 1 ) {
215+ runAddAccountForAuthenticator (setOfRelevantAccountTypes .iterator ().next ());
216+ } else {
217+ startChooseAccountTypeActivity ();
218+ }
219+ return ;
220+ }
221+
222+ // if there is only one allowable account return it
223+ if (!intent .getBooleanExtra (EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT , false )
224+ && mAccountInfos .size () == 1 ) {
225+ Account account = mAccountInfos .get (0 ).account ;
226+ setResultAndFinish (account .name , account .type );
227+ return ;
228+ }
229+ }
230+
231+ setContentView (R .layout .choose_type_and_account );
232+
197233 // there is more than one allowable account. initialize the list adapter to allow
198234 // the user to select an account.
199235 ListView list = (ListView ) findViewById (android .R .id .list );
200236 list .setAdapter (new AccountArrayAdapter (this ,
201237 android .R .layout .simple_list_item_1 , mAccountInfos ));
202238 list .setChoiceMode (ListView .CHOICE_MODE_SINGLE );
203239 list .setOnItemClickListener (new AdapterView .OnItemClickListener () {
240+ @ Override
204241 public void onItemClick (AdapterView <?> parent , View v , int position , long id ) {
205242 onListItemClick ((ListView )parent , v , position , id );
206243 }
@@ -209,26 +246,11 @@ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
209246 // set the listener for the addAccount button
210247 Button addAccountButton = (Button ) findViewById (R .id .addAccount );
211248 addAccountButton .setOnClickListener (new View .OnClickListener () {
249+ @ Override
212250 public void onClick (final View v ) {
213251 startChooseAccountTypeActivity ();
214252 }
215253 });
216-
217- if (mPendingRequest == REQUEST_NULL ) {
218- // If there are no allowable accounts go directly to add account
219- if (shouldSkipToChooseAccountTypeFlow ()) {
220- startChooseAccountTypeActivity ();
221- return ;
222- }
223-
224- // if there is only one allowable account return it
225- if (!intent .getBooleanExtra (EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT , false )
226- && mAccountInfos .size () == 1 ) {
227- Account account = mAccountInfos .get (0 ).account ;
228- setResultAndFinish (account .name , account .type );
229- return ;
230- }
231- }
232254 }
233255
234256 @ Override
@@ -267,7 +289,7 @@ protected void onActivityResult(final int requestCode, final int resultCode,
267289 if (resultCode == RESULT_CANCELED ) {
268290 // if cancelling out of addAccount and the original state caused us to skip this,
269291 // finish this activity
270- if (shouldSkipToChooseAccountTypeFlow ()) {
292+ if (mAccountInfos . isEmpty ()) {
271293 setResult (Activity .RESULT_CANCELED );
272294 finish ();
273295 }
@@ -324,14 +346,6 @@ protected void onActivityResult(final int requestCode, final int resultCode,
324346 finish ();
325347 }
326348
327- /**
328- * convenience method to check if we should skip the accounts list display and immediately
329- * jump to the flow that asks the user to select from the account type list
330- */
331- private boolean shouldSkipToChooseAccountTypeFlow () {
332- return mAccountInfos .isEmpty ();
333- }
334-
335349 protected void runAddAccountForAuthenticator (String type ) {
336350 if (Log .isLoggable (TAG , Log .VERBOSE )) {
337351 Log .v (TAG , "runAddAccountForAuthenticator: " + type );
0 commit comments