blob: 2d83bf19872c012f3615f0779d00c4d3229b4788 [file] [log] [blame]
Wenyi Wangcc3016e2015-12-30 16:54:05 -08001/*
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 Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.preference;
Wenyi Wangcc3016e2015-12-30 16:54:05 -080018
Marcus Hagerott819214d2016-09-29 14:58:27 -070019import android.content.res.Configuration;
Wenyi Wanga0313442016-05-16 11:55:19 -070020import android.database.Cursor;
Wenyi Wangcc3016e2015-12-30 16:54:05 -080021import android.os.Bundle;
Wenyi Wang77964252016-02-19 17:14:52 -080022import android.preference.PreferenceActivity;
Wenyi Wang14cb7fa2016-06-09 16:50:20 -070023import android.provider.ContactsContract.ProviderStatus;
Marcus Hagerott819214d2016-09-29 14:58:27 -070024import android.support.annotation.LayoutRes;
25import android.support.annotation.NonNull;
26import android.support.annotation.StringRes;
27import android.support.v7.app.ActionBar;
28import android.support.v7.app.AppCompatDelegate;
29import android.support.v7.widget.Toolbar;
Wenyi Wanga0313442016-05-16 11:55:19 -070030import android.text.TextUtils;
Marcus Hagerott819214d2016-09-29 14:58:27 -070031import android.view.MenuInflater;
Wenyi Wangcc3016e2015-12-30 16:54:05 -080032import android.view.MenuItem;
Marcus Hagerott819214d2016-09-29 14:58:27 -070033import android.view.View;
34import android.view.ViewGroup;
Wenyi Wangcc3016e2015-12-30 16:54:05 -080035
Arthur Wang3f6a2442016-12-05 14:51:59 -080036import com.android.contacts.R;
Gary Mai0a49afa2016-12-05 15:53:58 -080037import com.android.contacts.editor.SelectAccountDialogFragment;
Gary Mai69c182a2016-12-05 13:07:03 -080038import com.android.contacts.interactions.ImportDialogFragment;
39import com.android.contacts.list.ProviderStatusWatcher;
40import com.android.contacts.model.account.AccountWithDataSet;
41import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileListener;
42import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileQuery;
43import com.android.contacts.util.AccountSelectionUtil;
Wenyi Wangcc3016e2015-12-30 16:54:05 -080044
45/**
46 * Contacts settings.
47 */
Wenyi Wangc3322282016-11-11 14:53:29 -080048public final class ContactsPreferenceActivity extends PreferenceActivity
49 implements ProfileListener, SelectAccountDialogFragment.Listener {
Wenyi Wangcc3016e2015-12-30 16:54:05 -080050
Wenyi Wanga0313442016-05-16 11:55:19 -070051 private static final String TAG_ABOUT = "about_contacts";
52 private static final String TAG_DISPLAY_OPTIONS = "display_options";
53
54 private String mNewLocalProfileExtra;
Wenyi Wang13874012016-05-27 15:23:53 -070055 private boolean mAreContactsAvailable;
Wenyi Wanga0313442016-05-16 11:55:19 -070056
Wenyi Wang14cb7fa2016-06-09 16:50:20 -070057 private ProviderStatusWatcher mProviderStatusWatcher;
58
Marcus Hagerott819214d2016-09-29 14:58:27 -070059 private AppCompatDelegate mCompatDelegate;
60
Wenyi Wanga0313442016-05-16 11:55:19 -070061 public static final String EXTRA_NEW_LOCAL_PROFILE = "newLocalProfile";
Wenyi Wangcc3016e2015-12-30 16:54:05 -080062
63 @Override
64 protected void onCreate(Bundle savedInstanceState) {
Marcus Hagerott819214d2016-09-29 14:58:27 -070065 mCompatDelegate = AppCompatDelegate.create(this, null);
Wenyi Wangcc3016e2015-12-30 16:54:05 -080066
Marcus Hagerott819214d2016-09-29 14:58:27 -070067 super.onCreate(savedInstanceState);
68 mCompatDelegate.onCreate(savedInstanceState);
69
70
71 final ActionBar actionBar = mCompatDelegate.getSupportActionBar();
Wenyi Wangcc3016e2015-12-30 16:54:05 -080072 if (actionBar != null) {
73 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
74 }
75
Wenyi Wang14cb7fa2016-06-09 16:50:20 -070076 mProviderStatusWatcher = ProviderStatusWatcher.getInstance(this);
77
Wenyi Wanga0313442016-05-16 11:55:19 -070078 mNewLocalProfileExtra = getIntent().getStringExtra(EXTRA_NEW_LOCAL_PROFILE);
Wenyi Wang14cb7fa2016-06-09 16:50:20 -070079 final int providerStatus = mProviderStatusWatcher.getProviderStatus();
80 mAreContactsAvailable = providerStatus == ProviderStatus.STATUS_NORMAL;
Wenyi Wanga0313442016-05-16 11:55:19 -070081
Wenyi Wangcc3016e2015-12-30 16:54:05 -080082 if (savedInstanceState == null) {
Wenyi Wanga0313442016-05-16 11:55:19 -070083 final DisplayOptionsPreferenceFragment fragment = DisplayOptionsPreferenceFragment
Walter Jang0396cf72016-09-21 13:23:29 -070084 .newInstance(mNewLocalProfileExtra, mAreContactsAvailable);
Wenyi Wangcc3016e2015-12-30 16:54:05 -080085 getFragmentManager().beginTransaction()
Wenyi Wanga0313442016-05-16 11:55:19 -070086 .replace(android.R.id.content, fragment, TAG_DISPLAY_OPTIONS)
Wenyi Wangcc3016e2015-12-30 16:54:05 -080087 .commit();
88 setActivityTitle(R.string.activity_title_settings);
89 } else {
Wenyi Wanga0313442016-05-16 11:55:19 -070090 final AboutPreferenceFragment aboutFragment = (AboutPreferenceFragment)
91 getFragmentManager().findFragmentByTag(TAG_ABOUT);
Tingting Wang940f0152016-06-30 13:15:49 -070092
93 if (aboutFragment != null) {
94 setActivityTitle(R.string.setting_about);
Tingting Wang940f0152016-06-30 13:15:49 -070095 } else {
96 setActivityTitle(R.string.activity_title_settings);
97 }
Wenyi Wangcc3016e2015-12-30 16:54:05 -080098 }
99 }
100
Marcus Hagerott819214d2016-09-29 14:58:27 -0700101 @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 Wang940f0152016-06-30 13:15:49 -0700166 protected void showAboutFragment() {
Wenyi Wangcc3016e2015-12-30 16:54:05 -0800167 getFragmentManager().beginTransaction()
Wenyi Wanga0313442016-05-16 11:55:19 -0700168 .replace(android.R.id.content, AboutPreferenceFragment.newInstance(), TAG_ABOUT)
Wenyi Wangcc3016e2015-12-30 16:54:05 -0800169 .addToBackStack(null)
170 .commit();
Wenyi Wang2882a5a2016-05-13 19:54:45 +0000171 setActivityTitle(R.string.setting_about);
Wenyi Wangcc3016e2015-12-30 16:54:05 -0800172 }
173
Wenyi Wangcc3016e2015-12-30 16:54:05 -0800174 @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 Hagerott819214d2016-09-29 14:58:27 -0700193 private void setActivityTitle(@StringRes int res) {
194 final ActionBar actionBar = mCompatDelegate.getSupportActionBar();
Wenyi Wangcc3016e2015-12-30 16:54:05 -0800195 if (actionBar != null) {
196 actionBar.setTitle(res);
197 }
198 }
Wenyi Wanga0313442016-05-16 11:55:19 -0700199
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 Wangc3322282016-11-11 14:53:29 -0800217
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 Wangcc3016e2015-12-30 16:54:05 -0800227}