2323import android .graphics .drawable .Drawable ;
2424import android .os .Bundle ;
2525import android .os .Parcelable ;
26+ import android .text .TextUtils ;
2627import android .util .Log ;
2728import android .view .LayoutInflater ;
2829import android .view .View ;
@@ -57,25 +58,68 @@ public class ChooseTypeAndAccountActivity extends Activity {
5758 * that match the types in this list, if this parameter is supplied. This list is also
5859 * used to filter the allowable account types if add account is selected.
5960 */
60- public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST = "allowableAccountTypes" ;
61+ public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes" ;
6162
6263 /**
63- * This is passed as the options bundle in AccountManager.addAccount() if it is called.
64+ * This is passed as the addAccountOptions parameter in AccountManager.addAccount()
65+ * if it is called.
6466 */
6567 public static final String EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE = "addAccountOptions" ;
6668
69+ /**
70+ * This is passed as the requiredFeatures parameter in AccountManager.addAccount()
71+ * if it is called.
72+ */
73+ public static final String EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY =
74+ "addAccountRequiredFeatures" ;
75+
76+ /**
77+ * This is passed as the authTokenType string in AccountManager.addAccount()
78+ * if it is called.
79+ */
80+ public static final String EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING = "authTokenType" ;
81+
6782 /**
6883 * If set then the specified account is already "selected".
6984 */
7085 public static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount" ;
7186
87+ /**
88+ * If true then display the account selection list even if there is just
89+ * one account to choose from. boolean.
90+ */
91+ public static final String EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT =
92+ "alwaysPromptForAccount" ;
93+
94+ /**
95+ * If set then this string willb e used as the description rather than
96+ * the default.
97+ */
98+ public static final String EXTRA_DESCRIPTION_TEXT_OVERRIDE =
99+ "descriptionTextOverride" ;
100+
72101 private ArrayList <AccountInfo > mAccountInfos ;
73102
74103 @ Override
75104 public void onCreate (Bundle savedInstanceState ) {
76105 super .onCreate (savedInstanceState );
77106 setContentView (R .layout .choose_type_and_account );
107+
108+ // save some items we use frequently
78109 final AccountManager accountManager = AccountManager .get (this );
110+ final Intent intent = getIntent ();
111+
112+ // override the description text if supplied
113+ final String descriptionOverride =
114+ intent .getStringExtra (EXTRA_DESCRIPTION_TEXT_OVERRIDE );
115+ if (!TextUtils .isEmpty (descriptionOverride )) {
116+ ((TextView )findViewById (R .id .description )).setText (descriptionOverride );
117+ }
118+
119+ // If the selected account matches one in the list we will place a
120+ // checkmark next to it.
121+ final Account selectedAccount =
122+ (Account )intent .getParcelableExtra (EXTRA_SELECTED_ACCOUNT );
79123
80124 // build an efficiently queryable map of account types to authenticator descriptions
81125 final HashMap <String , AuthenticatorDescription > typeToAuthDescription =
@@ -87,7 +131,7 @@ public void onCreate(Bundle savedInstanceState) {
87131 // Read the validAccounts, if present, and add them to the setOfAllowableAccounts
88132 Set <Account > setOfAllowableAccounts = null ;
89133 final ArrayList <Parcelable > validAccounts =
90- getIntent () .getParcelableArrayListExtra (EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST );
134+ intent .getParcelableArrayListExtra (EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST );
91135 if (validAccounts != null ) {
92136 setOfAllowableAccounts = new HashSet <Account >(validAccounts .size ());
93137 for (Parcelable parcelable : validAccounts ) {
@@ -98,7 +142,7 @@ public void onCreate(Bundle savedInstanceState) {
98142 // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
99143 Set <String > setOfAllowableAccountTypes = null ;
100144 final ArrayList <String > validAccountTypes =
101- getIntent () .getStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST );
145+ intent .getStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY );
102146 if (validAccountTypes != null ) {
103147 setOfAllowableAccountTypes = new HashSet <String >(validAccountTypes .size ());
104148 for (String type : validAccountTypes ) {
@@ -121,7 +165,8 @@ public void onCreate(Bundle savedInstanceState) {
121165 continue ;
122166 }
123167 mAccountInfos .add (new AccountInfo (account ,
124- getDrawableForType (typeToAuthDescription , account .type )));
168+ getDrawableForType (typeToAuthDescription , account .type ),
169+ account .equals (selectedAccount )));
125170 }
126171
127172 // If there are no allowable accounts go directly to add account
@@ -131,7 +176,8 @@ public void onCreate(Bundle savedInstanceState) {
131176 }
132177
133178 // if there is only one allowable account return it
134- if (mAccountInfos .size () == 1 ) {
179+ if (!intent .getBooleanExtra (EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT , false )
180+ && mAccountInfos .size () == 1 ) {
135181 Account account = mAccountInfos .get (0 ).account ;
136182 setResultAndFinish (account .name , account .type );
137183 return ;
@@ -143,7 +189,6 @@ public void onCreate(Bundle savedInstanceState) {
143189 list .setAdapter (new AccountArrayAdapter (this ,
144190 android .R .layout .simple_list_item_1 , mAccountInfos ));
145191 list .setChoiceMode (ListView .CHOICE_MODE_SINGLE );
146- list .setTextFilterEnabled (false );
147192 list .setOnItemClickListener (new AdapterView .OnItemClickListener () {
148193 public void onItemClick (AdapterView <?> parent , View v , int position , long id ) {
149194 onListItemClick ((ListView )parent , v , position , id );
@@ -173,10 +218,12 @@ protected void onActivityResult(final int requestCode, final int resultCode,
173218 return ;
174219 }
175220 }
221+ Log .d (TAG , "ChooseTypeAndAccountActivity.onActivityResult: canceled" );
176222 setResult (Activity .RESULT_CANCELED );
177223 finish ();
178224 }
179225
226+
180227 private Drawable getDrawableForType (
181228 final HashMap <String , AuthenticatorDescription > typeToAuthDescription ,
182229 String accountType ) {
@@ -212,31 +259,40 @@ private void setResultAndFinish(final String accountName, final String accountTy
212259 bundle .putString (AccountManager .KEY_ACCOUNT_NAME , accountName );
213260 bundle .putString (AccountManager .KEY_ACCOUNT_TYPE , accountType );
214261 setResult (Activity .RESULT_OK , new Intent ().putExtras (bundle ));
262+ Log .d (TAG , "ChooseTypeAndAccountActivity.setResultAndFinish: "
263+ + "selected account " + accountName + ", " + accountType );
215264 finish ();
216265 }
217266
218267 private void startChooseAccountTypeActivity () {
219268 final Intent intent = new Intent (this , ChooseAccountTypeActivity .class );
220- intent .putStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST ,
221- getIntent ().getStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST ));
269+ intent .putStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY ,
270+ getIntent ().getStringArrayListExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY ));
222271 intent .putExtra (EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE ,
223- getIntent ().getBundleExtra (EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST ));
272+ getIntent ().getBundleExtra (EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE ));
273+ intent .putExtra (EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY ,
274+ getIntent ().getStringArrayExtra (EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY ));
275+ intent .putExtra (EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING ,
276+ getIntent ().getStringArrayExtra (EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING ));
224277 startActivityForResult (intent , 0 );
225278 }
226279
227280 private static class AccountInfo {
228281 final Account account ;
229282 final Drawable drawable ;
283+ private final boolean checked ;
230284
231- AccountInfo (Account account , Drawable drawable ) {
285+ AccountInfo (Account account , Drawable drawable , boolean checked ) {
232286 this .account = account ;
233287 this .drawable = drawable ;
288+ this .checked = checked ;
234289 }
235290 }
236291
237292 private static class ViewHolder {
238293 ImageView icon ;
239294 TextView text ;
295+ ImageView checkmark ;
240296 }
241297
242298 private static class AccountArrayAdapter extends ArrayAdapter <AccountInfo > {
@@ -256,18 +312,21 @@ public View getView(int position, View convertView, ViewGroup parent) {
256312 ViewHolder holder ;
257313
258314 if (convertView == null ) {
259- convertView = mLayoutInflater .inflate (R .layout .choose_account_row , null );
315+ convertView = mLayoutInflater .inflate (R .layout .choose_selected_account_row , null );
260316 holder = new ViewHolder ();
261317 holder .text = (TextView ) convertView .findViewById (R .id .account_row_text );
262318 holder .icon = (ImageView ) convertView .findViewById (R .id .account_row_icon );
319+ holder .checkmark = (ImageView ) convertView .findViewById (R .id .account_row_checkmark );
263320 convertView .setTag (holder );
264321 } else {
265322 holder = (ViewHolder ) convertView .getTag ();
266323 }
267324
268325 holder .text .setText (mInfos .get (position ).account .name );
269326 holder .icon .setImageDrawable (mInfos .get (position ).drawable );
270-
327+ final int displayCheckmark =
328+ mInfos .get (position ).checked ? View .VISIBLE : View .INVISIBLE ;
329+ holder .checkmark .setVisibility (displayCheckmark );
271330 return convertView ;
272331 }
273332 }
0 commit comments