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 17 package com.android.contacts.common.util; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.content.Intent; 24 import android.net.Uri; 25 import android.util.Log; 26 import android.view.ContextThemeWrapper; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.ArrayAdapter; 31 import android.widget.TextView; 32 33 import com.android.contacts.common.R; 34 import com.android.contacts.common.model.AccountTypeManager; 35 import com.android.contacts.common.model.account.AccountType; 36 import com.android.contacts.common.model.account.AccountWithDataSet; 37 import com.android.contacts.common.vcard.ImportVCardActivity; 38 39 import java.util.List; 40 41 /** 42 * Utility class for selecting an Account for importing contact(s) 43 */ 44 public class AccountSelectionUtil { 45 // TODO: maybe useful for EditContactActivity.java... 46 private static final String LOG_TAG = "AccountSelectionUtil"; 47 48 public static boolean mVCardShare = false; 49 50 public static Uri mPath; 51 52 public static class AccountSelectedListener 53 implements DialogInterface.OnClickListener { 54 55 final private Context mContext; 56 final private int mResId; 57 final private int mSubscriptionId; 58 59 final protected List<AccountWithDataSet> mAccountList; 60 AccountSelectedListener(Context context, List<AccountWithDataSet> accountList, int resId, int subscriptionId)61 public AccountSelectedListener(Context context, List<AccountWithDataSet> accountList, 62 int resId, int subscriptionId) { 63 if (accountList == null || accountList.size() == 0) { 64 Log.e(LOG_TAG, "The size of Account list is 0."); 65 } 66 mContext = context; 67 mAccountList = accountList; 68 mResId = resId; 69 mSubscriptionId = subscriptionId; 70 } 71 AccountSelectedListener(Context context, List<AccountWithDataSet> accountList, int resId)72 public AccountSelectedListener(Context context, List<AccountWithDataSet> accountList, 73 int resId) { 74 // Subscription id is only needed for importing from SIM card. We can safely ignore 75 // its value for SD card importing. 76 this(context, accountList, resId, /* subscriptionId = */ -1); 77 } 78 onClick(DialogInterface dialog, int which)79 public void onClick(DialogInterface dialog, int which) { 80 dialog.dismiss(); 81 doImport(mContext, mResId, mAccountList.get(which), mSubscriptionId); 82 } 83 } 84 getSelectAccountDialog(Context context, int resId)85 public static Dialog getSelectAccountDialog(Context context, int resId) { 86 return getSelectAccountDialog(context, resId, null, null); 87 } 88 getSelectAccountDialog(Context context, int resId, DialogInterface.OnClickListener onClickListener)89 public static Dialog getSelectAccountDialog(Context context, int resId, 90 DialogInterface.OnClickListener onClickListener) { 91 return getSelectAccountDialog(context, resId, onClickListener, null); 92 } 93 94 /** 95 * When OnClickListener or OnCancelListener is null, uses a default listener. 96 * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}. 97 */ getSelectAccountDialog(Context context, int resId, DialogInterface.OnClickListener onClickListener, DialogInterface.OnCancelListener onCancelListener)98 public static Dialog getSelectAccountDialog(Context context, int resId, 99 DialogInterface.OnClickListener onClickListener, 100 DialogInterface.OnCancelListener onCancelListener) { 101 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context); 102 final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true); 103 104 Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size()); 105 106 // Assume accountList.size() > 1 107 108 // Wrap our context to inflate list items using correct theme 109 final Context dialogContext = new ContextThemeWrapper( 110 context, android.R.style.Theme_Light); 111 final LayoutInflater dialogInflater = (LayoutInflater)dialogContext 112 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 113 final ArrayAdapter<AccountWithDataSet> accountAdapter = 114 new ArrayAdapter<AccountWithDataSet>(context, android.R.layout.simple_list_item_2, 115 writableAccountList) { 116 117 @Override 118 public View getView(int position, View convertView, ViewGroup parent) { 119 if (convertView == null) { 120 convertView = dialogInflater.inflate( 121 android.R.layout.simple_list_item_2, 122 parent, false); 123 } 124 125 // TODO: show icon along with title 126 final TextView text1 = 127 (TextView)convertView.findViewById(android.R.id.text1); 128 final TextView text2 = 129 (TextView)convertView.findViewById(android.R.id.text2); 130 131 final AccountWithDataSet account = this.getItem(position); 132 final AccountType accountType = accountTypes.getAccountType( 133 account.type, account.dataSet); 134 final Context context = getContext(); 135 136 text1.setText(account.name); 137 text2.setText(accountType.getDisplayLabel(context)); 138 139 return convertView; 140 } 141 }; 142 143 if (onClickListener == null) { 144 AccountSelectedListener accountSelectedListener = 145 new AccountSelectedListener(context, writableAccountList, resId); 146 onClickListener = accountSelectedListener; 147 } 148 if (onCancelListener == null) { 149 onCancelListener = new DialogInterface.OnCancelListener() { 150 public void onCancel(DialogInterface dialog) { 151 dialog.dismiss(); 152 } 153 }; 154 } 155 return new AlertDialog.Builder(context) 156 .setTitle(R.string.dialog_new_contact_account) 157 .setSingleChoiceItems(accountAdapter, 0, onClickListener) 158 .setOnCancelListener(onCancelListener) 159 .create(); 160 } 161 doImport(Context context, int resId, AccountWithDataSet account, int subscriptionId)162 public static void doImport(Context context, int resId, AccountWithDataSet account, 163 int subscriptionId) { 164 switch (resId) { 165 case R.string.import_from_sim: { 166 doImportFromSim(context, account, subscriptionId); 167 break; 168 } 169 case R.string.import_from_vcf_file: { 170 doImportFromVcfFile(context, account); 171 break; 172 } 173 } 174 } 175 doImportFromSim(Context context, AccountWithDataSet account, int subscriptionId)176 public static void doImportFromSim(Context context, AccountWithDataSet account, 177 int subscriptionId) { 178 Intent importIntent = new Intent(Intent.ACTION_VIEW); 179 importIntent.setType("vnd.android.cursor.item/sim-contact"); 180 if (account != null) { 181 importIntent.putExtra("account_name", account.name); 182 importIntent.putExtra("account_type", account.type); 183 importIntent.putExtra("data_set", account.dataSet); 184 } 185 importIntent.putExtra("subscription_id", (Integer) subscriptionId); 186 importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts"); 187 context.startActivity(importIntent); 188 } 189 doImportFromVcfFile(Context context, AccountWithDataSet account)190 public static void doImportFromVcfFile(Context context, AccountWithDataSet account) { 191 Intent importIntent = new Intent(context, ImportVCardActivity.class); 192 if (account != null) { 193 importIntent.putExtra("account_name", account.name); 194 importIntent.putExtra("account_type", account.type); 195 importIntent.putExtra("data_set", account.dataSet); 196 } 197 198 if (mVCardShare) { 199 importIntent.setAction(Intent.ACTION_VIEW); 200 importIntent.setData(mPath); 201 } 202 mVCardShare = false; 203 mPath = null; 204 context.startActivity(importIntent); 205 } 206 } 207