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