blob: 78c9c215a75dfd2116d50aabbed3d82490036c31 [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 Zhangc6ca3142017-02-14 15:02:35 -080059import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070060import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan 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;
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 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080096 * The metrics category constant for logging source when a setting fragment is opened.
97 */
98 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
99
100 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800101 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
102 * this extra can also be specified to supply a Bundle of arguments to pass
103 * to that fragment when it is instantiated during the initial creation
104 * of the activity.
105 */
106 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
107
108 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700109 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
110 */
111 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
112
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800113 public static final String BACK_STACK_PREFS = ":settings:prefs";
114
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115 // extras that allow any preference activity to be launched as part of a wizard
116
117 // show Back and Next buttons? takes boolean parameter
118 // Back will then return RESULT_CANCELED and Next RESULT_OK
119 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
120
121 // add a Skip button?
122 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
123
124 // specify custom text for the Back or Next buttons, or cause a button to not appear
125 // at all by setting it to null
126 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
127 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
128
129 /**
130 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700131 * 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 -0800132 * that fragment.
133 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700134 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100135 /**
136 * The package name used to resolve the title resource id.
137 */
138 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
139 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700140 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
141 ":settings:show_fragment_title_resid";
142 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
143 ":settings:show_fragment_as_shortcut";
144
145 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
146 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800147
Fan Zhang78ab9f92017-02-27 16:10:05 -0800148 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700149 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
150
Jason Monk30695812015-11-17 09:01:08 -0500151 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152 "com.android.settings.FRAGMENT_CLASS";
153
154 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
155
Jason Monkd4f03ec2016-01-07 16:25:34 -0500156 private static final int REQUEST_SUGGESTION = 42;
157
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800158 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800159
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800160 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700161 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800162
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700163 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
164 "android.settings.APPLICATION_DETAILS_SETTINGS"
165 };
166
Tony Mantler3d84d562017-07-31 10:48:55 -0700167 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800169 private boolean mBatteryPresent = true;
170 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 @Override
172 public void onReceive(Context context, Intent intent) {
173 String action = intent.getAction();
174 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
175 boolean batteryPresent = Utils.isBatteryPresent(intent);
176
177 if (mBatteryPresent != batteryPresent) {
178 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400179 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800180 }
181 }
182 }
183 };
184
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700185 private SwitchBar mSwitchBar;
186
187 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700188
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700189 @VisibleForTesting
190 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700191
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700192 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700193 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700194
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700195 private ViewGroup mContent;
196
Fan Zhangc6ca3142017-02-14 15:02:35 -0800197 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700198
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700199 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800200 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201
Fan Zhanga96a2d82016-09-27 17:51:11 -0700202 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500203 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700204
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700205 public SwitchBar getSwitchBar() {
206 return mSwitchBar;
207 }
208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 @Override
210 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800211 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 return true;
213 }
214
215 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400216 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 return false;
218 }
219
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500221 public SharedPreferences getSharedPreferences(String name, int mode) {
222 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500223 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500224 }
225 return super.getSharedPreferences(name, mode);
226 }
227
Jason Monke4ebcd12016-02-21 09:37:41 -0500228 private String getMetricsTag() {
229 String tag = getClass().getName();
230 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
231 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
232 }
233 if (tag.startsWith("com.android.settings.")) {
234 tag = tag.replace("com.android.settings.", "");
235 }
236 return tag;
237 }
238
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700239 private static boolean isShortCutIntent(final Intent intent) {
240 Set<String> categories = intent.getCategories();
241 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
242 }
243
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700244 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700245 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700246 if (action == null) {
247 return false;
248 }
249 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
250 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
251 }
252 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700253 }
254
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700255 @Override
256 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700257 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500258 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700259
260 final FeatureFactory factory = FeatureFactory.getFactory(this);
261
262 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800263 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700264
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700265 // Should happen before any call to getIntent()
266 getMetaData();
267
268 final Intent intent = getIntent();
269 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
270 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700273 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700274 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700275
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700276 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700277 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
278
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700279 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700280 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700281
Doris Ling1694d4b2017-03-03 14:21:37 -0800282 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700283
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700284 // This is a "Sub Settings" when:
285 // - this is a real SubSettings
286 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700287 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700288 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
289
Fan Zhang92f1e942017-01-21 10:07:26 -0800290 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
291 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700292 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800293 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700294 }
295
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700296 setContentView(mIsShowingDashboard ?
297 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700299 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700300
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 getFragmentManager().addOnBackStackChangedListener(this);
302
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700303 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700304 // We are restarting from a previous saved state; used that to initialize, instead
305 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700306 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800307
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700308 ArrayList<DashboardCategory> categories =
309 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
310 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700311 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700312 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700313 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700315
316 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700317
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800319 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 }
321
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700322 if (mIsShowingDashboard) {
323 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
324 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800325 final Toolbar toolbar = findViewById(R.id.search_action_bar);
326 FeatureFactory.getFactory(this).getSearchFeatureProvider()
327 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700328 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700329
330 // Please forgive me for what I am about to do.
331 //
332 // Need to make the navigation icon non-clickable so that the entire card is clickable
333 // and goes to the search UI. Also set the background to null so there's no ripple.
334 View navView = toolbar.getNavigationView();
335 navView.setClickable(false);
336 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700337 }
338
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700339 ActionBar actionBar = getActionBar();
340 if (actionBar != null) {
341 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
342 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
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 Zhangaeb94f02017-07-05 13:46:04 -0700753 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400754 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700755 somethingChanged = setTileEnabled(
756 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
757 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700758
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800760 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700761 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
762 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700763
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700764 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
765 .getDataPlanFeatureProvider()
766 .isEnabled();
767
768 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
769 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
770 // reverse.
771
772 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
773 // disable DataPlanUsageSummaryActivity.
774 somethingChanged = setTileEnabled(
775 new ComponentName(packageName,
776 isDataPlanFeatureEnabled
777 ? Settings.DataUsageSummaryActivity.class.getName()
778 : Settings.DataPlanUsageSummaryActivity.class.getName()),
779 false /* enabled */,
780 isAdmin) || somethingChanged;
781
782 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
783 // enable DataPlanUsageSummaryActivity.
784 somethingChanged = setTileEnabled(
785 new ComponentName(packageName,
786 isDataPlanFeatureEnabled
787 ? Settings.DataPlanUsageSummaryActivity.class.getName()
788 : Settings.DataUsageSummaryActivity.class.getName()),
789 Utils.isBandwidthControlEnabled() /* enabled */,
790 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700791
Fan Zhangaeb94f02017-07-05 13:46:04 -0700792 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800793 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700794 Utils.showSimCardTile(this), isAdmin)
795 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700796
Fan Zhangaeb94f02017-07-05 13:46:04 -0700797 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800798 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700799 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400800
Fan Zhangaeb94f02017-07-05 13:46:04 -0700801 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800802 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400803 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700804 && !Utils.isMonkeyRunning(), isAdmin)
805 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 !UserManager.isDeviceInDemoMode(this), isAdmin)
810 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800811
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 !UserManager.isDeviceInDemoMode(this), isAdmin)
815 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700816
Fan Zhangaeb94f02017-07-05 13:46:04 -0700817 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700818 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700819 !UserManager.isDeviceInDemoMode(this), isAdmin)
820 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400821
Fan Zhangaeb94f02017-07-05 13:46:04 -0700822 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800823 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700824 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
825 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400826
Tony Mantler3d84d562017-07-31 10:48:55 -0700827 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Dan Sandler12c4ba42016-03-28 11:13:40 -0400828 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
jeffreyhuangcb823d52017-11-17 11:19:44 -0800829
Fan Zhang4c26da92017-09-08 15:29:54 -0700830 somethingChanged = setTileEnabled(new ComponentName(packageName,
831 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800832 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700833 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400834
Fan Zhanga6986e72017-03-08 09:24:45 -0800835 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700836 somethingChanged = setTileEnabled(new ComponentName(packageName,
837 BackupSettingsActivity.class.getName()), true, isAdmin)
838 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800839
Fan Zhangaeb94f02017-07-05 13:46:04 -0700840 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800841 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700842 WifiDisplaySettings.isAvailable(this), isAdmin)
843 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800844
Jason Monk4da79e02015-09-10 10:54:36 -0400845 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800846
Jason Monk4da79e02015-09-10 10:54:36 -0400847 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800848 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800849 synchronized (categories) {
850 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700851 final int tileCount = category.getTilesCount();
852 for (int i = 0; i < tileCount; i++) {
853 final ComponentName component = category.getTile(i).intent.getComponent();
854
Fan Zhanga6986e72017-03-08 09:24:45 -0800855 final String name = component.getClassName();
856 final boolean isEnabledForRestricted = ArrayUtils.contains(
857 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
858 if (packageName.equals(component.getPackageName())
859 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700860 somethingChanged = setTileEnabled(component, false, isAdmin)
861 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800862 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700863 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700864 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700865 }
866 }
Doris Ling26bf0032016-06-15 18:16:09 -0700867
Fan Zhang224caad2017-01-06 11:42:48 -0800868 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700869 if (somethingChanged) {
870 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
871 updateCategories();
872 } else {
873 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
874 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500875 }
876
Fan Zhangaeb94f02017-07-05 13:46:04 -0700877 /**
878 * @return whether or not the enabled state actually changed.
879 */
880 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400881 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800882 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
883 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400884 enabled = false;
885 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700886 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500887 }
888
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800889 private void getMetaData() {
890 try {
891 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
892 PackageManager.GET_META_DATA);
893 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800894 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
895 } catch (NameNotFoundException nnfe) {
896 // No recovery
897 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
898 }
899 }
900
901 // give subclasses access to the Next button
902 public boolean hasNextButton() {
903 return mNextButton != null;
904 }
905
906 public Button getNextButton() {
907 return mNextButton;
908 }
909
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800910 @Override
911 public boolean shouldUpRecreateTask(Intent targetIntent) {
912 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
913 }
914
Jason Monkd4f03ec2016-01-07 16:25:34 -0500915 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700916 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700917 return;
918 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700919 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500920 startActivityForResult(intent, REQUEST_SUGGESTION);
921 }
922
923 @Override
924 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
925 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
926 && resultCode != RESULT_CANCELED) {
927 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
928 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
929 }
930 super.onActivityResult(requestCode, resultCode, data);
931 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800932
933 @VisibleForTesting
934 Bitmap getBitmapFromXmlResource(int drawableRes) {
935 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
936 Canvas canvas = new Canvas();
937 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
938 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
939 canvas.setBitmap(bitmap);
940 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
941 drawable.draw(canvas);
942
943 return bitmap;
944 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700945}