Home | History | Annotate | Download | only in activities
      1 /*
      2  * Copyright (C) 2010 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 com.android.contacts.ContactSaveService;
     20 import com.android.contacts.ContactsActivity;
     21 import com.android.contacts.R;
     22 import com.android.contacts.editor.ContactEditorFragment;
     23 import com.android.contacts.editor.ContactEditorFragment.SaveMode;
     24 import com.android.contacts.model.AccountType;
     25 import com.android.contacts.model.AccountTypeManager;
     26 import com.android.contacts.model.AccountWithDataSet;
     27 import com.android.contacts.util.DialogManager;
     28 
     29 import android.app.ActionBar;
     30 import android.app.Dialog;
     31 import android.content.ContentValues;
     32 import android.content.Context;
     33 import android.content.Intent;
     34 import android.net.Uri;
     35 import android.os.Bundle;
     36 import android.provider.ContactsContract;
     37 import android.provider.ContactsContract.Contacts;
     38 import android.provider.ContactsContract.RawContacts;
     39 import android.util.Log;
     40 import android.view.LayoutInflater;
     41 import android.view.View;
     42 import android.view.View.OnClickListener;
     43 
     44 import java.util.ArrayList;
     45 
     46 public class ContactEditorActivity extends ContactsActivity
     47         implements DialogManager.DialogShowingViewActivity {
     48     private static final String TAG = "ContactEditorActivity";
     49 
     50     public static final String ACTION_JOIN_COMPLETED = "joinCompleted";
     51     public static final String ACTION_SAVE_COMPLETED = "saveCompleted";
     52 
     53     /**
     54      * Boolean intent key that specifies that this activity should finish itself
     55      * (instead of launching a new view intent) after the editor changes have been
     56      * saved.
     57      */
     58     public static final String INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED =
     59             "finishActivityOnSaveCompleted";
     60 
     61     private ContactEditorFragment mFragment;
     62     private boolean mFinishActivityOnSaveCompleted;
     63 
     64     private DialogManager mDialogManager = new DialogManager(this);
     65 
     66     @Override
     67     public void onCreate(Bundle savedState) {
     68         super.onCreate(savedState);
     69 
     70         final Intent intent = getIntent();
     71         final String action = intent.getAction();
     72 
     73         // Determine whether or not this activity should be finished after the user is done
     74         // editing the contact or if this activity should launch another activity to view the
     75         // contact's details.
     76         mFinishActivityOnSaveCompleted = intent.getBooleanExtra(
     77                 INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, false);
     78 
     79         // The only situation where action could be ACTION_JOIN_COMPLETED is if the
     80         // user joined the contact with another and closed the activity before
     81         // the save operation was completed.  The activity should remain closed then.
     82         if (ACTION_JOIN_COMPLETED.equals(action)) {
     83             finish();
     84             return;
     85         }
     86 
     87         if (ACTION_SAVE_COMPLETED.equals(action)) {
     88             finish();
     89             return;
     90         }
     91 
     92         setContentView(R.layout.contact_editor_activity);
     93 
     94         ActionBar actionBar = getActionBar();
     95         if (actionBar != null) {
     96             // Inflate a custom action bar that contains the "done" button for saving changes
     97             // to the contact
     98             LayoutInflater inflater = (LayoutInflater) getSystemService
     99                     (Context.LAYOUT_INFLATER_SERVICE);
    100             View customActionBarView = inflater.inflate(R.layout.editor_custom_action_bar, null);
    101             View saveMenuItem = customActionBarView.findViewById(R.id.save_menu_item);
    102             saveMenuItem.setOnClickListener(new OnClickListener() {
    103                 @Override
    104                 public void onClick(View v) {
    105                     mFragment.doSaveAction();
    106                 }
    107             });
    108             // Show the custom action bar but hide the home icon and title
    109             actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
    110                     ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME |
    111                     ActionBar.DISPLAY_SHOW_TITLE);
    112             actionBar.setCustomView(customActionBarView);
    113         }
    114 
    115         mFragment = (ContactEditorFragment) getFragmentManager().findFragmentById(
    116                 R.id.contact_editor_fragment);
    117         mFragment.setListener(mFragmentListener);
    118         Uri uri = Intent.ACTION_EDIT.equals(action) ? getIntent().getData() : null;
    119         mFragment.load(action, uri, getIntent().getExtras());
    120     }
    121 
    122     @Override
    123     protected void onNewIntent(Intent intent) {
    124         super.onNewIntent(intent);
    125 
    126         if (mFragment == null) {
    127             return;
    128         }
    129 
    130         String action = intent.getAction();
    131         if (Intent.ACTION_EDIT.equals(action)) {
    132             mFragment.setIntentExtras(intent.getExtras());
    133         } else if (ACTION_SAVE_COMPLETED.equals(action)) {
    134             mFragment.onSaveCompleted(true,
    135                     intent.getIntExtra(ContactEditorFragment.SAVE_MODE_EXTRA_KEY, SaveMode.CLOSE),
    136                     intent.getBooleanExtra(ContactSaveService.EXTRA_SAVE_SUCCEEDED, false),
    137                     intent.getData());
    138         } else if (ACTION_JOIN_COMPLETED.equals(action)) {
    139             mFragment.onJoinCompleted(intent.getData());
    140         }
    141     }
    142 
    143     @Override
    144     protected Dialog onCreateDialog(int id, Bundle args) {
    145         if (DialogManager.isManagedId(id)) return mDialogManager.onCreateDialog(id, args);
    146 
    147         // Nobody knows about the Dialog
    148         Log.w(TAG, "Unknown dialog requested, id: " + id + ", args: " + args);
    149         return null;
    150     }
    151 
    152     @Override
    153     public void onBackPressed() {
    154         mFragment.save(SaveMode.CLOSE);
    155     }
    156 
    157     private final ContactEditorFragment.Listener mFragmentListener =
    158             new ContactEditorFragment.Listener() {
    159         @Override
    160         public void onReverted() {
    161             finish();
    162         }
    163 
    164         @Override
    165         public void onSaveFinished(Intent resultIntent) {
    166             if (mFinishActivityOnSaveCompleted) {
    167                 setResult(resultIntent == null ? RESULT_CANCELED : RESULT_OK, resultIntent);
    168             } else if (resultIntent != null) {
    169                 startActivity(resultIntent);
    170             }
    171             finish();
    172         }
    173 
    174         @Override
    175         public void onContactSplit(Uri newLookupUri) {
    176             finish();
    177         }
    178 
    179         @Override
    180         public void onContactNotFound() {
    181             finish();
    182         }
    183 
    184         @Override
    185         public void onEditOtherContactRequested(
    186                 Uri contactLookupUri, ArrayList<ContentValues> values) {
    187             Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
    188             intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    189                     | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    190             intent.putExtra(ContactEditorFragment.INTENT_EXTRA_ADD_TO_DEFAULT_DIRECTORY, "");
    191 
    192             // Pass on all the data that has been entered so far
    193             if (values != null && values.size() != 0) {
    194                 intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, values);
    195             }
    196 
    197             startActivity(intent);
    198             finish();
    199         }
    200 
    201         @Override
    202         public void onCustomCreateContactActivityRequested(AccountWithDataSet account,
    203                 Bundle intentExtras) {
    204             final AccountTypeManager accountTypes =
    205                     AccountTypeManager.getInstance(ContactEditorActivity.this);
    206             final AccountType accountType = accountTypes.getAccountType(
    207                     account.type, account.dataSet);
    208 
    209             Intent intent = new Intent();
    210             intent.setClassName(accountType.syncAdapterPackageName,
    211                     accountType.getCreateContactActivityClassName());
    212             intent.setAction(Intent.ACTION_INSERT);
    213             intent.setType(Contacts.CONTENT_ITEM_TYPE);
    214             if (intentExtras != null) {
    215                 intent.putExtras(intentExtras);
    216             }
    217             intent.putExtra(RawContacts.ACCOUNT_NAME, account.name);
    218             intent.putExtra(RawContacts.ACCOUNT_TYPE, account.type);
    219             intent.putExtra(RawContacts.DATA_SET, account.dataSet);
    220             intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    221                     | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    222             startActivity(intent);
    223             finish();
    224         }
    225 
    226         @Override
    227         public void onCustomEditContactActivityRequested(AccountWithDataSet account,
    228                 Uri rawContactUri, Bundle intentExtras, boolean redirect) {
    229             final AccountTypeManager accountTypes =
    230                     AccountTypeManager.getInstance(ContactEditorActivity.this);
    231             final AccountType accountType = accountTypes.getAccountType(
    232                     account.type, account.dataSet);
    233 
    234             Intent intent = new Intent();
    235             intent.setClassName(accountType.syncAdapterPackageName,
    236                     accountType.getEditContactActivityClassName());
    237             intent.setAction(Intent.ACTION_EDIT);
    238             intent.setData(rawContactUri);
    239             if (intentExtras != null) {
    240                 intent.putExtras(intentExtras);
    241             }
    242 
    243             if (redirect) {
    244                 intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    245                         | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    246                 startActivity(intent);
    247                 finish();
    248             } else {
    249                 startActivity(intent);
    250             }
    251         }
    252     };
    253 
    254     @Override
    255     public DialogManager getDialogManager() {
    256         return mDialogManager;
    257     }
    258 }
    259