Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.activities; |
| 18 | |
Wenyi Wang | bf96dd1 | 2016-02-21 11:23:24 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 21 | import android.app.LoaderManager; |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 22 | import android.content.DialogInterface; |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 24 | import android.content.Loader; |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.provider.ContactsContract.Intents; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.widget.AdapterView; |
| 30 | import android.widget.AdapterView.OnItemClickListener; |
| 31 | import android.widget.Button; |
| 32 | import android.widget.ListView; |
| 33 | import android.widget.TextView; |
| 34 | |
| 35 | import com.android.contacts.R; |
| 36 | import com.android.contacts.editor.ContactEditorUtils; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 37 | import com.android.contacts.model.AccountTypeManager; |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 38 | import com.android.contacts.model.account.AccountInfo; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 39 | import com.android.contacts.model.account.AccountWithDataSet; |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 40 | import com.android.contacts.model.account.AccountsLoader; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 41 | import com.android.contacts.util.AccountsListAdapter; |
| 42 | import com.android.contacts.util.AccountsListAdapter.AccountListFilter; |
| 43 | import com.android.contacts.util.ImplicitIntentsUtil; |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 44 | import com.google.common.util.concurrent.Futures; |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 45 | |
| 46 | import java.util.List; |
| 47 | |
| 48 | /** |
| 49 | * This activity can be shown to the user when creating a new contact to inform the user about |
| 50 | * which account the contact will be saved in. There is also an option to add an account at |
| 51 | * this time. The {@link Intent} in the activity result will contain an extra |
| 52 | * {@link #Intents.Insert.ACCOUNT} that contains the {@link AccountWithDataSet} to create |
| 53 | * the new contact in. If the activity result doesn't contain intent data, then there is no |
| 54 | * account for this contact. |
| 55 | */ |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 56 | public class ContactEditorAccountsChangedActivity extends Activity |
| 57 | implements LoaderManager.LoaderCallbacks<List<AccountInfo>> { |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 58 | |
| 59 | private static final String TAG = ContactEditorAccountsChangedActivity.class.getSimpleName(); |
| 60 | |
| 61 | private static final int SUBACTIVITY_ADD_NEW_ACCOUNT = 1; |
| 62 | |
| 63 | private AccountsListAdapter mAccountListAdapter; |
| 64 | private ContactEditorUtils mEditorUtils; |
Gary Mai | 85b15a6 | 2016-10-19 14:28:48 -0700 | [diff] [blame] | 65 | private AlertDialog mDialog; |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 66 | |
| 67 | private final OnItemClickListener mAccountListItemClickListener = new OnItemClickListener() { |
| 68 | @Override |
| 69 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 70 | if (mAccountListAdapter == null) { |
| 71 | return; |
| 72 | } |
| 73 | saveAccountAndReturnResult(mAccountListAdapter.getItem(position)); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | private final OnClickListener mAddAccountClickListener = new OnClickListener() { |
| 78 | @Override |
| 79 | public void onClick(View v) { |
Walter Jang | 5e19c23 | 2016-07-14 12:38:04 -0700 | [diff] [blame] | 80 | final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount(); |
guanxiongliu | f597ec6 | 2016-01-25 19:49:42 -0800 | [diff] [blame] | 81 | startActivityForResult(intent, SUBACTIVITY_ADD_NEW_ACCOUNT); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | @Override |
Gary Mai | 85b15a6 | 2016-10-19 14:28:48 -0700 | [diff] [blame] | 86 | protected void onResume() { |
| 87 | super.onResume(); |
| 88 | if (mDialog != null && !mDialog.isShowing()) { |
| 89 | mDialog.show(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected void onPause() { |
| 95 | super.onPause(); |
| 96 | if (mDialog != null) { |
| 97 | mDialog.dismiss(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 102 | protected void onCreate(Bundle savedInstanceState) { |
| 103 | super.onCreate(savedInstanceState); |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 104 | mEditorUtils = ContactEditorUtils.create(this); |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 105 | getLoaderManager().initLoader(0, null, this); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 110 | if (requestCode == SUBACTIVITY_ADD_NEW_ACCOUNT) { |
| 111 | // If the user canceled the account setup process, then keep this activity visible to |
| 112 | // the user. |
| 113 | if (resultCode != RESULT_OK) { |
| 114 | return; |
| 115 | } |
| 116 | // Subactivity was successful, so pass the result back and finish the activity. |
| 117 | AccountWithDataSet account = mEditorUtils.getCreatedAccount(resultCode, data); |
| 118 | if (account == null) { |
| 119 | setResult(resultCode); |
| 120 | finish(); |
| 121 | return; |
| 122 | } |
| 123 | saveAccountAndReturnResult(account); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private void updateDisplayedAccounts(List<AccountInfo> accounts) { |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 128 | final int numAccounts = accounts.size(); |
| 129 | if (numAccounts < 0) { |
| 130 | throw new IllegalStateException("Cannot have a negative number of accounts"); |
| 131 | } |
| 132 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 133 | final View view; |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 134 | if (numAccounts >= 2) { |
| 135 | // When the user has 2+ writable accounts, show a list of accounts so the user can pick |
| 136 | // which account to create a contact in. |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 137 | view = View.inflate(this, |
| 138 | R.layout.contact_editor_accounts_changed_activity_with_picker, null); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 139 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 140 | final TextView textView = (TextView) view.findViewById(R.id.text); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 141 | textView.setText(getString(R.string.contact_editor_prompt_multiple_accounts)); |
| 142 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 143 | final Button button = (Button) view.findViewById(R.id.add_account_button); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 144 | button.setText(getString(R.string.add_new_account)); |
| 145 | button.setOnClickListener(mAddAccountClickListener); |
| 146 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 147 | final ListView accountListView = (ListView) view.findViewById(R.id.account_list); |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 148 | mAccountListAdapter = new AccountsListAdapter(this, |
| 149 | AccountListFilter.ACCOUNTS_CONTACT_WRITABLE); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 150 | accountListView.setAdapter(mAccountListAdapter); |
| 151 | accountListView.setOnItemClickListener(mAccountListItemClickListener); |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 152 | } else if (numAccounts == 1 && !accounts.get(0).getAccount().isNullAccount()) { |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 153 | // If the user has 1 writable account we will just show the user a message with 2 |
| 154 | // possible action buttons. |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 155 | view = View.inflate(this, |
| 156 | R.layout.contact_editor_accounts_changed_activity_with_text, null); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 157 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 158 | final TextView textView = (TextView) view.findViewById(R.id.text); |
| 159 | final Button leftButton = (Button) view.findViewById(R.id.left_button); |
| 160 | final Button rightButton = (Button) view.findViewById(R.id.right_button); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 161 | |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 162 | final AccountInfo accountInfo = accounts.get(0); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 163 | textView.setText(getString(R.string.contact_editor_prompt_one_account, |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 164 | accountInfo.getNameLabel())); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 165 | |
| 166 | // This button allows the user to add a new account to the device and return to |
| 167 | // this app afterwards. |
| 168 | leftButton.setText(getString(R.string.add_new_account)); |
| 169 | leftButton.setOnClickListener(mAddAccountClickListener); |
| 170 | |
| 171 | // This button allows the user to continue creating the contact in the specified |
| 172 | // account. |
| 173 | rightButton.setText(getString(android.R.string.ok)); |
| 174 | rightButton.setOnClickListener(new OnClickListener() { |
| 175 | @Override |
| 176 | public void onClick(View v) { |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 177 | saveAccountAndReturnResult(accountInfo.getAccount()); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 178 | } |
| 179 | }); |
| 180 | } else { |
| 181 | // If the user has 0 writable accounts, we will just show the user a message with 2 |
| 182 | // possible action buttons. |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 183 | view = View.inflate(this, |
| 184 | R.layout.contact_editor_accounts_changed_activity_with_text, null); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 185 | |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 186 | final TextView textView = (TextView) view.findViewById(R.id.text); |
| 187 | final Button leftButton = (Button) view.findViewById(R.id.left_button); |
| 188 | final Button rightButton = (Button) view.findViewById(R.id.right_button); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 189 | |
| 190 | textView.setText(getString(R.string.contact_editor_prompt_zero_accounts)); |
| 191 | |
| 192 | // This button allows the user to continue editing the contact as a phone-only |
| 193 | // local contact. |
guanxiongliu | cb26b37 | 2016-07-29 15:44:13 -0700 | [diff] [blame] | 194 | leftButton.setText(getString(android.R.string.cancel)); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 195 | leftButton.setOnClickListener(new OnClickListener() { |
| 196 | @Override |
| 197 | public void onClick(View v) { |
| 198 | // Remember that the user wants to create local contacts, so the user is not |
| 199 | // prompted again with this activity. |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 200 | mEditorUtils.saveDefaultAccount(AccountWithDataSet.getNullAccount()); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 201 | setResult(RESULT_OK); |
| 202 | finish(); |
| 203 | } |
| 204 | }); |
| 205 | |
| 206 | // This button allows the user to add a new account to the device and return to |
| 207 | // this app afterwards. |
| 208 | rightButton.setText(getString(R.string.add_account)); |
| 209 | rightButton.setOnClickListener(mAddAccountClickListener); |
| 210 | } |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 211 | |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 212 | if (mDialog != null && mDialog.isShowing()) { |
| 213 | mDialog.dismiss(); |
| 214 | } |
Gary Mai | 85b15a6 | 2016-10-19 14:28:48 -0700 | [diff] [blame] | 215 | mDialog = new AlertDialog.Builder(this) |
Wenyi Wang | 60b44bf | 2015-11-05 10:39:22 -0800 | [diff] [blame] | 216 | .setView(view) |
| 217 | .setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 218 | @Override |
| 219 | public void onCancel(DialogInterface dialog) { |
| 220 | finish(); |
| 221 | } |
| 222 | }) |
Gary Mai | 85b15a6 | 2016-10-19 14:28:48 -0700 | [diff] [blame] | 223 | .create(); |
| 224 | mDialog.show(); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 227 | private void saveAccountAndReturnResult(AccountWithDataSet account) { |
| 228 | // Save this as the default account |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 229 | mEditorUtils.saveDefaultAccount(account); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 230 | |
| 231 | // Pass account info in activity result intent |
| 232 | Intent intent = new Intent(); |
Brian Attwell | 4a1c574 | 2015-01-26 15:59:58 -0800 | [diff] [blame] | 233 | intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account); |
Katherine Kuan | 0353a24 | 2011-09-23 10:38:28 -0700 | [diff] [blame] | 234 | setResult(RESULT_OK, intent); |
| 235 | finish(); |
| 236 | } |
Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 237 | |
| 238 | @Override |
| 239 | public Loader<List<AccountInfo>> onCreateLoader(int id, Bundle args) { |
| 240 | return new AccountsLoader(this, AccountTypeManager.writableFilter()); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void onLoadFinished(Loader<List<AccountInfo>> loader, List<AccountInfo> data) { |
| 245 | updateDisplayedAccounts(data); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void onLoaderReset(Loader<List<AccountInfo>> loader) { |
| 250 | } |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 251 | } |