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