blob: 50e11f38f52e363f2d41151d216fc7814e53d6b8 [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;
21import android.content.DialogInterface;
Katherine Kuan0353a242011-09-23 10:38:28 -070022import android.content.Intent;
23import android.os.Bundle;
24import android.provider.ContactsContract.Intents;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.widget.AdapterView;
28import android.widget.AdapterView.OnItemClickListener;
29import android.widget.Button;
30import android.widget.ListView;
31import android.widget.TextView;
32
33import com.android.contacts.R;
34import com.android.contacts.editor.ContactEditorUtils;
Gary Mai69c182a2016-12-05 13:07:03 -080035import com.android.contacts.model.AccountTypeManager;
36import com.android.contacts.model.account.AccountWithDataSet;
37import com.android.contacts.util.AccountsListAdapter;
38import com.android.contacts.util.AccountsListAdapter.AccountListFilter;
39import com.android.contacts.util.ImplicitIntentsUtil;
Katherine Kuan0353a242011-09-23 10:38:28 -070040
41import 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 Wangbf96dd12016-02-21 11:23:24 -080051public class ContactEditorAccountsChangedActivity extends Activity {
Katherine Kuan0353a242011-09-23 10:38:28 -070052
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 Mai85b15a62016-10-19 14:28:48 -070059 private AlertDialog mDialog;
Katherine Kuan0353a242011-09-23 10:38:28 -070060
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 Jang5e19c232016-07-14 12:38:04 -070074 final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount();
guanxiongliuf597ec62016-01-25 19:49:42 -080075 startActivityForResult(intent, SUBACTIVITY_ADD_NEW_ACCOUNT);
Katherine Kuan0353a242011-09-23 10:38:28 -070076 }
77 };
78
79 @Override
Gary Mai85b15a62016-10-19 14:28:48 -070080 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 Kuan0353a242011-09-23 10:38:28 -070096 protected void onCreate(Bundle savedInstanceState) {
97 super.onCreate(savedInstanceState);
98
Marcus Hagerott949d4e82016-09-20 13:23:05 -070099 mEditorUtils = ContactEditorUtils.create(this);
Katherine Kuan0353a242011-09-23 10:38:28 -0700100 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 Wang60b44bf2015-11-05 10:39:22 -0800107 final View view;
Katherine Kuan0353a242011-09-23 10:38:28 -0700108 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 Wang60b44bf2015-11-05 10:39:22 -0800111 view = View.inflate(this,
112 R.layout.contact_editor_accounts_changed_activity_with_picker, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700113
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800114 final TextView textView = (TextView) view.findViewById(R.id.text);
Katherine Kuan0353a242011-09-23 10:38:28 -0700115 textView.setText(getString(R.string.contact_editor_prompt_multiple_accounts));
116
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800117 final Button button = (Button) view.findViewById(R.id.add_account_button);
Katherine Kuan0353a242011-09-23 10:38:28 -0700118 button.setText(getString(R.string.add_new_account));
119 button.setOnClickListener(mAddAccountClickListener);
120
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800121 final ListView accountListView = (ListView) view.findViewById(R.id.account_list);
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700122 mAccountListAdapter = new AccountsListAdapter(this,
123 AccountListFilter.ACCOUNTS_CONTACT_WRITABLE);
Katherine Kuan0353a242011-09-23 10:38:28 -0700124 accountListView.setAdapter(mAccountListAdapter);
125 accountListView.setOnItemClickListener(mAccountListItemClickListener);
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700126 } else if (numAccounts == 1 && !accounts.get(0).isNullAccount()) {
Katherine Kuan0353a242011-09-23 10:38:28 -0700127 // If the user has 1 writable account we will just show the user a message with 2
128 // possible action buttons.
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800129 view = View.inflate(this,
130 R.layout.contact_editor_accounts_changed_activity_with_text, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700131
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800132 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 Kuan0353a242011-09-23 10:38:28 -0700135
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 Wang60b44bf2015-11-05 10:39:22 -0800157 view = View.inflate(this,
158 R.layout.contact_editor_accounts_changed_activity_with_text, null);
Katherine Kuan0353a242011-09-23 10:38:28 -0700159
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800160 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 Kuan0353a242011-09-23 10:38:28 -0700163
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.
guanxiongliucb26b372016-07-29 15:44:13 -0700168 leftButton.setText(getString(android.R.string.cancel));
Katherine Kuan0353a242011-09-23 10:38:28 -0700169 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 Hagerott949d4e82016-09-20 13:23:05 -0700174 mEditorUtils.saveDefaultAccount(AccountWithDataSet.getNullAccount());
Katherine Kuan0353a242011-09-23 10:38:28 -0700175 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 Wang60b44bf2015-11-05 10:39:22 -0800185
Gary Mai85b15a62016-10-19 14:28:48 -0700186 mDialog = new AlertDialog.Builder(this)
Wenyi Wang60b44bf2015-11-05 10:39:22 -0800187 .setView(view)
188 .setOnCancelListener(new DialogInterface.OnCancelListener() {
189 @Override
190 public void onCancel(DialogInterface dialog) {
191 finish();
192 }
193 })
Gary Mai85b15a62016-10-19 14:28:48 -0700194 .create();
195 mDialog.show();
Katherine Kuan0353a242011-09-23 10:38:28 -0700196 }
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 Hagerott949d4e82016-09-20 13:23:05 -0700219 mEditorUtils.saveDefaultAccount(account);
Katherine Kuan0353a242011-09-23 10:38:28 -0700220
221 // Pass account info in activity result intent
222 Intent intent = new Intent();
Brian Attwell4a1c5742015-01-26 15:59:58 -0800223 intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account);
Katherine Kuan0353a242011-09-23 10:38:28 -0700224 setResult(RESULT_OK, intent);
225 finish();
226 }
Chiao Cheng428f0082012-11-13 18:38:56 -0800227}