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
17package com.android.contacts.activities;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.os.Bundle;
22import android.provider.ContactsContract.Intents;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.widget.AdapterView;
26import android.widget.AdapterView.OnItemClickListener;
27import android.widget.Button;
28import android.widget.ListView;
29import android.widget.TextView;
30
31import com.android.contacts.R;
32import com.android.contacts.editor.ContactEditorUtils;
33import com.android.contacts.common.model.AccountTypeManager;
34import com.android.contacts.common.model.account.AccountWithDataSet;
35import com.android.contacts.common.util.AccountsListAdapter;
36import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
37
38import java.util.List;
39
40/**
41 * This activity can be shown to the user when creating a new contact to inform the user about
42 * which account the contact will be saved in. There is also an option to add an account at
43 * this time. The {@link Intent} in the activity result will contain an extra
44 * {@link #Intents.Insert.ACCOUNT} that contains the {@link AccountWithDataSet} to create
45 * the new contact in. If the activity result doesn't contain intent data, then there is no
46 * account for this contact.
47 */
48public class ContactEditorAccountsChangedActivity extends Activity {
49
50    private static final String TAG = ContactEditorAccountsChangedActivity.class.getSimpleName();
51
52    private static final int SUBACTIVITY_ADD_NEW_ACCOUNT = 1;
53
54    private AccountsListAdapter mAccountListAdapter;
55    private ContactEditorUtils mEditorUtils;
56
57    private final OnItemClickListener mAccountListItemClickListener = new OnItemClickListener() {
58        @Override
59        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
60            if (mAccountListAdapter == null) {
61                return;
62            }
63            saveAccountAndReturnResult(mAccountListAdapter.getItem(position));
64        }
65    };
66
67    private final OnClickListener mAddAccountClickListener = new OnClickListener() {
68        @Override
69        public void onClick(View v) {
70            startActivityForResult(mEditorUtils.createAddWritableAccountIntent(),
71                    SUBACTIVITY_ADD_NEW_ACCOUNT);
72        }
73    };
74
75    @Override
76    protected void onCreate(Bundle savedInstanceState) {
77        super.onCreate(savedInstanceState);
78
79        mEditorUtils = ContactEditorUtils.getInstance(this);
80        final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this).
81                getAccounts(true);
82        final int numAccounts = accounts.size();
83        if (numAccounts < 0) {
84            throw new IllegalStateException("Cannot have a negative number of accounts");
85        }
86
87        if (numAccounts >= 2) {
88            // When the user has 2+ writable accounts, show a list of accounts so the user can pick
89            // which account to create a contact in.
90            setContentView(R.layout.contact_editor_accounts_changed_activity_with_picker);
91
92            final TextView textView = (TextView) findViewById(R.id.text);
93            textView.setText(getString(R.string.contact_editor_prompt_multiple_accounts));
94
95            final Button button = (Button) findViewById(R.id.add_account_button);
96            button.setText(getString(R.string.add_new_account));
97            button.setOnClickListener(mAddAccountClickListener);
98
99            final ListView accountListView = (ListView) findViewById(R.id.account_list);
100            mAccountListAdapter = new AccountsListAdapter(this,
101                    AccountListFilter.ACCOUNTS_CONTACT_WRITABLE);
102            accountListView.setAdapter(mAccountListAdapter);
103            accountListView.setOnItemClickListener(mAccountListItemClickListener);
104        } else if (numAccounts == 1) {
105            // If the user has 1 writable account we will just show the user a message with 2
106            // possible action buttons.
107            setContentView(R.layout.contact_editor_accounts_changed_activity_with_text);
108
109            final TextView textView = (TextView) findViewById(R.id.text);
110            final Button leftButton = (Button) findViewById(R.id.left_button);
111            final Button rightButton = (Button) findViewById(R.id.right_button);
112
113            final AccountWithDataSet account = accounts.get(0);
114            textView.setText(getString(R.string.contact_editor_prompt_one_account,
115                    account.name));
116
117            // This button allows the user to add a new account to the device and return to
118            // this app afterwards.
119            leftButton.setText(getString(R.string.add_new_account));
120            leftButton.setOnClickListener(mAddAccountClickListener);
121
122            // This button allows the user to continue creating the contact in the specified
123            // account.
124            rightButton.setText(getString(android.R.string.ok));
125            rightButton.setOnClickListener(new OnClickListener() {
126                @Override
127                public void onClick(View v) {
128                    saveAccountAndReturnResult(account);
129                }
130            });
131        } else {
132            // If the user has 0 writable accounts, we will just show the user a message with 2
133            // possible action buttons.
134            setContentView(R.layout.contact_editor_accounts_changed_activity_with_text);
135
136            final TextView textView = (TextView) findViewById(R.id.text);
137            final Button leftButton = (Button) findViewById(R.id.left_button);
138            final Button rightButton = (Button) findViewById(R.id.right_button);
139
140            textView.setText(getString(R.string.contact_editor_prompt_zero_accounts));
141
142            // This button allows the user to continue editing the contact as a phone-only
143            // local contact.
144            leftButton.setText(getString(R.string.keep_local));
145            leftButton.setOnClickListener(new OnClickListener() {
146                @Override
147                public void onClick(View v) {
148                    // Remember that the user wants to create local contacts, so the user is not
149                    // prompted again with this activity.
150                    mEditorUtils.saveDefaultAndAllAccounts(null);
151                    setResult(RESULT_OK);
152                    finish();
153                }
154            });
155
156            // This button allows the user to add a new account to the device and return to
157            // this app afterwards.
158            rightButton.setText(getString(R.string.add_account));
159            rightButton.setOnClickListener(mAddAccountClickListener);
160        }
161    }
162
163    @Override
164    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
165        if (requestCode == SUBACTIVITY_ADD_NEW_ACCOUNT) {
166            // If the user canceled the account setup process, then keep this activity visible to
167            // the user.
168            if (resultCode != RESULT_OK) {
169                return;
170            }
171            // Subactivity was successful, so pass the result back and finish the activity.
172            AccountWithDataSet account = mEditorUtils.getCreatedAccount(resultCode, data);
173            if (account == null) {
174                setResult(resultCode);
175                finish();
176                return;
177            }
178            saveAccountAndReturnResult(account);
179        }
180    }
181
182    private void saveAccountAndReturnResult(AccountWithDataSet account) {
183        // Save this as the default account
184        mEditorUtils.saveDefaultAndAllAccounts(account);
185
186        // Pass account info in activity result intent
187        Intent intent = new Intent();
188        intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account);
189        setResult(RESULT_OK, intent);
190        finish();
191    }
192}
193