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 selectiong 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 58 final protected List<AccountWithDataSet> mAccountList; 59 60 public AccountSelectedListener(Context context, List<AccountWithDataSet> accountList, 61 int resId) { 62 if (accountList == null || accountList.size() == 0) { 63 Log.e(LOG_TAG, "The size of Account list is 0."); 64 } 65 mContext = context; 66 mAccountList = accountList; 67 mResId = resId; 68 } 69 70 public void onClick(DialogInterface dialog, int which) { 71 dialog.dismiss(); 72 doImport(mContext, mResId, mAccountList.get(which)); 73 } 74 } 75 76 public static Dialog getSelectAccountDialog(Context context, int resId) { 77 return getSelectAccountDialog(context, resId, null, null); 78 } 79 80 public static Dialog getSelectAccountDialog(Context context, int resId, 81 DialogInterface.OnClickListener onClickListener) { 82 return getSelectAccountDialog(context, resId, onClickListener, null); 83 } 84 85 /** 86 * When OnClickListener or OnCancelListener is null, uses a default listener. 87 * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}. 88 */ 89 public static Dialog getSelectAccountDialog(Context context, int resId, 90 DialogInterface.OnClickListener onClickListener, 91 DialogInterface.OnCancelListener onCancelListener) { 92 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context); 93 final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true); 94 95 Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size()); 96 97 // Assume accountList.size() > 1 98 99 // Wrap our context to inflate list items using correct theme 100 final Context dialogContext = new ContextThemeWrapper( 101 context, android.R.style.Theme_Light); 102 final LayoutInflater dialogInflater = (LayoutInflater)dialogContext 103 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 104 final ArrayAdapter<AccountWithDataSet> accountAdapter = 105 new ArrayAdapter<AccountWithDataSet>(context, android.R.layout.simple_list_item_2, 106 writableAccountList) { 107 108 @Override 109 public View getView(int position, View convertView, ViewGroup parent) { 110 if (convertView == null) { 111 convertView = dialogInflater.inflate( 112 android.R.layout.simple_list_item_2, 113 parent, false); 114 } 115 116 // TODO: show icon along with title 117 final TextView text1 = 118 (TextView)convertView.findViewById(android.R.id.text1); 119 final TextView text2 = 120 (TextView)convertView.findViewById(android.R.id.text2); 121 122 final AccountWithDataSet account = this.getItem(position); 123 final AccountType accountType = accountTypes.getAccountType( 124 account.type, account.dataSet); 125 final Context context = getContext(); 126 127 text1.setText(account.name); 128 text2.setText(accountType.getDisplayLabel(context)); 129 130 return convertView; 131 } 132 }; 133 134 if (onClickListener == null) { 135 AccountSelectedListener accountSelectedListener = 136 new AccountSelectedListener(context, writableAccountList, resId); 137 onClickListener = accountSelectedListener; 138 } 139 if (onCancelListener == null) { 140 onCancelListener = new DialogInterface.OnCancelListener() { 141 public void onCancel(DialogInterface dialog) { 142 dialog.dismiss(); 143 } 144 }; 145 } 146 return new AlertDialog.Builder(context) 147 .setTitle(R.string.dialog_new_contact_account) 148 .setSingleChoiceItems(accountAdapter, 0, onClickListener) 149 .setOnCancelListener(onCancelListener) 150 .create(); 151 } 152 153 public static void doImport(Context context, int resId, AccountWithDataSet account) { 154 switch (resId) { 155 case R.string.import_from_sim: { 156 doImportFromSim(context, account); 157 break; 158 } 159 case R.string.import_from_sdcard: { 160 doImportFromSdCard(context, account); 161 break; 162 } 163 } 164 } 165 166 public static void doImportFromSim(Context context, AccountWithDataSet account) { 167 Intent importIntent = new Intent(Intent.ACTION_VIEW); 168 importIntent.setType("vnd.android.cursor.item/sim-contact"); 169 if (account != null) { 170 importIntent.putExtra("account_name", account.name); 171 importIntent.putExtra("account_type", account.type); 172 importIntent.putExtra("data_set", account.dataSet); 173 } 174 importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts"); 175 context.startActivity(importIntent); 176 } 177 178 public static void doImportFromSdCard(Context context, AccountWithDataSet account) { 179 Intent importIntent = new Intent(context, ImportVCardActivity.class); 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 186 if (mVCardShare) { 187 importIntent.setAction(Intent.ACTION_VIEW); 188 importIntent.setData(mPath); 189 } 190 mVCardShare = false; 191 mPath = null; 192 context.startActivity(importIntent); 193 } 194 } 195