blob: cc90619431ff0be116aa50b8c63c11bb825ebce3 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fan Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050036import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.UserHandle;
39import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080040import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070042import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.view.View;
49import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070052import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -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;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070056import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000057import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080058import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070059import com.android.settings.dashboard.DashboardFeatureProvider;
60import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080062import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070063import com.android.settings.widget.SwitchBar;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080064import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
65import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070066import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040067import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070069
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070072import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073
Jason Monk4da79e02015-09-10 10:54:36 -040074public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075 implements PreferenceManager.OnPreferenceTreeClickListener,
76 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080077 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078
79 private static final String LOG_TAG = "Settings";
80
81 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070082 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070083 @VisibleForTesting
84 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085
86 /**
87 * When starting this activity, the invoking Intent can contain this extra
88 * string to specify which fragment should be initially displayed.
89 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
90 * will call isValidFragment() to confirm that the fragment class name is valid for this
91 * activity.
92 */
93 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
94
95 /**
96 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
97 * this extra can also be specified to supply a Bundle of arguments to pass
98 * to that fragment when it is instantiated during the initial creation
99 * of the activity.
100 */
101 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
102
103 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700104 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
105 */
106 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
107
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800108 public static final String BACK_STACK_PREFS = ":settings:prefs";
109
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110 // extras that allow any preference activity to be launched as part of a wizard
111
112 // show Back and Next buttons? takes boolean parameter
113 // Back will then return RESULT_CANCELED and Next RESULT_OK
114 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
115
116 // add a Skip button?
117 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
118
119 // specify custom text for the Back or Next buttons, or cause a button to not appear
120 // at all by setting it to null
121 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
122 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
123
124 /**
125 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700126 * 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 -0800127 * that fragment.
128 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700129 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100130 /**
131 * The package name used to resolve the title resource id.
132 */
133 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
134 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700135 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
136 ":settings:show_fragment_title_resid";
137 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
138 ":settings:show_fragment_as_shortcut";
139
140 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
141 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
Fan Zhang78ab9f92017-02-27 16:10:05 -0800143 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700144 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
145
Jason Monk30695812015-11-17 09:01:08 -0500146 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800147 "com.android.settings.FRAGMENT_CLASS";
148
149 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
150
Jason Monkd4f03ec2016-01-07 16:25:34 -0500151 private static final int REQUEST_SUGGESTION = 42;
152
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800155 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700156 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800157
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700158 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
159 "android.settings.APPLICATION_DETAILS_SETTINGS"
160 };
161
Tony Mantler3d84d562017-07-31 10:48:55 -0700162 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 private boolean mBatteryPresent = true;
165 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 @Override
167 public void onReceive(Context context, Intent intent) {
168 String action = intent.getAction();
169 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
170 boolean batteryPresent = Utils.isBatteryPresent(intent);
171
172 if (mBatteryPresent != batteryPresent) {
173 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400174 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 }
176 }
177 }
178 };
179
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700180 private SwitchBar mSwitchBar;
181
182 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700183
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700184 @VisibleForTesting
185 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700186
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700187 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700188 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700189
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700190 private ViewGroup mContent;
191
Fan Zhangc6ca3142017-02-14 15:02:35 -0800192 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700193
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700194 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800195 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196
Fan Zhanga96a2d82016-09-27 17:51:11 -0700197 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500198 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700199
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700200 public SwitchBar getSwitchBar() {
201 return mSwitchBar;
202 }
203
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 @Override
205 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800206 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 return true;
208 }
209
210 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400211 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 return false;
213 }
214
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500216 public SharedPreferences getSharedPreferences(String name, int mode) {
217 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800218 return new SharedPreferencesLogger(this, getMetricsTag(),
219 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500220 }
221 return super.getSharedPreferences(name, mode);
222 }
223
Jason Monke4ebcd12016-02-21 09:37:41 -0500224 private String getMetricsTag() {
225 String tag = getClass().getName();
226 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
227 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
228 }
229 if (tag.startsWith("com.android.settings.")) {
230 tag = tag.replace("com.android.settings.", "");
231 }
232 return tag;
233 }
234
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700235 private static boolean isShortCutIntent(final Intent intent) {
236 Set<String> categories = intent.getCategories();
237 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
238 }
239
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700240 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700241 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700242 if (action == null) {
243 return false;
244 }
245 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
246 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
247 }
248 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700249 }
250
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700251 @Override
252 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700253 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500254 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700255
256 final FeatureFactory factory = FeatureFactory.getFactory(this);
257
258 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800259 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700260
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700261 // Should happen before any call to getIntent()
262 getMetaData();
263
264 final Intent intent = getIntent();
265 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
266 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800267 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800268
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700269 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700270 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700271
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700272 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700273 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
274
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700275 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700276 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700277
Doris Ling1694d4b2017-03-03 14:21:37 -0800278 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700279
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700280 // This is a "Sub Settings" when:
281 // - this is a real SubSettings
282 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700283 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700284 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
285
Fan Zhang92f1e942017-01-21 10:07:26 -0800286 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
287 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700288 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800289 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700290 }
291
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700292 setContentView(mIsShowingDashboard ?
293 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700295 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700296
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 getFragmentManager().addOnBackStackChangedListener(this);
298
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700299 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700300 // We are restarting from a previous saved state; used that to initialize, instead
301 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700302 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800303
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700304 ArrayList<DashboardCategory> categories =
305 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
306 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700307 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700308 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700309 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700311
312 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700313
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800315 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 }
317
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700318 if (mIsShowingDashboard) {
319 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
320 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800321 final Toolbar toolbar = findViewById(R.id.search_action_bar);
322 FeatureFactory.getFactory(this).getSearchFeatureProvider()
323 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700324 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700325
326 // Please forgive me for what I am about to do.
327 //
328 // Need to make the navigation icon non-clickable so that the entire card is clickable
329 // and goes to the search UI. Also set the background to null so there's no ripple.
330 View navView = toolbar.getNavigationView();
331 navView.setClickable(false);
332 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700333 }
334
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700335 ActionBar actionBar = getActionBar();
336 if (actionBar != null) {
337 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
338 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fan Zhange096ae72017-11-30 14:41:04 -0800339 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700340 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700341 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500342 if (mSwitchBar != null) {
343 mSwitchBar.setMetricsTag(getMetricsTag());
344 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700345
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
348
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700349 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 if (buttonBar != null) {
351 buttonBar.setVisibility(View.VISIBLE);
352
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700353 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 backButton.setOnClickListener(new OnClickListener() {
355 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700356 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 finish();
358 }
359 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700360 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 skipButton.setOnClickListener(new OnClickListener() {
362 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700363 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 finish();
365 }
366 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700367 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 mNextButton.setOnClickListener(new OnClickListener() {
369 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700370 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 finish();
372 }
373 });
374
375 // set our various button parameters
376 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
377 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
378 if (TextUtils.isEmpty(buttonText)) {
379 mNextButton.setVisibility(View.GONE);
380 }
381 else {
382 mNextButton.setText(buttonText);
383 }
384 }
385 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
386 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
387 if (TextUtils.isEmpty(buttonText)) {
388 backButton.setVisibility(View.GONE);
389 }
390 else {
391 backButton.setText(buttonText);
392 }
393 }
394 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
395 skipButton.setVisibility(View.VISIBLE);
396 }
397 }
398 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700399
Fan Zhangefba6b42017-02-07 17:40:59 -0800400 if (DEBUG_TIMING) {
401 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
402 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700403 }
404
Doris Lingcb69baf2017-02-23 17:50:03 -0800405 @VisibleForTesting
406 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
407 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800408 // UP will be shown only if it is a sub settings
409 if (mIsShortcut) {
410 mDisplayHomeAsUpEnabled = isSubSettings;
411 } else if (isSubSettings) {
412 mDisplayHomeAsUpEnabled = true;
413 } else {
414 mDisplayHomeAsUpEnabled = false;
415 }
416 setTitleFromIntent(intent);
417
418 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
419 switchToFragment(initialFragmentName, initialArguments, true, false,
420 mInitialTitleResId, mInitialTitle, false);
421 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700422 // Show search icon as up affordance if we are displaying the main Dashboard
423 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800424 mInitialTitleResId = R.string.dashboard_title;
425
426 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
427 mInitialTitleResId, mInitialTitle, false);
428 }
429 }
430
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700431 private void setTitleFromIntent(Intent intent) {
432 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
433 if (initialTitleResId > 0) {
434 mInitialTitle = null;
435 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100436
437 final String initialTitleResPackageName = intent.getStringExtra(
438 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
439 if (initialTitleResPackageName != null) {
440 try {
441 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
442 0 /* flags */, new UserHandle(UserHandle.myUserId()));
443 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
444 setTitle(mInitialTitle);
445 mInitialTitleResId = -1;
446 return;
447 } catch (NameNotFoundException e) {
448 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
449 }
450 } else {
451 setTitle(mInitialTitleResId);
452 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700453 } else {
454 mInitialTitleResId = -1;
455 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
456 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
457 setTitle(mInitialTitle);
458 }
459 }
460
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800461 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700463 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 }
465
Fan Zhang28691572016-03-23 15:31:08 -0700466 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800467 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700468
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800469 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700470 if (mInitialTitleResId > 0) {
471 setTitle(mInitialTitleResId);
472 } else {
473 setTitle(mInitialTitle);
474 }
Fan Zhang28691572016-03-23 15:31:08 -0700475 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800476 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700477
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800478 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
479 setTitleFromBackStackEntry(bse);
480 }
481
482 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
483 final CharSequence title;
484 final int titleRes = bse.getBreadCrumbTitleRes();
485 if (titleRes > 0) {
486 title = getText(titleRes);
487 } else {
488 title = bse.getBreadCrumbTitle();
489 }
490 if (title != null) {
491 setTitle(title);
492 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800493 }
494
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 @Override
496 protected void onSaveInstanceState(Bundle outState) {
497 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700498 saveState(outState);
499 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800500
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700501 /**
502 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
503 */
504 @VisibleForTesting
505 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700506 if (mCategories.size() > 0) {
507 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800508 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700509
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700510 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700511 }
512
513 @Override
514 protected void onRestoreInstanceState(Bundle savedInstanceState) {
515 super.onRestoreInstanceState(savedInstanceState);
516
517 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518 }
519
520 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400521 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800522 super.onResume();
523
Tony Mantler3d84d562017-07-31 10:48:55 -0700524 mDevelopmentSettingsListener = new BroadcastReceiver() {
525 @Override
526 public void onReceive(Context context, Intent intent) {
527 updateTilesList();
528 }
529 };
530 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
531 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700534
Jason Monk4da79e02015-09-10 10:54:36 -0400535 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 }
537
538 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400539 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700541 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
542 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544 }
545
546 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800547 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
548 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
549 taskDescription.setIcon(icon);
550 super.setTaskDescription(taskDescription);
551 }
552
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 protected boolean isValidFragment(String fragmentName) {
554 // Almost all fragments are wrapped in this,
555 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800556 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
557 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 }
559 return false;
560 }
561
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 @Override
563 public Intent getIntent() {
564 Intent superIntent = super.getIntent();
565 String startingFragment = getStartingFragmentClass(superIntent);
566 // This is called from super.onCreate, isMultiPane() is not yet reliable
567 // Do not use onIsHidingHeaders either, which relies itself on this method
568 if (startingFragment != null) {
569 Intent modIntent = new Intent(superIntent);
570 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
571 Bundle args = superIntent.getExtras();
572 if (args != null) {
573 args = new Bundle(args);
574 } else {
575 args = new Bundle();
576 }
577 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100578 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 return modIntent;
580 }
581 return superIntent;
582 }
583
584 /**
585 * Checks if the component name in the intent is different from the Settings class and
586 * returns the class name to load as a fragment.
587 */
588 private String getStartingFragmentClass(Intent intent) {
589 if (mFragmentClass != null) return mFragmentClass;
590
591 String intentClass = intent.getComponent().getClassName();
592 if (intentClass.equals(getClass().getName())) return null;
593
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700594 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
596 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700597 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598 }
599
600 return intentClass;
601 }
602
603 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000604 * Start a new fragment containing a preference panel. If the preferences
605 * are being displayed in multi-pane mode, the given fragment class will
606 * be instantiated and placed in the appropriate pane. If running in
607 * single-pane mode, a new activity will be launched in which to show the
608 * fragment.
609 *
610 * @param fragmentClass Full name of the class implementing the fragment.
611 * @param args Any desired arguments to supply to the fragment.
612 * @param titleRes Optional resource identifier of the title of this
613 * fragment.
614 * @param titleText Optional text of the title of this fragment.
615 * @param resultTo Optional fragment that result data should be sent to.
616 * If non-null, resultTo.onActivityResult() will be called when this
617 * preference panel is done. The launched panel must use
618 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
619 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700620 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000621 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800622 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
623 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700624 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800625 if (titleRes < 0 && titleText != null) {
626 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700627 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800629 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000630 }
631
632 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100633 * Start a new fragment in a new activity containing a preference panel for a given user. If the
634 * preferences are being displayed in multi-pane mode, the given fragment class will be
635 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
636 * activity will be launched in which to show the fragment.
637 *
638 * @param fragmentClass Full name of the class implementing the fragment.
639 * @param args Any desired arguments to supply to the fragment.
640 * @param titleRes Optional resource identifier of the title of this fragment.
641 * @param titleText Optional text of the title of this fragment.
642 * @param userHandle The user for which the panel has to be started.
643 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800644 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
645 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700646 // This is a workaround.
647 //
648 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
649 // starting the fragment could cause a native stack corruption. See b/17523189. However,
650 // adding that flag and start the preference panel with the same UserHandler will make it
651 // impossible to use back button to return to the previous screen. See b/20042570.
652 //
653 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
654 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
655 // when we're calling it as the same user.
656 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800657 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700658 } else {
659 String title = null;
660 if (titleRes < 0) {
661 if (titleText != null) {
662 title = titleText.toString();
663 } else {
664 // There not much we can do in that case
665 title = "";
666 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100667 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800668 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
669 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100670 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100671 }
672
673 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800674 * Called by a preference panel fragment to finish itself.
675 *
676 * @param caller The fragment that is asking to be finished.
677 * @param resultCode Optional result code to send back to the original
678 * launching fragment.
679 * @param resultData Optional result data to send back to the original
680 * launching fragment.
681 */
682 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
683 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700684 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800685 }
686
687 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000688 * Start a new fragment.
689 *
690 * @param fragment The fragment to start
691 * @param push If true, the current fragment will be pushed onto the back stack. If false,
692 * the current fragment will be replaced.
693 */
694 public void startPreferenceFragment(Fragment fragment, boolean push) {
695 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700696 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000697 if (push) {
698 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
699 transaction.addToBackStack(BACK_STACK_PREFS);
700 } else {
701 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
702 }
703 transaction.commitAllowingStateLoss();
704 }
705
706 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700707 * Switch to a specific Fragment with taking care of validation, Title and BackStack
708 */
709 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700710 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700711 if (validate && !isValidFragment(fragmentName)) {
712 throw new IllegalArgumentException("Invalid fragment for this activity: "
713 + fragmentName);
714 }
715 Fragment f = Fragment.instantiate(this, fragmentName, args);
716 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700717 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700718 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700719 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700720 }
721 if (addToBackStack) {
722 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
723 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700724 if (titleResId > 0) {
725 transaction.setBreadCrumbTitle(titleResId);
726 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700727 transaction.setBreadCrumbTitle(title);
728 }
729 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700730 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700731 return f;
732 }
733
Jason Monk4da79e02015-09-10 10:54:36 -0400734 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500735 // Generally the items that are will be changing from these updates will
736 // not be in the top list of tiles, so run it in the background and the
737 // SettingsDrawerActivity will pick up on the updates automatically.
738 AsyncTask.execute(new Runnable() {
739 @Override
740 public void run() {
741 doUpdateTilesList();
742 }
743 });
744 }
745
746 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400747 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700748 final UserManager um = UserManager.get(this);
749 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800750 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700751 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400752 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700753 somethingChanged = setTileEnabled(
754 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
755 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700756
Fan Zhangaeb94f02017-07-05 13:46:04 -0700757 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800758 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
760 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700761
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700762 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
763 .getDataPlanFeatureProvider()
764 .isEnabled();
765
766 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
767 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
768 // reverse.
769
770 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
771 // disable DataPlanUsageSummaryActivity.
772 somethingChanged = setTileEnabled(
773 new ComponentName(packageName,
774 isDataPlanFeatureEnabled
775 ? Settings.DataUsageSummaryActivity.class.getName()
776 : Settings.DataPlanUsageSummaryActivity.class.getName()),
777 false /* enabled */,
778 isAdmin) || somethingChanged;
779
780 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
781 // enable DataPlanUsageSummaryActivity.
782 somethingChanged = setTileEnabled(
783 new ComponentName(packageName,
784 isDataPlanFeatureEnabled
785 ? Settings.DataPlanUsageSummaryActivity.class.getName()
786 : Settings.DataUsageSummaryActivity.class.getName()),
787 Utils.isBandwidthControlEnabled() /* enabled */,
788 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700789
jackqdyulei76ba0f72017-11-17 14:14:51 -0800790 final boolean isConnectedDeviceV2Enabled =
791 Settings.ConnectedDeviceDashboardActivity.isEnabled();
792 // Enable new connected page if v2 enabled
793 somethingChanged = setTileEnabled(
794 new ComponentName(packageName,
795 Settings.ConnectedDeviceDashboardActivity.class.getName()),
796 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
797 isAdmin) || somethingChanged;
798 // Enable old connected page if v2 disabled
799 somethingChanged = setTileEnabled(
800 new ComponentName(packageName,
801 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
802 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
803 isAdmin) || somethingChanged;
804
Fan Zhangf3144942017-12-22 09:59:37 -0800805 final boolean isSecurityV2Enabled = featureFactory.getSecurityFeatureProvider()
806 .isSecuritySettingsV2Enabled(this);
807
808 // Enable new security page if v2 enabled
809 somethingChanged = setTileEnabled(
810 new ComponentName(packageName,Settings.SecuritySettingsActivityV2.class.getName()),
811 isSecurityV2Enabled,
812 isAdmin) || somethingChanged;
813 // Enable old security page if v2 disabled
814 somethingChanged = setTileEnabled(
815 new ComponentName(packageName,Settings.SecuritySettingsActivity.class.getName()),
816 !isSecurityV2Enabled,
817 isAdmin) || somethingChanged;
818
Fan Zhangaeb94f02017-07-05 13:46:04 -0700819 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800820 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700821 Utils.showSimCardTile(this), isAdmin)
822 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700823
Fan Zhangf3144942017-12-22 09:59:37 -0800824 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800825 .getPowerUsageFeatureProvider(this)
826 .isBatteryV2Enabled();
827 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700828 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800829 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800830 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
831 // Enable legacy battery page if v2 disabled
832 somethingChanged = setTileEnabled(new ComponentName(packageName,
833 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
834 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400835
Fan Zhangaeb94f02017-07-05 13:46:04 -0700836 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800837 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400838 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700839 && !Utils.isMonkeyRunning(), isAdmin)
840 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400841
Fan Zhangaeb94f02017-07-05 13:46:04 -0700842 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800843 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700844 !UserManager.isDeviceInDemoMode(this), isAdmin)
845 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800846
Fan Zhangaeb94f02017-07-05 13:46:04 -0700847 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700848 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700849 !UserManager.isDeviceInDemoMode(this), isAdmin)
850 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400851
Fan Zhangaeb94f02017-07-05 13:46:04 -0700852 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800853 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700854 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
855 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400856
Tony Mantler3d84d562017-07-31 10:48:55 -0700857 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800858 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
859 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800860
Fan Zhang4c26da92017-09-08 15:29:54 -0700861 somethingChanged = setTileEnabled(new ComponentName(packageName,
862 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800863 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700864 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400865
Fan Zhanga6986e72017-03-08 09:24:45 -0800866 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700867 somethingChanged = setTileEnabled(new ComponentName(packageName,
868 BackupSettingsActivity.class.getName()), true, isAdmin)
869 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800870
Fan Zhangaeb94f02017-07-05 13:46:04 -0700871 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800872 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700873 WifiDisplaySettings.isAvailable(this), isAdmin)
874 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800875
Daniel Nishi1e620952017-12-19 10:15:14 -0800876 // Enable/disable the Me Card page.
877 final boolean isMeCardEnabled = featureFactory
878 .getAccountFeatureProvider()
879 .isMeCardEnabled(this);
880 somethingChanged = setTileEnabled(new ComponentName(packageName,
881 Settings.MeCardActivity.class.getName()),
882 isMeCardEnabled, isAdmin)
883 || somethingChanged;
884 somethingChanged = setTileEnabled(new ComponentName(packageName,
885 Settings.DeviceInfoSettingsActivity.class.getName()),
886 !isMeCardEnabled, isAdmin)
887 || somethingChanged;
888
Jason Monk4da79e02015-09-10 10:54:36 -0400889 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800890
Jason Monk4da79e02015-09-10 10:54:36 -0400891 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800892 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800893 synchronized (categories) {
894 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700895 final int tileCount = category.getTilesCount();
896 for (int i = 0; i < tileCount; i++) {
897 final ComponentName component = category.getTile(i).intent.getComponent();
898
Fan Zhanga6986e72017-03-08 09:24:45 -0800899 final String name = component.getClassName();
900 final boolean isEnabledForRestricted = ArrayUtils.contains(
901 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
902 if (packageName.equals(component.getPackageName())
903 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700904 somethingChanged = setTileEnabled(component, false, isAdmin)
905 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800906 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700907 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700908 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700909 }
910 }
Doris Ling26bf0032016-06-15 18:16:09 -0700911
Fan Zhang224caad2017-01-06 11:42:48 -0800912 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700913 if (somethingChanged) {
914 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
915 updateCategories();
916 } else {
917 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
918 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500919 }
920
Fan Zhangaeb94f02017-07-05 13:46:04 -0700921 /**
922 * @return whether or not the enabled state actually changed.
923 */
924 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400925 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800926 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
927 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400928 enabled = false;
929 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700930 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500931 }
932
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800933 private void getMetaData() {
934 try {
935 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
936 PackageManager.GET_META_DATA);
937 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800938 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
939 } catch (NameNotFoundException nnfe) {
940 // No recovery
941 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
942 }
943 }
944
945 // give subclasses access to the Next button
946 public boolean hasNextButton() {
947 return mNextButton != null;
948 }
949
950 public Button getNextButton() {
951 return mNextButton;
952 }
953
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800954 @Override
955 public boolean shouldUpRecreateTask(Intent targetIntent) {
956 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
957 }
958
Jason Monkd4f03ec2016-01-07 16:25:34 -0500959 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700960 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700961 return;
962 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700963 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500964 startActivityForResult(intent, REQUEST_SUGGESTION);
965 }
966
967 @Override
968 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
969 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
970 && resultCode != RESULT_CANCELED) {
971 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
972 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
973 }
974 super.onActivityResult(requestCode, resultCode, data);
975 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800976
977 @VisibleForTesting
978 Bitmap getBitmapFromXmlResource(int drawableRes) {
979 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
980 Canvas canvas = new Canvas();
981 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
982 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
983 canvas.setBitmap(bitmap);
984 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
985 drawable.draw(canvas);
986
987 return bitmap;
988 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700989}