Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.res.Resources; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.os.Bundle; |
| 25 | import android.util.Log; |
| 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 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.HashSet; |
| 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | /** |
| 43 | * @hide |
| 44 | */ |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 45 | public class ChooseAccountTypeActivity extends Activity { |
Fred Quintana | 0c65ba4 | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 46 | private static final String TAG = "AccountChooser"; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 47 | |
| 48 | private HashMap<String, AuthInfo> mTypeToAuthenticatorInfo = new HashMap<String, AuthInfo>(); |
| 49 | private ArrayList<AuthInfo> mAuthenticatorInfosToDisplay; |
| 50 | |
| 51 | @Override |
| 52 | public void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 54 | |
Fred Quintana | 0c65ba4 | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 55 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 56 | Log.v(TAG, "ChooseAccountTypeActivity.onCreate(savedInstanceState=" |
| 57 | + savedInstanceState + ")"); |
| 58 | } |
| 59 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 60 | // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes |
| 61 | Set<String> setOfAllowableAccountTypes = null; |
Fred Quintana | a77253a | 2011-09-19 15:28:18 -0700 | [diff] [blame] | 62 | String[] validAccountTypes = getIntent().getStringArrayExtra( |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 63 | ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 64 | if (validAccountTypes != null) { |
Fred Quintana | a77253a | 2011-09-19 15:28:18 -0700 | [diff] [blame] | 65 | setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.length); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 66 | for (String type : validAccountTypes) { |
| 67 | setOfAllowableAccountTypes.add(type); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // create a map of account authenticators |
| 72 | buildTypeToAuthDescriptionMap(); |
| 73 | |
| 74 | // Create a list of authenticators that are allowable. Filter out those that |
| 75 | // don't match the allowable account types, if provided. |
| 76 | mAuthenticatorInfosToDisplay = new ArrayList<AuthInfo>(mTypeToAuthenticatorInfo.size()); |
| 77 | for (Map.Entry<String, AuthInfo> entry: mTypeToAuthenticatorInfo.entrySet()) { |
| 78 | final String type = entry.getKey(); |
| 79 | final AuthInfo info = entry.getValue(); |
| 80 | if (setOfAllowableAccountTypes != null |
| 81 | && !setOfAllowableAccountTypes.contains(type)) { |
| 82 | continue; |
| 83 | } |
| 84 | mAuthenticatorInfosToDisplay.add(info); |
| 85 | } |
| 86 | |
| 87 | if (mAuthenticatorInfosToDisplay.isEmpty()) { |
| 88 | Bundle bundle = new Bundle(); |
| 89 | bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "no allowable account types"); |
| 90 | setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
| 91 | finish(); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (mAuthenticatorInfosToDisplay.size() == 1) { |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 96 | setResultAndFinish(mAuthenticatorInfosToDisplay.get(0).desc.type); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 100 | setContentView(R.layout.choose_account_type); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 101 | // Setup the list |
| 102 | ListView list = (ListView) findViewById(android.R.id.list); |
| 103 | // Use an existing ListAdapter that will map an array of strings to TextViews |
| 104 | list.setAdapter(new AccountArrayAdapter(this, |
| 105 | android.R.layout.simple_list_item_1, mAuthenticatorInfosToDisplay)); |
| 106 | list.setChoiceMode(ListView.CHOICE_MODE_NONE); |
| 107 | list.setTextFilterEnabled(false); |
| 108 | list.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 109 | public void onItemClick(AdapterView<?> parent, View v, int position, long id) { |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 110 | setResultAndFinish(mAuthenticatorInfosToDisplay.get(position).desc.type); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 111 | } |
| 112 | }); |
| 113 | } |
| 114 | |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 115 | private void setResultAndFinish(final String type) { |
| 116 | Bundle bundle = new Bundle(); |
| 117 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, type); |
| 118 | setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
Fred Quintana | 0c65ba4 | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 119 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 120 | Log.v(TAG, "ChooseAccountTypeActivity.setResultAndFinish: " |
| 121 | + "selected account type " + type); |
| 122 | } |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 123 | finish(); |
| 124 | } |
| 125 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 126 | private void buildTypeToAuthDescriptionMap() { |
| 127 | for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) { |
| 128 | String name = null; |
| 129 | Drawable icon = null; |
| 130 | try { |
| 131 | Context authContext = createPackageContext(desc.packageName, 0); |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 132 | icon = authContext.getDrawable(desc.iconId); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 133 | final CharSequence sequence = authContext.getResources().getText(desc.labelId); |
| 134 | if (sequence != null) { |
| 135 | name = sequence.toString(); |
| 136 | } |
| 137 | name = sequence.toString(); |
| 138 | } catch (PackageManager.NameNotFoundException e) { |
| 139 | // Nothing we can do much here, just log |
| 140 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 141 | Log.w(TAG, "No icon name for account type " + desc.type); |
| 142 | } |
| 143 | } catch (Resources.NotFoundException e) { |
| 144 | // Nothing we can do much here, just log |
| 145 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 146 | Log.w(TAG, "No icon resource for account type " + desc.type); |
| 147 | } |
| 148 | } |
| 149 | AuthInfo authInfo = new AuthInfo(desc, name, icon); |
| 150 | mTypeToAuthenticatorInfo.put(desc.type, authInfo); |
| 151 | } |
| 152 | } |
| 153 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 154 | private static class AuthInfo { |
| 155 | final AuthenticatorDescription desc; |
| 156 | final String name; |
| 157 | final Drawable drawable; |
| 158 | |
| 159 | AuthInfo(AuthenticatorDescription desc, String name, Drawable drawable) { |
| 160 | this.desc = desc; |
| 161 | this.name = name; |
| 162 | this.drawable = drawable; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private static class ViewHolder { |
| 167 | ImageView icon; |
| 168 | TextView text; |
| 169 | } |
| 170 | |
| 171 | private static class AccountArrayAdapter extends ArrayAdapter<AuthInfo> { |
| 172 | private LayoutInflater mLayoutInflater; |
| 173 | private ArrayList<AuthInfo> mInfos; |
| 174 | |
| 175 | public AccountArrayAdapter(Context context, int textViewResourceId, |
| 176 | ArrayList<AuthInfo> infos) { |
| 177 | super(context, textViewResourceId, infos); |
| 178 | mInfos = infos; |
| 179 | mLayoutInflater = (LayoutInflater) context.getSystemService( |
| 180 | Context.LAYOUT_INFLATER_SERVICE); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public View getView(int position, View convertView, ViewGroup parent) { |
| 185 | ViewHolder holder; |
| 186 | |
| 187 | if (convertView == null) { |
| 188 | convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null); |
| 189 | holder = new ViewHolder(); |
| 190 | holder.text = (TextView) convertView.findViewById(R.id.account_row_text); |
| 191 | holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon); |
| 192 | convertView.setTag(holder); |
| 193 | } else { |
| 194 | holder = (ViewHolder) convertView.getTag(); |
| 195 | } |
| 196 | |
| 197 | holder.text.setText(mInfos.get(position).name); |
| 198 | holder.icon.setImageDrawable(mInfos.get(position).drawable); |
| 199 | |
| 200 | return convertView; |
| 201 | } |
| 202 | } |
| 203 | } |