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