blob: 928eae1114dd802bd1d6f00a59e613847adb817d [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;
Jason Monkfd2c7222015-12-02 15:38:38 -050036import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.UserHandle;
39import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080040import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070042import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import 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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040054import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000055import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080056import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080057import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070058import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070059import com.android.settings.dashboard.DashboardFeatureProvider;
60import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070061import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070062import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050063import com.android.settings.search.DynamicIndexableContentMonitor;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070064import com.android.settings.search.SearchActivity;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080065import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070066import com.android.settings.widget.SwitchBar;
Tony Mantler3d84d562017-07-31 10:48:55 -070067import com.android.settingslib.development.DevelopmentSettingsEnabler;
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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070072import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073
Jason Monk4da79e02015-09-10 10:54:36 -040074public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075 implements PreferenceManager.OnPreferenceTreeClickListener,
76 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070077 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078
79 private static final String LOG_TAG = "Settings";
80
Matthew Fritzebe0989c2017-04-25 09:38:56 -070081 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080082
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070084 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070085 @VisibleForTesting
86 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087
88 /**
89 * When starting this activity, the invoking Intent can contain this extra
90 * string to specify which fragment should be initially displayed.
91 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
92 * will call isValidFragment() to confirm that the fragment class name is valid for this
93 * activity.
94 */
95 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
96
97 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080098 * The metrics category constant for logging source when a setting fragment is opened.
99 */
100 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
101
102 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
104 * this extra can also be specified to supply a Bundle of arguments to pass
105 * to that fragment when it is instantiated during the initial creation
106 * of the activity.
107 */
108 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
109
110 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700111 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
112 */
113 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
114
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800115 public static final String BACK_STACK_PREFS = ":settings:prefs";
116
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117 // extras that allow any preference activity to be launched as part of a wizard
118
119 // show Back and Next buttons? takes boolean parameter
120 // Back will then return RESULT_CANCELED and Next RESULT_OK
121 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
122
123 // add a Skip button?
124 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
125
126 // specify custom text for the Back or Next buttons, or cause a button to not appear
127 // at all by setting it to null
128 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
129 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
130
131 /**
132 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700133 * 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 -0800134 * that fragment.
135 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100137 /**
138 * The package name used to resolve the title resource id.
139 */
140 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
141 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700142 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
143 ":settings:show_fragment_title_resid";
144 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
145 ":settings:show_fragment_as_shortcut";
146
147 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
148 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
Fan Zhang78ab9f92017-02-27 16:10:05 -0800150 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700151 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
152
Jason Monk30695812015-11-17 09:01:08 -0500153 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154 "com.android.settings.FRAGMENT_CLASS";
155
156 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
157
Jason Monkd4f03ec2016-01-07 16:25:34 -0500158 private static final int REQUEST_SUGGESTION = 42;
159
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800162 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700163 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800164
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700165 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
166 "android.settings.APPLICATION_DETAILS_SETTINGS"
167 };
168
Tony Mantler3d84d562017-07-31 10:48:55 -0700169 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 private boolean mBatteryPresent = true;
172 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 @Override
174 public void onReceive(Context context, Intent intent) {
175 String action = intent.getAction();
176 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
177 boolean batteryPresent = Utils.isBatteryPresent(intent);
178
179 if (mBatteryPresent != batteryPresent) {
180 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400181 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 }
183 }
184 }
185 };
186
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800187 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700188
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700189 private SwitchBar mSwitchBar;
190
191 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700192
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700193 @VisibleForTesting
194 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700195
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700196 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700197 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700198
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700199 private ViewGroup mContent;
200
Fan Zhangc6ca3142017-02-14 15:02:35 -0800201 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700202
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700203 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800204 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205
Fan Zhanga96a2d82016-09-27 17:51:11 -0700206 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500207 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700208
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700209 public SwitchBar getSwitchBar() {
210 return mSwitchBar;
211 }
212
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213 @Override
214 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800215 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800216 null, 0);
217 return true;
218 }
219
220 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400221 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800222 return false;
223 }
224
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800225 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500226 public SharedPreferences getSharedPreferences(String name, int mode) {
227 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500228 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500229 }
230 return super.getSharedPreferences(name, mode);
231 }
232
Jason Monke4ebcd12016-02-21 09:37:41 -0500233 private String getMetricsTag() {
234 String tag = getClass().getName();
235 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
236 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
237 }
238 if (tag.startsWith("com.android.settings.")) {
239 tag = tag.replace("com.android.settings.", "");
240 }
241 return tag;
242 }
243
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700244 private static boolean isShortCutIntent(final Intent intent) {
245 Set<String> categories = intent.getCategories();
246 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
247 }
248
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700249 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700250 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700251 if (action == null) {
252 return false;
253 }
254 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
255 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
256 }
257 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700258 }
259
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700260 @Override
261 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700262 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500263 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700264
265 final FeatureFactory factory = FeatureFactory.getFactory(this);
266
267 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800268 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700269
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700270 // Should happen before any call to getIntent()
271 getMetaData();
272
273 final Intent intent = getIntent();
274 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
275 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800277
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700278 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700279 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700280
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700281 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700282 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
283
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700284 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700285 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700286
Doris Ling1694d4b2017-03-03 14:21:37 -0800287 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700288
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700289 // This is a "Sub Settings" when:
290 // - this is a real SubSettings
291 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700292 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700293 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
294
Fan Zhang92f1e942017-01-21 10:07:26 -0800295 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
296 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700297 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800298 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700299 }
300
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700301 setContentView(mIsShowingDashboard ?
302 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700304 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700305
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 getFragmentManager().addOnBackStackChangedListener(this);
307
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700308 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700309 // We are restarting from a previous saved state; used that to initialize, instead
310 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700311 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800312
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700313 ArrayList<DashboardCategory> categories =
314 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
315 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700316 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700317 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700318 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700320
321 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700322
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800324 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 }
326
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700327 if (mIsShowingDashboard) {
328 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
329 findViewById(R.id.action_bar).setVisibility(View.GONE);
330 Toolbar toolbar = findViewById(R.id.search_action_bar);
331 toolbar.setOnClickListener(this);
332 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700333
334 // Please forgive me for what I am about to do.
335 //
336 // Need to make the navigation icon non-clickable so that the entire card is clickable
337 // and goes to the search UI. Also set the background to null so there's no ripple.
338 View navView = toolbar.getNavigationView();
339 navView.setClickable(false);
340 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700341 }
342
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700343 ActionBar actionBar = getActionBar();
344 if (actionBar != null) {
345 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
346 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700347 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700348 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500349 if (mSwitchBar != null) {
350 mSwitchBar.setMetricsTag(getMetricsTag());
351 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700352
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
355
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700356 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 if (buttonBar != null) {
358 buttonBar.setVisibility(View.VISIBLE);
359
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700360 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 backButton.setOnClickListener(new OnClickListener() {
362 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700363 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 finish();
365 }
366 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700367 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 skipButton.setOnClickListener(new OnClickListener() {
369 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700370 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 finish();
372 }
373 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700374 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 mNextButton.setOnClickListener(new OnClickListener() {
376 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700377 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800378 finish();
379 }
380 });
381
382 // set our various button parameters
383 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
384 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
385 if (TextUtils.isEmpty(buttonText)) {
386 mNextButton.setVisibility(View.GONE);
387 }
388 else {
389 mNextButton.setText(buttonText);
390 }
391 }
392 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
393 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
394 if (TextUtils.isEmpty(buttonText)) {
395 backButton.setVisibility(View.GONE);
396 }
397 else {
398 backButton.setText(buttonText);
399 }
400 }
401 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
402 skipButton.setVisibility(View.VISIBLE);
403 }
404 }
405 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700406
Fan Zhangefba6b42017-02-07 17:40:59 -0800407 if (DEBUG_TIMING) {
408 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
409 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700410 }
411
Doris Lingcb69baf2017-02-23 17:50:03 -0800412 @VisibleForTesting
413 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
414 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800415 // UP will be shown only if it is a sub settings
416 if (mIsShortcut) {
417 mDisplayHomeAsUpEnabled = isSubSettings;
418 } else if (isSubSettings) {
419 mDisplayHomeAsUpEnabled = true;
420 } else {
421 mDisplayHomeAsUpEnabled = false;
422 }
423 setTitleFromIntent(intent);
424
425 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
426 switchToFragment(initialFragmentName, initialArguments, true, false,
427 mInitialTitleResId, mInitialTitle, false);
428 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700429 // Show search icon as up affordance if we are displaying the main Dashboard
430 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800431 mInitialTitleResId = R.string.dashboard_title;
432
433 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
434 mInitialTitleResId, mInitialTitle, false);
435 }
436 }
437
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700438 private void setTitleFromIntent(Intent intent) {
439 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
440 if (initialTitleResId > 0) {
441 mInitialTitle = null;
442 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100443
444 final String initialTitleResPackageName = intent.getStringExtra(
445 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
446 if (initialTitleResPackageName != null) {
447 try {
448 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
449 0 /* flags */, new UserHandle(UserHandle.myUserId()));
450 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
451 setTitle(mInitialTitle);
452 mInitialTitleResId = -1;
453 return;
454 } catch (NameNotFoundException e) {
455 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
456 }
457 } else {
458 setTitle(mInitialTitleResId);
459 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700460 } else {
461 mInitialTitleResId = -1;
462 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
463 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
464 setTitle(mInitialTitle);
465 }
466 }
467
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800468 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800469 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700470 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 }
472
Fan Zhang28691572016-03-23 15:31:08 -0700473 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800474 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700475
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800476 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700477 if (mInitialTitleResId > 0) {
478 setTitle(mInitialTitleResId);
479 } else {
480 setTitle(mInitialTitle);
481 }
Fan Zhang28691572016-03-23 15:31:08 -0700482 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800483 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700484
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800485 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
486 setTitleFromBackStackEntry(bse);
487 }
488
489 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
490 final CharSequence title;
491 final int titleRes = bse.getBreadCrumbTitleRes();
492 if (titleRes > 0) {
493 title = getText(titleRes);
494 } else {
495 title = bse.getBreadCrumbTitle();
496 }
497 if (title != null) {
498 setTitle(title);
499 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800500 }
501
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502 @Override
503 protected void onSaveInstanceState(Bundle outState) {
504 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700505 saveState(outState);
506 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700508 /**
509 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
510 */
511 @VisibleForTesting
512 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700513 if (mCategories.size() > 0) {
514 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700516
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700517 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700518 }
519
520 @Override
521 protected void onRestoreInstanceState(Bundle savedInstanceState) {
522 super.onRestoreInstanceState(savedInstanceState);
523
524 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 }
526
527 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400528 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529 super.onResume();
530
Tony Mantler3d84d562017-07-31 10:48:55 -0700531 mDevelopmentSettingsListener = new BroadcastReceiver() {
532 @Override
533 public void onReceive(Context context, Intent intent) {
534 updateTilesList();
535 }
536 };
537 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
538 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539
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();
Tony Mantler3d84d562017-07-31 10:48:55 -0700552 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
553 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800555 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900556 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800557 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 }
559
560 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800561 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
562 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
563 taskDescription.setIcon(icon);
564 super.setTaskDescription(taskDescription);
565 }
566
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 protected boolean isValidFragment(String fragmentName) {
568 // Almost all fragments are wrapped in this,
569 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800570 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
571 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572 }
573 return false;
574 }
575
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 @Override
577 public Intent getIntent() {
578 Intent superIntent = super.getIntent();
579 String startingFragment = getStartingFragmentClass(superIntent);
580 // This is called from super.onCreate, isMultiPane() is not yet reliable
581 // Do not use onIsHidingHeaders either, which relies itself on this method
582 if (startingFragment != null) {
583 Intent modIntent = new Intent(superIntent);
584 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
585 Bundle args = superIntent.getExtras();
586 if (args != null) {
587 args = new Bundle(args);
588 } else {
589 args = new Bundle();
590 }
591 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100592 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593 return modIntent;
594 }
595 return superIntent;
596 }
597
598 /**
599 * Checks if the component name in the intent is different from the Settings class and
600 * returns the class name to load as a fragment.
601 */
602 private String getStartingFragmentClass(Intent intent) {
603 if (mFragmentClass != null) return mFragmentClass;
604
605 String intentClass = intent.getComponent().getClassName();
606 if (intentClass.equals(getClass().getName())) return null;
607
608 if ("com.android.settings.ManageApplications".equals(intentClass)
609 || "com.android.settings.RunningServices".equals(intentClass)
610 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
611 // Old names of manage apps.
612 intentClass = com.android.settings.applications.ManageApplications.class.getName();
613 }
614
615 return intentClass;
616 }
617
618 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000619 * Start a new fragment containing a preference panel. If the preferences
620 * are being displayed in multi-pane mode, the given fragment class will
621 * be instantiated and placed in the appropriate pane. If running in
622 * single-pane mode, a new activity will be launched in which to show the
623 * fragment.
624 *
625 * @param fragmentClass Full name of the class implementing the fragment.
626 * @param args Any desired arguments to supply to the fragment.
627 * @param titleRes Optional resource identifier of the title of this
628 * fragment.
629 * @param titleText Optional text of the title of this fragment.
630 * @param resultTo Optional fragment that result data should be sent to.
631 * If non-null, resultTo.onActivityResult() will be called when this
632 * preference panel is done. The launched panel must use
633 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
634 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700635 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000636 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800637 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
638 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700639 String title = null;
640 if (titleRes < 0) {
641 if (titleText != null) {
642 title = titleText.toString();
643 } else {
644 // There not much we can do in that case
645 title = "";
646 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700647 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700648 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800649 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000650 }
651
652 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100653 * Start a new fragment in a new activity containing a preference panel for a given user. If the
654 * preferences are being displayed in multi-pane mode, the given fragment class will be
655 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
656 * activity will be launched in which to show the fragment.
657 *
658 * @param fragmentClass Full name of the class implementing the fragment.
659 * @param args Any desired arguments to supply to the fragment.
660 * @param titleRes Optional resource identifier of the title of this fragment.
661 * @param titleText Optional text of the title of this fragment.
662 * @param userHandle The user for which the panel has to be started.
663 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800664 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
665 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700666 // This is a workaround.
667 //
668 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
669 // starting the fragment could cause a native stack corruption. See b/17523189. However,
670 // adding that flag and start the preference panel with the same UserHandler will make it
671 // impossible to use back button to return to the previous screen. See b/20042570.
672 //
673 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
674 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
675 // when we're calling it as the same user.
676 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800677 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700678 } else {
679 String title = null;
680 if (titleRes < 0) {
681 if (titleText != null) {
682 title = titleText.toString();
683 } else {
684 // There not much we can do in that case
685 title = "";
686 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100687 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800688 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
689 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100690 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100691 }
692
693 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800694 * Called by a preference panel fragment to finish itself.
695 *
696 * @param caller The fragment that is asking to be finished.
697 * @param resultCode Optional result code to send back to the original
698 * launching fragment.
699 * @param resultData Optional result data to send back to the original
700 * launching fragment.
701 */
702 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
703 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700704 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800705 }
706
707 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000708 * Start a new fragment.
709 *
710 * @param fragment The fragment to start
711 * @param push If true, the current fragment will be pushed onto the back stack. If false,
712 * the current fragment will be replaced.
713 */
714 public void startPreferenceFragment(Fragment fragment, boolean push) {
715 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700716 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000717 if (push) {
718 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
719 transaction.addToBackStack(BACK_STACK_PREFS);
720 } else {
721 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
722 }
723 transaction.commitAllowingStateLoss();
724 }
725
726 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700727 * Switch to a specific Fragment with taking care of validation, Title and BackStack
728 */
729 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700730 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700731 if (validate && !isValidFragment(fragmentName)) {
732 throw new IllegalArgumentException("Invalid fragment for this activity: "
733 + fragmentName);
734 }
735 Fragment f = Fragment.instantiate(this, fragmentName, args);
736 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700737 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700738 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700739 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700740 }
741 if (addToBackStack) {
742 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
743 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700744 if (titleResId > 0) {
745 transaction.setBreadCrumbTitle(titleResId);
746 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700747 transaction.setBreadCrumbTitle(title);
748 }
749 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700750 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700751 return f;
752 }
753
Jason Monk4da79e02015-09-10 10:54:36 -0400754 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500755 // Generally the items that are will be changing from these updates will
756 // not be in the top list of tiles, so run it in the background and the
757 // SettingsDrawerActivity will pick up on the updates automatically.
758 AsyncTask.execute(new Runnable() {
759 @Override
760 public void run() {
761 doUpdateTilesList();
762 }
763 });
764 }
765
766 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400767 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700768 final UserManager um = UserManager.get(this);
769 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700770 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400771 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700772 somethingChanged = setTileEnabled(
773 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
774 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700775
Fan Zhangaeb94f02017-07-05 13:46:04 -0700776 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800777 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700778 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
779 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700780
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700781 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
782 .getDataPlanFeatureProvider()
783 .isEnabled();
784
785 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
786 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
787 // reverse.
788
789 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
790 // disable DataPlanUsageSummaryActivity.
791 somethingChanged = setTileEnabled(
792 new ComponentName(packageName,
793 isDataPlanFeatureEnabled
794 ? Settings.DataUsageSummaryActivity.class.getName()
795 : Settings.DataPlanUsageSummaryActivity.class.getName()),
796 false /* enabled */,
797 isAdmin) || somethingChanged;
798
799 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
800 // enable DataPlanUsageSummaryActivity.
801 somethingChanged = setTileEnabled(
802 new ComponentName(packageName,
803 isDataPlanFeatureEnabled
804 ? Settings.DataPlanUsageSummaryActivity.class.getName()
805 : Settings.DataUsageSummaryActivity.class.getName()),
806 Utils.isBandwidthControlEnabled() /* enabled */,
807 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700808
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800810 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700811 Utils.showSimCardTile(this), isAdmin)
812 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700813
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800815 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700816 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400817
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800819 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400820 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700821 && !Utils.isMonkeyRunning(), isAdmin)
822 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400823
Fan Zhangaeb94f02017-07-05 13:46:04 -0700824 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800825 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700826 !UserManager.isDeviceInDemoMode(this), isAdmin)
827 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800828
Fan Zhangaeb94f02017-07-05 13:46:04 -0700829 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800830 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700831 !UserManager.isDeviceInDemoMode(this), isAdmin)
832 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700833
Fan Zhangaeb94f02017-07-05 13:46:04 -0700834 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700835 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700836 !UserManager.isDeviceInDemoMode(this), isAdmin)
837 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400838
Fan Zhangaeb94f02017-07-05 13:46:04 -0700839 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800840 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700841 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
842 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400843
Tony Mantler3d84d562017-07-31 10:48:55 -0700844 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Dan Sandler12c4ba42016-03-28 11:13:40 -0400845 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700846 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400847 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700848 showDev, isAdmin)
849 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400850
Fan Zhanga6986e72017-03-08 09:24:45 -0800851 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700852 somethingChanged = setTileEnabled(new ComponentName(packageName,
853 BackupSettingsActivity.class.getName()), true, isAdmin)
854 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800855
Fan Zhangaeb94f02017-07-05 13:46:04 -0700856 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800857 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700858 WifiDisplaySettings.isAvailable(this), isAdmin)
859 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800860
Jason Monk4da79e02015-09-10 10:54:36 -0400861 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800862
Jason Monk4da79e02015-09-10 10:54:36 -0400863 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800864 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800865 synchronized (categories) {
866 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700867 final int tileCount = category.getTilesCount();
868 for (int i = 0; i < tileCount; i++) {
869 final ComponentName component = category.getTile(i).intent.getComponent();
870
Fan Zhanga6986e72017-03-08 09:24:45 -0800871 final String name = component.getClassName();
872 final boolean isEnabledForRestricted = ArrayUtils.contains(
873 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
874 if (packageName.equals(component.getPackageName())
875 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700876 somethingChanged = setTileEnabled(component, false, isAdmin)
877 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800878 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700879 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700880 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700881 }
882 }
Doris Ling26bf0032016-06-15 18:16:09 -0700883
Fan Zhang224caad2017-01-06 11:42:48 -0800884 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700885 if (somethingChanged) {
886 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
887 updateCategories();
888 } else {
889 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
890 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500891 }
892
Fan Zhangaeb94f02017-07-05 13:46:04 -0700893 /**
894 * @return whether or not the enabled state actually changed.
895 */
896 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400897 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800898 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
899 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400900 enabled = false;
901 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700902 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500903 }
904
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800905 private void getMetaData() {
906 try {
907 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
908 PackageManager.GET_META_DATA);
909 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800910 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
911 } catch (NameNotFoundException nnfe) {
912 // No recovery
913 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
914 }
915 }
916
917 // give subclasses access to the Next button
918 public boolean hasNextButton() {
919 return mNextButton != null;
920 }
921
922 public Button getNextButton() {
923 return mNextButton;
924 }
925
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800926 @Override
927 public boolean shouldUpRecreateTask(Intent targetIntent) {
928 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
929 }
930
Jason Monkd4f03ec2016-01-07 16:25:34 -0500931 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700932 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700933 return;
934 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700935 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500936 startActivityForResult(intent, REQUEST_SUGGESTION);
937 }
938
939 @Override
940 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
941 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
942 && resultCode != RESULT_CANCELED) {
943 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
944 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
945 }
946 super.onActivityResult(requestCode, resultCode, data);
947 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800948
949 @VisibleForTesting
950 Bitmap getBitmapFromXmlResource(int drawableRes) {
951 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
952 Canvas canvas = new Canvas();
953 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
954 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
955 canvas.setBitmap(bitmap);
956 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
957 drawable.draw(canvas);
958
959 return bitmap;
960 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700961
962 @Override
963 public void onClick(View v) {
964 Intent intent = new Intent(this, SearchActivity.class);
965 startActivity(intent);
966 }
967}