blob: 3531125e4af8716f994b876452391ba5a202a61d [file] [log] [blame]
Katherine Kuan0353a242011-09-23 10:38:28 -07001/*
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
17package com.android.contacts.activities;
18
Wenyi Wangbf96dd12016-02-21 11:23:24 -080019import android.app.Activity;
Wenyi Wang60b44bf2015-11-05 10:39:22 -080020import android.app.AlertDialog;
Marcus Hagerott9a679e72016-12-15 09:30:17 -080021import android.app.LoaderManager;
Wenyi Wang60b44bf2015-11-05 10:39:22 -080022import android.content.DialogInterface;
Katherine Kuan0353a242011-09-23 10:38:28 -070023import android.content.Intent;
Marcus Hagerott9a679e72016-12-15 09:30:17 -080024import android.content.Loader;
Katherine Kuan0353a242011-09-23 10:38:28 -070025import android.os.Bundle;
26import android.provider.ContactsContract.Intents;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.AdapterView;
30import android.widget.AdapterView.OnItemClickListener;
31import android.widget.Button;
32import android.widget.ListView;
33import android.widget.TextView;
34
35import com.android.contacts.R;
36import com.android.contacts.editor.ContactEditorUtils;
Gary Mai69c182a2016-12-05 13:07:03 -080037import com.android.contacts.model.AccountTypeManager;
Marcus Hagerott9a679e72016-12-15 09:30:17 -080038import com.android.contacts.model.account.AccountInfo;
Gary Mai69c182a2016-12-05 13:07:03 -080039import com.android.contacts.model.account.AccountWithDataSet;
Marcus Hagerott9a679e72016-12-15 09:30:17 -080040import com.android.contacts.model.account.AccountsLoader;
Gary Mai69c182a2016-12-05 13:07:03 -080041import com.android.contacts.util.AccountsListAdapter;
42import com.android.contacts.util.AccountsListAdapter.AccountListFilter;
43import com.android.contacts.util.ImplicitIntentsUtil;
Marcus Hagerott9a679e72016-12-15 09:30:17 -080044import com.google.common.util.concurrent.Futures;
Katherine Kuan0353a242011-09-23 10:38:28 -070045
46import 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 Hagerott9a679e72016-12-15 09:30:17 -080056public class ContactEditorAccountsChangedActivity extends Activity
57 implements LoaderManager.LoaderCallbacks<List<AccountInfo>> {
Katherine Kuan0353a242011-09-23 10:38:28 -070058
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 Mai85b15a62016-10-19 14:28:48 -070065 private AlertDialog mDialog;
Katherine Kuan0353a242011-09-23 10:38:28 -070066
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 Jang5e19c232016-07-14 12:38:04 -070080 final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount();
guanxiongliuf597ec62016-01-25 19:49:42 -080081 startActivityForResult(intent, SUBACTIVITY_ADD_NEW_ACCOUNT);
Katherine Kuan0353a242011-09-23 10:38:28 -070082 }
83 };
84
85 @Override
Gary Mai85b15a62016-10-19 14:28:48 -070086 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 Kuan0353a242011-09-23 10:38:28 -0700102 protected void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700104 mEditorUtils = ContactEditorUtils.create(this);
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800105 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 Kuan0353a242011-09-23 10:38:28 -0700128 final int numAccounts = accounts.size();
129 if (numAccounts < 0) {
130 throw new IllegalStateException("Cannot have a negative number of accounts");
131 }
132
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800133 final View view;
Katherine Kuan0353a242011-09-23 10:38:28 -0700134 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 Wang60b44bf2015-11-05 10:39:22 -0800137 view = View.inflate(this,
138 R.layout.contact_editor_accounts_changed_activity_with_picker, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700139
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800140 final TextView textView = (TextView) view.findViewById(R.id.text);
Katherine Kuan0353a242011-09-23 10:38:28 -0700141 textView.setText(getString(R.string.contact_editor_prompt_multiple_accounts));
142
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800143 final Button button = (Button) view.findViewById(R.id.add_account_button);
Katherine Kuan0353a242011-09-23 10:38:28 -0700144 button.setText(getString(R.string.add_new_account));
145 button.setOnClickListener(mAddAccountClickListener);
146
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800147 final ListView accountListView = (ListView) view.findViewById(R.id.account_list);
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700148 mAccountListAdapter = new AccountsListAdapter(this,
149 AccountListFilter.ACCOUNTS_CONTACT_WRITABLE);
Katherine Kuan0353a242011-09-23 10:38:28 -0700150 accountListView.setAdapter(mAccountListAdapter);
151 accountListView.setOnItemClickListener(mAccountListItemClickListener);
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800152 } else if (numAccounts == 1 && !accounts.get(0).getAccount().isNullAccount()) {
Katherine Kuan0353a242011-09-23 10:38:28 -0700153 // If the user has 1 writable account we will just show the user a message with 2
154 // possible action buttons.
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800155 view = View.inflate(this,
156 R.layout.contact_editor_accounts_changed_activity_with_text, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700157
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800158 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 Kuan0353a242011-09-23 10:38:28 -0700161
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800162 final AccountInfo accountInfo = accounts.get(0);
Katherine Kuan0353a242011-09-23 10:38:28 -0700163 textView.setText(getString(R.string.contact_editor_prompt_one_account,
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800164 accountInfo.getNameLabel()));
Katherine Kuan0353a242011-09-23 10:38:28 -0700165
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 Hagerott9a679e72016-12-15 09:30:17 -0800177 saveAccountAndReturnResult(accountInfo.getAccount());
Katherine Kuan0353a242011-09-23 10:38:28 -0700178 }
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 Wang60b44bf2015-11-05 10:39:22 -0800183 view = View.inflate(this,
184 R.layout.contact_editor_accounts_changed_activity_with_text, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700185
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800186 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 Kuan0353a242011-09-23 10:38:28 -0700189
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.
guanxiongliucb26b372016-07-29 15:44:13 -0700194 leftButton.setText(getString(android.R.string.cancel));
Katherine Kuan0353a242011-09-23 10:38:28 -0700195 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 Hagerott949d4e82016-09-20 13:23:05 -0700200 mEditorUtils.saveDefaultAccount(AccountWithDataSet.getNullAccount());
Katherine Kuan0353a242011-09-23 10:38:28 -0700201 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 Wang60b44bf2015-11-05 10:39:22 -0800211
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800212 if (mDialog != null && mDialog.isShowing()) {
213 mDialog.dismiss();
214 }
Gary Mai85b15a62016-10-19 14:28:48 -0700215 mDialog = new AlertDialog.Builder(this)
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800216 .setView(view)
217 .setOnCancelListener(new DialogInterface.OnCancelListener() {
218 @Override
219 public void onCancel(DialogInterface dialog) {
220 finish();
221 }
222 })
Gary Mai85b15a62016-10-19 14:28:48 -0700223 .create();
224 mDialog.show();
Katherine Kuan0353a242011-09-23 10:38:28 -0700225 }
226
Katherine Kuan0353a242011-09-23 10:38:28 -0700227 private void saveAccountAndReturnResult(AccountWithDataSet account) {
228 // Save this as the default account
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700229 mEditorUtils.saveDefaultAccount(account);
Katherine Kuan0353a242011-09-23 10:38:28 -0700230
231 // Pass account info in activity result intent
232 Intent intent = new Intent();
Brian Attwell4a1c5742015-01-26 15:59:58 -0800233 intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account);
Katherine Kuan0353a242011-09-23 10:38:28 -0700234 setResult(RESULT_OK, intent);
235 finish();
236 }
Marcus Hagerott9a679e72016-12-15 09:30:17 -0800237
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 Cheng428f0082012-11-13 18:38:56 -0800251}