blob: 741979e96034252d5b8881ef905b86acc3324e5b [file] [log] [blame]
Chiao Cheng7903d242012-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 Jang6b87ef12016-11-04 14:03:39 -070019import android.app.Activity;
Chiao Cheng7903d242012-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;
32import android.widget.TextView;
33
34import com.android.contacts.common.R;
35import com.android.contacts.common.model.AccountTypeManager;
36import com.android.contacts.common.model.account.AccountType;
37import com.android.contacts.common.model.account.AccountWithDataSet;
38import com.android.contacts.common.vcard.ImportVCardActivity;
39
40import java.util.List;
41
42/**
Brian Attwell90b8b6d2015-02-03 10:28:11 -080043 * Utility class for selecting an Account for importing contact(s)
Chiao Cheng7903d242012-12-03 17:15:58 -080044 */
45public 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
Walter Jang6b87ef12016-11-04 14:03:39 -070056 final private Activity mActivity;
Chiao Cheng7903d242012-12-03 17:15:58 -080057 final private int mResId;
Wink Savilled09e43c2014-10-23 10:25:58 -070058 final private int mSubscriptionId;
Chiao Cheng7903d242012-12-03 17:15:58 -080059
60 final protected List<AccountWithDataSet> mAccountList;
61
Walter Jang6b87ef12016-11-04 14:03:39 -070062 public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
Wink Savilled09e43c2014-10-23 10:25:58 -070063 int resId, int subscriptionId) {
Chiao Cheng7903d242012-12-03 17:15:58 -080064 if (accountList == null || accountList.size() == 0) {
65 Log.e(LOG_TAG, "The size of Account list is 0.");
66 }
Walter Jang6b87ef12016-11-04 14:03:39 -070067 mActivity = activity;
Chiao Cheng7903d242012-12-03 17:15:58 -080068 mAccountList = accountList;
69 mResId = resId;
Brian Attwell9524fd12014-10-06 14:55:49 -070070 mSubscriptionId = subscriptionId;
71 }
72
Walter Jang6b87ef12016-11-04 14:03:39 -070073 public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
Brian Attwell9524fd12014-10-06 14:55:49 -070074 int resId) {
75 // Subscription id is only needed for importing from SIM card. We can safely ignore
76 // its value for SD card importing.
Walter Jang6b87ef12016-11-04 14:03:39 -070077 this(activity, accountList, resId, /* subscriptionId = */ -1);
Chiao Cheng7903d242012-12-03 17:15:58 -080078 }
79
80 public void onClick(DialogInterface dialog, int which) {
81 dialog.dismiss();
Walter Jang6b87ef12016-11-04 14:03:39 -070082 doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId);
Chiao Cheng7903d242012-12-03 17:15:58 -080083 }
84 }
85
Walter Jang6b87ef12016-11-04 14:03:39 -070086 public static Dialog getSelectAccountDialog(Activity activity, int resId) {
87 return getSelectAccountDialog(activity, resId, null, null);
Chiao Cheng7903d242012-12-03 17:15:58 -080088 }
89
Walter Jang6b87ef12016-11-04 14:03:39 -070090 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Cheng7903d242012-12-03 17:15:58 -080091 DialogInterface.OnClickListener onClickListener) {
Walter Jang6b87ef12016-11-04 14:03:39 -070092 return getSelectAccountDialog(activity, resId, onClickListener, null);
Chiao Cheng7903d242012-12-03 17:15:58 -080093 }
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 */
Walter Jang6b87ef12016-11-04 14:03:39 -070099 public static Dialog getSelectAccountDialog(Activity activity, int resId,
Chiao Cheng7903d242012-12-03 17:15:58 -0800100 DialogInterface.OnClickListener onClickListener,
101 DialogInterface.OnCancelListener onCancelListener) {
Walter Jang6b87ef12016-11-04 14:03:39 -0700102 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
Chiao Cheng7903d242012-12-03 17:15:58 -0800103 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(
Walter Jang6b87ef12016-11-04 14:03:39 -0700111 activity, android.R.style.Theme_Light);
Chiao Cheng7903d242012-12-03 17:15:58 -0800112 final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
113 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
114 final ArrayAdapter<AccountWithDataSet> accountAdapter =
Walter Jang6b87ef12016-11-04 14:03:39 -0700115 new ArrayAdapter<AccountWithDataSet>(activity, android.R.layout.simple_list_item_2,
Chiao Cheng7903d242012-12-03 17:15:58 -0800116 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 =
Walter Jang6b87ef12016-11-04 14:03:39 -0700146 new AccountSelectedListener(activity, writableAccountList, resId);
Chiao Cheng7903d242012-12-03 17:15:58 -0800147 onClickListener = accountSelectedListener;
148 }
149 if (onCancelListener == null) {
150 onCancelListener = new DialogInterface.OnCancelListener() {
151 public void onCancel(DialogInterface dialog) {
152 dialog.dismiss();
153 }
154 };
155 }
Walter Jang6b87ef12016-11-04 14:03:39 -0700156 return new AlertDialog.Builder(activity)
Chiao Cheng7903d242012-12-03 17:15:58 -0800157 .setTitle(R.string.dialog_new_contact_account)
158 .setSingleChoiceItems(accountAdapter, 0, onClickListener)
159 .setOnCancelListener(onCancelListener)
160 .create();
161 }
162
Walter Jang6b87ef12016-11-04 14:03:39 -0700163 public static void doImport(Activity activity, int resId, AccountWithDataSet account,
Jay Shrauner2f3fd5f2014-11-25 13:52:50 -0800164 int subscriptionId) {
Chiao Cheng7903d242012-12-03 17:15:58 -0800165 switch (resId) {
Rakesh Pallerla4b04e2d2013-09-11 15:52:40 +0530166 case R.string.manage_sim_contacts: {
Walter Jang6b87ef12016-11-04 14:03:39 -0700167 doImportFromSim(activity, account, subscriptionId);
Chiao Cheng7903d242012-12-03 17:15:58 -0800168 break;
169 }
Walter Janged2a24c2015-07-14 10:56:03 -0700170 case R.string.import_from_vcf_file: {
Walter Jang6b87ef12016-11-04 14:03:39 -0700171 doImportFromVcfFile(activity, account);
Chiao Cheng7903d242012-12-03 17:15:58 -0800172 break;
173 }
174 }
175 }
176
Brian Attwell9524fd12014-10-06 14:55:49 -0700177 public static void doImportFromSim(Context context, AccountWithDataSet account,
Jay Shrauner2f3fd5f2014-11-25 13:52:50 -0800178 int subscriptionId) {
Chiao Cheng7903d242012-12-03 17:15:58 -0800179 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 }
Jay Shrauner2f3fd5f2014-11-25 13:52:50 -0800186 importIntent.putExtra("subscription_id", (Integer) subscriptionId);
Chiao Cheng7903d242012-12-03 17:15:58 -0800187 importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
Brian Attwell60849572015-02-26 11:44:26 -0800188 context.startActivity(importIntent);
Chiao Cheng7903d242012-12-03 17:15:58 -0800189 }
190
Walter Jang6b87ef12016-11-04 14:03:39 -0700191 public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) {
192 Intent importIntent = new Intent(activity, ImportVCardActivity.class);
Chiao Cheng7903d242012-12-03 17:15:58 -0800193 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;
Walter Jang6b87ef12016-11-04 14:03:39 -0700205 activity.startActivityForResult(importIntent, 0);
Chiao Cheng7903d242012-12-03 17:15:58 -0800206 }
207}