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