Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.preference; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 18 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 19 | import android.content.res.Configuration; |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 20 | import android.database.Cursor; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
Wenyi Wang | 7796425 | 2016-02-19 17:14:52 -0800 | [diff] [blame] | 22 | import android.preference.PreferenceActivity; |
Wenyi Wang | 14cb7fa | 2016-06-09 16:50:20 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract.ProviderStatus; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 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; |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 31 | import android.view.MenuInflater; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 32 | import android.view.MenuItem; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 35 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 36 | import com.android.contacts.R; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 37 | import com.android.contacts.editor.SelectAccountDialogFragment; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 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; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Contacts settings. |
| 47 | */ |
Wenyi Wang | c332228 | 2016-11-11 14:53:29 -0800 | [diff] [blame] | 48 | public final class ContactsPreferenceActivity extends PreferenceActivity |
| 49 | implements ProfileListener, SelectAccountDialogFragment.Listener { |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 50 | |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 51 | private static final String TAG_ABOUT = "about_contacts"; |
| 52 | private static final String TAG_DISPLAY_OPTIONS = "display_options"; |
| 53 | |
| 54 | private String mNewLocalProfileExtra; |
Wenyi Wang | 1387401 | 2016-05-27 15:23:53 -0700 | [diff] [blame] | 55 | private boolean mAreContactsAvailable; |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 56 | |
Wenyi Wang | 14cb7fa | 2016-06-09 16:50:20 -0700 | [diff] [blame] | 57 | private ProviderStatusWatcher mProviderStatusWatcher; |
| 58 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 59 | private AppCompatDelegate mCompatDelegate; |
| 60 | |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 61 | public static final String EXTRA_NEW_LOCAL_PROFILE = "newLocalProfile"; |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | protected void onCreate(Bundle savedInstanceState) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 65 | mCompatDelegate = AppCompatDelegate.create(this, null); |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 66 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 67 | super.onCreate(savedInstanceState); |
| 68 | mCompatDelegate.onCreate(savedInstanceState); |
| 69 | |
| 70 | |
| 71 | final ActionBar actionBar = mCompatDelegate.getSupportActionBar(); |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 72 | if (actionBar != null) { |
| 73 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); |
| 74 | } |
| 75 | |
Wenyi Wang | 14cb7fa | 2016-06-09 16:50:20 -0700 | [diff] [blame] | 76 | mProviderStatusWatcher = ProviderStatusWatcher.getInstance(this); |
| 77 | |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 78 | mNewLocalProfileExtra = getIntent().getStringExtra(EXTRA_NEW_LOCAL_PROFILE); |
Wenyi Wang | 14cb7fa | 2016-06-09 16:50:20 -0700 | [diff] [blame] | 79 | final int providerStatus = mProviderStatusWatcher.getProviderStatus(); |
| 80 | mAreContactsAvailable = providerStatus == ProviderStatus.STATUS_NORMAL; |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 81 | |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 82 | if (savedInstanceState == null) { |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 83 | final DisplayOptionsPreferenceFragment fragment = DisplayOptionsPreferenceFragment |
Walter Jang | 0396cf7 | 2016-09-21 13:23:29 -0700 | [diff] [blame] | 84 | .newInstance(mNewLocalProfileExtra, mAreContactsAvailable); |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 85 | getFragmentManager().beginTransaction() |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 86 | .replace(android.R.id.content, fragment, TAG_DISPLAY_OPTIONS) |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 87 | .commit(); |
| 88 | setActivityTitle(R.string.activity_title_settings); |
| 89 | } else { |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 90 | final AboutPreferenceFragment aboutFragment = (AboutPreferenceFragment) |
| 91 | getFragmentManager().findFragmentByTag(TAG_ABOUT); |
Tingting Wang | 940f015 | 2016-06-30 13:15:49 -0700 | [diff] [blame] | 92 | |
| 93 | if (aboutFragment != null) { |
| 94 | setActivityTitle(R.string.setting_about); |
Tingting Wang | 940f015 | 2016-06-30 13:15:49 -0700 | [diff] [blame] | 95 | } else { |
| 96 | setActivityTitle(R.string.activity_title_settings); |
| 97 | } |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 101 | @Override |
| 102 | protected void onPostCreate(Bundle savedInstanceState) { |
| 103 | super.onPostCreate(savedInstanceState); |
| 104 | mCompatDelegate.onPostCreate(savedInstanceState); |
| 105 | } |
| 106 | |
| 107 | public void setSupportActionBar(Toolbar toolbar) { |
| 108 | mCompatDelegate.setSupportActionBar(toolbar); |
| 109 | } |
| 110 | |
| 111 | @NonNull |
| 112 | @Override |
| 113 | public MenuInflater getMenuInflater() { |
| 114 | return mCompatDelegate.getMenuInflater(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void setContentView(@LayoutRes int layoutRes) { |
| 119 | mCompatDelegate.setContentView(layoutRes); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void setContentView(View view) { |
| 124 | mCompatDelegate.setContentView(view); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 129 | mCompatDelegate.setContentView(view, params); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 134 | mCompatDelegate.addContentView(view, params); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | protected void onPostResume() { |
| 139 | super.onPostResume(); |
| 140 | mCompatDelegate.onPostResume(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected void onTitleChanged(CharSequence title, int color) { |
| 145 | super.onTitleChanged(title, color); |
| 146 | mCompatDelegate.setTitle(title); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onConfigurationChanged(Configuration newConfig) { |
| 151 | super.onConfigurationChanged(newConfig); |
| 152 | mCompatDelegate.onConfigurationChanged(newConfig); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onDestroy() { |
| 157 | super.onDestroy(); |
| 158 | mCompatDelegate.onDestroy(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void invalidateOptionsMenu() { |
| 163 | mCompatDelegate.invalidateOptionsMenu(); |
| 164 | } |
| 165 | |
Tingting Wang | 940f015 | 2016-06-30 13:15:49 -0700 | [diff] [blame] | 166 | protected void showAboutFragment() { |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 167 | getFragmentManager().beginTransaction() |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 168 | .replace(android.R.id.content, AboutPreferenceFragment.newInstance(), TAG_ABOUT) |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 169 | .addToBackStack(null) |
| 170 | .commit(); |
Wenyi Wang | 2882a5a | 2016-05-13 19:54:45 +0000 | [diff] [blame] | 171 | setActivityTitle(R.string.setting_about); |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 174 | @Override |
| 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 |
| 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 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 193 | private void setActivityTitle(@StringRes int res) { |
| 194 | final ActionBar actionBar = mCompatDelegate.getSupportActionBar(); |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 195 | if (actionBar != null) { |
| 196 | actionBar.setTitle(res); |
| 197 | } |
| 198 | } |
Wenyi Wang | a031344 | 2016-05-16 11:55:19 -0700 | [diff] [blame] | 199 | |
| 200 | @Override |
| 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 | } |
Wenyi Wang | c332228 | 2016-11-11 14:53:29 -0800 | [diff] [blame] | 217 | |
| 218 | @Override |
| 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 |
| 225 | public void onAccountSelectorCancelled() { |
| 226 | } |
Wenyi Wang | cc3016e | 2015-12-30 16:54:05 -0800 | [diff] [blame] | 227 | } |