blob: ca0a185c23921e0dace82ff167537752632683a8 [file] [log] [blame]
Chiao Cheng7903d242012-12-03 17:15:58 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
Rakesh Pallerla14e31e52014-08-22 19:50:34 +05303 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * Not a Contribution.
Chiao Cheng7903d242012-12-03 17:15:58 -08005 *
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
19package com.android.contacts.common.util;
20
Walter Jang95970102016-10-25 15:42:33 -070021import android.app.Activity;
Chiao Cheng7903d242012-12-03 17:15:58 -080022import android.app.AlertDialog;
23import android.app.Dialog;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.net.Uri;
Rakesh Pallerla14e31e52014-08-22 19:50:34 +053028import android.telephony.TelephonyManager;
Chiao Cheng7903d242012-12-03 17:15:58 -080029import android.util.Log;
30import android.view.ContextThemeWrapper;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.ArrayAdapter;
35import android.widget.TextView;
36
37import com.android.contacts.common.R;
Yujing Gu8ff950a2014-07-30 15:25:14 +080038import com.android.contacts.common.SimContactsConstants;
Chiao Cheng7903d242012-12-03 17:15:58 -080039import com.android.contacts.common.model.AccountTypeManager;
40import com.android.contacts.common.model.account.AccountType;
41import com.android.contacts.common.model.account.AccountWithDataSet;
42import com.android.contacts.common.vcard.ImportVCardActivity;
43
Yujing Gu8ff950a2014-07-30 15:25:14 +080044import java.util.ArrayList;
Chiao Cheng7903d242012-12-03 17:15:58 -080045import java.util.List;
46
47/**
48 * Utility class for selectiong an Account for importing contact(s)
49 */
50public 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 Pallerla14e31e52014-08-22 19:50:34 +053055 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 Cheng7903d242012-12-03 17:15:58 -080060
61 public static Uri mPath;
Yujing Gu8ff950a2014-07-30 15:25:14 +080062 // QRD enhancement: import subscription selected by user
63 private static int mImportSub = SimContactsConstants.SUB_INVALID;
Chiao Cheng7903d242012-12-03 17:15:58 -080064
65 public static class AccountSelectedListener
66 implements DialogInterface.OnClickListener {
67
Walter Jang95970102016-10-25 15:42:33 -070068 final private Activity mActivity;
Chiao Cheng7903d242012-12-03 17:15:58 -080069 final private int mResId;
70
Yujing Gu1122a582014-07-24 14:32:13 +080071 protected List<AccountWithDataSet> mAccountList;
Chiao Cheng7903d242012-12-03 17:15:58 -080072
Walter Jang95970102016-10-25 15:42:33 -070073 public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
Chiao Cheng7903d242012-12-03 17:15:58 -080074 int resId) {
75 if (accountList == null || accountList.size() == 0) {
76 Log.e(LOG_TAG, "The size of Account list is 0.");
77 }
Walter Jang95970102016-10-25 15:42:33 -070078 mActivity = activity;
Chiao Cheng7903d242012-12-03 17:15:58 -080079 mAccountList = accountList;
80 mResId = resId;
81 }
82
83 public void onClick(DialogInterface dialog, int which) {
84 dialog.dismiss();
Walter Jang95970102016-10-25 15:42:33 -070085 doImport(mActivity, mResId, mAccountList.get(which));
Chiao Cheng7903d242012-12-03 17:15:58 -080086 }
Yujing Gu1122a582014-07-24 14:32:13 +080087 /**
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 Cheng7903d242012-12-03 17:15:58 -080096 }
97
Yujing Gu8ff950a2014-07-30 15:25:14 +080098 public static void setImportSubscription(int subscription) {
99 mImportSub = subscription;
100 }
101
Walter Jang95970102016-10-25 15:42:33 -0700102 public static Dialog getSelectAccountDialog(Activity activity, int resId) {
103 return getSelectAccountDialog(activity, resId, null, null);
Chiao Cheng7903d242012-12-03 17:15:58 -0800104 }
105
Walter Jang95970102016-10-25 15:42:33 -0700106 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Cheng7903d242012-12-03 17:15:58 -0800107 DialogInterface.OnClickListener onClickListener) {
Walter Jang95970102016-10-25 15:42:33 -0700108 return getSelectAccountDialog(activity, resId, onClickListener, null);
Chiao Cheng7903d242012-12-03 17:15:58 -0800109 }
110
Walter Jang95970102016-10-25 15:42:33 -0700111 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Yujing Gu1122a582014-07-24 14:32:13 +0800112 DialogInterface.OnClickListener onClickListener,
113 DialogInterface.OnCancelListener onCancelListener) {
Walter Jang95970102016-10-25 15:42:33 -0700114 return getSelectAccountDialog(activity, resId, onClickListener,
Yujing Gu1122a582014-07-24 14:32:13 +0800115 onCancelListener, true);
116 }
117
Chiao Cheng7903d242012-12-03 17:15:58 -0800118 /**
119 * When OnClickListener or OnCancelListener is null, uses a default listener.
120 * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
121 */
Walter Jang95970102016-10-25 15:42:33 -0700122 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Cheng7903d242012-12-03 17:15:58 -0800123 DialogInterface.OnClickListener onClickListener,
Yujing Gu1122a582014-07-24 14:32:13 +0800124 DialogInterface.OnCancelListener onCancelListener, boolean includeSIM) {
Walter Jang95970102016-10-25 15:42:33 -0700125 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
Yujing Gu1122a582014-07-24 14:32:13 +0800126 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 Cheng7903d242012-12-03 17:15:58 -0800133
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 Jang95970102016-10-25 15:42:33 -0700140 activity, android.R.style.Theme_Light);
Chiao Cheng7903d242012-12-03 17:15:58 -0800141 final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
142 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
143 final ArrayAdapter<AccountWithDataSet> accountAdapter =
Walter Jang95970102016-10-25 15:42:33 -0700144 new ArrayAdapter<AccountWithDataSet>(activity, android.R.layout.simple_list_item_2,
Chiao Cheng7903d242012-12-03 17:15:58 -0800145 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 Jang95970102016-10-25 15:42:33 -0700175 new AccountSelectedListener(activity, writableAccountList, resId);
Chiao Cheng7903d242012-12-03 17:15:58 -0800176 onClickListener = accountSelectedListener;
Yujing Gu1122a582014-07-24 14:32:13 +0800177 } 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 Cheng7903d242012-12-03 17:15:58 -0800184 }
185 if (onCancelListener == null) {
186 onCancelListener = new DialogInterface.OnCancelListener() {
187 public void onCancel(DialogInterface dialog) {
188 dialog.dismiss();
189 }
190 };
191 }
Walter Jang95970102016-10-25 15:42:33 -0700192 return new AlertDialog.Builder(activity)
Chiao Cheng7903d242012-12-03 17:15:58 -0800193 .setTitle(R.string.dialog_new_contact_account)
194 .setSingleChoiceItems(accountAdapter, 0, onClickListener)
195 .setOnCancelListener(onCancelListener)
196 .create();
197 }
198
Walter Jang95970102016-10-25 15:42:33 -0700199 public static void doImport(Activity activity, int resId, AccountWithDataSet account) {
Chiao Cheng7903d242012-12-03 17:15:58 -0800200 switch (resId) {
Yujing Gu8ff950a2014-07-30 15:25:14 +0800201 case R.string.import_from_sim: {
Walter Jang95970102016-10-25 15:42:33 -0700202 doImportFromSim(activity, account);
Chiao Cheng7903d242012-12-03 17:15:58 -0800203 break;
204 }
205 case R.string.import_from_sdcard: {
Walter Jang95970102016-10-25 15:42:33 -0700206 doImportFromSdCard(activity, account);
Chiao Cheng7903d242012-12-03 17:15:58 -0800207 break;
208 }
209 }
210 }
211
Walter Jang95970102016-10-25 15:42:33 -0700212 public static void doImportFromSim(Activity activity, AccountWithDataSet account) {
Yujing Gu8ff950a2014-07-30 15:25:14 +0800213 Intent importIntent = new Intent(SimContactsConstants.ACTION_MULTI_PICK_SIM);
Chiao Cheng7903d242012-12-03 17:15:58 -0800214 if (account != null) {
Yujing Gu8ff950a2014-07-30 15:25:14 +0800215 importIntent.putExtra(SimContactsConstants.ACCOUNT_NAME, account.name);
216 importIntent.putExtra(SimContactsConstants.ACCOUNT_TYPE, account.type);
217 importIntent.putExtra(SimContactsConstants.ACCOUNT_DATA, account.dataSet);
Chiao Cheng7903d242012-12-03 17:15:58 -0800218 }
Yujing Gu8ff950a2014-07-30 15:25:14 +0800219 if (TelephonyManager.getDefault().isMultiSimEnabled()) {
220 importIntent.putExtra(SimContactsConstants.SUB, mImportSub);
221 } else {
222 importIntent.putExtra(SimContactsConstants.SUB,SimContactsConstants.SUB_1);
223 }
Walter Jang95970102016-10-25 15:42:33 -0700224 activity.startActivityForResult(importIntent, 0);
Rakesh Pallerla14e31e52014-08-22 19:50:34 +0530225 }
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 Cheng7903d242012-12-03 17:15:58 -0800238 context.startActivity(importIntent);
239 }
240
Walter Jang95970102016-10-25 15:42:33 -0700241 public static void doImportFromSdCard(Activity activity, AccountWithDataSet account) {
242 Intent importIntent = new Intent(activity, ImportVCardActivity.class);
Chiao Cheng7903d242012-12-03 17:15:58 -0800243 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 Jang95970102016-10-25 15:42:33 -0700255 activity.startActivityForResult(importIntent, 0);
Chiao Cheng7903d242012-12-03 17:15:58 -0800256 }
Rakesh Pallerla14e31e52014-08-22 19:50:34 +0530257
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 Cheng7903d242012-12-03 17:15:58 -0800323}