blob: 4045fd2afdd42df141cc662425703b3e470523f7 [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;
Fan Zhang28691572016-03-23 15:31:08 -070053
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040055import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000056import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080057import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080058import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070059import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070060import com.android.settings.dashboard.DashboardFeatureProvider;
61import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070062import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050064import com.android.settings.search.DynamicIndexableContentMonitor;
Matthew Fritze0ed37c32016-10-24 10:12:49 -070065import com.android.settings.search2.SearchFeatureProvider;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080066import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070067import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050070
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070073import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074
Jason Monk4da79e02015-09-10 10:54:36 -040075public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076 implements PreferenceManager.OnPreferenceTreeClickListener,
77 PreferenceFragment.OnPreferenceStartFragmentCallback,
Matthew Fritze0b431fc2017-03-16 13:19:20 -070078 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079
80 private static final String LOG_TAG = "Settings";
81
Matthew Fritzeab0b3f62017-04-25 09:38:56 -070082 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080083
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070085 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070086 @VisibleForTesting
87 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
88 @VisibleForTesting
89 static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090
91 /**
92 * When starting this activity, the invoking Intent can contain this extra
93 * string to specify which fragment should be initially displayed.
94 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
95 * will call isValidFragment() to confirm that the fragment class name is valid for this
96 * activity.
97 */
98 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
99
100 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800101 * The metrics category constant for logging source when a setting fragment is opened.
102 */
103 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
104
105 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800106 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
107 * this extra can also be specified to supply a Bundle of arguments to pass
108 * to that fragment when it is instantiated during the initial creation
109 * of the activity.
110 */
111 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
112
113 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700114 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
115 */
116 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
117
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800118 public static final String BACK_STACK_PREFS = ":settings:prefs";
119
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120 // extras that allow any preference activity to be launched as part of a wizard
121
122 // show Back and Next buttons? takes boolean parameter
123 // Back will then return RESULT_CANCELED and Next RESULT_OK
124 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
125
126 // add a Skip button?
127 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
128
129 // specify custom text for the Back or Next buttons, or cause a button to not appear
130 // at all by setting it to null
131 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
132 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
133
134 /**
135 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700136 * 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 -0800137 * that fragment.
138 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100140 /**
141 * The package name used to resolve the title resource id.
142 */
143 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
144 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700145 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
146 ":settings:show_fragment_title_resid";
147 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
148 ":settings:show_fragment_as_shortcut";
149
150 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
151 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
Fan Zhang78ab9f92017-02-27 16:10:05 -0800153 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700154 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
155
Jason Monk30695812015-11-17 09:01:08 -0500156 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800157 "com.android.settings.FRAGMENT_CLASS";
158
159 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
160
Jason Monkd4f03ec2016-01-07 16:25:34 -0500161 private static final int REQUEST_SUGGESTION = 42;
162
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800165 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700166 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800167
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700168 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
169 "android.settings.APPLICATION_DETAILS_SETTINGS"
170 };
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 private SharedPreferences mDevelopmentPreferences;
173 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
174
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 private boolean mBatteryPresent = true;
176 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 @Override
178 public void onReceive(Context context, Intent intent) {
179 String action = intent.getAction();
180 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
181 boolean batteryPresent = Utils.isBatteryPresent(intent);
182
183 if (mBatteryPresent != batteryPresent) {
184 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400185 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186 }
187 }
188 }
189 };
190
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800191 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700192
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700193 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700194 private SwitchBar mSwitchBar;
195
196 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700197
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700198 @VisibleForTesting
199 boolean mDisplayHomeAsUpEnabled;
200 @VisibleForTesting
201 boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700202
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700203 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700204 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700205
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700206 private ViewGroup mContent;
207
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700208 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800209 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700210
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700211 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800212 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213
Fan Zhanga96a2d82016-09-27 17:51:11 -0700214 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500215 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700216
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700217 public SwitchBar getSwitchBar() {
218 return mSwitchBar;
219 }
220
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 @Override
222 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800223 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 null, 0);
225 return true;
226 }
227
228 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400229 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800230 return false;
231 }
232
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800233 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700234 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700235 if (!mDisplaySearch) {
236 return false;
237 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700238 mSearchFeatureProvider.setUpSearchMenu(menu, this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700239 return true;
240 }
241
Jason Monk31c7c322016-01-20 14:41:52 -0500242 @Override
243 public SharedPreferences getSharedPreferences(String name, int mode) {
244 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500245 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500246 }
247 return super.getSharedPreferences(name, mode);
248 }
249
Jason Monke4ebcd12016-02-21 09:37:41 -0500250 private String getMetricsTag() {
251 String tag = getClass().getName();
252 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
253 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
254 }
255 if (tag.startsWith("com.android.settings.")) {
256 tag = tag.replace("com.android.settings.", "");
257 }
258 return tag;
259 }
260
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700261 private static boolean isShortCutIntent(final Intent intent) {
262 Set<String> categories = intent.getCategories();
263 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
264 }
265
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700266 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700267 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700268 if (action == null) {
269 return false;
270 }
271 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
272 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
273 }
274 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700275 }
276
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700277 @Override
278 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700279 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500280 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700281
282 final FeatureFactory factory = FeatureFactory.getFactory(this);
283
284 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800285 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800286 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700287
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700288 // Should happen before any call to getIntent()
289 getMetaData();
290
291 final Intent intent = getIntent();
292 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
293 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800295
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
297 Context.MODE_PRIVATE);
298
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700299 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700300 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700301
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700302 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700303 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
304
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700305 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700306 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700307
Doris Ling1694d4b2017-03-03 14:21:37 -0800308 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700309
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700310 // This is a "Sub Settings" when:
311 // - this is a real SubSettings
312 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700313 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700314 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
315
Fan Zhang92f1e942017-01-21 10:07:26 -0800316 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
317 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700318 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800319 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700320 }
321
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700322 setContentView(mIsShowingDashboard ?
323 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700325 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700326
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 getFragmentManager().addOnBackStackChangedListener(this);
328
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700329 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700330 // We are restarting from a previous saved state; used that to initialize, instead
331 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700332 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800333
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700334 ArrayList<DashboardCategory> categories =
335 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
336 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700337 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700338 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700339 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700341
342 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700343
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800344 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800345 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 }
347
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700348 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700349 if (mActionBar != null) {
350 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
351 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
352 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700353 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500354 if (mSwitchBar != null) {
355 mSwitchBar.setMetricsTag(getMetricsTag());
356 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700357
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
360
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700361 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 if (buttonBar != null) {
363 buttonBar.setVisibility(View.VISIBLE);
364
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700365 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 backButton.setOnClickListener(new OnClickListener() {
367 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700368 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 finish();
370 }
371 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700372 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 skipButton.setOnClickListener(new OnClickListener() {
374 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700375 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 finish();
377 }
378 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700379 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800380 mNextButton.setOnClickListener(new OnClickListener() {
381 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700382 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800383 finish();
384 }
385 });
386
387 // set our various button parameters
388 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
389 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
390 if (TextUtils.isEmpty(buttonText)) {
391 mNextButton.setVisibility(View.GONE);
392 }
393 else {
394 mNextButton.setText(buttonText);
395 }
396 }
397 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
398 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
399 if (TextUtils.isEmpty(buttonText)) {
400 backButton.setVisibility(View.GONE);
401 }
402 else {
403 backButton.setText(buttonText);
404 }
405 }
406 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
407 skipButton.setVisibility(View.VISIBLE);
408 }
409 }
410 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700411
Fan Zhangefba6b42017-02-07 17:40:59 -0800412 if (DEBUG_TIMING) {
413 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
414 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700415 }
416
Doris Lingcb69baf2017-02-23 17:50:03 -0800417 @VisibleForTesting
418 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
419 if (!mIsShowingDashboard && initialFragmentName != null) {
420 mDisplaySearch = false;
421 // UP will be shown only if it is a sub settings
422 if (mIsShortcut) {
423 mDisplayHomeAsUpEnabled = isSubSettings;
424 } else if (isSubSettings) {
425 mDisplayHomeAsUpEnabled = true;
426 } else {
427 mDisplayHomeAsUpEnabled = false;
428 }
429 setTitleFromIntent(intent);
430
431 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
432 switchToFragment(initialFragmentName, initialArguments, true, false,
433 mInitialTitleResId, mInitialTitle, false);
434 } else {
435 // No UP affordance if we are displaying the main Dashboard
436 mDisplayHomeAsUpEnabled = false;
437 // Show Search affordance
438 mDisplaySearch = true;
439 mInitialTitleResId = R.string.dashboard_title;
440
441 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
442 mInitialTitleResId, mInitialTitle, false);
443 }
444 }
445
Fan Zhangece8ff62016-06-30 13:20:12 -0700446 public void setDisplaySearchMenu(boolean displaySearch) {
447 if (displaySearch != mDisplaySearch) {
448 mDisplaySearch = displaySearch;
449 invalidateOptionsMenu();
450 }
451 }
452
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700453 private void setTitleFromIntent(Intent intent) {
454 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
455 if (initialTitleResId > 0) {
456 mInitialTitle = null;
457 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100458
459 final String initialTitleResPackageName = intent.getStringExtra(
460 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
461 if (initialTitleResPackageName != null) {
462 try {
463 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
464 0 /* flags */, new UserHandle(UserHandle.myUserId()));
465 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
466 setTitle(mInitialTitle);
467 mInitialTitleResId = -1;
468 return;
469 } catch (NameNotFoundException e) {
470 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
471 }
472 } else {
473 setTitle(mInitialTitleResId);
474 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700475 } else {
476 mInitialTitleResId = -1;
477 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
478 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
479 setTitle(mInitialTitle);
480 }
481 }
482
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800483 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800484 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700485 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800486 }
487
Fan Zhang28691572016-03-23 15:31:08 -0700488 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800489 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700490
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800491 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700492 if (mInitialTitleResId > 0) {
493 setTitle(mInitialTitleResId);
494 } else {
495 setTitle(mInitialTitle);
496 }
Fan Zhang28691572016-03-23 15:31:08 -0700497 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800498 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700499
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800500 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
501 setTitleFromBackStackEntry(bse);
502 }
503
504 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
505 final CharSequence title;
506 final int titleRes = bse.getBreadCrumbTitleRes();
507 if (titleRes > 0) {
508 title = getText(titleRes);
509 } else {
510 title = bse.getBreadCrumbTitle();
511 }
512 if (title != null) {
513 setTitle(title);
514 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800515 }
516
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517 @Override
518 protected void onSaveInstanceState(Bundle outState) {
519 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700520 saveState(outState);
521 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800522
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700523 /**
524 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
525 */
526 @VisibleForTesting
527 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700528 if (mCategories.size() > 0) {
529 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700531
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700532 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700533 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
534 }
535
536 @Override
537 protected void onRestoreInstanceState(Bundle savedInstanceState) {
538 super.onRestoreInstanceState(savedInstanceState);
539
540 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
541 mDisplaySearch = savedInstanceState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 }
543
544 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400545 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 super.onResume();
547
548 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
549 @Override
550 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400551 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 }
553 };
554 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
555 mDevelopmentPreferencesListener);
556
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800557 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800558 if (mDynamicIndexableContentMonitor == null) {
559 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
560 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800561 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700562
Jason Monk4da79e02015-09-10 10:54:36 -0400563 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 }
565
566 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400567 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800569 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
570 mDevelopmentPreferencesListener);
571 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800573 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900574 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800575 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 }
577
578 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800579 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
580 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
581 taskDescription.setIcon(icon);
582 super.setTaskDescription(taskDescription);
583 }
584
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 protected boolean isValidFragment(String fragmentName) {
586 // Almost all fragments are wrapped in this,
587 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800588 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
589 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590 }
591 return false;
592 }
593
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 @Override
595 public Intent getIntent() {
596 Intent superIntent = super.getIntent();
597 String startingFragment = getStartingFragmentClass(superIntent);
598 // This is called from super.onCreate, isMultiPane() is not yet reliable
599 // Do not use onIsHidingHeaders either, which relies itself on this method
600 if (startingFragment != null) {
601 Intent modIntent = new Intent(superIntent);
602 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
603 Bundle args = superIntent.getExtras();
604 if (args != null) {
605 args = new Bundle(args);
606 } else {
607 args = new Bundle();
608 }
609 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100610 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800611 return modIntent;
612 }
613 return superIntent;
614 }
615
616 /**
617 * Checks if the component name in the intent is different from the Settings class and
618 * returns the class name to load as a fragment.
619 */
620 private String getStartingFragmentClass(Intent intent) {
621 if (mFragmentClass != null) return mFragmentClass;
622
623 String intentClass = intent.getComponent().getClassName();
624 if (intentClass.equals(getClass().getName())) return null;
625
626 if ("com.android.settings.ManageApplications".equals(intentClass)
627 || "com.android.settings.RunningServices".equals(intentClass)
628 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
629 // Old names of manage apps.
630 intentClass = com.android.settings.applications.ManageApplications.class.getName();
631 }
632
633 return intentClass;
634 }
635
636 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000637 * Start a new fragment containing a preference panel. If the preferences
638 * are being displayed in multi-pane mode, the given fragment class will
639 * be instantiated and placed in the appropriate pane. If running in
640 * single-pane mode, a new activity will be launched in which to show the
641 * fragment.
642 *
643 * @param fragmentClass Full name of the class implementing the fragment.
644 * @param args Any desired arguments to supply to the fragment.
645 * @param titleRes Optional resource identifier of the title of this
646 * fragment.
647 * @param titleText Optional text of the title of this fragment.
648 * @param resultTo Optional fragment that result data should be sent to.
649 * If non-null, resultTo.onActivityResult() will be called when this
650 * preference panel is done. The launched panel must use
651 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
652 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700653 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000654 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800655 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
656 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700657 String title = null;
658 if (titleRes < 0) {
659 if (titleText != null) {
660 title = titleText.toString();
661 } else {
662 // There not much we can do in that case
663 title = "";
664 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700665 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700666 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800667 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000668 }
669
670 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100671 * Start a new fragment in a new activity containing a preference panel for a given user. If the
672 * preferences are being displayed in multi-pane mode, the given fragment class will be
673 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
674 * activity will be launched in which to show the fragment.
675 *
676 * @param fragmentClass Full name of the class implementing the fragment.
677 * @param args Any desired arguments to supply to the fragment.
678 * @param titleRes Optional resource identifier of the title of this fragment.
679 * @param titleText Optional text of the title of this fragment.
680 * @param userHandle The user for which the panel has to be started.
681 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800682 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
683 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700684 // This is a workaround.
685 //
686 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
687 // starting the fragment could cause a native stack corruption. See b/17523189. However,
688 // adding that flag and start the preference panel with the same UserHandler will make it
689 // impossible to use back button to return to the previous screen. See b/20042570.
690 //
691 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
692 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
693 // when we're calling it as the same user.
694 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800695 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700696 } else {
697 String title = null;
698 if (titleRes < 0) {
699 if (titleText != null) {
700 title = titleText.toString();
701 } else {
702 // There not much we can do in that case
703 title = "";
704 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100705 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800706 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
707 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100708 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100709 }
710
711 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800712 * Called by a preference panel fragment to finish itself.
713 *
714 * @param caller The fragment that is asking to be finished.
715 * @param resultCode Optional result code to send back to the original
716 * launching fragment.
717 * @param resultData Optional result data to send back to the original
718 * launching fragment.
719 */
720 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
721 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700722 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800723 }
724
725 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000726 * Start a new fragment.
727 *
728 * @param fragment The fragment to start
729 * @param push If true, the current fragment will be pushed onto the back stack. If false,
730 * the current fragment will be replaced.
731 */
732 public void startPreferenceFragment(Fragment fragment, boolean push) {
733 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700734 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000735 if (push) {
736 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
737 transaction.addToBackStack(BACK_STACK_PREFS);
738 } else {
739 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
740 }
741 transaction.commitAllowingStateLoss();
742 }
743
744 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700745 * Switch to a specific Fragment with taking care of validation, Title and BackStack
746 */
747 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700748 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700749 if (validate && !isValidFragment(fragmentName)) {
750 throw new IllegalArgumentException("Invalid fragment for this activity: "
751 + fragmentName);
752 }
753 Fragment f = Fragment.instantiate(this, fragmentName, args);
754 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700755 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700756 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700757 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700758 }
759 if (addToBackStack) {
760 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
761 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700762 if (titleResId > 0) {
763 transaction.setBreadCrumbTitle(titleResId);
764 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700765 transaction.setBreadCrumbTitle(title);
766 }
767 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700768 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700769 return f;
770 }
771
Jason Monk4da79e02015-09-10 10:54:36 -0400772 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500773 // Generally the items that are will be changing from these updates will
774 // not be in the top list of tiles, so run it in the background and the
775 // SettingsDrawerActivity will pick up on the updates automatically.
776 AsyncTask.execute(new Runnable() {
777 @Override
778 public void run() {
779 doUpdateTilesList();
780 }
781 });
782 }
783
784 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400785 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700786 final UserManager um = UserManager.get(this);
787 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700788
Jason Monk4da79e02015-09-10 10:54:36 -0400789 String packageName = getPackageName();
790 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800791 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700792
Jason Monk4da79e02015-09-10 10:54:36 -0400793 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800794 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800795 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700796
Jason Monk4da79e02015-09-10 10:54:36 -0400797 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800798 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800799 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700800
Jason Monk4da79e02015-09-10 10:54:36 -0400801 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800802 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800803 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700804
Jason Monk4da79e02015-09-10 10:54:36 -0400805 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800806 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800807 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400808
809 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800810 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400811 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800812 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400813
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700814 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800815 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800816 !UserManager.isDeviceInDemoMode(this), isAdmin);
817
818 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800819 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800820 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700821
822 setTileEnabled(new ComponentName(packageName,
823 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800824 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400825 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
826 setTileEnabled(new ComponentName(packageName,
827 Settings.PaymentSettingsActivity.class.getName()),
828 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
829 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800830 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400831
832 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800833 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800834 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400835
836 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800837 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400838 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400839 setTileEnabled(new ComponentName(packageName,
840 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800841 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400842
Fan Zhanga6986e72017-03-08 09:24:45 -0800843 // Enable/disable backup settings depending on whether the user is admin.
844 setTileEnabled(new ComponentName(packageName,
Christine Franks2d029392017-05-26 18:23:38 -0700845 BackupSettingsActivity.class.getName()), true, isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800846
847 setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800848 Settings.WifiDisplaySettingsActivity.class.getName()),
849 WifiDisplaySettings.isAvailable(this), isAdmin);
850
Jason Monk4da79e02015-09-10 10:54:36 -0400851 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800852
Jason Monk4da79e02015-09-10 10:54:36 -0400853 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800854 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800855 synchronized (categories) {
856 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700857 final int tileCount = category.getTilesCount();
858 for (int i = 0; i < tileCount; i++) {
859 final ComponentName component = category.getTile(i).intent.getComponent();
860
Fan Zhanga6986e72017-03-08 09:24:45 -0800861 final String name = component.getClassName();
862 final boolean isEnabledForRestricted = ArrayUtils.contains(
863 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
864 if (packageName.equals(component.getPackageName())
865 && !isEnabledForRestricted) {
866 setTileEnabled(component, false, isAdmin);
867 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700868 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700869 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700870 }
871 }
Doris Ling26bf0032016-06-15 18:16:09 -0700872
Fan Zhang224caad2017-01-06 11:42:48 -0800873 // Final step, refresh categories.
874 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500875 }
876
Fan Zhang224caad2017-01-06 11:42:48 -0800877 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400878 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800879 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
880 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400881 enabled = false;
882 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400883 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500884 }
885
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800886 private void getMetaData() {
887 try {
888 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
889 PackageManager.GET_META_DATA);
890 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800891 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
892 } catch (NameNotFoundException nnfe) {
893 // No recovery
894 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
895 }
896 }
897
898 // give subclasses access to the Next button
899 public boolean hasNextButton() {
900 return mNextButton != null;
901 }
902
903 public Button getNextButton() {
904 return mNextButton;
905 }
906
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800907 @Override
908 public boolean shouldUpRecreateTask(Intent targetIntent) {
909 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
910 }
911
Jason Monkd4f03ec2016-01-07 16:25:34 -0500912 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700913 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700914 return;
915 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700916 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500917 startActivityForResult(intent, REQUEST_SUGGESTION);
918 }
919
920 @Override
921 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
922 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
923 && resultCode != RESULT_CANCELED) {
924 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
925 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
926 }
927 super.onActivityResult(requestCode, resultCode, data);
928 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800929
930 @VisibleForTesting
931 Bitmap getBitmapFromXmlResource(int drawableRes) {
932 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
933 Canvas canvas = new Canvas();
934 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
935 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
936 canvas.setBitmap(bitmap);
937 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
938 drawable.draw(canvas);
939
940 return bitmap;
941 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700942}