Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 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 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.Intent; |
| 25 | import android.net.Uri; |
| 26 | import android.util.Log; |
| 27 | import android.view.ContextThemeWrapper; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | import android.widget.ArrayAdapter; |
| 32 | import android.widget.TextView; |
| 33 | |
| 34 | import com.android.contacts.common.R; |
| 35 | import com.android.contacts.common.model.AccountTypeManager; |
| 36 | import com.android.contacts.common.model.account.AccountType; |
| 37 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 38 | import com.android.contacts.common.vcard.ImportVCardActivity; |
| 39 | |
| 40 | import java.util.List; |
| 41 | |
| 42 | /** |
Brian Attwell | 90b8b6d | 2015-02-03 10:28:11 -0800 | [diff] [blame] | 43 | * Utility class for selecting an Account for importing contact(s) |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 44 | */ |
| 45 | public 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 56 | final private Activity mActivity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 57 | final private int mResId; |
Wink Saville | d09e43c | 2014-10-23 10:25:58 -0700 | [diff] [blame] | 58 | final private int mSubscriptionId; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 59 | |
| 60 | final protected List<AccountWithDataSet> mAccountList; |
| 61 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 62 | public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, |
Wink Saville | d09e43c | 2014-10-23 10:25:58 -0700 | [diff] [blame] | 63 | int resId, int subscriptionId) { |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 64 | if (accountList == null || accountList.size() == 0) { |
| 65 | Log.e(LOG_TAG, "The size of Account list is 0."); |
| 66 | } |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 67 | mActivity = activity; |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 68 | mAccountList = accountList; |
| 69 | mResId = resId; |
Brian Attwell | 9524fd1 | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 70 | mSubscriptionId = subscriptionId; |
| 71 | } |
| 72 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 73 | public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, |
Brian Attwell | 9524fd1 | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 74 | 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 77 | this(activity, accountList, resId, /* subscriptionId = */ -1); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | public void onClick(DialogInterface dialog, int which) { |
| 81 | dialog.dismiss(); |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 82 | doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 86 | public static Dialog getSelectAccountDialog(Activity activity, int resId) { |
| 87 | return getSelectAccountDialog(activity, resId, null, null); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 90 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 91 | DialogInterface.OnClickListener onClickListener) { |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 92 | return getSelectAccountDialog(activity, resId, onClickListener, null); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 93 | } |
| 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 99 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 100 | DialogInterface.OnClickListener onClickListener, |
| 101 | DialogInterface.OnCancelListener onCancelListener) { |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 102 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 103 | 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 111 | activity, android.R.style.Theme_Light); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 112 | final LayoutInflater dialogInflater = (LayoutInflater)dialogContext |
| 113 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 114 | final ArrayAdapter<AccountWithDataSet> accountAdapter = |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 115 | new ArrayAdapter<AccountWithDataSet>(activity, android.R.layout.simple_list_item_2, |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 116 | 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 146 | new AccountSelectedListener(activity, writableAccountList, resId); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 147 | 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 156 | return new AlertDialog.Builder(activity) |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 157 | .setTitle(R.string.dialog_new_contact_account) |
| 158 | .setSingleChoiceItems(accountAdapter, 0, onClickListener) |
| 159 | .setOnCancelListener(onCancelListener) |
| 160 | .create(); |
| 161 | } |
| 162 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 163 | public static void doImport(Activity activity, int resId, AccountWithDataSet account, |
Jay Shrauner | 2f3fd5f | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 164 | int subscriptionId) { |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 165 | switch (resId) { |
Rakesh Pallerla | 4b04e2d | 2013-09-11 15:52:40 +0530 | [diff] [blame] | 166 | case R.string.manage_sim_contacts: { |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 167 | doImportFromSim(activity, account, subscriptionId); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 168 | break; |
| 169 | } |
Walter Jang | ed2a24c | 2015-07-14 10:56:03 -0700 | [diff] [blame] | 170 | case R.string.import_from_vcf_file: { |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 171 | doImportFromVcfFile(activity, account); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Brian Attwell | 9524fd1 | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 177 | public static void doImportFromSim(Context context, AccountWithDataSet account, |
Jay Shrauner | 2f3fd5f | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 178 | int subscriptionId) { |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 179 | 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 Shrauner | 2f3fd5f | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 186 | importIntent.putExtra("subscription_id", (Integer) subscriptionId); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 187 | importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts"); |
Brian Attwell | 6084957 | 2015-02-26 11:44:26 -0800 | [diff] [blame] | 188 | context.startActivity(importIntent); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Walter Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 191 | public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) { |
| 192 | Intent importIntent = new Intent(activity, ImportVCardActivity.class); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 193 | 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 Jang | 6b87ef1 | 2016-11-04 14:03:39 -0700 | [diff] [blame] | 205 | activity.startActivityForResult(importIntent, 0); |
Chiao Cheng | 7903d24 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 206 | } |
| 207 | } |