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