|
| 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 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +/** |
| 44 | + * @hide |
| 45 | + */ |
| 46 | +public class ChooseAccountTypeActivity extends Activity implements AccountManagerCallback<Bundle> { |
| 47 | + private static final String TAG = "AccountManager"; |
| 48 | + |
| 49 | + private HashMap<String, AuthInfo> mTypeToAuthenticatorInfo = new HashMap<String, AuthInfo>(); |
| 50 | + private ArrayList<AuthInfo> mAuthenticatorInfosToDisplay; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onCreate(Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setContentView(R.layout.choose_account); |
| 56 | + |
| 57 | + // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes |
| 58 | + Set<String> setOfAllowableAccountTypes = null; |
| 59 | + ArrayList<String> validAccountTypes = getIntent().getStringArrayListExtra( |
| 60 | + ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST); |
| 61 | + if (validAccountTypes != null) { |
| 62 | + setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.size()); |
| 63 | + for (String type : validAccountTypes) { |
| 64 | + setOfAllowableAccountTypes.add(type); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // create a map of account authenticators |
| 69 | + buildTypeToAuthDescriptionMap(); |
| 70 | + |
| 71 | + // Create a list of authenticators that are allowable. Filter out those that |
| 72 | + // don't match the allowable account types, if provided. |
| 73 | + mAuthenticatorInfosToDisplay = new ArrayList<AuthInfo>(mTypeToAuthenticatorInfo.size()); |
| 74 | + for (Map.Entry<String, AuthInfo> entry: mTypeToAuthenticatorInfo.entrySet()) { |
| 75 | + final String type = entry.getKey(); |
| 76 | + final AuthInfo info = entry.getValue(); |
| 77 | + if (setOfAllowableAccountTypes != null |
| 78 | + && !setOfAllowableAccountTypes.contains(type)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + mAuthenticatorInfosToDisplay.add(info); |
| 82 | + } |
| 83 | + |
| 84 | + if (mAuthenticatorInfosToDisplay.isEmpty()) { |
| 85 | + Bundle bundle = new Bundle(); |
| 86 | + bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "no allowable account types"); |
| 87 | + setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
| 88 | + finish(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (mAuthenticatorInfosToDisplay.size() == 1) { |
| 93 | + runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(0)); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // Setup the list |
| 98 | + ListView list = (ListView) findViewById(android.R.id.list); |
| 99 | + // Use an existing ListAdapter that will map an array of strings to TextViews |
| 100 | + list.setAdapter(new AccountArrayAdapter(this, |
| 101 | + android.R.layout.simple_list_item_1, mAuthenticatorInfosToDisplay)); |
| 102 | + list.setChoiceMode(ListView.CHOICE_MODE_NONE); |
| 103 | + list.setTextFilterEnabled(false); |
| 104 | + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 105 | + public void onItemClick(AdapterView<?> parent, View v, int position, long id) { |
| 106 | + runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(position)); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + private void buildTypeToAuthDescriptionMap() { |
| 112 | + for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) { |
| 113 | + String name = null; |
| 114 | + Drawable icon = null; |
| 115 | + try { |
| 116 | + Context authContext = createPackageContext(desc.packageName, 0); |
| 117 | + icon = authContext.getResources().getDrawable(desc.iconId); |
| 118 | + final CharSequence sequence = authContext.getResources().getText(desc.labelId); |
| 119 | + if (sequence != null) { |
| 120 | + name = sequence.toString(); |
| 121 | + } |
| 122 | + name = sequence.toString(); |
| 123 | + } catch (PackageManager.NameNotFoundException e) { |
| 124 | + // Nothing we can do much here, just log |
| 125 | + if (Log.isLoggable(TAG, Log.WARN)) { |
| 126 | + Log.w(TAG, "No icon name for account type " + desc.type); |
| 127 | + } |
| 128 | + } catch (Resources.NotFoundException e) { |
| 129 | + // Nothing we can do much here, just log |
| 130 | + if (Log.isLoggable(TAG, Log.WARN)) { |
| 131 | + Log.w(TAG, "No icon resource for account type " + desc.type); |
| 132 | + } |
| 133 | + } |
| 134 | + AuthInfo authInfo = new AuthInfo(desc, name, icon); |
| 135 | + mTypeToAuthenticatorInfo.put(desc.type, authInfo); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + protected void runAddAccountForAuthenticator(AuthInfo authInfo) { |
| 140 | + Log.d(TAG, "selected account type " + authInfo.name); |
| 141 | + Bundle options = getIntent().getBundleExtra( |
| 142 | + ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE); |
| 143 | + AccountManager.get(this).addAccount(authInfo.desc.type, null, null, options, |
| 144 | + this, this, null); |
| 145 | + } |
| 146 | + |
| 147 | + public void run(final AccountManagerFuture<Bundle> accountManagerFuture) { |
| 148 | + try { |
| 149 | + Bundle accountManagerResult = accountManagerFuture.getResult(); |
| 150 | + Bundle bundle = new Bundle(); |
| 151 | + bundle.putString(AccountManager.KEY_ACCOUNT_NAME, |
| 152 | + accountManagerResult.getString(AccountManager.KEY_ACCOUNT_NAME)); |
| 153 | + bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, |
| 154 | + accountManagerResult.getString(AccountManager.KEY_ACCOUNT_TYPE)); |
| 155 | + setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
| 156 | + finish(); |
| 157 | + return; |
| 158 | + } catch (OperationCanceledException e) { |
| 159 | + setResult(Activity.RESULT_CANCELED); |
| 160 | + finish(); |
| 161 | + return; |
| 162 | + } catch (IOException e) { |
| 163 | + } catch (AuthenticatorException e) { |
| 164 | + } |
| 165 | + Bundle bundle = new Bundle(); |
| 166 | + bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server"); |
| 167 | + setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
| 168 | + finish(); |
| 169 | + } |
| 170 | + |
| 171 | + private static class AuthInfo { |
| 172 | + final AuthenticatorDescription desc; |
| 173 | + final String name; |
| 174 | + final Drawable drawable; |
| 175 | + |
| 176 | + AuthInfo(AuthenticatorDescription desc, String name, Drawable drawable) { |
| 177 | + this.desc = desc; |
| 178 | + this.name = name; |
| 179 | + this.drawable = drawable; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static class ViewHolder { |
| 184 | + ImageView icon; |
| 185 | + TextView text; |
| 186 | + } |
| 187 | + |
| 188 | + private static class AccountArrayAdapter extends ArrayAdapter<AuthInfo> { |
| 189 | + private LayoutInflater mLayoutInflater; |
| 190 | + private ArrayList<AuthInfo> mInfos; |
| 191 | + |
| 192 | + public AccountArrayAdapter(Context context, int textViewResourceId, |
| 193 | + ArrayList<AuthInfo> infos) { |
| 194 | + super(context, textViewResourceId, infos); |
| 195 | + mInfos = infos; |
| 196 | + mLayoutInflater = (LayoutInflater) context.getSystemService( |
| 197 | + Context.LAYOUT_INFLATER_SERVICE); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public View getView(int position, View convertView, ViewGroup parent) { |
| 202 | + ViewHolder holder; |
| 203 | + |
| 204 | + if (convertView == null) { |
| 205 | + convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null); |
| 206 | + holder = new ViewHolder(); |
| 207 | + holder.text = (TextView) convertView.findViewById(R.id.account_row_text); |
| 208 | + holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon); |
| 209 | + convertView.setTag(holder); |
| 210 | + } else { |
| 211 | + holder = (ViewHolder) convertView.getTag(); |
| 212 | + } |
| 213 | + |
| 214 | + holder.text.setText(mInfos.get(position).name); |
| 215 | + holder.icon.setImageDrawable(mInfos.get(position).drawable); |
| 216 | + |
| 217 | + return convertView; |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments