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 com.android.contacts.ContactsActivity;
     20 import com.android.contacts.R;
     21 import com.android.contacts.editor.ContactEditorFragment.SaveMode;
     22 import com.android.contacts.group.GroupEditorFragment;
     23 import com.android.contacts.util.DialogManager;
     24 import com.android.contacts.util.PhoneCapabilityTester;
     25 
     26 import android.app.ActionBar;
     27 import android.app.Dialog;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.net.Uri;
     31 import android.os.Bundle;
     32 import android.util.Log;
     33 import android.view.LayoutInflater;
     34 import android.view.View;
     35 import android.view.View.OnClickListener;
     36 
     37 public 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.onDoneClicked();
     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 void onBackPressed() {
    110         // If the change could not be saved, then revert to the default "back" button behavior.
    111         if (!mFragment.save(SaveMode.CLOSE)) {
    112             super.onBackPressed();
    113         }
    114     }
    115 
    116     @Override
    117     protected void onNewIntent(Intent intent) {
    118         super.onNewIntent(intent);
    119 
    120         if (mFragment == null) {
    121             return;
    122         }
    123 
    124         String action = intent.getAction();
    125         if (ACTION_SAVE_COMPLETED.equals(action)) {
    126             mFragment.onSaveCompleted(true,
    127                     intent.getIntExtra(GroupEditorFragment.SAVE_MODE_EXTRA_KEY, SaveMode.CLOSE),
    128                     intent.getData());
    129         }
    130     }
    131 
    132     private final GroupEditorFragment.Listener mFragmentListener =
    133             new GroupEditorFragment.Listener() {
    134         @Override
    135         public void onGroupNotFound() {
    136             finish();
    137         }
    138 
    139         @Override
    140         public void onReverted() {
    141             finish();
    142         }
    143 
    144         @Override
    145         public void onAccountsNotFound() {
    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