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.preference; 18 19 import android.content.res.Configuration; 20 import android.database.Cursor; 21 import android.os.Bundle; 22 import android.preference.PreferenceActivity; 23 import android.provider.ContactsContract.ProviderStatus; 24 import android.support.annotation.LayoutRes; 25 import android.support.annotation.NonNull; 26 import android.support.annotation.StringRes; 27 import android.support.v7.app.ActionBar; 28 import android.support.v7.app.AppCompatDelegate; 29 import android.support.v7.widget.Toolbar; 30 import android.text.TextUtils; 31 import android.view.MenuInflater; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.view.ViewGroup; 35 36 import com.android.contacts.R; 37 import com.android.contacts.editor.SelectAccountDialogFragment; 38 import com.android.contacts.interactions.ImportDialogFragment; 39 import com.android.contacts.list.ProviderStatusWatcher; 40 import com.android.contacts.model.account.AccountWithDataSet; 41 import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileListener; 42 import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileQuery; 43 import com.android.contacts.util.AccountSelectionUtil; 44 45 /** 46 * Contacts settings. 47 */ 48 public final class ContactsPreferenceActivity extends PreferenceActivity 49 implements ProfileListener, SelectAccountDialogFragment.Listener { 50 51 private static final String TAG_ABOUT = "about_contacts"; 52 private static final String TAG_DISPLAY_OPTIONS = "display_options"; 53 54 private String mNewLocalProfileExtra; 55 private boolean mAreContactsAvailable; 56 57 private ProviderStatusWatcher mProviderStatusWatcher; 58 59 private AppCompatDelegate mCompatDelegate; 60 61 public static final String EXTRA_NEW_LOCAL_PROFILE = "newLocalProfile"; 62 63 @Override onCreate(Bundle savedInstanceState)64 protected void onCreate(Bundle savedInstanceState) { 65 mCompatDelegate = AppCompatDelegate.create(this, null); 66 67 super.onCreate(savedInstanceState); 68 mCompatDelegate.onCreate(savedInstanceState); 69 70 71 final ActionBar actionBar = mCompatDelegate.getSupportActionBar(); 72 if (actionBar != null) { 73 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 74 } 75 76 mProviderStatusWatcher = ProviderStatusWatcher.getInstance(this); 77 78 mNewLocalProfileExtra = getIntent().getStringExtra(EXTRA_NEW_LOCAL_PROFILE); 79 final int providerStatus = mProviderStatusWatcher.getProviderStatus(); 80 mAreContactsAvailable = providerStatus == ProviderStatus.STATUS_NORMAL; 81 82 if (savedInstanceState == null) { 83 final DisplayOptionsPreferenceFragment fragment = DisplayOptionsPreferenceFragment 84 .newInstance(mNewLocalProfileExtra, mAreContactsAvailable); 85 getFragmentManager().beginTransaction() 86 .replace(android.R.id.content, fragment, TAG_DISPLAY_OPTIONS) 87 .commit(); 88 setActivityTitle(R.string.activity_title_settings); 89 } else { 90 final AboutPreferenceFragment aboutFragment = (AboutPreferenceFragment) 91 getFragmentManager().findFragmentByTag(TAG_ABOUT); 92 93 if (aboutFragment != null) { 94 setActivityTitle(R.string.setting_about); 95 } else { 96 setActivityTitle(R.string.activity_title_settings); 97 } 98 } 99 } 100 101 @Override onPostCreate(Bundle savedInstanceState)102 protected void onPostCreate(Bundle savedInstanceState) { 103 super.onPostCreate(savedInstanceState); 104 mCompatDelegate.onPostCreate(savedInstanceState); 105 } 106 setSupportActionBar(Toolbar toolbar)107 public void setSupportActionBar(Toolbar toolbar) { 108 mCompatDelegate.setSupportActionBar(toolbar); 109 } 110 111 @NonNull 112 @Override getMenuInflater()113 public MenuInflater getMenuInflater() { 114 return mCompatDelegate.getMenuInflater(); 115 } 116 117 @Override setContentView(@ayoutRes int layoutRes)118 public void setContentView(@LayoutRes int layoutRes) { 119 mCompatDelegate.setContentView(layoutRes); 120 } 121 122 @Override setContentView(View view)123 public void setContentView(View view) { 124 mCompatDelegate.setContentView(view); 125 } 126 127 @Override setContentView(View view, ViewGroup.LayoutParams params)128 public void setContentView(View view, ViewGroup.LayoutParams params) { 129 mCompatDelegate.setContentView(view, params); 130 } 131 132 @Override addContentView(View view, ViewGroup.LayoutParams params)133 public void addContentView(View view, ViewGroup.LayoutParams params) { 134 mCompatDelegate.addContentView(view, params); 135 } 136 137 @Override onPostResume()138 protected void onPostResume() { 139 super.onPostResume(); 140 mCompatDelegate.onPostResume(); 141 } 142 143 @Override onTitleChanged(CharSequence title, int color)144 protected void onTitleChanged(CharSequence title, int color) { 145 super.onTitleChanged(title, color); 146 mCompatDelegate.setTitle(title); 147 } 148 149 @Override onConfigurationChanged(Configuration newConfig)150 public void onConfigurationChanged(Configuration newConfig) { 151 super.onConfigurationChanged(newConfig); 152 mCompatDelegate.onConfigurationChanged(newConfig); 153 } 154 155 @Override onDestroy()156 protected void onDestroy() { 157 super.onDestroy(); 158 mCompatDelegate.onDestroy(); 159 } 160 161 @Override invalidateOptionsMenu()162 public void invalidateOptionsMenu() { 163 mCompatDelegate.invalidateOptionsMenu(); 164 } 165 showAboutFragment()166 protected void showAboutFragment() { 167 getFragmentManager().beginTransaction() 168 .replace(android.R.id.content, AboutPreferenceFragment.newInstance(), TAG_ABOUT) 169 .addToBackStack(null) 170 .commit(); 171 setActivityTitle(R.string.setting_about); 172 } 173 174 @Override onOptionsItemSelected(MenuItem item)175 public boolean onOptionsItemSelected(MenuItem item) { 176 if (item.getItemId() == android.R.id.home) { 177 onBackPressed(); 178 return true; 179 } 180 return false; 181 } 182 183 @Override onBackPressed()184 public void onBackPressed() { 185 if (getFragmentManager().getBackStackEntryCount() > 0) { 186 setActivityTitle(R.string.activity_title_settings); 187 getFragmentManager().popBackStack(); 188 } else { 189 super.onBackPressed(); 190 } 191 } 192 setActivityTitle(@tringRes int res)193 private void setActivityTitle(@StringRes int res) { 194 final ActionBar actionBar = mCompatDelegate.getSupportActionBar(); 195 if (actionBar != null) { 196 actionBar.setTitle(res); 197 } 198 } 199 200 @Override onProfileLoaded(Cursor cursor)201 public void onProfileLoaded(Cursor cursor) { 202 boolean hasProfile = false; 203 String displayName = null; 204 long contactId = -1; 205 if (cursor != null && cursor.moveToFirst()) { 206 hasProfile = cursor.getInt(ProfileQuery.CONTACT_IS_USER_PROFILE) == 1; 207 displayName = cursor.getString(ProfileQuery.CONTACT_DISPLAY_NAME); 208 contactId = cursor.getLong(ProfileQuery.CONTACT_ID); 209 } 210 if (hasProfile && TextUtils.isEmpty(displayName)) { 211 displayName = getString(R.string.missing_name); 212 } 213 final DisplayOptionsPreferenceFragment fragment = (DisplayOptionsPreferenceFragment) 214 getFragmentManager().findFragmentByTag(TAG_DISPLAY_OPTIONS); 215 fragment.updateMyInfoPreference(hasProfile, displayName, contactId); 216 } 217 218 @Override onAccountChosen(AccountWithDataSet account, Bundle extraArgs)219 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) { 220 AccountSelectionUtil.doImport(this, extraArgs.getInt(ImportDialogFragment 221 .KEY_RES_ID), account, extraArgs.getInt(ImportDialogFragment.KEY_SUBSCRIPTION_ID)); 222 } 223 224 @Override onAccountSelectorCancelled()225 public void onAccountSelectorCancelled() { 226 } 227 } 228