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