Home | History | Annotate | Download | only in activities
      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 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.content.DialogInterface;
     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;
     35 import com.android.contacts.common.model.AccountTypeManager;
     36 import com.android.contacts.common.model.account.AccountWithDataSet;
     37 import com.android.contacts.common.util.AccountsListAdapter;
     38 import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
     39 import com.android.contacts.common.util.ImplicitIntentsUtil;
     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  */
     51 public class ContactEditorAccountsChangedActivity extends Activity {
     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;
     59 
     60     private final OnItemClickListener mAccountListItemClickListener = new OnItemClickListener() {
     61         @Override
     62         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     63             if (mAccountListAdapter == null) {
     64                 return;
     65             }
     66             saveAccountAndReturnResult(mAccountListAdapter.getItem(position));
     67         }
     68     };
     69 
     70     private final OnClickListener mAddAccountClickListener = new OnClickListener() {
     71         @Override
     72         public void onClick(View v) {
     73             final Intent intent = ImplicitIntentsUtil.getIntentForAddingAccount();
     74             startActivityForResult(intent, SUBACTIVITY_ADD_NEW_ACCOUNT);
     75         }
     76     };
     77 
     78     @Override
     79     protected void onCreate(Bundle savedInstanceState) {
     80         super.onCreate(savedInstanceState);
     81 
     82         mEditorUtils = ContactEditorUtils.getInstance(this);
     83         final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this).
     84                 getAccounts(true);
     85         final int numAccounts = accounts.size();
     86         if (numAccounts < 0) {
     87             throw new IllegalStateException("Cannot have a negative number of accounts");
     88         }
     89 
     90         final View view;
     91         if (numAccounts >= 2) {
     92             // When the user has 2+ writable accounts, show a list of accounts so the user can pick
     93             // which account to create a contact in.
     94             view = View.inflate(this,
     95                     R.layout.contact_editor_accounts_changed_activity_with_picker, null);
     96 
     97             final TextView textView = (TextView) view.findViewById(R.id.text);
     98             textView.setText(getString(R.string.contact_editor_prompt_multiple_accounts));
     99 
    100             final Button button = (Button) view.findViewById(R.id.add_account_button);
    101             button.setText(getString(R.string.add_new_account));
    102             button.setOnClickListener(mAddAccountClickListener);
    103 
    104             final ListView accountListView = (ListView) view.findViewById(R.id.account_list);
    105             mAccountListAdapter = new AccountsListAdapter(this,
    106                     AccountListFilter.ACCOUNTS_CONTACT_WRITABLE);
    107             accountListView.setAdapter(mAccountListAdapter);
    108             accountListView.setOnItemClickListener(mAccountListItemClickListener);
    109         } else if (numAccounts == 1) {
    110             // If the user has 1 writable account we will just show the user a message with 2
    111             // possible action buttons.
    112             view = View.inflate(this,
    113                     R.layout.contact_editor_accounts_changed_activity_with_text, null);
    114 
    115             final TextView textView = (TextView) view.findViewById(R.id.text);
    116             final Button leftButton = (Button) view.findViewById(R.id.left_button);
    117             final Button rightButton = (Button) view.findViewById(R.id.right_button);
    118 
    119             final AccountWithDataSet account = accounts.get(0);
    120             textView.setText(getString(R.string.contact_editor_prompt_one_account,
    121                     account.name));
    122 
    123             // This button allows the user to add a new account to the device and return to
    124             // this app afterwards.
    125             leftButton.setText(getString(R.string.add_new_account));
    126             leftButton.setOnClickListener(mAddAccountClickListener);
    127 
    128             // This button allows the user to continue creating the contact in the specified
    129             // account.
    130             rightButton.setText(getString(android.R.string.ok));
    131             rightButton.setOnClickListener(new OnClickListener() {
    132                 @Override
    133                 public void onClick(View v) {
    134                     saveAccountAndReturnResult(account);
    135                 }
    136             });
    137         } else {
    138             // If the user has 0 writable accounts, we will just show the user a message with 2
    139             // possible action buttons.
    140             view = View.inflate(this,
    141                     R.layout.contact_editor_accounts_changed_activity_with_text, null);
    142 
    143             final TextView textView = (TextView) view.findViewById(R.id.text);
    144             final Button leftButton = (Button) view.findViewById(R.id.left_button);
    145             final Button rightButton = (Button) view.findViewById(R.id.right_button);
    146 
    147             textView.setText(getString(R.string.contact_editor_prompt_zero_accounts));
    148 
    149             // This button allows the user to continue editing the contact as a phone-only
    150             // local contact.
    151             leftButton.setText(getString(R.string.keep_local));
    152             leftButton.setOnClickListener(new OnClickListener() {
    153                 @Override
    154                 public void onClick(View v) {
    155                     // Remember that the user wants to create local contacts, so the user is not
    156                     // prompted again with this activity.
    157                     mEditorUtils.saveDefaultAndAllAccounts(null);
    158                     setResult(RESULT_OK);
    159                     finish();
    160                 }
    161             });
    162 
    163             // This button allows the user to add a new account to the device and return to
    164             // this app afterwards.
    165             rightButton.setText(getString(R.string.add_account));
    166             rightButton.setOnClickListener(mAddAccountClickListener);
    167         }
    168 
    169         new AlertDialog.Builder(this)
    170                 .setView(view)
    171                 .setOnCancelListener(new DialogInterface.OnCancelListener() {
    172                     @Override
    173                     public void onCancel(DialogInterface dialog) {
    174                         finish();
    175                     }
    176                 })
    177                 .create()
    178                 .show();
    179     }
    180 
    181     @Override
    182     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    183         if (requestCode == SUBACTIVITY_ADD_NEW_ACCOUNT) {
    184             // If the user canceled the account setup process, then keep this activity visible to
    185             // the user.
    186             if (resultCode != RESULT_OK) {
    187                 return;
    188             }
    189             // Subactivity was successful, so pass the result back and finish the activity.
    190             AccountWithDataSet account = mEditorUtils.getCreatedAccount(resultCode, data);
    191             if (account == null) {
    192                 setResult(resultCode);
    193                 finish();
    194                 return;
    195             }
    196             saveAccountAndReturnResult(account);
    197         }
    198     }
    199 
    200     private void saveAccountAndReturnResult(AccountWithDataSet account) {
    201         // Save this as the default account
    202         mEditorUtils.saveDefaultAndAllAccounts(account);
    203 
    204         // Pass account info in activity result intent
    205         Intent intent = new Intent();
    206         intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account);
    207         setResult(RESULT_OK, intent);
    208         finish();
    209     }
    210 }
    211