blob: 5e05329100433efba14975c36bba2fd405936414 [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 Zhangcabc5092017-03-14 12:38:43 -070063import com.android.settings.enterprise.EnterprisePrivacySettings;
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;
Matthew Fritze0ed37c32016-10-24 10:12:49 -070066import com.android.settings.search2.SearchFeatureProvider;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080067import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070068import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050071
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070074import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075
Jason Monk4da79e02015-09-10 10:54:36 -040076public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077 implements PreferenceManager.OnPreferenceTreeClickListener,
78 PreferenceFragment.OnPreferenceStartFragmentCallback,
Matthew Fritze0b431fc2017-03-16 13:19:20 -070079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080083 private static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
84
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070086 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070087 @VisibleForTesting
88 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
89 @VisibleForTesting
90 static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080091
92 /**
93 * When starting this activity, the invoking Intent can contain this extra
94 * string to specify which fragment should be initially displayed.
95 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
96 * will call isValidFragment() to confirm that the fragment class name is valid for this
97 * activity.
98 */
99 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
100
101 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800102 * The metrics category constant for logging source when a setting fragment is opened.
103 */
104 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
105
106 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800107 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
108 * this extra can also be specified to supply a Bundle of arguments to pass
109 * to that fragment when it is instantiated during the initial creation
110 * of the activity.
111 */
112 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
113
114 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700115 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
116 */
117 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
118
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800119 public static final String BACK_STACK_PREFS = ":settings:prefs";
120
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121 // extras that allow any preference activity to be launched as part of a wizard
122
123 // show Back and Next buttons? takes boolean parameter
124 // Back will then return RESULT_CANCELED and Next RESULT_OK
125 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
126
127 // add a Skip button?
128 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
129
130 // specify custom text for the Back or Next buttons, or cause a button to not appear
131 // at all by setting it to null
132 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
133 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
134
135 /**
136 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700137 * 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 -0800138 * that fragment.
139 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700140 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100141 /**
142 * The package name used to resolve the title resource id.
143 */
144 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
145 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700146 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
147 ":settings:show_fragment_title_resid";
148 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
149 ":settings:show_fragment_as_shortcut";
150
151 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
152 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153
Fan Zhang78ab9f92017-02-27 16:10:05 -0800154 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700155 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
156
Jason Monk30695812015-11-17 09:01:08 -0500157 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800158 "com.android.settings.FRAGMENT_CLASS";
159
160 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
161
Jason Monkd4f03ec2016-01-07 16:25:34 -0500162 private static final int REQUEST_SUGGESTION = 42;
163
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800166 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700167 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800168
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700169 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
170 "android.settings.APPLICATION_DETAILS_SETTINGS"
171 };
172
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 private SharedPreferences mDevelopmentPreferences;
174 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
175
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176 private boolean mBatteryPresent = true;
177 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800178 @Override
179 public void onReceive(Context context, Intent intent) {
180 String action = intent.getAction();
181 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
182 boolean batteryPresent = Utils.isBatteryPresent(intent);
183
184 if (mBatteryPresent != batteryPresent) {
185 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400186 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187 }
188 }
189 }
190 };
191
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800192 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700193
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700194 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700195 private SwitchBar mSwitchBar;
196
197 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700198
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700199 @VisibleForTesting
200 boolean mDisplayHomeAsUpEnabled;
201 @VisibleForTesting
202 boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700203
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700204 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700205 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700206
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700207 private ViewGroup mContent;
208
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700209 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800210 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700211
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700212 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800213 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800214
Fan Zhanga96a2d82016-09-27 17:51:11 -0700215 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500216 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700217
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700218 public SwitchBar getSwitchBar() {
219 return mSwitchBar;
220 }
221
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800222 @Override
223 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800224 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800225 null, 0);
226 return true;
227 }
228
229 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400230 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 return false;
232 }
233
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800234 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700235 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700236 if (!mDisplaySearch) {
237 return false;
238 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700239 mSearchFeatureProvider.setUpSearchMenu(menu, this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700240 return true;
241 }
242
Jason Monk31c7c322016-01-20 14:41:52 -0500243 @Override
244 public SharedPreferences getSharedPreferences(String name, int mode) {
245 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500246 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500247 }
248 return super.getSharedPreferences(name, mode);
249 }
250
Jason Monke4ebcd12016-02-21 09:37:41 -0500251 private String getMetricsTag() {
252 String tag = getClass().getName();
253 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
254 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
255 }
256 if (tag.startsWith("com.android.settings.")) {
257 tag = tag.replace("com.android.settings.", "");
258 }
259 return tag;
260 }
261
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700262 private static boolean isShortCutIntent(final Intent intent) {
263 Set<String> categories = intent.getCategories();
264 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
265 }
266
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700267 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700268 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700269 if (action == null) {
270 return false;
271 }
272 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
273 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
274 }
275 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700276 }
277
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700278 @Override
279 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700280 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500281 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700282
283 final FeatureFactory factory = FeatureFactory.getFactory(this);
284
285 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800286 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800287 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700288
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700289 // Should happen before any call to getIntent()
290 getMetaData();
291
292 final Intent intent = getIntent();
293 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
294 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800295 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
298 Context.MODE_PRIVATE);
299
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700300 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700301 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700302
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700303 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700304 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
305
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700306 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700307 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700308
Doris Ling1694d4b2017-03-03 14:21:37 -0800309 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700310
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700311 // This is a "Sub Settings" when:
312 // - this is a real SubSettings
313 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700314 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700315 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
316
Fan Zhang92f1e942017-01-21 10:07:26 -0800317 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
318 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700319 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800320 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700321 }
322
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700323 setContentView(mIsShowingDashboard ?
324 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700326 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700327
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800328 getFragmentManager().addOnBackStackChangedListener(this);
329
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700330 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700331 // We are restarting from a previous saved state; used that to initialize, instead
332 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700333 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800334
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700335 ArrayList<DashboardCategory> categories =
336 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
337 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700338 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700339 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700340 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700342
343 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700344
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800346 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 }
348
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700349 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700350 if (mActionBar != null) {
351 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
352 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
353 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700354 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500355 if (mSwitchBar != null) {
356 mSwitchBar.setMetricsTag(getMetricsTag());
357 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700358
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
361
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 if (buttonBar != null) {
364 buttonBar.setVisibility(View.VISIBLE);
365
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700366 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 backButton.setOnClickListener(new OnClickListener() {
368 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700369 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 finish();
371 }
372 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700373 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 skipButton.setOnClickListener(new OnClickListener() {
375 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700376 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800377 finish();
378 }
379 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700380 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800381 mNextButton.setOnClickListener(new OnClickListener() {
382 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700383 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800384 finish();
385 }
386 });
387
388 // set our various button parameters
389 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
390 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
391 if (TextUtils.isEmpty(buttonText)) {
392 mNextButton.setVisibility(View.GONE);
393 }
394 else {
395 mNextButton.setText(buttonText);
396 }
397 }
398 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
399 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
400 if (TextUtils.isEmpty(buttonText)) {
401 backButton.setVisibility(View.GONE);
402 }
403 else {
404 backButton.setText(buttonText);
405 }
406 }
407 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
408 skipButton.setVisibility(View.VISIBLE);
409 }
410 }
411 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700412
Fan Zhangefba6b42017-02-07 17:40:59 -0800413 if (DEBUG_TIMING) {
414 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
415 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700416 }
417
Doris Lingcb69baf2017-02-23 17:50:03 -0800418 @VisibleForTesting
419 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
420 if (!mIsShowingDashboard && initialFragmentName != null) {
421 mDisplaySearch = false;
422 // UP will be shown only if it is a sub settings
423 if (mIsShortcut) {
424 mDisplayHomeAsUpEnabled = isSubSettings;
425 } else if (isSubSettings) {
426 mDisplayHomeAsUpEnabled = true;
427 } else {
428 mDisplayHomeAsUpEnabled = false;
429 }
430 setTitleFromIntent(intent);
431
432 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
433 switchToFragment(initialFragmentName, initialArguments, true, false,
434 mInitialTitleResId, mInitialTitle, false);
435 } else {
436 // No UP affordance if we are displaying the main Dashboard
437 mDisplayHomeAsUpEnabled = false;
438 // Show Search affordance
439 mDisplaySearch = true;
440 mInitialTitleResId = R.string.dashboard_title;
441
442 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
443 mInitialTitleResId, mInitialTitle, false);
444 }
445 }
446
Fan Zhangece8ff62016-06-30 13:20:12 -0700447 public void setDisplaySearchMenu(boolean displaySearch) {
448 if (displaySearch != mDisplaySearch) {
449 mDisplaySearch = displaySearch;
450 invalidateOptionsMenu();
451 }
452 }
453
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700454 private void setTitleFromIntent(Intent intent) {
455 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
456 if (initialTitleResId > 0) {
457 mInitialTitle = null;
458 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100459
460 final String initialTitleResPackageName = intent.getStringExtra(
461 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
462 if (initialTitleResPackageName != null) {
463 try {
464 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
465 0 /* flags */, new UserHandle(UserHandle.myUserId()));
466 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
467 setTitle(mInitialTitle);
468 mInitialTitleResId = -1;
469 return;
470 } catch (NameNotFoundException e) {
471 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
472 }
473 } else {
474 setTitle(mInitialTitleResId);
475 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700476 } else {
477 mInitialTitleResId = -1;
478 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
479 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
480 setTitle(mInitialTitle);
481 }
482 }
483
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800484 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800485 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700486 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800487 }
488
Fan Zhang28691572016-03-23 15:31:08 -0700489 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800490 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700491
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800492 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700493 if (mInitialTitleResId > 0) {
494 setTitle(mInitialTitleResId);
495 } else {
496 setTitle(mInitialTitle);
497 }
Fan Zhang28691572016-03-23 15:31:08 -0700498 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800499 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700500
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800501 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
502 setTitleFromBackStackEntry(bse);
503 }
504
505 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
506 final CharSequence title;
507 final int titleRes = bse.getBreadCrumbTitleRes();
508 if (titleRes > 0) {
509 title = getText(titleRes);
510 } else {
511 title = bse.getBreadCrumbTitle();
512 }
513 if (title != null) {
514 setTitle(title);
515 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800516 }
517
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518 @Override
519 protected void onSaveInstanceState(Bundle outState) {
520 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700521 saveState(outState);
522 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800523
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700524 /**
525 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
526 */
527 @VisibleForTesting
528 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700529 if (mCategories.size() > 0) {
530 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700532
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700533 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700534 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
535 }
536
537 @Override
538 protected void onRestoreInstanceState(Bundle savedInstanceState) {
539 super.onRestoreInstanceState(savedInstanceState);
540
541 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
542 mDisplaySearch = savedInstanceState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 }
544
545 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400546 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 super.onResume();
548
549 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
550 @Override
551 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400552 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 }
554 };
555 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
556 mDevelopmentPreferencesListener);
557
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800559 if (mDynamicIndexableContentMonitor == null) {
560 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
561 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800562 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700563
Jason Monk4da79e02015-09-10 10:54:36 -0400564 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565 }
566
567 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400568 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800569 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800570 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
571 mDevelopmentPreferencesListener);
572 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800574 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900575 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800576 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 }
578
579 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800580 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
581 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
582 taskDescription.setIcon(icon);
583 super.setTaskDescription(taskDescription);
584 }
585
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800586 protected boolean isValidFragment(String fragmentName) {
587 // Almost all fragments are wrapped in this,
588 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800589 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
590 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800591 }
592 return false;
593 }
594
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 @Override
596 public Intent getIntent() {
597 Intent superIntent = super.getIntent();
598 String startingFragment = getStartingFragmentClass(superIntent);
599 // This is called from super.onCreate, isMultiPane() is not yet reliable
600 // Do not use onIsHidingHeaders either, which relies itself on this method
601 if (startingFragment != null) {
602 Intent modIntent = new Intent(superIntent);
603 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
604 Bundle args = superIntent.getExtras();
605 if (args != null) {
606 args = new Bundle(args);
607 } else {
608 args = new Bundle();
609 }
610 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100611 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 return modIntent;
613 }
614 return superIntent;
615 }
616
617 /**
618 * Checks if the component name in the intent is different from the Settings class and
619 * returns the class name to load as a fragment.
620 */
621 private String getStartingFragmentClass(Intent intent) {
622 if (mFragmentClass != null) return mFragmentClass;
623
624 String intentClass = intent.getComponent().getClassName();
625 if (intentClass.equals(getClass().getName())) return null;
626
627 if ("com.android.settings.ManageApplications".equals(intentClass)
628 || "com.android.settings.RunningServices".equals(intentClass)
629 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
630 // Old names of manage apps.
631 intentClass = com.android.settings.applications.ManageApplications.class.getName();
632 }
633
634 return intentClass;
635 }
636
637 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000638 * Start a new fragment containing a preference panel. If the preferences
639 * are being displayed in multi-pane mode, the given fragment class will
640 * be instantiated and placed in the appropriate pane. If running in
641 * single-pane mode, a new activity will be launched in which to show the
642 * fragment.
643 *
644 * @param fragmentClass Full name of the class implementing the fragment.
645 * @param args Any desired arguments to supply to the fragment.
646 * @param titleRes Optional resource identifier of the title of this
647 * fragment.
648 * @param titleText Optional text of the title of this fragment.
649 * @param resultTo Optional fragment that result data should be sent to.
650 * If non-null, resultTo.onActivityResult() will be called when this
651 * preference panel is done. The launched panel must use
652 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
653 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700654 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000655 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800656 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
657 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700658 String title = null;
659 if (titleRes < 0) {
660 if (titleText != null) {
661 title = titleText.toString();
662 } else {
663 // There not much we can do in that case
664 title = "";
665 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700666 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700667 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800668 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000669 }
670
671 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100672 * Start a new fragment in a new activity containing a preference panel for a given user. If the
673 * preferences are being displayed in multi-pane mode, the given fragment class will be
674 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
675 * activity will be launched in which to show the fragment.
676 *
677 * @param fragmentClass Full name of the class implementing the fragment.
678 * @param args Any desired arguments to supply to the fragment.
679 * @param titleRes Optional resource identifier of the title of this fragment.
680 * @param titleText Optional text of the title of this fragment.
681 * @param userHandle The user for which the panel has to be started.
682 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800683 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
684 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700685 // This is a workaround.
686 //
687 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
688 // starting the fragment could cause a native stack corruption. See b/17523189. However,
689 // adding that flag and start the preference panel with the same UserHandler will make it
690 // impossible to use back button to return to the previous screen. See b/20042570.
691 //
692 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
693 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
694 // when we're calling it as the same user.
695 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800696 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700697 } else {
698 String title = null;
699 if (titleRes < 0) {
700 if (titleText != null) {
701 title = titleText.toString();
702 } else {
703 // There not much we can do in that case
704 title = "";
705 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100706 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800707 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
708 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100709 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100710 }
711
712 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800713 * Called by a preference panel fragment to finish itself.
714 *
715 * @param caller The fragment that is asking to be finished.
716 * @param resultCode Optional result code to send back to the original
717 * launching fragment.
718 * @param resultData Optional result data to send back to the original
719 * launching fragment.
720 */
721 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
722 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700723 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 }
725
726 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000727 * Start a new fragment.
728 *
729 * @param fragment The fragment to start
730 * @param push If true, the current fragment will be pushed onto the back stack. If false,
731 * the current fragment will be replaced.
732 */
733 public void startPreferenceFragment(Fragment fragment, boolean push) {
734 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700735 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000736 if (push) {
737 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
738 transaction.addToBackStack(BACK_STACK_PREFS);
739 } else {
740 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
741 }
742 transaction.commitAllowingStateLoss();
743 }
744
745 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700746 * Switch to a specific Fragment with taking care of validation, Title and BackStack
747 */
748 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700749 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700750 if (validate && !isValidFragment(fragmentName)) {
751 throw new IllegalArgumentException("Invalid fragment for this activity: "
752 + fragmentName);
753 }
754 Fragment f = Fragment.instantiate(this, fragmentName, args);
755 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700756 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700757 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700758 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700759 }
760 if (addToBackStack) {
761 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
762 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700763 if (titleResId > 0) {
764 transaction.setBreadCrumbTitle(titleResId);
765 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700766 transaction.setBreadCrumbTitle(title);
767 }
768 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700769 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700770 return f;
771 }
772
Jason Monk4da79e02015-09-10 10:54:36 -0400773 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500774 // Generally the items that are will be changing from these updates will
775 // not be in the top list of tiles, so run it in the background and the
776 // SettingsDrawerActivity will pick up on the updates automatically.
777 AsyncTask.execute(new Runnable() {
778 @Override
779 public void run() {
780 doUpdateTilesList();
781 }
782 });
783 }
784
785 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400786 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700787 final UserManager um = UserManager.get(this);
788 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700789
Jason Monk4da79e02015-09-10 10:54:36 -0400790 String packageName = getPackageName();
791 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800792 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700793
Jason Monk4da79e02015-09-10 10:54:36 -0400794 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800795 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800796 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700797
Jason Monk4da79e02015-09-10 10:54:36 -0400798 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800799 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800800 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700801
Jason Monk4da79e02015-09-10 10:54:36 -0400802 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800803 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800804 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700805
Jason Monk4da79e02015-09-10 10:54:36 -0400806 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800807 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800808 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400809
810 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800811 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400812 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400814
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700815 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800816 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800817 !UserManager.isDeviceInDemoMode(this), isAdmin);
818
819 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800820 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800821 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700822
823 setTileEnabled(new ComponentName(packageName,
824 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800825 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400826 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
827 setTileEnabled(new ComponentName(packageName,
828 Settings.PaymentSettingsActivity.class.getName()),
829 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
830 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800831 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400832
833 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800834 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800835 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400836
837 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400839 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400840 setTileEnabled(new ComponentName(packageName,
841 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800842 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400843
Fan Zhanga6986e72017-03-08 09:24:45 -0800844 // Enable/disable backup settings depending on whether the user is admin.
845 setTileEnabled(new ComponentName(packageName,
846 BackupSettingsActivity.class.getName()), true,
847 isAdmin || Utils.isCarrierDemoUser(this));
848
849 setTileEnabled(new ComponentName(packageName,
850 Settings.EnterprisePrivacySettingsActivity.class.getName()),
Fan Zhangcabc5092017-03-14 12:38:43 -0700851 EnterprisePrivacySettings.isPageEnabled(this), isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800852
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800853 setTileEnabled(new ComponentName(packageName,
854 Settings.WifiDisplaySettingsActivity.class.getName()),
855 WifiDisplaySettings.isAvailable(this), isAdmin);
856
Jason Monk4da79e02015-09-10 10:54:36 -0400857 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800858
Jason Monk4da79e02015-09-10 10:54:36 -0400859 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800860 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800861 synchronized (categories) {
862 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700863 final int tileCount = category.getTilesCount();
864 for (int i = 0; i < tileCount; i++) {
865 final ComponentName component = category.getTile(i).intent.getComponent();
866
Fan Zhanga6986e72017-03-08 09:24:45 -0800867 final String name = component.getClassName();
868 final boolean isEnabledForRestricted = ArrayUtils.contains(
869 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
870 if (packageName.equals(component.getPackageName())
871 && !isEnabledForRestricted) {
872 setTileEnabled(component, false, isAdmin);
873 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700874 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700875 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700876 }
877 }
Doris Ling26bf0032016-06-15 18:16:09 -0700878
Fan Zhang224caad2017-01-06 11:42:48 -0800879 // Final step, refresh categories.
880 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500881 }
882
Fan Zhang224caad2017-01-06 11:42:48 -0800883 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400884 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800885 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
886 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400887 enabled = false;
888 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400889 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500890 }
891
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800892 private void getMetaData() {
893 try {
894 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
895 PackageManager.GET_META_DATA);
896 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800897 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
898 } catch (NameNotFoundException nnfe) {
899 // No recovery
900 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
901 }
902 }
903
904 // give subclasses access to the Next button
905 public boolean hasNextButton() {
906 return mNextButton != null;
907 }
908
909 public Button getNextButton() {
910 return mNextButton;
911 }
912
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800913 @Override
914 public boolean shouldUpRecreateTask(Intent targetIntent) {
915 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
916 }
917
Jason Monkd4f03ec2016-01-07 16:25:34 -0500918 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700919 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700920 return;
921 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700922 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500923 startActivityForResult(intent, REQUEST_SUGGESTION);
924 }
925
926 @Override
927 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
928 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
929 && resultCode != RESULT_CANCELED) {
930 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
931 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
932 }
933 super.onActivityResult(requestCode, resultCode, data);
934 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800935
936 @VisibleForTesting
937 Bitmap getBitmapFromXmlResource(int drawableRes) {
938 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
939 Canvas canvas = new Canvas();
940 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
941 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
942 canvas.setBitmap(bitmap);
943 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
944 drawable.draw(canvas);
945
946 return bitmap;
947 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700948}