Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.accounts; |
| 17 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageManager; |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
| 24 | import android.os.Parcelable; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.AdapterView; |
| 30 | import android.widget.ArrayAdapter; |
| 31 | import android.widget.ImageView; |
| 32 | import android.widget.ListView; |
| 33 | import android.widget.TextView; |
| 34 | import com.android.internal.R; |
| 35 | |
| 36 | import java.util.HashMap; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 37 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @hide |
| 40 | */ |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 41 | public class ChooseAccountActivity extends Activity { |
| 42 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 43 | private static final String TAG = "AccountManager"; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 44 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 45 | private Parcelable[] mAccounts = null; |
| 46 | private AccountManagerResponse mAccountManagerResponse = null; |
| 47 | private Bundle mResult; |
| 48 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 49 | private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription |
| 50 | = new HashMap<String, AuthenticatorDescription>(); |
| 51 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 52 | @Override |
| 53 | public void onCreate(Bundle savedInstanceState) { |
| 54 | super.onCreate(savedInstanceState); |
| 55 | |
Fred Quintana | 2622261 | 2010-03-01 13:45:22 -0800 | [diff] [blame] | 56 | mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS); |
| 57 | mAccountManagerResponse = |
| 58 | getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE); |
| 59 | |
| 60 | // KEY_ACCOUNTS is a required parameter |
| 61 | if (mAccounts == null) { |
| 62 | setResult(RESULT_CANCELED); |
| 63 | finish(); |
| 64 | return; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 67 | getAuthDescriptions(); |
| 68 | |
| 69 | AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length]; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 70 | for (int i = 0; i < mAccounts.length; i++) { |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 71 | mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name, |
| 72 | getDrawableForType(((Account) mAccounts[i]).type)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 75 | setContentView(R.layout.choose_account); |
| 76 | |
| 77 | // Setup the list |
| 78 | ListView list = (ListView) findViewById(android.R.id.list); |
| 79 | // Use an existing ListAdapter that will map an array of strings to TextViews |
| 80 | list.setAdapter(new AccountArrayAdapter(this, |
| 81 | android.R.layout.simple_list_item_1, mAccountInfos)); |
| 82 | list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 83 | list.setTextFilterEnabled(true); |
| 84 | list.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 85 | public void onItemClick(AdapterView<?> parent, View v, int position, long id) { |
| 86 | onListItemClick((ListView)parent, v, position, id); |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | private void getAuthDescriptions() { |
| 92 | for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) { |
| 93 | mTypeToAuthDescription.put(desc.type, desc); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private Drawable getDrawableForType(String accountType) { |
| 98 | Drawable icon = null; |
| 99 | if(mTypeToAuthDescription.containsKey(accountType)) { |
| 100 | try { |
| 101 | AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); |
| 102 | Context authContext = createPackageContext(desc.packageName, 0); |
| 103 | icon = authContext.getResources().getDrawable(desc.iconId); |
| 104 | } catch (PackageManager.NameNotFoundException e) { |
| 105 | // Nothing we can do much here, just log |
| 106 | if (Log.isLoggable(TAG, Log.WARN)) { |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 107 | Log.w(TAG, "No icon name for account type " + accountType); |
| 108 | } |
| 109 | } catch (Resources.NotFoundException e) { |
| 110 | // Nothing we can do much here, just log |
| 111 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 112 | Log.w(TAG, "No icon resource for account type " + accountType); |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | return icon; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 120 | Account account = (Account) mAccounts[position]; |
| 121 | Log.d(TAG, "selected account " + account); |
| 122 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 123 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 124 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 125 | mResult = bundle; |
| 126 | finish(); |
| 127 | } |
| 128 | |
| 129 | public void finish() { |
| 130 | if (mAccountManagerResponse != null) { |
| 131 | if (mResult != null) { |
| 132 | mAccountManagerResponse.onResult(mResult); |
| 133 | } else { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 134 | mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | super.finish(); |
| 138 | } |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 139 | |
| 140 | private static class AccountInfo { |
| 141 | final String name; |
| 142 | final Drawable drawable; |
| 143 | |
| 144 | AccountInfo(String name, Drawable drawable) { |
| 145 | this.name = name; |
| 146 | this.drawable = drawable; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private static class ViewHolder { |
| 151 | ImageView icon; |
| 152 | TextView text; |
| 153 | } |
| 154 | |
| 155 | private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> { |
| 156 | private LayoutInflater mLayoutInflater; |
| 157 | private AccountInfo[] mInfos; |
| 158 | |
| 159 | public AccountArrayAdapter(Context context, int textViewResourceId, AccountInfo[] infos) { |
| 160 | super(context, textViewResourceId, infos); |
| 161 | mInfos = infos; |
| 162 | mLayoutInflater = (LayoutInflater) context.getSystemService( |
| 163 | Context.LAYOUT_INFLATER_SERVICE); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public View getView(int position, View convertView, ViewGroup parent) { |
| 168 | ViewHolder holder; |
| 169 | |
| 170 | if (convertView == null) { |
| 171 | convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null); |
| 172 | holder = new ViewHolder(); |
| 173 | holder.text = (TextView) convertView.findViewById(R.id.account_row_text); |
| 174 | holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon); |
| 175 | convertView.setTag(holder); |
| 176 | } else { |
| 177 | holder = (ViewHolder) convertView.getTag(); |
| 178 | } |
| 179 | |
| 180 | holder.text.setText(mInfos[position].name); |
| 181 | holder.icon.setImageDrawable(mInfos[position].drawable); |
| 182 | |
| 183 | return convertView; |
| 184 | } |
| 185 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 186 | } |