blob: 225c5ea51b67df2c69e1c2c31044646bcc187b39 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 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
17package com.android.settings;
18
Fan Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080036import android.nfc.NfcAdapter;
Jason Monkfd2c7222015-12-02 15:38:38 -050037import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.UserHandle;
40import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080041import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040042import android.support.v14.preference.PreferenceFragment;
43import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.view.View;
49import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070052import android.widget.Toolbar;
Fan Zhang28691572016-03-23 15:31:08 -070053
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040055import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000056import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080057import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080058import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070059import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070060import com.android.settings.dashboard.DashboardFeatureProvider;
61import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070062import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050064import com.android.settings.search.DynamicIndexableContentMonitor;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070065import com.android.settings.search.SearchActivity;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080066import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070067import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050070
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070073import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074
Jason Monk4da79e02015-09-10 10:54:36 -040075public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076 implements PreferenceManager.OnPreferenceTreeClickListener,
77 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070078 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079
80 private static final String LOG_TAG = "Settings";
81
Matthew Fritzebe0989c2017-04-25 09:38:56 -070082 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080083
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070085 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070086 @VisibleForTesting
87 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088
89 /**
90 * When starting this activity, the invoking Intent can contain this extra
91 * string to specify which fragment should be initially displayed.
92 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
93 * will call isValidFragment() to confirm that the fragment class name is valid for this
94 * activity.
95 */
96 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
97
98 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080099 * The metrics category constant for logging source when a setting fragment is opened.
100 */
101 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
102
103 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800104 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
105 * this extra can also be specified to supply a Bundle of arguments to pass
106 * to that fragment when it is instantiated during the initial creation
107 * of the activity.
108 */
109 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
110
111 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700112 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
113 */
114 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
115
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800116 public static final String BACK_STACK_PREFS = ":settings:prefs";
117
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118 // extras that allow any preference activity to be launched as part of a wizard
119
120 // show Back and Next buttons? takes boolean parameter
121 // Back will then return RESULT_CANCELED and Next RESULT_OK
122 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
123
124 // add a Skip button?
125 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
126
127 // specify custom text for the Back or Next buttons, or cause a button to not appear
128 // at all by setting it to null
129 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
130 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
131
132 /**
133 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700134 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800135 * that fragment.
136 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100138 /**
139 * The package name used to resolve the title resource id.
140 */
141 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
142 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700143 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
144 ":settings:show_fragment_title_resid";
145 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
146 ":settings:show_fragment_as_shortcut";
147
148 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
149 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
Fan Zhang78ab9f92017-02-27 16:10:05 -0800151 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700152 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
153
Jason Monk30695812015-11-17 09:01:08 -0500154 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 "com.android.settings.FRAGMENT_CLASS";
156
157 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
158
Jason Monkd4f03ec2016-01-07 16:25:34 -0500159 private static final int REQUEST_SUGGESTION = 42;
160
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800163 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700164 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800165
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700166 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
167 "android.settings.APPLICATION_DETAILS_SETTINGS"
168 };
169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 private SharedPreferences mDevelopmentPreferences;
171 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
172
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 private boolean mBatteryPresent = true;
174 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 @Override
176 public void onReceive(Context context, Intent intent) {
177 String action = intent.getAction();
178 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
179 boolean batteryPresent = Utils.isBatteryPresent(intent);
180
181 if (mBatteryPresent != batteryPresent) {
182 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400183 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800184 }
185 }
186 }
187 };
188
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800189 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700190
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700191 private SwitchBar mSwitchBar;
192
193 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700194
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700195 @VisibleForTesting
196 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700197
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700198 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700199 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700200
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700201 private ViewGroup mContent;
202
Fan Zhangc6ca3142017-02-14 15:02:35 -0800203 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700204
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700205 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800206 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207
Fan Zhanga96a2d82016-09-27 17:51:11 -0700208 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500209 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700210
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700211 public SwitchBar getSwitchBar() {
212 return mSwitchBar;
213 }
214
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215 @Override
216 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800217 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800218 null, 0);
219 return true;
220 }
221
222 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400223 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 return false;
225 }
226
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800227 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500228 public SharedPreferences getSharedPreferences(String name, int mode) {
229 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500230 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500231 }
232 return super.getSharedPreferences(name, mode);
233 }
234
Jason Monke4ebcd12016-02-21 09:37:41 -0500235 private String getMetricsTag() {
236 String tag = getClass().getName();
237 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
238 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
239 }
240 if (tag.startsWith("com.android.settings.")) {
241 tag = tag.replace("com.android.settings.", "");
242 }
243 return tag;
244 }
245
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700246 private static boolean isShortCutIntent(final Intent intent) {
247 Set<String> categories = intent.getCategories();
248 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
249 }
250
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700251 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700252 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700253 if (action == null) {
254 return false;
255 }
256 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
257 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
258 }
259 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700260 }
261
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700262 @Override
263 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700264 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500265 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700266
267 final FeatureFactory factory = FeatureFactory.getFactory(this);
268
269 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800270 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700271
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700272 // Should happen before any call to getIntent()
273 getMetaData();
274
275 final Intent intent = getIntent();
276 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
277 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
281 Context.MODE_PRIVATE);
282
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700283 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700284 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700285
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700286 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700287 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
288
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700289 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700290 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700291
Doris Ling1694d4b2017-03-03 14:21:37 -0800292 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700293
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700294 // This is a "Sub Settings" when:
295 // - this is a real SubSettings
296 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700297 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700298 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
299
Fan Zhang92f1e942017-01-21 10:07:26 -0800300 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
301 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700302 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800303 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700304 }
305
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700306 setContentView(mIsShowingDashboard ?
307 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700309 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700310
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 getFragmentManager().addOnBackStackChangedListener(this);
312
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700313 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700314 // We are restarting from a previous saved state; used that to initialize, instead
315 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700316 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800317
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700318 ArrayList<DashboardCategory> categories =
319 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
320 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700321 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700322 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700323 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700325
326 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700327
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800328 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800329 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800330 }
331
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700332 if (mIsShowingDashboard) {
333 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
334 findViewById(R.id.action_bar).setVisibility(View.GONE);
335 Toolbar toolbar = findViewById(R.id.search_action_bar);
336 toolbar.setOnClickListener(this);
337 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700338
339 // Please forgive me for what I am about to do.
340 //
341 // Need to make the navigation icon non-clickable so that the entire card is clickable
342 // and goes to the search UI. Also set the background to null so there's no ripple.
343 View navView = toolbar.getNavigationView();
344 navView.setClickable(false);
345 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700346 }
347
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700348 ActionBar actionBar = getActionBar();
349 if (actionBar != null) {
350 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
351 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700352 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700353 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500354 if (mSwitchBar != null) {
355 mSwitchBar.setMetricsTag(getMetricsTag());
356 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700357
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
360
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700361 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 if (buttonBar != null) {
363 buttonBar.setVisibility(View.VISIBLE);
364
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700365 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 backButton.setOnClickListener(new OnClickListener() {
367 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700368 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 finish();
370 }
371 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700372 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 skipButton.setOnClickListener(new OnClickListener() {
374 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700375 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 finish();
377 }
378 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700379 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800380 mNextButton.setOnClickListener(new OnClickListener() {
381 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700382 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800383 finish();
384 }
385 });
386
387 // set our various button parameters
388 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
389 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
390 if (TextUtils.isEmpty(buttonText)) {
391 mNextButton.setVisibility(View.GONE);
392 }
393 else {
394 mNextButton.setText(buttonText);
395 }
396 }
397 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
398 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
399 if (TextUtils.isEmpty(buttonText)) {
400 backButton.setVisibility(View.GONE);
401 }
402 else {
403 backButton.setText(buttonText);
404 }
405 }
406 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
407 skipButton.setVisibility(View.VISIBLE);
408 }
409 }
410 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700411
Fan Zhangefba6b42017-02-07 17:40:59 -0800412 if (DEBUG_TIMING) {
413 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
414 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700415 }
416
Doris Lingcb69baf2017-02-23 17:50:03 -0800417 @VisibleForTesting
418 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
419 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800420 // UP will be shown only if it is a sub settings
421 if (mIsShortcut) {
422 mDisplayHomeAsUpEnabled = isSubSettings;
423 } else if (isSubSettings) {
424 mDisplayHomeAsUpEnabled = true;
425 } else {
426 mDisplayHomeAsUpEnabled = false;
427 }
428 setTitleFromIntent(intent);
429
430 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
431 switchToFragment(initialFragmentName, initialArguments, true, false,
432 mInitialTitleResId, mInitialTitle, false);
433 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700434 // Show search icon as up affordance if we are displaying the main Dashboard
435 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800436 mInitialTitleResId = R.string.dashboard_title;
437
438 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
439 mInitialTitleResId, mInitialTitle, false);
440 }
441 }
442
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700443 private void setTitleFromIntent(Intent intent) {
444 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
445 if (initialTitleResId > 0) {
446 mInitialTitle = null;
447 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100448
449 final String initialTitleResPackageName = intent.getStringExtra(
450 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
451 if (initialTitleResPackageName != null) {
452 try {
453 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
454 0 /* flags */, new UserHandle(UserHandle.myUserId()));
455 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
456 setTitle(mInitialTitle);
457 mInitialTitleResId = -1;
458 return;
459 } catch (NameNotFoundException e) {
460 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
461 }
462 } else {
463 setTitle(mInitialTitleResId);
464 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700465 } else {
466 mInitialTitleResId = -1;
467 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
468 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
469 setTitle(mInitialTitle);
470 }
471 }
472
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800473 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700475 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
477
Fan Zhang28691572016-03-23 15:31:08 -0700478 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800479 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700480
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800481 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700482 if (mInitialTitleResId > 0) {
483 setTitle(mInitialTitleResId);
484 } else {
485 setTitle(mInitialTitle);
486 }
Fan Zhang28691572016-03-23 15:31:08 -0700487 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800488 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700489
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800490 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
491 setTitleFromBackStackEntry(bse);
492 }
493
494 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
495 final CharSequence title;
496 final int titleRes = bse.getBreadCrumbTitleRes();
497 if (titleRes > 0) {
498 title = getText(titleRes);
499 } else {
500 title = bse.getBreadCrumbTitle();
501 }
502 if (title != null) {
503 setTitle(title);
504 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800505 }
506
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507 @Override
508 protected void onSaveInstanceState(Bundle outState) {
509 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700510 saveState(outState);
511 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700513 /**
514 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
515 */
516 @VisibleForTesting
517 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700518 if (mCategories.size() > 0) {
519 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800520 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700521
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700522 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700523 }
524
525 @Override
526 protected void onRestoreInstanceState(Bundle savedInstanceState) {
527 super.onRestoreInstanceState(savedInstanceState);
528
529 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530 }
531
532 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400533 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534 super.onResume();
535
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700536 mDevelopmentPreferencesListener = (sharedPreferences, key) -> updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
538 mDevelopmentPreferencesListener);
539
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800541 if (mDynamicIndexableContentMonitor == null) {
542 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
543 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800544 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700545
Jason Monk4da79e02015-09-10 10:54:36 -0400546 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 }
548
549 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400550 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800551 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800552 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
553 mDevelopmentPreferencesListener);
554 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800555 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800556 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900557 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800558 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800559 }
560
561 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800562 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
563 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
564 taskDescription.setIcon(icon);
565 super.setTaskDescription(taskDescription);
566 }
567
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 protected boolean isValidFragment(String fragmentName) {
569 // Almost all fragments are wrapped in this,
570 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800571 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
572 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 }
574 return false;
575 }
576
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 @Override
578 public Intent getIntent() {
579 Intent superIntent = super.getIntent();
580 String startingFragment = getStartingFragmentClass(superIntent);
581 // This is called from super.onCreate, isMultiPane() is not yet reliable
582 // Do not use onIsHidingHeaders either, which relies itself on this method
583 if (startingFragment != null) {
584 Intent modIntent = new Intent(superIntent);
585 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
586 Bundle args = superIntent.getExtras();
587 if (args != null) {
588 args = new Bundle(args);
589 } else {
590 args = new Bundle();
591 }
592 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100593 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 return modIntent;
595 }
596 return superIntent;
597 }
598
599 /**
600 * Checks if the component name in the intent is different from the Settings class and
601 * returns the class name to load as a fragment.
602 */
603 private String getStartingFragmentClass(Intent intent) {
604 if (mFragmentClass != null) return mFragmentClass;
605
606 String intentClass = intent.getComponent().getClassName();
607 if (intentClass.equals(getClass().getName())) return null;
608
609 if ("com.android.settings.ManageApplications".equals(intentClass)
610 || "com.android.settings.RunningServices".equals(intentClass)
611 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
612 // Old names of manage apps.
613 intentClass = com.android.settings.applications.ManageApplications.class.getName();
614 }
615
616 return intentClass;
617 }
618
619 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000620 * Start a new fragment containing a preference panel. If the preferences
621 * are being displayed in multi-pane mode, the given fragment class will
622 * be instantiated and placed in the appropriate pane. If running in
623 * single-pane mode, a new activity will be launched in which to show the
624 * fragment.
625 *
626 * @param fragmentClass Full name of the class implementing the fragment.
627 * @param args Any desired arguments to supply to the fragment.
628 * @param titleRes Optional resource identifier of the title of this
629 * fragment.
630 * @param titleText Optional text of the title of this fragment.
631 * @param resultTo Optional fragment that result data should be sent to.
632 * If non-null, resultTo.onActivityResult() will be called when this
633 * preference panel is done. The launched panel must use
634 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
635 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700636 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000637 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800638 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
639 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700640 String title = null;
641 if (titleRes < 0) {
642 if (titleText != null) {
643 title = titleText.toString();
644 } else {
645 // There not much we can do in that case
646 title = "";
647 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700648 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700649 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800650 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000651 }
652
653 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100654 * Start a new fragment in a new activity containing a preference panel for a given user. If the
655 * preferences are being displayed in multi-pane mode, the given fragment class will be
656 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
657 * activity will be launched in which to show the fragment.
658 *
659 * @param fragmentClass Full name of the class implementing the fragment.
660 * @param args Any desired arguments to supply to the fragment.
661 * @param titleRes Optional resource identifier of the title of this fragment.
662 * @param titleText Optional text of the title of this fragment.
663 * @param userHandle The user for which the panel has to be started.
664 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800665 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
666 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700667 // This is a workaround.
668 //
669 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
670 // starting the fragment could cause a native stack corruption. See b/17523189. However,
671 // adding that flag and start the preference panel with the same UserHandler will make it
672 // impossible to use back button to return to the previous screen. See b/20042570.
673 //
674 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
675 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
676 // when we're calling it as the same user.
677 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800678 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700679 } else {
680 String title = null;
681 if (titleRes < 0) {
682 if (titleText != null) {
683 title = titleText.toString();
684 } else {
685 // There not much we can do in that case
686 title = "";
687 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100688 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800689 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
690 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100691 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100692 }
693
694 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800695 * Called by a preference panel fragment to finish itself.
696 *
697 * @param caller The fragment that is asking to be finished.
698 * @param resultCode Optional result code to send back to the original
699 * launching fragment.
700 * @param resultData Optional result data to send back to the original
701 * launching fragment.
702 */
703 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
704 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700705 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800706 }
707
708 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000709 * Start a new fragment.
710 *
711 * @param fragment The fragment to start
712 * @param push If true, the current fragment will be pushed onto the back stack. If false,
713 * the current fragment will be replaced.
714 */
715 public void startPreferenceFragment(Fragment fragment, boolean push) {
716 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700717 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000718 if (push) {
719 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
720 transaction.addToBackStack(BACK_STACK_PREFS);
721 } else {
722 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
723 }
724 transaction.commitAllowingStateLoss();
725 }
726
727 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700728 * Switch to a specific Fragment with taking care of validation, Title and BackStack
729 */
730 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700731 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700732 if (validate && !isValidFragment(fragmentName)) {
733 throw new IllegalArgumentException("Invalid fragment for this activity: "
734 + fragmentName);
735 }
736 Fragment f = Fragment.instantiate(this, fragmentName, args);
737 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700738 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700739 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700740 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 }
742 if (addToBackStack) {
743 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
744 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700745 if (titleResId > 0) {
746 transaction.setBreadCrumbTitle(titleResId);
747 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700748 transaction.setBreadCrumbTitle(title);
749 }
750 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700751 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700752 return f;
753 }
754
Jason Monk4da79e02015-09-10 10:54:36 -0400755 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500756 // Generally the items that are will be changing from these updates will
757 // not be in the top list of tiles, so run it in the background and the
758 // SettingsDrawerActivity will pick up on the updates automatically.
759 AsyncTask.execute(new Runnable() {
760 @Override
761 public void run() {
762 doUpdateTilesList();
763 }
764 });
765 }
766
767 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400768 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700769 final UserManager um = UserManager.get(this);
770 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700771 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400772 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700773 somethingChanged = setTileEnabled(
774 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
775 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700776
Fan Zhangaeb94f02017-07-05 13:46:04 -0700777 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800778 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700779 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
780 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700781
Fan Zhangaeb94f02017-07-05 13:46:04 -0700782 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800783 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700784 Utils.isBandwidthControlEnabled(), isAdmin)
785 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700786
Fan Zhangaeb94f02017-07-05 13:46:04 -0700787 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800788 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700789 Utils.showSimCardTile(this), isAdmin)
790 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700791
Fan Zhangaeb94f02017-07-05 13:46:04 -0700792 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800793 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700794 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400795
Fan Zhangaeb94f02017-07-05 13:46:04 -0700796 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800797 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400798 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700799 && !Utils.isMonkeyRunning(), isAdmin)
800 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400801
Fan Zhangaeb94f02017-07-05 13:46:04 -0700802 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800803 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700804 !UserManager.isDeviceInDemoMode(this), isAdmin)
805 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 !UserManager.isDeviceInDemoMode(this), isAdmin)
810 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700811
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700813 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 !UserManager.isDeviceInDemoMode(this), isAdmin)
815 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400816
Fan Zhangaeb94f02017-07-05 13:46:04 -0700817 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800818 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700819 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
820 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400821
822 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800823 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400824 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400826 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700827 showDev, isAdmin)
828 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400829
Fan Zhanga6986e72017-03-08 09:24:45 -0800830 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700831 somethingChanged = setTileEnabled(new ComponentName(packageName,
832 BackupSettingsActivity.class.getName()), true, isAdmin)
833 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800834
Fan Zhangaeb94f02017-07-05 13:46:04 -0700835 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800836 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700837 WifiDisplaySettings.isAvailable(this), isAdmin)
838 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800839
Jason Monk4da79e02015-09-10 10:54:36 -0400840 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800841
Jason Monk4da79e02015-09-10 10:54:36 -0400842 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800843 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800844 synchronized (categories) {
845 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700846 final int tileCount = category.getTilesCount();
847 for (int i = 0; i < tileCount; i++) {
848 final ComponentName component = category.getTile(i).intent.getComponent();
849
Fan Zhanga6986e72017-03-08 09:24:45 -0800850 final String name = component.getClassName();
851 final boolean isEnabledForRestricted = ArrayUtils.contains(
852 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
853 if (packageName.equals(component.getPackageName())
854 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700855 somethingChanged = setTileEnabled(component, false, isAdmin)
856 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800857 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700858 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700859 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700860 }
861 }
Doris Ling26bf0032016-06-15 18:16:09 -0700862
Fan Zhang224caad2017-01-06 11:42:48 -0800863 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700864 if (somethingChanged) {
865 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
866 updateCategories();
867 } else {
868 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
869 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500870 }
871
Fan Zhangaeb94f02017-07-05 13:46:04 -0700872 /**
873 * @return whether or not the enabled state actually changed.
874 */
875 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400876 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800877 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
878 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400879 enabled = false;
880 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700881 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500882 }
883
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800884 private void getMetaData() {
885 try {
886 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
887 PackageManager.GET_META_DATA);
888 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800889 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
890 } catch (NameNotFoundException nnfe) {
891 // No recovery
892 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
893 }
894 }
895
896 // give subclasses access to the Next button
897 public boolean hasNextButton() {
898 return mNextButton != null;
899 }
900
901 public Button getNextButton() {
902 return mNextButton;
903 }
904
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800905 @Override
906 public boolean shouldUpRecreateTask(Intent targetIntent) {
907 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
908 }
909
Jason Monkd4f03ec2016-01-07 16:25:34 -0500910 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700911 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700912 return;
913 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700914 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500915 startActivityForResult(intent, REQUEST_SUGGESTION);
916 }
917
918 @Override
919 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
920 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
921 && resultCode != RESULT_CANCELED) {
922 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
923 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
924 }
925 super.onActivityResult(requestCode, resultCode, data);
926 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800927
928 @VisibleForTesting
929 Bitmap getBitmapFromXmlResource(int drawableRes) {
930 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
931 Canvas canvas = new Canvas();
932 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
933 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
934 canvas.setBitmap(bitmap);
935 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
936 drawable.draw(canvas);
937
938 return bitmap;
939 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700940
941 @Override
942 public void onClick(View v) {
943 Intent intent = new Intent(this, SearchActivity.class);
944 startActivity(intent);
945 }
946}