GroupEditorActivity.java revision bec0e12648b7c90aab9eda5b2837faca2493e5c3
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 com.android.contacts.ContactsActivity;
20import com.android.contacts.R;
21import com.android.contacts.editor.ContactEditorFragment.SaveMode;
22import com.android.contacts.group.GroupEditorFragment;
23import com.android.contacts.util.DialogManager;
24import com.android.contacts.util.PhoneCapabilityTester;
25
26import android.app.ActionBar;
27import android.app.Dialog;
28import android.content.Context;
29import android.content.Intent;
30import android.net.Uri;
31import android.os.Bundle;
32import android.util.Log;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.View.OnClickListener;
36
37public class GroupEditorActivity extends ContactsActivity
38        implements DialogManager.DialogShowingViewActivity {
39
40    private static final String TAG = "GroupEditorActivity";
41
42    public static final String ACTION_SAVE_COMPLETED = "saveCompleted";
43    public static final String ACTION_ADD_MEMBER_COMPLETED = "addMemberCompleted";
44    public static final String ACTION_REMOVE_MEMBER_COMPLETED = "removeMemberCompleted";
45
46    private GroupEditorFragment mFragment;
47
48    private DialogManager mDialogManager = new DialogManager(this);
49
50    @Override
51    public void onCreate(Bundle savedState) {
52        super.onCreate(savedState);
53        String action = getIntent().getAction();
54
55        if (ACTION_SAVE_COMPLETED.equals(action)) {
56            finish();
57            return;
58        }
59
60        setContentView(R.layout.group_editor_activity);
61
62        ActionBar actionBar = getActionBar();
63        if (actionBar != null) {
64            // Inflate a custom action bar that contains the "done" button for saving changes
65            // to the group
66            LayoutInflater inflater = (LayoutInflater) getSystemService
67                    (Context.LAYOUT_INFLATER_SERVICE);
68            View customActionBarView = inflater.inflate(R.layout.editor_custom_action_bar,
69                    null);
70            View saveMenuItem = customActionBarView.findViewById(R.id.save_menu_item);
71            saveMenuItem.setOnClickListener(new OnClickListener() {
72                @Override
73                public void onClick(View v) {
74                    mFragment.doSaveAction();
75                }
76            });
77            // Show the custom action bar but hide the home icon and title
78            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
79                    ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME |
80                    ActionBar.DISPLAY_SHOW_TITLE);
81            actionBar.setCustomView(customActionBarView);
82        }
83
84        mFragment = (GroupEditorFragment) getFragmentManager().findFragmentById(
85                R.id.group_editor_fragment);
86        mFragment.setListener(mFragmentListener);
87        mFragment.setContentResolver(getContentResolver());
88
89        // NOTE The fragment will restore its state by itself after orientation changes, so
90        // we need to do this only for a new instance.
91        if (savedState == null) {
92            Uri uri = Intent.ACTION_EDIT.equals(action) ? getIntent().getData() : null;
93            mFragment.load(action, uri, getIntent().getExtras());
94        }
95    }
96
97    @Override
98    protected Dialog onCreateDialog(int id, Bundle args) {
99        if (DialogManager.isManagedId(id)) {
100            return mDialogManager.onCreateDialog(id, args);
101        } else {
102            // Nobody knows about the Dialog
103            Log.w(TAG, "Unknown dialog requested, id: " + id + ", args: " + args);
104            return null;
105        }
106    }
107
108    @Override
109    public boolean onSearchRequested() {
110        return true; // Don't respond to the search key.
111    }
112
113    @Override
114    public void onBackPressed() {
115        // If the change could not be saved, then revert to the default "back" button behavior.
116        if (!mFragment.save(SaveMode.CLOSE)) {
117            super.onBackPressed();
118        }
119    }
120
121    @Override
122    protected void onNewIntent(Intent intent) {
123        super.onNewIntent(intent);
124
125        if (mFragment == null) {
126            return;
127        }
128
129        String action = intent.getAction();
130        if (ACTION_SAVE_COMPLETED.equals(action)) {
131            mFragment.onSaveCompleted(true,
132                    intent.getIntExtra(GroupEditorFragment.SAVE_MODE_EXTRA_KEY, SaveMode.CLOSE),
133                    intent.getData());
134        }
135    }
136
137    private final GroupEditorFragment.Listener mFragmentListener =
138            new GroupEditorFragment.Listener() {
139        @Override
140        public void onGroupNotFound() {
141            finish();
142        }
143
144        @Override
145        public void onReverted() {
146            finish();
147        }
148
149        @Override
150        public void onSaveFinished(int resultCode, Intent resultIntent) {
151            // TODO: Collapse these 2 cases into 1 that will just launch an intent with the VIEW
152            // action to see the group URI (when group URIs are supported)
153            // For a 2-pane screen, set the activity result, so the original activity (that launched
154            // the editor) can display the group detail page
155            if (PhoneCapabilityTester.isUsingTwoPanes(GroupEditorActivity.this)) {
156                setResult(resultCode, resultIntent);
157            } else if (resultIntent != null) {
158                // For a 1-pane screen, launch the group detail page
159                Intent intent = new Intent(GroupEditorActivity.this, GroupDetailActivity.class);
160                intent.setData(resultIntent.getData());
161                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
162                startActivity(intent);
163            }
164            finish();
165        }
166    };
167
168    @Override
169    public DialogManager getDialogManager() {
170        return mDialogManager;
171    }
172}
173