Chiao Cheng | d80c434 | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.util; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 18 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Chiao Cheng | d80c434 | 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; |
Wenyi Wang | 6a1fb6b | 2015-11-04 14:42:03 -0800 | [diff] [blame] | 32 | import android.widget.ImageView; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 35 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 36 | import com.android.contacts.model.AccountTypeManager; |
| 37 | import com.android.contacts.model.account.AccountType; |
| 38 | import com.android.contacts.model.account.AccountWithDataSet; |
| 39 | import com.android.contacts.vcard.ImportVCardActivity; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 40 | |
| 41 | import java.util.List; |
| 42 | |
| 43 | /** |
Brian Attwell | 87ffb82 | 2015-02-03 10:28:11 -0800 | [diff] [blame] | 44 | * Utility class for selecting an Account for importing contact(s) |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 45 | */ |
| 46 | public 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 57 | final private Activity mActivity; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 58 | final private int mResId; |
Wink Saville | d46d905 | 2014-10-23 10:25:58 -0700 | [diff] [blame] | 59 | final private int mSubscriptionId; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 60 | |
| 61 | final protected List<AccountWithDataSet> mAccountList; |
| 62 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 63 | public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, |
Wink Saville | d46d905 | 2014-10-23 10:25:58 -0700 | [diff] [blame] | 64 | int resId, int subscriptionId) { |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 65 | if (accountList == null || accountList.size() == 0) { |
| 66 | Log.e(LOG_TAG, "The size of Account list is 0."); |
| 67 | } |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 68 | mActivity = activity; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 69 | mAccountList = accountList; |
| 70 | mResId = resId; |
Brian Attwell | c07c8bb | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 71 | mSubscriptionId = subscriptionId; |
| 72 | } |
| 73 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 74 | public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, |
Brian Attwell | c07c8bb | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 75 | 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 78 | this(activity, accountList, resId, /* subscriptionId = */ -1); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public void onClick(DialogInterface dialog, int which) { |
| 82 | dialog.dismiss(); |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 83 | doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 87 | public static Dialog getSelectAccountDialog(Activity activity, int resId) { |
| 88 | return getSelectAccountDialog(activity, resId, null, null); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 91 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 92 | DialogInterface.OnClickListener onClickListener) { |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 93 | return getSelectAccountDialog(activity, resId, onClickListener, null); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 94 | } |
| 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 100 | public static Dialog getSelectAccountDialog(Activity activity, int resId, |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 101 | DialogInterface.OnClickListener onClickListener, |
| 102 | DialogInterface.OnCancelListener onCancelListener) { |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 103 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 104 | 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 112 | activity, android.R.style.Theme_Light); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 113 | final LayoutInflater dialogInflater = (LayoutInflater)dialogContext |
| 114 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 115 | final ArrayAdapter<AccountWithDataSet> accountAdapter = |
Wenyi Wang | 6a1fb6b | 2015-11-04 14:42:03 -0800 | [diff] [blame] | 116 | new ArrayAdapter<AccountWithDataSet>( |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 117 | activity, R.layout.account_selector_list_item_condensed, writableAccountList) { |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public View getView(int position, View convertView, ViewGroup parent) { |
| 120 | if (convertView == null) { |
| 121 | convertView = dialogInflater.inflate( |
Wenyi Wang | 6a1fb6b | 2015-11-04 14:42:03 -0800 | [diff] [blame] | 122 | R.layout.account_selector_list_item_condensed, |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 123 | parent, false); |
| 124 | } |
| 125 | |
Wenyi Wang | 6a1fb6b | 2015-11-04 14:42:03 -0800 | [diff] [blame] | 126 | 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 Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 129 | |
| 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 Wang | 6a1fb6b | 2015-11-04 14:42:03 -0800 | [diff] [blame] | 135 | text1.setText(accountType.getDisplayLabel(context)); |
| 136 | text2.setText(account.name); |
| 137 | icon.setImageDrawable(accountType.getDisplayIcon(getContext())); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 138 | |
| 139 | return convertView; |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | if (onClickListener == null) { |
| 144 | AccountSelectedListener accountSelectedListener = |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 145 | new AccountSelectedListener(activity, writableAccountList, resId); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 146 | 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 155 | final AlertDialog.Builder builder = new AlertDialog.Builder(activity); |
| 156 | final TextView title = (TextView) View.inflate(activity, R.layout.dialog_title, null); |
guanxiongliu | d0b4e6c | 2016-07-22 10:38:49 -0700 | [diff] [blame] | 157 | 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 Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 165 | public static void doImport(Activity activity, int resId, AccountWithDataSet account, |
Jay Shrauner | cf08222 | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 166 | int subscriptionId) { |
Sailesh Nepal | b4a522e | 2016-02-20 21:17:32 -0800 | [diff] [blame] | 167 | if (resId == R.string.import_from_sim) { |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 168 | doImportFromSim(activity, account, subscriptionId); |
Sailesh Nepal | b4a522e | 2016-02-20 21:17:32 -0800 | [diff] [blame] | 169 | } else if (resId == R.string.import_from_vcf_file) { |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 170 | doImportFromVcfFile(activity, account); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Brian Attwell | c07c8bb | 2014-10-06 14:55:49 -0700 | [diff] [blame] | 174 | public static void doImportFromSim(Context context, AccountWithDataSet account, |
Jay Shrauner | cf08222 | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 175 | int subscriptionId) { |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 176 | 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 Shrauner | cf08222 | 2014-11-25 13:52:50 -0800 | [diff] [blame] | 183 | importIntent.putExtra("subscription_id", (Integer) subscriptionId); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 184 | importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts"); |
Brian Attwell | 4234774 | 2015-02-26 11:44:26 -0800 | [diff] [blame] | 185 | context.startActivity(importIntent); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 188 | public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) { |
| 189 | Intent importIntent = new Intent(activity, ImportVCardActivity.class); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 190 | 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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 202 | activity.startActivityForResult(importIntent, 0); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 203 | } |
| 204 | } |