1 /* 2 * Copyright (C) 2009 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.common.preference; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.app.ActionBar; 22 import android.preference.PreferenceActivity; 23 import android.view.MenuItem; 24 25 import com.android.contacts.common.R; 26 27 /** 28 * Contacts settings. 29 */ 30 public final class ContactsPreferenceActivity extends PreferenceActivity { 31 32 private static final String TAG_ABOUT_CONTACTS = "about_contacts"; 33 34 @Override onCreate(Bundle savedInstanceState)35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 final ActionBar actionBar = getActionBar(); 39 if (actionBar != null) { 40 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 41 } 42 43 if (savedInstanceState == null) { 44 getFragmentManager().beginTransaction() 45 .replace(android.R.id.content, new DisplayOptionsPreferenceFragment()) 46 .commit(); 47 setActivityTitle(R.string.activity_title_settings); 48 } else { 49 final AboutPreferenceFragment fragment = (AboutPreferenceFragment) getFragmentManager() 50 .findFragmentByTag(TAG_ABOUT_CONTACTS); 51 setActivityTitle(fragment == null ? 52 R.string.activity_title_settings : R.string.setting_about); 53 } 54 } 55 showAboutFragment()56 public void showAboutFragment() { 57 getFragmentManager().beginTransaction() 58 .replace(android.R.id.content, new AboutPreferenceFragment(), TAG_ABOUT_CONTACTS) 59 .addToBackStack(null) 60 .commit(); 61 setActivityTitle(R.string.setting_about); 62 } 63 64 /** 65 * Returns true if there are no preferences to display and therefore the 66 * corresponding menu item can be removed. 67 */ isEmpty(Context context)68 public static boolean isEmpty(Context context) { 69 return !context.getResources().getBoolean(R.bool.config_sort_order_user_changeable) 70 && !context.getResources().getBoolean(R.bool.config_display_order_user_changeable) 71 && !context.getResources().getBoolean( 72 R.bool.config_default_account_user_changeable); 73 } 74 75 @Override onOptionsItemSelected(MenuItem item)76 public boolean onOptionsItemSelected(MenuItem item) { 77 if (item.getItemId() == android.R.id.home) { 78 onBackPressed(); 79 return true; 80 } 81 return false; 82 } 83 84 @Override onBackPressed()85 public void onBackPressed() { 86 if (getFragmentManager().getBackStackEntryCount() > 0) { 87 setActivityTitle(R.string.activity_title_settings); 88 getFragmentManager().popBackStack(); 89 } else { 90 super.onBackPressed(); 91 } 92 } 93 setActivityTitle(int res)94 private void setActivityTitle(int res) { 95 final ActionBar actionBar = getActionBar(); 96 if (actionBar != null) { 97 actionBar.setTitle(res); 98 } 99 } 100 } 101