blob: f61dfd3002cc0589b4b260c3bff1afa255ad6beb [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 Megliod25314d2014-03-21 19:24:43 -070048import android.view.Menu;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.view.View;
50import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070053import android.widget.Toolbar;
Fan Zhang28691572016-03-23 15:31:08 -070054
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040056import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000057import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080058import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080059import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070060import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070063import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050065import com.android.settings.search.DynamicIndexableContentMonitor;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070066import com.android.settings.search.SearchActivity;
Matthew Fritze2078bc22017-05-22 13:31:54 -070067import com.android.settings.search.SearchFeatureProvider;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080068import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070069import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040071import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050072
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070075import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076
Jason Monk4da79e02015-09-10 10:54:36 -040077public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078 implements PreferenceManager.OnPreferenceTreeClickListener,
79 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070080 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081
82 private static final String LOG_TAG = "Settings";
83
Matthew Fritzebe0989c2017-04-25 09:38:56 -070084 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080085
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080086 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070087 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070088 @VisibleForTesting
89 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
90 @VisibleForTesting
91 static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080092
93 /**
94 * When starting this activity, the invoking Intent can contain this extra
95 * string to specify which fragment should be initially displayed.
96 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
97 * will call isValidFragment() to confirm that the fragment class name is valid for this
98 * activity.
99 */
100 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
101
102 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800103 * The metrics category constant for logging source when a setting fragment is opened.
104 */
105 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
106
107 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
109 * this extra can also be specified to supply a Bundle of arguments to pass
110 * to that fragment when it is instantiated during the initial creation
111 * of the activity.
112 */
113 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
114
115 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700116 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
117 */
118 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
119
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800120 public static final String BACK_STACK_PREFS = ":settings:prefs";
121
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122 // extras that allow any preference activity to be launched as part of a wizard
123
124 // show Back and Next buttons? takes boolean parameter
125 // Back will then return RESULT_CANCELED and Next RESULT_OK
126 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
127
128 // add a Skip button?
129 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
130
131 // specify custom text for the Back or Next buttons, or cause a button to not appear
132 // at all by setting it to null
133 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
134 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
135
136 /**
137 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700138 * 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 -0800139 * that fragment.
140 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700141 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100142 /**
143 * The package name used to resolve the title resource id.
144 */
145 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
146 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700147 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
148 ":settings:show_fragment_title_resid";
149 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
150 ":settings:show_fragment_as_shortcut";
151
152 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
153 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154
Fan Zhang78ab9f92017-02-27 16:10:05 -0800155 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700156 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
157
Jason Monk30695812015-11-17 09:01:08 -0500158 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800159 "com.android.settings.FRAGMENT_CLASS";
160
161 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
162
Jason Monkd4f03ec2016-01-07 16:25:34 -0500163 private static final int REQUEST_SUGGESTION = 42;
164
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800167 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700168 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800169
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700170 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
171 "android.settings.APPLICATION_DETAILS_SETTINGS"
172 };
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 private SharedPreferences mDevelopmentPreferences;
175 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
176
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 private boolean mBatteryPresent = true;
178 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800179 @Override
180 public void onReceive(Context context, Intent intent) {
181 String action = intent.getAction();
182 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
183 boolean batteryPresent = Utils.isBatteryPresent(intent);
184
185 if (mBatteryPresent != batteryPresent) {
186 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400187 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188 }
189 }
190 }
191 };
192
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800193 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700194
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700195 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700196 private SwitchBar mSwitchBar;
197
198 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700199
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700200 @VisibleForTesting
201 boolean mDisplayHomeAsUpEnabled;
202 @VisibleForTesting
203 boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700204
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700205 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700206 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700207
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700208 private ViewGroup mContent;
209
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700210 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800211 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700212
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700213 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800214 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215
Fan Zhanga96a2d82016-09-27 17:51:11 -0700216 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500217 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700218
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700219 public SwitchBar getSwitchBar() {
220 return mSwitchBar;
221 }
222
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800223 @Override
224 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800225 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800226 null, 0);
227 return true;
228 }
229
230 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400231 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800232 return false;
233 }
234
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800235 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700236 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700237 if (!mDisplaySearch) {
238 return false;
239 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700240 mSearchFeatureProvider.setUpSearchMenu(menu, this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700241 return true;
242 }
243
Jason Monk31c7c322016-01-20 14:41:52 -0500244 @Override
245 public SharedPreferences getSharedPreferences(String name, int mode) {
246 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500247 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500248 }
249 return super.getSharedPreferences(name, mode);
250 }
251
Jason Monke4ebcd12016-02-21 09:37:41 -0500252 private String getMetricsTag() {
253 String tag = getClass().getName();
254 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
255 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
256 }
257 if (tag.startsWith("com.android.settings.")) {
258 tag = tag.replace("com.android.settings.", "");
259 }
260 return tag;
261 }
262
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700263 private static boolean isShortCutIntent(final Intent intent) {
264 Set<String> categories = intent.getCategories();
265 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
266 }
267
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700268 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700269 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700270 if (action == null) {
271 return false;
272 }
273 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
274 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
275 }
276 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700277 }
278
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700279 @Override
280 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700281 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500282 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700283
284 final FeatureFactory factory = FeatureFactory.getFactory(this);
285
286 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800287 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800288 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700289
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700290 // Should happen before any call to getIntent()
291 getMetaData();
292
293 final Intent intent = getIntent();
294 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
295 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
299 Context.MODE_PRIVATE);
300
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700301 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700302 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700303
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700304 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700305 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
306
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700307 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700308 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700309
Doris Ling1694d4b2017-03-03 14:21:37 -0800310 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700311
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700312 // This is a "Sub Settings" when:
313 // - this is a real SubSettings
314 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700315 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700316 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
317
Fan Zhang92f1e942017-01-21 10:07:26 -0800318 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
319 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700320 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800321 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700322 }
323
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700324 setContentView(mIsShowingDashboard ?
325 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700327 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700328
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329 getFragmentManager().addOnBackStackChangedListener(this);
330
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700331 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700332 // We are restarting from a previous saved state; used that to initialize, instead
333 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700334 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800335
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700336 ArrayList<DashboardCategory> categories =
337 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
338 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700339 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700340 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700341 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700343
344 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700345
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800347 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 }
349
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700350 if (mIsShowingDashboard) {
351 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
352 findViewById(R.id.action_bar).setVisibility(View.GONE);
353 Toolbar toolbar = findViewById(R.id.search_action_bar);
354 toolbar.setOnClickListener(this);
355 setActionBar(toolbar);
356 }
357
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700358 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700359 if (mActionBar != null) {
360 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
361 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
362 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700363 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500364 if (mSwitchBar != null) {
365 mSwitchBar.setMetricsTag(getMetricsTag());
366 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700367
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
370
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700371 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800372 if (buttonBar != null) {
373 buttonBar.setVisibility(View.VISIBLE);
374
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700375 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 backButton.setOnClickListener(new OnClickListener() {
377 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700378 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800379 finish();
380 }
381 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700382 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800383 skipButton.setOnClickListener(new OnClickListener() {
384 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700385 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800386 finish();
387 }
388 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700389 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800390 mNextButton.setOnClickListener(new OnClickListener() {
391 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700392 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800393 finish();
394 }
395 });
396
397 // set our various button parameters
398 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
399 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
400 if (TextUtils.isEmpty(buttonText)) {
401 mNextButton.setVisibility(View.GONE);
402 }
403 else {
404 mNextButton.setText(buttonText);
405 }
406 }
407 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
408 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
409 if (TextUtils.isEmpty(buttonText)) {
410 backButton.setVisibility(View.GONE);
411 }
412 else {
413 backButton.setText(buttonText);
414 }
415 }
416 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
417 skipButton.setVisibility(View.VISIBLE);
418 }
419 }
420 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700421
Fan Zhangefba6b42017-02-07 17:40:59 -0800422 if (DEBUG_TIMING) {
423 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
424 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700425 }
426
Doris Lingcb69baf2017-02-23 17:50:03 -0800427 @VisibleForTesting
428 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
429 if (!mIsShowingDashboard && initialFragmentName != null) {
430 mDisplaySearch = false;
431 // UP will be shown only if it is a sub settings
432 if (mIsShortcut) {
433 mDisplayHomeAsUpEnabled = isSubSettings;
434 } else if (isSubSettings) {
435 mDisplayHomeAsUpEnabled = true;
436 } else {
437 mDisplayHomeAsUpEnabled = false;
438 }
439 setTitleFromIntent(intent);
440
441 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
442 switchToFragment(initialFragmentName, initialArguments, true, false,
443 mInitialTitleResId, mInitialTitle, false);
444 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700445 // Show search icon as up affordance if we are displaying the main Dashboard
446 mDisplayHomeAsUpEnabled = true;
447 // toolbar is search affordance so don't show search
448 mDisplaySearch = false;
Doris Lingcb69baf2017-02-23 17:50:03 -0800449 mInitialTitleResId = R.string.dashboard_title;
450
451 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
452 mInitialTitleResId, mInitialTitle, false);
453 }
454 }
455
Fan Zhangece8ff62016-06-30 13:20:12 -0700456 public void setDisplaySearchMenu(boolean displaySearch) {
457 if (displaySearch != mDisplaySearch) {
458 mDisplaySearch = displaySearch;
459 invalidateOptionsMenu();
460 }
461 }
462
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700463 private void setTitleFromIntent(Intent intent) {
464 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
465 if (initialTitleResId > 0) {
466 mInitialTitle = null;
467 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100468
469 final String initialTitleResPackageName = intent.getStringExtra(
470 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
471 if (initialTitleResPackageName != null) {
472 try {
473 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
474 0 /* flags */, new UserHandle(UserHandle.myUserId()));
475 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
476 setTitle(mInitialTitle);
477 mInitialTitleResId = -1;
478 return;
479 } catch (NameNotFoundException e) {
480 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
481 }
482 } else {
483 setTitle(mInitialTitleResId);
484 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700485 } else {
486 mInitialTitleResId = -1;
487 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
488 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
489 setTitle(mInitialTitle);
490 }
491 }
492
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800493 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800494 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700495 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800496 }
497
Fan Zhang28691572016-03-23 15:31:08 -0700498 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800499 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700500
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800501 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700502 if (mInitialTitleResId > 0) {
503 setTitle(mInitialTitleResId);
504 } else {
505 setTitle(mInitialTitle);
506 }
Fan Zhang28691572016-03-23 15:31:08 -0700507 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800508 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700509
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800510 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
511 setTitleFromBackStackEntry(bse);
512 }
513
514 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
515 final CharSequence title;
516 final int titleRes = bse.getBreadCrumbTitleRes();
517 if (titleRes > 0) {
518 title = getText(titleRes);
519 } else {
520 title = bse.getBreadCrumbTitle();
521 }
522 if (title != null) {
523 setTitle(title);
524 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800525 }
526
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 @Override
528 protected void onSaveInstanceState(Bundle outState) {
529 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700530 saveState(outState);
531 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700533 /**
534 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
535 */
536 @VisibleForTesting
537 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700538 if (mCategories.size() > 0) {
539 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700541
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700542 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700543 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
544 }
545
546 @Override
547 protected void onRestoreInstanceState(Bundle savedInstanceState) {
548 super.onRestoreInstanceState(savedInstanceState);
549
550 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
551 mDisplaySearch = savedInstanceState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 }
553
554 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400555 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800556 super.onResume();
557
558 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
559 @Override
560 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400561 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 }
563 };
564 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
565 mDevelopmentPreferencesListener);
566
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800568 if (mDynamicIndexableContentMonitor == null) {
569 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
570 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800571 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700572
Jason Monk4da79e02015-09-10 10:54:36 -0400573 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 }
575
576 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400577 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800578 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800579 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
580 mDevelopmentPreferencesListener);
581 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800583 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900584 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800585 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800586 }
587
588 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800589 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
590 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
591 taskDescription.setIcon(icon);
592 super.setTaskDescription(taskDescription);
593 }
594
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 protected boolean isValidFragment(String fragmentName) {
596 // Almost all fragments are wrapped in this,
597 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800598 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
599 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800600 }
601 return false;
602 }
603
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 @Override
605 public Intent getIntent() {
606 Intent superIntent = super.getIntent();
607 String startingFragment = getStartingFragmentClass(superIntent);
608 // This is called from super.onCreate, isMultiPane() is not yet reliable
609 // Do not use onIsHidingHeaders either, which relies itself on this method
610 if (startingFragment != null) {
611 Intent modIntent = new Intent(superIntent);
612 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
613 Bundle args = superIntent.getExtras();
614 if (args != null) {
615 args = new Bundle(args);
616 } else {
617 args = new Bundle();
618 }
619 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100620 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800621 return modIntent;
622 }
623 return superIntent;
624 }
625
626 /**
627 * Checks if the component name in the intent is different from the Settings class and
628 * returns the class name to load as a fragment.
629 */
630 private String getStartingFragmentClass(Intent intent) {
631 if (mFragmentClass != null) return mFragmentClass;
632
633 String intentClass = intent.getComponent().getClassName();
634 if (intentClass.equals(getClass().getName())) return null;
635
636 if ("com.android.settings.ManageApplications".equals(intentClass)
637 || "com.android.settings.RunningServices".equals(intentClass)
638 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
639 // Old names of manage apps.
640 intentClass = com.android.settings.applications.ManageApplications.class.getName();
641 }
642
643 return intentClass;
644 }
645
646 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000647 * Start a new fragment containing a preference panel. If the preferences
648 * are being displayed in multi-pane mode, the given fragment class will
649 * be instantiated and placed in the appropriate pane. If running in
650 * single-pane mode, a new activity will be launched in which to show the
651 * fragment.
652 *
653 * @param fragmentClass Full name of the class implementing the fragment.
654 * @param args Any desired arguments to supply to the fragment.
655 * @param titleRes Optional resource identifier of the title of this
656 * fragment.
657 * @param titleText Optional text of the title of this fragment.
658 * @param resultTo Optional fragment that result data should be sent to.
659 * If non-null, resultTo.onActivityResult() will be called when this
660 * preference panel is done. The launched panel must use
661 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
662 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700663 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000664 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800665 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
666 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700667 String title = null;
668 if (titleRes < 0) {
669 if (titleText != null) {
670 title = titleText.toString();
671 } else {
672 // There not much we can do in that case
673 title = "";
674 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700675 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700676 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800677 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000678 }
679
680 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100681 * Start a new fragment in a new activity containing a preference panel for a given user. If the
682 * preferences are being displayed in multi-pane mode, the given fragment class will be
683 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
684 * activity will be launched in which to show the fragment.
685 *
686 * @param fragmentClass Full name of the class implementing the fragment.
687 * @param args Any desired arguments to supply to the fragment.
688 * @param titleRes Optional resource identifier of the title of this fragment.
689 * @param titleText Optional text of the title of this fragment.
690 * @param userHandle The user for which the panel has to be started.
691 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800692 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
693 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700694 // This is a workaround.
695 //
696 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
697 // starting the fragment could cause a native stack corruption. See b/17523189. However,
698 // adding that flag and start the preference panel with the same UserHandler will make it
699 // impossible to use back button to return to the previous screen. See b/20042570.
700 //
701 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
702 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
703 // when we're calling it as the same user.
704 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800705 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700706 } else {
707 String title = null;
708 if (titleRes < 0) {
709 if (titleText != null) {
710 title = titleText.toString();
711 } else {
712 // There not much we can do in that case
713 title = "";
714 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100715 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800716 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
717 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100718 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100719 }
720
721 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800722 * Called by a preference panel fragment to finish itself.
723 *
724 * @param caller The fragment that is asking to be finished.
725 * @param resultCode Optional result code to send back to the original
726 * launching fragment.
727 * @param resultData Optional result data to send back to the original
728 * launching fragment.
729 */
730 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
731 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700732 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 }
734
735 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000736 * Start a new fragment.
737 *
738 * @param fragment The fragment to start
739 * @param push If true, the current fragment will be pushed onto the back stack. If false,
740 * the current fragment will be replaced.
741 */
742 public void startPreferenceFragment(Fragment fragment, boolean push) {
743 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700744 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000745 if (push) {
746 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
747 transaction.addToBackStack(BACK_STACK_PREFS);
748 } else {
749 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
750 }
751 transaction.commitAllowingStateLoss();
752 }
753
754 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700755 * Switch to a specific Fragment with taking care of validation, Title and BackStack
756 */
757 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700758 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700759 if (validate && !isValidFragment(fragmentName)) {
760 throw new IllegalArgumentException("Invalid fragment for this activity: "
761 + fragmentName);
762 }
763 Fragment f = Fragment.instantiate(this, fragmentName, args);
764 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700765 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700766 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700767 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700768 }
769 if (addToBackStack) {
770 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
771 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700772 if (titleResId > 0) {
773 transaction.setBreadCrumbTitle(titleResId);
774 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700775 transaction.setBreadCrumbTitle(title);
776 }
777 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700778 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700779 return f;
780 }
781
Jason Monk4da79e02015-09-10 10:54:36 -0400782 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500783 // Generally the items that are will be changing from these updates will
784 // not be in the top list of tiles, so run it in the background and the
785 // SettingsDrawerActivity will pick up on the updates automatically.
786 AsyncTask.execute(new Runnable() {
787 @Override
788 public void run() {
789 doUpdateTilesList();
790 }
791 });
792 }
793
794 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400795 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700796 final UserManager um = UserManager.get(this);
797 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700798
Jason Monk4da79e02015-09-10 10:54:36 -0400799 String packageName = getPackageName();
800 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800801 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700802
Jason Monk4da79e02015-09-10 10:54:36 -0400803 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800804 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800805 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700806
Jason Monk4da79e02015-09-10 10:54:36 -0400807 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800809 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700810
Jason Monk4da79e02015-09-10 10:54:36 -0400811 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800812 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800813 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700814
Jason Monk4da79e02015-09-10 10:54:36 -0400815 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800816 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800817 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400818
819 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800820 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400821 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800822 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400823
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700824 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800825 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800826 !UserManager.isDeviceInDemoMode(this), isAdmin);
827
828 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800829 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800830 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700831
832 setTileEnabled(new ComponentName(packageName,
833 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800834 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400835 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
836 setTileEnabled(new ComponentName(packageName,
837 Settings.PaymentSettingsActivity.class.getName()),
838 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
839 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800840 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400841
842 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800843 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800844 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400845
846 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800847 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400848 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400849 setTileEnabled(new ComponentName(packageName,
850 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800851 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400852
Fan Zhanga6986e72017-03-08 09:24:45 -0800853 // Enable/disable backup settings depending on whether the user is admin.
854 setTileEnabled(new ComponentName(packageName,
Christine Franks2d029392017-05-26 18:23:38 -0700855 BackupSettingsActivity.class.getName()), true, isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800856
857 setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800858 Settings.WifiDisplaySettingsActivity.class.getName()),
859 WifiDisplaySettings.isAvailable(this), isAdmin);
860
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) {
876 setTileEnabled(component, false, isAdmin);
877 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700878 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700879 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700880 }
881 }
Doris Ling26bf0032016-06-15 18:16:09 -0700882
Fan Zhang224caad2017-01-06 11:42:48 -0800883 // Final step, refresh categories.
884 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500885 }
886
Fan Zhang224caad2017-01-06 11:42:48 -0800887 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400888 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800889 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
890 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400891 enabled = false;
892 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400893 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500894 }
895
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800896 private void getMetaData() {
897 try {
898 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
899 PackageManager.GET_META_DATA);
900 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800901 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
902 } catch (NameNotFoundException nnfe) {
903 // No recovery
904 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
905 }
906 }
907
908 // give subclasses access to the Next button
909 public boolean hasNextButton() {
910 return mNextButton != null;
911 }
912
913 public Button getNextButton() {
914 return mNextButton;
915 }
916
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800917 @Override
918 public boolean shouldUpRecreateTask(Intent targetIntent) {
919 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
920 }
921
Jason Monkd4f03ec2016-01-07 16:25:34 -0500922 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700923 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700924 return;
925 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700926 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500927 startActivityForResult(intent, REQUEST_SUGGESTION);
928 }
929
930 @Override
931 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
932 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
933 && resultCode != RESULT_CANCELED) {
934 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
935 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
936 }
937 super.onActivityResult(requestCode, resultCode, data);
938 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800939
940 @VisibleForTesting
941 Bitmap getBitmapFromXmlResource(int drawableRes) {
942 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
943 Canvas canvas = new Canvas();
944 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
945 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
946 canvas.setBitmap(bitmap);
947 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
948 drawable.draw(canvas);
949
950 return bitmap;
951 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700952
953 @Override
954 public void onClick(View v) {
955 Intent intent = new Intent(this, SearchActivity.class);
956 startActivity(intent);
957 }
958}