Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
Rakesh Pallerla | 14e31e5 | 2014-08-22 19:50:34 +0530 | [diff] [blame] | 3 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 4 | * Not a Contribution. |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | package com.android.contacts.common.util; |
| 20 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 22 | import android.app.AlertDialog; |
| 23 | import android.app.Dialog; |
| 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.Intent; |
| 27 | import android.net.Uri; |
Rakesh Pallerla | 14e31e5 | 2014-08-22 19:50:34 +0530 | [diff] [blame] | 28 | import android.telephony.TelephonyManager; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.view.ContextThemeWrapper; |
| 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
| 34 | import android.widget.ArrayAdapter; |
| 35 | import android.widget.TextView; |
| 36 | |
| 37 | import com.android.contacts.common.R; |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 38 | import com.android.contacts.common.SimContactsConstants; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 39 | import com.android.contacts.common.model.AccountTypeManager; |
| 40 | import com.android.contacts.common.model.account.AccountType; |
| 41 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 42 | import com.android.contacts.common.vcard.ImportVCardActivity; |
| 43 | |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 44 | import java.util.ArrayList; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | |
| 47 | /** |
| 48 | * Utility class for selectiong an Account for importing contact(s) |
| 49 | */ |
| 50 | public class AccountSelectionUtil { |
| 51 | // TODO: maybe useful for EditContactActivity.java... |
| 52 | private static final String LOG_TAG = "AccountSelectionUtil"; |
| 53 | |
| 54 | public static boolean mVCardShare = false; |
Rakesh Pallerla | 14e31e5 | 2014-08-22 19:50:34 +0530 | [diff] [blame] | 55 | private static int SIM_ID_INVALID = -1; |
| 56 | private static int mSelectedSim = SIM_ID_INVALID; |
| 57 | private static final String SIM_INDEX = "sim_index"; |
| 58 | // Constant value to know option is import from all SIM's |
| 59 | private static int IMPORT_FROM_ALL = 8; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 60 | |
| 61 | public static Uri mPath; |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 62 | // QRD enhancement: import subscription selected by user |
| 63 | private static int mImportSub = SimContactsConstants.SUB_INVALID; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 64 | |
| 65 | public static class AccountSelectedListener |
| 66 | implements DialogInterface.OnClickListener { |
| 67 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 68 | final private Activity mActivity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 69 | final private int mResId; |
| 70 | |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 71 | protected List<AccountWithDataSet> mAccountList; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 72 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 73 | public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 74 | int resId) { |
| 75 | if (accountList == null || accountList.size() == 0) { |
| 76 | Log.e(LOG_TAG, "The size of Account list is 0."); |
| 77 | } |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 78 | mActivity = activity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 79 | mAccountList = accountList; |
| 80 | mResId = resId; |
| 81 | } |
| 82 | |
| 83 | public void onClick(DialogInterface dialog, int which) { |
| 84 | dialog.dismiss(); |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 85 | doImport(mActivity, mResId, mAccountList.get(which)); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 86 | } |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 87 | /** |
| 88 | * Reset the account list for this listener, to make sure the selected |
| 89 | * items reflect the displayed items. |
| 90 | * |
| 91 | * @param accountList The reset account list. |
| 92 | */ |
| 93 | void setAccountList(List<AccountWithDataSet> accountList) { |
| 94 | mAccountList = accountList; |
| 95 | } |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 98 | public static void setImportSubscription(int subscription) { |
| 99 | mImportSub = subscription; |
| 100 | } |
| 101 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 102 | public static Dialog getSelectAccountDialog(Activity activity, int resId) { |
| 103 | return getSelectAccountDialog(activity, resId, null, null); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 106 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 107 | DialogInterface.OnClickListener onClickListener) { |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 108 | return getSelectAccountDialog(activity, resId, onClickListener, null); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 111 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 112 | DialogInterface.OnClickListener onClickListener, |
| 113 | DialogInterface.OnCancelListener onCancelListener) { |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 114 | return getSelectAccountDialog(activity, resId, onClickListener, |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 115 | onCancelListener, true); |
| 116 | } |
| 117 | |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 118 | /** |
| 119 | * When OnClickListener or OnCancelListener is null, uses a default listener. |
| 120 | * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}. |
| 121 | */ |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 122 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 123 | DialogInterface.OnClickListener onClickListener, |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 124 | DialogInterface.OnCancelListener onCancelListener, boolean includeSIM) { |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 125 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity); |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 126 | List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true); |
| 127 | if (includeSIM) { |
| 128 | writableAccountList = accountTypes.getAccounts(true); |
| 129 | } else { |
| 130 | writableAccountList = accountTypes.getAccounts(true, |
| 131 | AccountTypeManager.FLAG_ALL_ACCOUNTS_WITHOUT_SIM); |
| 132 | } |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 133 | |
| 134 | Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size()); |
| 135 | |
| 136 | // Assume accountList.size() > 1 |
| 137 | |
| 138 | // Wrap our context to inflate list items using correct theme |
| 139 | final Context dialogContext = new ContextThemeWrapper( |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 140 | activity, android.R.style.Theme_Light); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 141 | final LayoutInflater dialogInflater = (LayoutInflater)dialogContext |
| 142 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 143 | final ArrayAdapter<AccountWithDataSet> accountAdapter = |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 144 | new ArrayAdapter<AccountWithDataSet>(activity, android.R.layout.simple_list_item_2, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 145 | writableAccountList) { |
| 146 | |
| 147 | @Override |
| 148 | public View getView(int position, View convertView, ViewGroup parent) { |
| 149 | if (convertView == null) { |
| 150 | convertView = dialogInflater.inflate( |
| 151 | android.R.layout.simple_list_item_2, |
| 152 | parent, false); |
| 153 | } |
| 154 | |
| 155 | // TODO: show icon along with title |
| 156 | final TextView text1 = |
| 157 | (TextView)convertView.findViewById(android.R.id.text1); |
| 158 | final TextView text2 = |
| 159 | (TextView)convertView.findViewById(android.R.id.text2); |
| 160 | |
| 161 | final AccountWithDataSet account = this.getItem(position); |
| 162 | final AccountType accountType = accountTypes.getAccountType( |
| 163 | account.type, account.dataSet); |
| 164 | final Context context = getContext(); |
| 165 | |
| 166 | text1.setText(account.name); |
| 167 | text2.setText(accountType.getDisplayLabel(context)); |
| 168 | |
| 169 | return convertView; |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | if (onClickListener == null) { |
| 174 | AccountSelectedListener accountSelectedListener = |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 175 | new AccountSelectedListener(activity, writableAccountList, resId); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 176 | onClickListener = accountSelectedListener; |
Yujing Gu | 1122a58 | 2014-07-24 14:32:13 +0800 | [diff] [blame] | 177 | } else if (onClickListener instanceof AccountSelectedListener) { |
| 178 | // Because the writableAccountList is different if includeSIM or not, so |
| 179 | // should reset the account list for the AccountSelectedListener which |
| 180 | // is initialized with FLAG_ALL_ACCOUNTS. |
| 181 | // Reset the account list to make sure the selected account is contained |
| 182 | // in these display accounts. |
| 183 | ((AccountSelectedListener) onClickListener).setAccountList(writableAccountList); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 184 | } |
| 185 | if (onCancelListener == null) { |
| 186 | onCancelListener = new DialogInterface.OnCancelListener() { |
| 187 | public void onCancel(DialogInterface dialog) { |
| 188 | dialog.dismiss(); |
| 189 | } |
| 190 | }; |
| 191 | } |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 192 | return new AlertDialog.Builder(activity) |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 193 | .setTitle(R.string.dialog_new_contact_account) |
| 194 | .setSingleChoiceItems(accountAdapter, 0, onClickListener) |
| 195 | .setOnCancelListener(onCancelListener) |
| 196 | .create(); |
| 197 | } |
| 198 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 199 | public static void doImport(Activity activity, int resId, AccountWithDataSet account) { |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 200 | switch (resId) { |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 201 | case R.string.import_from_sim: { |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 202 | doImportFromSim(activity, account); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | case R.string.import_from_sdcard: { |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 206 | doImportFromSdCard(activity, account); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 212 | public static void doImportFromSim(Activity activity, AccountWithDataSet account) { |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 213 | Intent importIntent = new Intent(SimContactsConstants.ACTION_MULTI_PICK_SIM); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 214 | if (account != null) { |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 215 | importIntent.putExtra(SimContactsConstants.ACCOUNT_NAME, account.name); |
| 216 | importIntent.putExtra(SimContactsConstants.ACCOUNT_TYPE, account.type); |
| 217 | importIntent.putExtra(SimContactsConstants.ACCOUNT_DATA, account.dataSet); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 218 | } |
Yujing Gu | 8ff950a | 2014-07-30 15:25:14 +0800 | [diff] [blame] | 219 | if (TelephonyManager.getDefault().isMultiSimEnabled()) { |
| 220 | importIntent.putExtra(SimContactsConstants.SUB, mImportSub); |
| 221 | } else { |
| 222 | importIntent.putExtra(SimContactsConstants.SUB,SimContactsConstants.SUB_1); |
| 223 | } |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 224 | activity.startActivityForResult(importIntent, 0); |
Rakesh Pallerla | 14e31e5 | 2014-08-22 19:50:34 +0530 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | public static void doImportFromMultiSim(Context context, AccountWithDataSet account, |
| 228 | int selectedSim) { |
| 229 | Intent importIntent = new Intent(Intent.ACTION_VIEW); |
| 230 | importIntent.setType("vnd.android.cursor.item/sim-contact"); |
| 231 | if (account != null) { |
| 232 | importIntent.putExtra("account_name", account.name); |
| 233 | importIntent.putExtra("account_type", account.type); |
| 234 | importIntent.putExtra("data_set", account.dataSet); |
| 235 | } |
| 236 | importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts"); |
| 237 | importIntent.putExtra(SIM_INDEX, selectedSim); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 238 | context.startActivity(importIntent); |
| 239 | } |
| 240 | |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 241 | public static void doImportFromSdCard(Activity activity, AccountWithDataSet account) { |
| 242 | Intent importIntent = new Intent(activity, ImportVCardActivity.class); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 243 | if (account != null) { |
| 244 | importIntent.putExtra("account_name", account.name); |
| 245 | importIntent.putExtra("account_type", account.type); |
| 246 | importIntent.putExtra("data_set", account.dataSet); |
| 247 | } |
| 248 | |
| 249 | if (mVCardShare) { |
| 250 | importIntent.setAction(Intent.ACTION_VIEW); |
| 251 | importIntent.setData(mPath); |
| 252 | } |
| 253 | mVCardShare = false; |
| 254 | mPath = null; |
Walter Jang | 9597010 | 2016-10-25 15:42:33 -0700 | [diff] [blame] | 255 | activity.startActivityForResult(importIntent, 0); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 256 | } |
Rakesh Pallerla | 14e31e5 | 2014-08-22 19:50:34 +0530 | [diff] [blame] | 257 | |
| 258 | public static class SimSelectedListener |
| 259 | implements DialogInterface.OnClickListener { |
| 260 | |
| 261 | final private Context mContext; |
| 262 | final private AccountWithDataSet mAccount; |
| 263 | |
| 264 | public SimSelectedListener(Context context, AccountWithDataSet account) { |
| 265 | mContext = context; |
| 266 | mAccount = account; |
| 267 | } |
| 268 | |
| 269 | public void onClick(DialogInterface dialog, int which) { |
| 270 | Log.d(LOG_TAG, "onClick OK: mSelectedSim = " + mSelectedSim); |
| 271 | if (mSelectedSim != SIM_ID_INVALID) { |
| 272 | doImportFromMultiSim(mContext, mAccount, mSelectedSim); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private static void displaySelectSimDialog(Context context, |
| 278 | SimSelectedListener simSelListner) { |
| 279 | Log.d(LOG_TAG, "displaySelectSimDialog"); |
| 280 | |
| 281 | mSelectedSim = SIM_ID_INVALID; |
| 282 | |
| 283 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 284 | builder.setTitle(R.string.select_sim); |
| 285 | final int numPhones = TelephonyManager.getDefault().getPhoneCount(); |
| 286 | CharSequence[] subList = new CharSequence[numPhones + 1]; |
| 287 | int i; |
| 288 | for (i = 1; i <= numPhones; i++) { |
| 289 | subList[i-1] = "SIM" + i; |
| 290 | } |
| 291 | subList[i-1] = context.getString(R.string.Import_All); |
| 292 | builder.setSingleChoiceItems(subList, -1, new DialogInterface.OnClickListener() { |
| 293 | @Override |
| 294 | public void onClick(DialogInterface dialog, int which) { |
| 295 | Log.d(LOG_TAG, "onClicked Dialog on which = " + which); |
| 296 | mSelectedSim = which; |
| 297 | if (mSelectedSim == numPhones) { |
| 298 | mSelectedSim = IMPORT_FROM_ALL; |
| 299 | } |
| 300 | } |
| 301 | }); |
| 302 | |
| 303 | AlertDialog dialog = builder.create(); |
| 304 | dialog.setButton(DialogInterface.BUTTON_POSITIVE, |
| 305 | context.getString(com.android.internal.R.string.ok), simSelListner); |
| 306 | dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString( |
| 307 | com.android.internal.R.string.cancel), new DialogInterface.OnClickListener() { |
| 308 | @Override |
| 309 | public void onClick(DialogInterface dialog, int which) { |
| 310 | Log.d(LOG_TAG, "onClicked Cancel"); |
| 311 | } |
| 312 | }); |
| 313 | |
| 314 | dialog.setOnDismissListener(new DialogInterface.OnDismissListener () { |
| 315 | @Override |
| 316 | public void onDismiss(DialogInterface dialog) { |
| 317 | Log.d(LOG_TAG, "onDismiss"); |
| 318 | Log.d(LOG_TAG, "Selected SUB = " + mSelectedSim); |
| 319 | } |
| 320 | }); |
| 321 | dialog.show(); |
| 322 | } |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 323 | } |