blob: 9fca64def1af2e855449c5970ee928404b2ee4e5 [file] [log] [blame]
Chiao Chengd80c4342012-12-03 17:15:58 -08001/*
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
17package com.android.contacts.common.util;
18
Walter Jang915ccdd2016-10-24 12:18:20 -070019import android.app.Activity;
Chiao Chengd80c4342012-12-03 17:15:58 -080020import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.net.Uri;
26import android.util.Log;
27import android.view.ContextThemeWrapper;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ArrayAdapter;
Wenyi Wang6a1fb6b2015-11-04 14:42:03 -080032import android.widget.ImageView;
Chiao Chengd80c4342012-12-03 17:15:58 -080033import android.widget.TextView;
34
35import com.android.contacts.common.R;
36import com.android.contacts.common.model.AccountTypeManager;
37import com.android.contacts.common.model.account.AccountType;
38import com.android.contacts.common.model.account.AccountWithDataSet;
39import com.android.contacts.common.vcard.ImportVCardActivity;
40
41import java.util.List;
42
43/**
Brian Attwell87ffb822015-02-03 10:28:11 -080044 * Utility class for selecting an Account for importing contact(s)
Chiao Chengd80c4342012-12-03 17:15:58 -080045 */
46public class AccountSelectionUtil {
47 // TODO: maybe useful for EditContactActivity.java...
48 private static final String LOG_TAG = "AccountSelectionUtil";
49
50 public static boolean mVCardShare = false;
51
52 public static Uri mPath;
53
54 public static class AccountSelectedListener
55 implements DialogInterface.OnClickListener {
56
Walter Jang915ccdd2016-10-24 12:18:20 -070057 final private Activity mActivity;
Chiao Chengd80c4342012-12-03 17:15:58 -080058 final private int mResId;
Wink Savilled46d9052014-10-23 10:25:58 -070059 final private int mSubscriptionId;
Chiao Chengd80c4342012-12-03 17:15:58 -080060
61 final protected List<AccountWithDataSet> mAccountList;
62
Walter Jang915ccdd2016-10-24 12:18:20 -070063 public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
Wink Savilled46d9052014-10-23 10:25:58 -070064 int resId, int subscriptionId) {
Chiao Chengd80c4342012-12-03 17:15:58 -080065 if (accountList == null || accountList.size() == 0) {
66 Log.e(LOG_TAG, "The size of Account list is 0.");
67 }
Walter Jang915ccdd2016-10-24 12:18:20 -070068 mActivity = activity;
Chiao Chengd80c4342012-12-03 17:15:58 -080069 mAccountList = accountList;
70 mResId = resId;
Brian Attwellc07c8bb2014-10-06 14:55:49 -070071 mSubscriptionId = subscriptionId;
72 }
73
Walter Jang915ccdd2016-10-24 12:18:20 -070074 public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
Brian Attwellc07c8bb2014-10-06 14:55:49 -070075 int resId) {
76 // Subscription id is only needed for importing from SIM card. We can safely ignore
77 // its value for SD card importing.
Walter Jang915ccdd2016-10-24 12:18:20 -070078 this(activity, accountList, resId, /* subscriptionId = */ -1);
Chiao Chengd80c4342012-12-03 17:15:58 -080079 }
80
81 public void onClick(DialogInterface dialog, int which) {
82 dialog.dismiss();
Walter Jang915ccdd2016-10-24 12:18:20 -070083 doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId);
Chiao Chengd80c4342012-12-03 17:15:58 -080084 }
85 }
86
Walter Jang915ccdd2016-10-24 12:18:20 -070087 public static Dialog getSelectAccountDialog(Activity activity, int resId) {
88 return getSelectAccountDialog(activity, resId, null, null);
Chiao Chengd80c4342012-12-03 17:15:58 -080089 }
90
Walter Jang915ccdd2016-10-24 12:18:20 -070091 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Chengd80c4342012-12-03 17:15:58 -080092 DialogInterface.OnClickListener onClickListener) {
Walter Jang915ccdd2016-10-24 12:18:20 -070093 return getSelectAccountDialog(activity, resId, onClickListener, null);
Chiao Chengd80c4342012-12-03 17:15:58 -080094 }
95
96 /**
97 * When OnClickListener or OnCancelListener is null, uses a default listener.
98 * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
99 */
Walter Jang915ccdd2016-10-24 12:18:20 -0700100 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Chengd80c4342012-12-03 17:15:58 -0800101 DialogInterface.OnClickListener onClickListener,
102 DialogInterface.OnCancelListener onCancelListener) {
Walter Jang915ccdd2016-10-24 12:18:20 -0700103 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
Chiao Chengd80c4342012-12-03 17:15:58 -0800104 final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
105
106 Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
107
108 // Assume accountList.size() > 1
109
110 // Wrap our context to inflate list items using correct theme
111 final Context dialogContext = new ContextThemeWrapper(
Walter Jang915ccdd2016-10-24 12:18:20 -0700112 activity, android.R.style.Theme_Light);
Chiao Chengd80c4342012-12-03 17:15:58 -0800113 final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
114 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115 final ArrayAdapter<AccountWithDataSet> accountAdapter =
Wenyi Wang6a1fb6b2015-11-04 14:42:03 -0800116 new ArrayAdapter<AccountWithDataSet>(
Walter Jang915ccdd2016-10-24 12:18:20 -0700117 activity, R.layout.account_selector_list_item_condensed, writableAccountList) {
Chiao Chengd80c4342012-12-03 17:15:58 -0800118 @Override
119 public View getView(int position, View convertView, ViewGroup parent) {
120 if (convertView == null) {
121 convertView = dialogInflater.inflate(
Wenyi Wang6a1fb6b2015-11-04 14:42:03 -0800122 R.layout.account_selector_list_item_condensed,
Chiao Chengd80c4342012-12-03 17:15:58 -0800123 parent, false);
124 }
125
Wenyi Wang6a1fb6b2015-11-04 14:42:03 -0800126 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
127 final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
128 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Chiao Chengd80c4342012-12-03 17:15:58 -0800129
130 final AccountWithDataSet account = this.getItem(position);
131 final AccountType accountType = accountTypes.getAccountType(
132 account.type, account.dataSet);
133 final Context context = getContext();
134
Wenyi Wang6a1fb6b2015-11-04 14:42:03 -0800135 text1.setText(accountType.getDisplayLabel(context));
136 text2.setText(account.name);
137 icon.setImageDrawable(accountType.getDisplayIcon(getContext()));
Chiao Chengd80c4342012-12-03 17:15:58 -0800138
139 return convertView;
140 }
141 };
142
143 if (onClickListener == null) {
144 AccountSelectedListener accountSelectedListener =
Walter Jang915ccdd2016-10-24 12:18:20 -0700145 new AccountSelectedListener(activity, writableAccountList, resId);
Chiao Chengd80c4342012-12-03 17:15:58 -0800146 onClickListener = accountSelectedListener;
147 }
148 if (onCancelListener == null) {
149 onCancelListener = new DialogInterface.OnCancelListener() {
150 public void onCancel(DialogInterface dialog) {
151 dialog.dismiss();
152 }
153 };
154 }
Walter Jang915ccdd2016-10-24 12:18:20 -0700155 final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
156 final TextView title = (TextView) View.inflate(activity, R.layout.dialog_title, null);
guanxiongliud0b4e6c2016-07-22 10:38:49 -0700157 title.setText(R.string.dialog_new_contact_account);
158 builder.setCustomTitle(title);
159 builder.setSingleChoiceItems(accountAdapter, 0, onClickListener);
160 builder.setOnCancelListener(onCancelListener);
161 final AlertDialog result = builder.create();
162 return result;
Chiao Chengd80c4342012-12-03 17:15:58 -0800163 }
164
Walter Jang915ccdd2016-10-24 12:18:20 -0700165 public static void doImport(Activity activity, int resId, AccountWithDataSet account,
Jay Shraunercf082222014-11-25 13:52:50 -0800166 int subscriptionId) {
Sailesh Nepalb4a522e2016-02-20 21:17:32 -0800167 if (resId == R.string.import_from_sim) {
Walter Jang915ccdd2016-10-24 12:18:20 -0700168 doImportFromSim(activity, account, subscriptionId);
Sailesh Nepalb4a522e2016-02-20 21:17:32 -0800169 } else if (resId == R.string.import_from_vcf_file) {
Walter Jang915ccdd2016-10-24 12:18:20 -0700170 doImportFromVcfFile(activity, account);
Chiao Chengd80c4342012-12-03 17:15:58 -0800171 }
172 }
173
Brian Attwellc07c8bb2014-10-06 14:55:49 -0700174 public static void doImportFromSim(Context context, AccountWithDataSet account,
Jay Shraunercf082222014-11-25 13:52:50 -0800175 int subscriptionId) {
Chiao Chengd80c4342012-12-03 17:15:58 -0800176 Intent importIntent = new Intent(Intent.ACTION_VIEW);
177 importIntent.setType("vnd.android.cursor.item/sim-contact");
178 if (account != null) {
179 importIntent.putExtra("account_name", account.name);
180 importIntent.putExtra("account_type", account.type);
181 importIntent.putExtra("data_set", account.dataSet);
182 }
Jay Shraunercf082222014-11-25 13:52:50 -0800183 importIntent.putExtra("subscription_id", (Integer) subscriptionId);
Chiao Chengd80c4342012-12-03 17:15:58 -0800184 importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
Brian Attwell42347742015-02-26 11:44:26 -0800185 context.startActivity(importIntent);
Chiao Chengd80c4342012-12-03 17:15:58 -0800186 }
187
Walter Jang915ccdd2016-10-24 12:18:20 -0700188 public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) {
189 Intent importIntent = new Intent(activity, ImportVCardActivity.class);
Chiao Chengd80c4342012-12-03 17:15:58 -0800190 if (account != null) {
191 importIntent.putExtra("account_name", account.name);
192 importIntent.putExtra("account_type", account.type);
193 importIntent.putExtra("data_set", account.dataSet);
194 }
195
196 if (mVCardShare) {
197 importIntent.setAction(Intent.ACTION_VIEW);
198 importIntent.setData(mPath);
199 }
200 mVCardShare = false;
201 mPath = null;
Walter Jang915ccdd2016-10-24 12:18:20 -0700202 activity.startActivityForResult(importIntent, 0);
Chiao Chengd80c4342012-12-03 17:15:58 -0800203 }
204}