blob: 01818e993137b86beeef22a0ea8b33ca26a3553b [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 Zhang6013a952018-02-06 11:36:07 -080019import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20
Fan Zhang60f01072016-10-13 16:53:41 -070021import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070022import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080023import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080035import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080036import android.graphics.Canvas;
37import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050038import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.os.UserHandle;
41import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080042import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070044import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040045import android.support.v7.preference.Preference;
46import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070048import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.view.View;
51import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070052import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070054import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070055
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080056import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040057import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070058import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000059import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080060import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080064import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070065import com.android.settings.widget.SwitchBar;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080066import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
67import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070068import com.android.settingslib.development.DevelopmentSettingsEnabler;
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;
Fan Zhang4c26da92017-09-08 15:29:54 -070071
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,
Fan Zhangff4da232017-11-09 14:07:23 -080079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
83 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070084 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070085 @VisibleForTesting
86 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087
88 /**
89 * When starting this activity, the invoking Intent can contain this extra
90 * string to specify which fragment should be initially displayed.
91 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
92 * will call isValidFragment() to confirm that the fragment class name is valid for this
93 * activity.
94 */
95 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
96
97 /**
98 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
99 * this extra can also be specified to supply a Bundle of arguments to pass
100 * to that fragment when it is instantiated during the initial creation
101 * of the activity.
102 */
103 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
104
105 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700106 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
107 */
108 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
109
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800110 public static final String BACK_STACK_PREFS = ":settings:prefs";
111
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112 // extras that allow any preference activity to be launched as part of a wizard
113
114 // show Back and Next buttons? takes boolean parameter
115 // Back will then return RESULT_CANCELED and Next RESULT_OK
116 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
117
118 // add a Skip button?
119 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
120
121 // specify custom text for the Back or Next buttons, or cause a button to not appear
122 // at all by setting it to null
123 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
124 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
125
126 /**
127 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700128 * 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 -0800129 * that fragment.
130 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700131 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100132 /**
133 * The package name used to resolve the title resource id.
134 */
135 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
136 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700137 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
138 ":settings:show_fragment_title_resid";
139 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
140 ":settings:show_fragment_as_shortcut";
141
142 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
143 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800144
Fan Zhang78ab9f92017-02-27 16:10:05 -0800145 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700146 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
147
Jason Monk30695812015-11-17 09:01:08 -0500148 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149 "com.android.settings.FRAGMENT_CLASS";
150
151 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
152
Jason Monkd4f03ec2016-01-07 16:25:34 -0500153 private static final int REQUEST_SUGGESTION = 42;
154
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800156
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800157 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700158 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800159
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700160 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
161 "android.settings.APPLICATION_DETAILS_SETTINGS"
162 };
163
Tony Mantler3d84d562017-07-31 10:48:55 -0700164 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 private boolean mBatteryPresent = true;
167 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 @Override
169 public void onReceive(Context context, Intent intent) {
170 String action = intent.getAction();
171 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
172 boolean batteryPresent = Utils.isBatteryPresent(intent);
173
174 if (mBatteryPresent != batteryPresent) {
175 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400176 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 }
178 }
179 }
180 };
181
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700182 private SwitchBar mSwitchBar;
183
184 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700185
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700186 @VisibleForTesting
187 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700188
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700189 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700190 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700191
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700192 private ViewGroup mContent;
193
Fan Zhangc6ca3142017-02-14 15:02:35 -0800194 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700195
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700196 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800197 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fan Zhanga96a2d82016-09-27 17:51:11 -0700199 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500200 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700201
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700202 public SwitchBar getSwitchBar() {
203 return mSwitchBar;
204 }
205
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206 @Override
207 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800208 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 return true;
210 }
211
212 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400213 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800214 return false;
215 }
216
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500218 public SharedPreferences getSharedPreferences(String name, int mode) {
219 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800220 return new SharedPreferencesLogger(this, getMetricsTag(),
221 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500222 }
223 return super.getSharedPreferences(name, mode);
224 }
225
Jason Monke4ebcd12016-02-21 09:37:41 -0500226 private String getMetricsTag() {
227 String tag = getClass().getName();
228 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
229 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
230 }
231 if (tag.startsWith("com.android.settings.")) {
232 tag = tag.replace("com.android.settings.", "");
233 }
234 return tag;
235 }
236
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700237 private static boolean isShortCutIntent(final Intent intent) {
238 Set<String> categories = intent.getCategories();
239 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
240 }
241
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700242 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700243 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700244 if (action == null) {
245 return false;
246 }
247 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
248 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
249 }
250 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700251 }
252
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700253 @Override
254 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700255 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500256 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700257
258 final FeatureFactory factory = FeatureFactory.getFactory(this);
259
260 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800261 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700262
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700263 // Should happen before any call to getIntent()
264 getMetaData();
265
266 final Intent intent = getIntent();
267 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
268 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700271 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700272 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700273
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700274 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700275 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
276
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700277 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700278 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700279
Doris Ling1694d4b2017-03-03 14:21:37 -0800280 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700281
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700282 // This is a "Sub Settings" when:
283 // - this is a real SubSettings
284 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700285 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700286 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
287
Fan Zhang92f1e942017-01-21 10:07:26 -0800288 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
289 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700290 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800291 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700292 }
293
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700294 setContentView(mIsShowingDashboard ?
295 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700297 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 getFragmentManager().addOnBackStackChangedListener(this);
300
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700301 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700302 // We are restarting from a previous saved state; used that to initialize, instead
303 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700304 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800305
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700306 ArrayList<DashboardCategory> categories =
307 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
308 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700309 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700310 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700311 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700313
314 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700315
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800317 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 }
319
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700320 if (mIsShowingDashboard) {
321 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
322 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800323 final Toolbar toolbar = findViewById(R.id.search_action_bar);
324 FeatureFactory.getFactory(this).getSearchFeatureProvider()
325 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700326 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700327
328 // Please forgive me for what I am about to do.
329 //
330 // Need to make the navigation icon non-clickable so that the entire card is clickable
331 // and goes to the search UI. Also set the background to null so there's no ripple.
332 View navView = toolbar.getNavigationView();
333 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800334 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700335 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700336 }
337
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700338 ActionBar actionBar = getActionBar();
339 if (actionBar != null) {
340 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
341 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fan Zhange096ae72017-11-30 14:41:04 -0800342 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700343 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700344 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500345 if (mSwitchBar != null) {
346 mSwitchBar.setMetricsTag(getMetricsTag());
347 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700348
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
351
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700352 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 if (buttonBar != null) {
354 buttonBar.setVisibility(View.VISIBLE);
355
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700356 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 backButton.setOnClickListener(new OnClickListener() {
358 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700359 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 finish();
361 }
362 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700363 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 skipButton.setOnClickListener(new OnClickListener() {
365 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700366 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 finish();
368 }
369 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700370 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 mNextButton.setOnClickListener(new OnClickListener() {
372 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700373 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 finish();
375 }
376 });
377
378 // set our various button parameters
379 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
380 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
381 if (TextUtils.isEmpty(buttonText)) {
382 mNextButton.setVisibility(View.GONE);
383 }
384 else {
385 mNextButton.setText(buttonText);
386 }
387 }
388 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
389 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
390 if (TextUtils.isEmpty(buttonText)) {
391 backButton.setVisibility(View.GONE);
392 }
393 else {
394 backButton.setText(buttonText);
395 }
396 }
397 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
398 skipButton.setVisibility(View.VISIBLE);
399 }
400 }
401 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700402
Fan Zhangefba6b42017-02-07 17:40:59 -0800403 if (DEBUG_TIMING) {
404 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
405 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700406 }
407
Doris Lingcb69baf2017-02-23 17:50:03 -0800408 @VisibleForTesting
409 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
410 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800411 // UP will be shown only if it is a sub settings
412 if (mIsShortcut) {
413 mDisplayHomeAsUpEnabled = isSubSettings;
414 } else if (isSubSettings) {
415 mDisplayHomeAsUpEnabled = true;
416 } else {
417 mDisplayHomeAsUpEnabled = false;
418 }
419 setTitleFromIntent(intent);
420
421 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
422 switchToFragment(initialFragmentName, initialArguments, true, false,
423 mInitialTitleResId, mInitialTitle, false);
424 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700425 // Show search icon as up affordance if we are displaying the main Dashboard
426 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800427 mInitialTitleResId = R.string.dashboard_title;
428
429 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
430 mInitialTitleResId, mInitialTitle, false);
431 }
432 }
433
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700434 private void setTitleFromIntent(Intent intent) {
435 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
436 if (initialTitleResId > 0) {
437 mInitialTitle = null;
438 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100439
440 final String initialTitleResPackageName = intent.getStringExtra(
441 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
442 if (initialTitleResPackageName != null) {
443 try {
444 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
445 0 /* flags */, new UserHandle(UserHandle.myUserId()));
446 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
447 setTitle(mInitialTitle);
448 mInitialTitleResId = -1;
449 return;
450 } catch (NameNotFoundException e) {
451 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
452 }
453 } else {
454 setTitle(mInitialTitleResId);
455 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700456 } else {
457 mInitialTitleResId = -1;
458 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
459 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
460 setTitle(mInitialTitle);
461 }
462 }
463
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800464 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700466 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467 }
468
Fan Zhang28691572016-03-23 15:31:08 -0700469 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800470 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700471
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800472 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700473 if (mInitialTitleResId > 0) {
474 setTitle(mInitialTitleResId);
475 } else {
476 setTitle(mInitialTitle);
477 }
Fan Zhang28691572016-03-23 15:31:08 -0700478 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800479 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700480
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800481 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
482 setTitleFromBackStackEntry(bse);
483 }
484
485 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
486 final CharSequence title;
487 final int titleRes = bse.getBreadCrumbTitleRes();
488 if (titleRes > 0) {
489 title = getText(titleRes);
490 } else {
491 title = bse.getBreadCrumbTitle();
492 }
493 if (title != null) {
494 setTitle(title);
495 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800496 }
497
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800498 @Override
499 protected void onSaveInstanceState(Bundle outState) {
500 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700501 saveState(outState);
502 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800503
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700504 /**
505 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
506 */
507 @VisibleForTesting
508 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700509 if (mCategories.size() > 0) {
510 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800511 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700512
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700513 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700514 }
515
516 @Override
517 protected void onRestoreInstanceState(Bundle savedInstanceState) {
518 super.onRestoreInstanceState(savedInstanceState);
519
520 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800521 }
522
523 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400524 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 super.onResume();
526
Tony Mantler3d84d562017-07-31 10:48:55 -0700527 mDevelopmentSettingsListener = new BroadcastReceiver() {
528 @Override
529 public void onReceive(Context context, Intent intent) {
530 updateTilesList();
531 }
532 };
533 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
534 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700537
Jason Monk4da79e02015-09-10 10:54:36 -0400538 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 }
540
541 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400542 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700544 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
545 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 }
548
549 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800550 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
551 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
552 taskDescription.setIcon(icon);
553 super.setTaskDescription(taskDescription);
554 }
555
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800556 protected boolean isValidFragment(String fragmentName) {
557 // Almost all fragments are wrapped in this,
558 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800559 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
560 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800561 }
562 return false;
563 }
564
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565 @Override
566 public Intent getIntent() {
567 Intent superIntent = super.getIntent();
568 String startingFragment = getStartingFragmentClass(superIntent);
569 // This is called from super.onCreate, isMultiPane() is not yet reliable
570 // Do not use onIsHidingHeaders either, which relies itself on this method
571 if (startingFragment != null) {
572 Intent modIntent = new Intent(superIntent);
573 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
574 Bundle args = superIntent.getExtras();
575 if (args != null) {
576 args = new Bundle(args);
577 } else {
578 args = new Bundle();
579 }
580 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100581 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582 return modIntent;
583 }
584 return superIntent;
585 }
586
587 /**
588 * Checks if the component name in the intent is different from the Settings class and
589 * returns the class name to load as a fragment.
590 */
591 private String getStartingFragmentClass(Intent intent) {
592 if (mFragmentClass != null) return mFragmentClass;
593
594 String intentClass = intent.getComponent().getClassName();
595 if (intentClass.equals(getClass().getName())) return null;
596
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700597 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
599 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700600 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 }
602
603 return intentClass;
604 }
605
606 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000607 * Start a new fragment containing a preference panel. If the preferences
608 * are being displayed in multi-pane mode, the given fragment class will
609 * be instantiated and placed in the appropriate pane. If running in
610 * single-pane mode, a new activity will be launched in which to show the
611 * fragment.
612 *
613 * @param fragmentClass Full name of the class implementing the fragment.
614 * @param args Any desired arguments to supply to the fragment.
615 * @param titleRes Optional resource identifier of the title of this
616 * fragment.
617 * @param titleText Optional text of the title of this fragment.
618 * @param resultTo Optional fragment that result data should be sent to.
619 * If non-null, resultTo.onActivityResult() will be called when this
620 * preference panel is done. The launched panel must use
621 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
622 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700623 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000624 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800625 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
626 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700627 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800628 if (titleRes < 0 && titleText != null) {
629 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700630 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700631 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800632 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000633 }
634
635 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100636 * Start a new fragment in a new activity containing a preference panel for a given user. If the
637 * preferences are being displayed in multi-pane mode, the given fragment class will be
638 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
639 * activity will be launched in which to show the fragment.
640 *
641 * @param fragmentClass Full name of the class implementing the fragment.
642 * @param args Any desired arguments to supply to the fragment.
643 * @param titleRes Optional resource identifier of the title of this fragment.
644 * @param titleText Optional text of the title of this fragment.
645 * @param userHandle The user for which the panel has to be started.
646 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800647 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
648 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700649 // This is a workaround.
650 //
651 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
652 // starting the fragment could cause a native stack corruption. See b/17523189. However,
653 // adding that flag and start the preference panel with the same UserHandler will make it
654 // impossible to use back button to return to the previous screen. See b/20042570.
655 //
656 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
657 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
658 // when we're calling it as the same user.
659 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800660 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700661 } else {
662 String title = null;
663 if (titleRes < 0) {
664 if (titleText != null) {
665 title = titleText.toString();
666 } else {
667 // There not much we can do in that case
668 title = "";
669 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100670 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800671 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
672 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100673 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100674 }
675
676 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800677 * Called by a preference panel fragment to finish itself.
678 *
679 * @param caller The fragment that is asking to be finished.
680 * @param resultCode Optional result code to send back to the original
681 * launching fragment.
682 * @param resultData Optional result data to send back to the original
683 * launching fragment.
684 */
685 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
686 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700687 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800688 }
689
690 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000691 * Start a new fragment.
692 *
693 * @param fragment The fragment to start
694 * @param push If true, the current fragment will be pushed onto the back stack. If false,
695 * the current fragment will be replaced.
696 */
697 public void startPreferenceFragment(Fragment fragment, boolean push) {
698 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700699 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000700 if (push) {
701 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
702 transaction.addToBackStack(BACK_STACK_PREFS);
703 } else {
704 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
705 }
706 transaction.commitAllowingStateLoss();
707 }
708
709 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700710 * Switch to a specific Fragment with taking care of validation, Title and BackStack
711 */
712 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700713 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700714 if (validate && !isValidFragment(fragmentName)) {
715 throw new IllegalArgumentException("Invalid fragment for this activity: "
716 + fragmentName);
717 }
718 Fragment f = Fragment.instantiate(this, fragmentName, args);
719 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700720 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700721 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700722 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700723 }
724 if (addToBackStack) {
725 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
726 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700727 if (titleResId > 0) {
728 transaction.setBreadCrumbTitle(titleResId);
729 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700730 transaction.setBreadCrumbTitle(title);
731 }
732 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700733 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 return f;
735 }
736
Jason Monk4da79e02015-09-10 10:54:36 -0400737 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500738 // Generally the items that are will be changing from these updates will
739 // not be in the top list of tiles, so run it in the background and the
740 // SettingsDrawerActivity will pick up on the updates automatically.
741 AsyncTask.execute(new Runnable() {
742 @Override
743 public void run() {
744 doUpdateTilesList();
745 }
746 });
747 }
748
749 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400750 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700751 final UserManager um = UserManager.get(this);
752 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800753 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700754 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400755 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700756 somethingChanged = setTileEnabled(
757 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
758 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700759
Fan Zhangaeb94f02017-07-05 13:46:04 -0700760 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800761 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700762 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
763 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700764
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700765 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
766 .getDataPlanFeatureProvider()
767 .isEnabled();
768
769 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
770 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
771 // reverse.
772
773 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
774 // disable DataPlanUsageSummaryActivity.
775 somethingChanged = setTileEnabled(
776 new ComponentName(packageName,
777 isDataPlanFeatureEnabled
778 ? Settings.DataUsageSummaryActivity.class.getName()
779 : Settings.DataPlanUsageSummaryActivity.class.getName()),
780 false /* enabled */,
781 isAdmin) || somethingChanged;
782
783 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
784 // enable DataPlanUsageSummaryActivity.
785 somethingChanged = setTileEnabled(
786 new ComponentName(packageName,
787 isDataPlanFeatureEnabled
788 ? Settings.DataPlanUsageSummaryActivity.class.getName()
789 : Settings.DataUsageSummaryActivity.class.getName()),
790 Utils.isBandwidthControlEnabled() /* enabled */,
791 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700792
jackqdyulei76ba0f72017-11-17 14:14:51 -0800793 final boolean isConnectedDeviceV2Enabled =
794 Settings.ConnectedDeviceDashboardActivity.isEnabled();
795 // Enable new connected page if v2 enabled
796 somethingChanged = setTileEnabled(
797 new ComponentName(packageName,
798 Settings.ConnectedDeviceDashboardActivity.class.getName()),
799 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
800 isAdmin) || somethingChanged;
801 // Enable old connected page if v2 disabled
802 somethingChanged = setTileEnabled(
803 new ComponentName(packageName,
804 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
805 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
806 isAdmin) || somethingChanged;
807
Fan Zhangaeb94f02017-07-05 13:46:04 -0700808 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800809 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700810 Utils.showSimCardTile(this), isAdmin)
811 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700812
Fan Zhangf3144942017-12-22 09:59:37 -0800813 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800814 .getPowerUsageFeatureProvider(this)
815 .isBatteryV2Enabled();
816 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700817 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800818 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800819 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
820 // Enable legacy battery page if v2 disabled
821 somethingChanged = setTileEnabled(new ComponentName(packageName,
822 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
823 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400824
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800826 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400827 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700828 && !Utils.isMonkeyRunning(), isAdmin)
829 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400830
Fan Zhangaeb94f02017-07-05 13:46:04 -0700831 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800832 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700833 !UserManager.isDeviceInDemoMode(this), isAdmin)
834 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800835
Fan Zhangaeb94f02017-07-05 13:46:04 -0700836 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700837 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700838 !UserManager.isDeviceInDemoMode(this), isAdmin)
839 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400840
Fan Zhangaeb94f02017-07-05 13:46:04 -0700841 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800842 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700843 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
844 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400845
Tony Mantler3d84d562017-07-31 10:48:55 -0700846 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800847 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
848 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800849
Fan Zhang4c26da92017-09-08 15:29:54 -0700850 somethingChanged = setTileEnabled(new ComponentName(packageName,
851 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800852 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700853 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400854
Fan Zhanga6986e72017-03-08 09:24:45 -0800855 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700856 somethingChanged = setTileEnabled(new ComponentName(packageName,
857 BackupSettingsActivity.class.getName()), true, isAdmin)
858 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800859
Fan Zhangaeb94f02017-07-05 13:46:04 -0700860 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800861 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700862 WifiDisplaySettings.isAvailable(this), isAdmin)
863 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800864
Daniel Nishi1e620952017-12-19 10:15:14 -0800865 // Enable/disable the Me Card page.
866 final boolean isMeCardEnabled = featureFactory
867 .getAccountFeatureProvider()
868 .isMeCardEnabled(this);
869 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800870 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800871 isMeCardEnabled, isAdmin)
872 || somethingChanged;
873 somethingChanged = setTileEnabled(new ComponentName(packageName,
874 Settings.DeviceInfoSettingsActivity.class.getName()),
875 !isMeCardEnabled, isAdmin)
876 || somethingChanged;
877
Jason Monk4da79e02015-09-10 10:54:36 -0400878 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800879
Jason Monk4da79e02015-09-10 10:54:36 -0400880 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800881 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800882 synchronized (categories) {
883 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700884 final int tileCount = category.getTilesCount();
885 for (int i = 0; i < tileCount; i++) {
886 final ComponentName component = category.getTile(i).intent.getComponent();
887
Fan Zhanga6986e72017-03-08 09:24:45 -0800888 final String name = component.getClassName();
889 final boolean isEnabledForRestricted = ArrayUtils.contains(
890 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
891 if (packageName.equals(component.getPackageName())
892 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700893 somethingChanged = setTileEnabled(component, false, isAdmin)
894 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800895 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700896 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700897 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700898 }
899 }
Doris Ling26bf0032016-06-15 18:16:09 -0700900
Fan Zhang224caad2017-01-06 11:42:48 -0800901 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700902 if (somethingChanged) {
903 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
904 updateCategories();
905 } else {
906 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
907 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500908 }
909
Fan Zhangaeb94f02017-07-05 13:46:04 -0700910 /**
911 * @return whether or not the enabled state actually changed.
912 */
913 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400914 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800915 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
916 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400917 enabled = false;
918 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700919 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500920 }
921
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800922 private void getMetaData() {
923 try {
924 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
925 PackageManager.GET_META_DATA);
926 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800927 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
928 } catch (NameNotFoundException nnfe) {
929 // No recovery
930 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
931 }
932 }
933
934 // give subclasses access to the Next button
935 public boolean hasNextButton() {
936 return mNextButton != null;
937 }
938
939 public Button getNextButton() {
940 return mNextButton;
941 }
942
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800943 @Override
944 public boolean shouldUpRecreateTask(Intent targetIntent) {
945 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
946 }
947
Jason Monkd4f03ec2016-01-07 16:25:34 -0500948 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700949 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700950 return;
951 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700952 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500953 startActivityForResult(intent, REQUEST_SUGGESTION);
954 }
955
956 @Override
957 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
958 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
959 && resultCode != RESULT_CANCELED) {
960 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
961 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
962 }
963 super.onActivityResult(requestCode, resultCode, data);
964 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800965
966 @VisibleForTesting
967 Bitmap getBitmapFromXmlResource(int drawableRes) {
968 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
969 Canvas canvas = new Canvas();
970 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
971 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
972 canvas.setBitmap(bitmap);
973 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
974 drawable.draw(canvas);
975
976 return bitmap;
977 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700978}