blob: 7203e248bb57781213b3d48762b1ef7c1a43f13a [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;
Doris Ling1a6887b2017-10-20 15:29:56 -070058import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080059import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080060import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070061import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070062import com.android.settings.dashboard.DashboardFeatureProvider;
63import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.overlay.FeatureFactory;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070065import com.android.settings.search.SearchActivity;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080066import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070067import com.android.settings.widget.SwitchBar;
Tony Mantler3d84d562017-07-31 10:48:55 -070068import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070071
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070074import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075
Jason Monk4da79e02015-09-10 10:54:36 -040076public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077 implements PreferenceManager.OnPreferenceTreeClickListener,
78 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
83 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070084 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070085 @VisibleForTesting
86 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087
88 /**
89 * When starting this activity, the invoking Intent can contain this extra
90 * string to specify which fragment should be initially displayed.
91 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
92 * will call isValidFragment() to confirm that the fragment class name is valid for this
93 * activity.
94 */
95 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
96
97 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080098 * The metrics category constant for logging source when a setting fragment is opened.
99 */
100 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
101
102 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
104 * this extra can also be specified to supply a Bundle of arguments to pass
105 * to that fragment when it is instantiated during the initial creation
106 * of the activity.
107 */
108 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
109
110 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700111 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
112 */
113 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
114
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800115 public static final String BACK_STACK_PREFS = ":settings:prefs";
116
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117 // extras that allow any preference activity to be launched as part of a wizard
118
119 // show Back and Next buttons? takes boolean parameter
120 // Back will then return RESULT_CANCELED and Next RESULT_OK
121 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
122
123 // add a Skip button?
124 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
125
126 // specify custom text for the Back or Next buttons, or cause a button to not appear
127 // at all by setting it to null
128 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
129 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
130
131 /**
132 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700133 * 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 -0800134 * that fragment.
135 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100137 /**
138 * The package name used to resolve the title resource id.
139 */
140 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
141 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700142 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
143 ":settings:show_fragment_title_resid";
144 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
145 ":settings:show_fragment_as_shortcut";
146
147 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
148 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
Fan Zhang78ab9f92017-02-27 16:10:05 -0800150 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700151 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
152
Jason Monk30695812015-11-17 09:01:08 -0500153 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154 "com.android.settings.FRAGMENT_CLASS";
155
156 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
157
Jason Monkd4f03ec2016-01-07 16:25:34 -0500158 private static final int REQUEST_SUGGESTION = 42;
159
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800162 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700163 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800164
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700165 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
166 "android.settings.APPLICATION_DETAILS_SETTINGS"
167 };
168
Tony Mantler3d84d562017-07-31 10:48:55 -0700169 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 private boolean mBatteryPresent = true;
172 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 @Override
174 public void onReceive(Context context, Intent intent) {
175 String action = intent.getAction();
176 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
177 boolean batteryPresent = Utils.isBatteryPresent(intent);
178
179 if (mBatteryPresent != batteryPresent) {
180 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400181 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 }
183 }
184 }
185 };
186
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700187 private SwitchBar mSwitchBar;
188
189 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700190
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700191 @VisibleForTesting
192 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700193
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700194 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700195 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700196
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700197 private ViewGroup mContent;
198
Fan Zhangc6ca3142017-02-14 15:02:35 -0800199 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700200
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700201 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800202 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203
Fan Zhanga96a2d82016-09-27 17:51:11 -0700204 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500205 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700206
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700207 public SwitchBar getSwitchBar() {
208 return mSwitchBar;
209 }
210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 @Override
212 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling1a6887b2017-10-20 15:29:56 -0700213 if (InstrumentedPreferenceFragment.usePreferenceScreenTitle()) {
214 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
215 } else {
216 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
217 null, 0);
218 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 return true;
220 }
221
222 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400223 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 return false;
225 }
226
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800227 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500228 public SharedPreferences getSharedPreferences(String name, int mode) {
229 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500230 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500231 }
232 return super.getSharedPreferences(name, mode);
233 }
234
Jason Monke4ebcd12016-02-21 09:37:41 -0500235 private String getMetricsTag() {
236 String tag = getClass().getName();
237 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
238 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
239 }
240 if (tag.startsWith("com.android.settings.")) {
241 tag = tag.replace("com.android.settings.", "");
242 }
243 return tag;
244 }
245
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700246 private static boolean isShortCutIntent(final Intent intent) {
247 Set<String> categories = intent.getCategories();
248 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
249 }
250
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700251 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700252 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700253 if (action == null) {
254 return false;
255 }
256 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
257 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
258 }
259 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700260 }
261
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700262 @Override
263 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700264 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500265 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700266
267 final FeatureFactory factory = FeatureFactory.getFactory(this);
268
269 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800270 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700271
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700272 // Should happen before any call to getIntent()
273 getMetaData();
274
275 final Intent intent = getIntent();
276 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
277 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700280 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700281 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700282
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700283 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700284 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
285
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700286 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700287 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700288
Doris Ling1694d4b2017-03-03 14:21:37 -0800289 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700290
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700291 // This is a "Sub Settings" when:
292 // - this is a real SubSettings
293 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700294 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700295 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
296
Fan Zhang92f1e942017-01-21 10:07:26 -0800297 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
298 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700299 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800300 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700301 }
302
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700303 setContentView(mIsShowingDashboard ?
304 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700306 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700307
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 getFragmentManager().addOnBackStackChangedListener(this);
309
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700310 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700311 // We are restarting from a previous saved state; used that to initialize, instead
312 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700313 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800314
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700315 ArrayList<DashboardCategory> categories =
316 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
317 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700318 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700319 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700320 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700322
323 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700324
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800326 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 }
328
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700329 if (mIsShowingDashboard) {
330 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
331 findViewById(R.id.action_bar).setVisibility(View.GONE);
332 Toolbar toolbar = findViewById(R.id.search_action_bar);
333 toolbar.setOnClickListener(this);
334 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700335
336 // Please forgive me for what I am about to do.
337 //
338 // Need to make the navigation icon non-clickable so that the entire card is clickable
339 // and goes to the search UI. Also set the background to null so there's no ripple.
340 View navView = toolbar.getNavigationView();
341 navView.setClickable(false);
342 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700343 }
344
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700345 ActionBar actionBar = getActionBar();
346 if (actionBar != null) {
347 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
348 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700349 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700350 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500351 if (mSwitchBar != null) {
352 mSwitchBar.setMetricsTag(getMetricsTag());
353 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700354
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
357
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700358 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 if (buttonBar != null) {
360 buttonBar.setVisibility(View.VISIBLE);
361
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 backButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700369 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 skipButton.setOnClickListener(new OnClickListener() {
371 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700372 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 finish();
374 }
375 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700376 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800377 mNextButton.setOnClickListener(new OnClickListener() {
378 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700379 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800380 finish();
381 }
382 });
383
384 // set our various button parameters
385 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
386 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
387 if (TextUtils.isEmpty(buttonText)) {
388 mNextButton.setVisibility(View.GONE);
389 }
390 else {
391 mNextButton.setText(buttonText);
392 }
393 }
394 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
395 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
396 if (TextUtils.isEmpty(buttonText)) {
397 backButton.setVisibility(View.GONE);
398 }
399 else {
400 backButton.setText(buttonText);
401 }
402 }
403 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
404 skipButton.setVisibility(View.VISIBLE);
405 }
406 }
407 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700408
Fan Zhangefba6b42017-02-07 17:40:59 -0800409 if (DEBUG_TIMING) {
410 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
411 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700412 }
413
Doris Lingcb69baf2017-02-23 17:50:03 -0800414 @VisibleForTesting
415 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
416 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800417 // UP will be shown only if it is a sub settings
418 if (mIsShortcut) {
419 mDisplayHomeAsUpEnabled = isSubSettings;
420 } else if (isSubSettings) {
421 mDisplayHomeAsUpEnabled = true;
422 } else {
423 mDisplayHomeAsUpEnabled = false;
424 }
425 setTitleFromIntent(intent);
426
427 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
428 switchToFragment(initialFragmentName, initialArguments, true, false,
429 mInitialTitleResId, mInitialTitle, false);
430 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700431 // Show search icon as up affordance if we are displaying the main Dashboard
432 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800433 mInitialTitleResId = R.string.dashboard_title;
434
435 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
436 mInitialTitleResId, mInitialTitle, false);
437 }
438 }
439
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700440 private void setTitleFromIntent(Intent intent) {
441 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
442 if (initialTitleResId > 0) {
443 mInitialTitle = null;
444 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100445
446 final String initialTitleResPackageName = intent.getStringExtra(
447 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
448 if (initialTitleResPackageName != null) {
449 try {
450 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
451 0 /* flags */, new UserHandle(UserHandle.myUserId()));
452 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
453 setTitle(mInitialTitle);
454 mInitialTitleResId = -1;
455 return;
456 } catch (NameNotFoundException e) {
457 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
458 }
459 } else {
460 setTitle(mInitialTitleResId);
461 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700462 } else {
463 mInitialTitleResId = -1;
464 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
465 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
466 setTitle(mInitialTitle);
467 }
468 }
469
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800470 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700472 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
474
Fan Zhang28691572016-03-23 15:31:08 -0700475 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800476 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700477
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800478 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700479 if (mInitialTitleResId > 0) {
480 setTitle(mInitialTitleResId);
481 } else {
482 setTitle(mInitialTitle);
483 }
Fan Zhang28691572016-03-23 15:31:08 -0700484 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800485 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700486
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800487 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
488 setTitleFromBackStackEntry(bse);
489 }
490
491 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
492 final CharSequence title;
493 final int titleRes = bse.getBreadCrumbTitleRes();
494 if (titleRes > 0) {
495 title = getText(titleRes);
496 } else {
497 title = bse.getBreadCrumbTitle();
498 }
499 if (title != null) {
500 setTitle(title);
501 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800502 }
503
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800504 @Override
505 protected void onSaveInstanceState(Bundle outState) {
506 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700507 saveState(outState);
508 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700510 /**
511 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
512 */
513 @VisibleForTesting
514 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700515 if (mCategories.size() > 0) {
516 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700518
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700519 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700520 }
521
522 @Override
523 protected void onRestoreInstanceState(Bundle savedInstanceState) {
524 super.onRestoreInstanceState(savedInstanceState);
525
526 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 }
528
529 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400530 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 super.onResume();
532
Tony Mantler3d84d562017-07-31 10:48:55 -0700533 mDevelopmentSettingsListener = new BroadcastReceiver() {
534 @Override
535 public void onReceive(Context context, Intent intent) {
536 updateTilesList();
537 }
538 };
539 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
540 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700543
Jason Monk4da79e02015-09-10 10:54:36 -0400544 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 }
546
547 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400548 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700550 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
551 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 }
554
555 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800556 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
557 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
558 taskDescription.setIcon(icon);
559 super.setTaskDescription(taskDescription);
560 }
561
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 protected boolean isValidFragment(String fragmentName) {
563 // Almost all fragments are wrapped in this,
564 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800565 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
566 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 }
568 return false;
569 }
570
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 @Override
572 public Intent getIntent() {
573 Intent superIntent = super.getIntent();
574 String startingFragment = getStartingFragmentClass(superIntent);
575 // This is called from super.onCreate, isMultiPane() is not yet reliable
576 // Do not use onIsHidingHeaders either, which relies itself on this method
577 if (startingFragment != null) {
578 Intent modIntent = new Intent(superIntent);
579 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
580 Bundle args = superIntent.getExtras();
581 if (args != null) {
582 args = new Bundle(args);
583 } else {
584 args = new Bundle();
585 }
586 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100587 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 return modIntent;
589 }
590 return superIntent;
591 }
592
593 /**
594 * Checks if the component name in the intent is different from the Settings class and
595 * returns the class name to load as a fragment.
596 */
597 private String getStartingFragmentClass(Intent intent) {
598 if (mFragmentClass != null) return mFragmentClass;
599
600 String intentClass = intent.getComponent().getClassName();
601 if (intentClass.equals(getClass().getName())) return null;
602
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700603 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
605 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700606 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800607 }
608
609 return intentClass;
610 }
611
612 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000613 * Start a new fragment containing a preference panel. If the preferences
614 * are being displayed in multi-pane mode, the given fragment class will
615 * be instantiated and placed in the appropriate pane. If running in
616 * single-pane mode, a new activity will be launched in which to show the
617 * fragment.
618 *
619 * @param fragmentClass Full name of the class implementing the fragment.
620 * @param args Any desired arguments to supply to the fragment.
621 * @param titleRes Optional resource identifier of the title of this
622 * fragment.
623 * @param titleText Optional text of the title of this fragment.
624 * @param resultTo Optional fragment that result data should be sent to.
625 * If non-null, resultTo.onActivityResult() will be called when this
626 * preference panel is done. The launched panel must use
627 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
628 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700629 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000630 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800631 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
632 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700633 String title = null;
634 if (titleRes < 0) {
635 if (titleText != null) {
636 title = titleText.toString();
Doris Ling1a6887b2017-10-20 15:29:56 -0700637 } else if (!InstrumentedPreferenceFragment.usePreferenceScreenTitle()) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700638 // There not much we can do in that case
639 title = "";
640 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700641 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700642 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800643 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000644 }
645
646 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100647 * Start a new fragment in a new activity containing a preference panel for a given user. If the
648 * preferences are being displayed in multi-pane mode, the given fragment class will be
649 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
650 * activity will be launched in which to show the fragment.
651 *
652 * @param fragmentClass Full name of the class implementing the fragment.
653 * @param args Any desired arguments to supply to the fragment.
654 * @param titleRes Optional resource identifier of the title of this fragment.
655 * @param titleText Optional text of the title of this fragment.
656 * @param userHandle The user for which the panel has to be started.
657 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800658 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
659 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700660 // This is a workaround.
661 //
662 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
663 // starting the fragment could cause a native stack corruption. See b/17523189. However,
664 // adding that flag and start the preference panel with the same UserHandler will make it
665 // impossible to use back button to return to the previous screen. See b/20042570.
666 //
667 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
668 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
669 // when we're calling it as the same user.
670 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800671 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700672 } else {
673 String title = null;
674 if (titleRes < 0) {
675 if (titleText != null) {
676 title = titleText.toString();
677 } else {
678 // There not much we can do in that case
679 title = "";
680 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100681 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800682 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
683 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100684 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100685 }
686
687 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800688 * Called by a preference panel fragment to finish itself.
689 *
690 * @param caller The fragment that is asking to be finished.
691 * @param resultCode Optional result code to send back to the original
692 * launching fragment.
693 * @param resultData Optional result data to send back to the original
694 * launching fragment.
695 */
696 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
697 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700698 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800699 }
700
701 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000702 * Start a new fragment.
703 *
704 * @param fragment The fragment to start
705 * @param push If true, the current fragment will be pushed onto the back stack. If false,
706 * the current fragment will be replaced.
707 */
708 public void startPreferenceFragment(Fragment fragment, boolean push) {
709 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700710 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000711 if (push) {
712 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
713 transaction.addToBackStack(BACK_STACK_PREFS);
714 } else {
715 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
716 }
717 transaction.commitAllowingStateLoss();
718 }
719
720 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700721 * Switch to a specific Fragment with taking care of validation, Title and BackStack
722 */
723 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700724 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700725 if (validate && !isValidFragment(fragmentName)) {
726 throw new IllegalArgumentException("Invalid fragment for this activity: "
727 + fragmentName);
728 }
729 Fragment f = Fragment.instantiate(this, fragmentName, args);
730 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700731 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700732 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700733 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 }
735 if (addToBackStack) {
736 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
737 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700738 if (titleResId > 0) {
739 transaction.setBreadCrumbTitle(titleResId);
740 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 transaction.setBreadCrumbTitle(title);
742 }
743 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700744 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700745 return f;
746 }
747
Jason Monk4da79e02015-09-10 10:54:36 -0400748 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500749 // Generally the items that are will be changing from these updates will
750 // not be in the top list of tiles, so run it in the background and the
751 // SettingsDrawerActivity will pick up on the updates automatically.
752 AsyncTask.execute(new Runnable() {
753 @Override
754 public void run() {
755 doUpdateTilesList();
756 }
757 });
758 }
759
760 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400761 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700762 final UserManager um = UserManager.get(this);
763 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700764 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400765 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700766 somethingChanged = setTileEnabled(
767 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
768 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700769
Fan Zhangaeb94f02017-07-05 13:46:04 -0700770 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800771 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700772 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
773 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700774
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700775 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
776 .getDataPlanFeatureProvider()
777 .isEnabled();
778
779 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
780 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
781 // reverse.
782
783 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
784 // disable DataPlanUsageSummaryActivity.
785 somethingChanged = setTileEnabled(
786 new ComponentName(packageName,
787 isDataPlanFeatureEnabled
788 ? Settings.DataUsageSummaryActivity.class.getName()
789 : Settings.DataPlanUsageSummaryActivity.class.getName()),
790 false /* enabled */,
791 isAdmin) || somethingChanged;
792
793 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
794 // enable DataPlanUsageSummaryActivity.
795 somethingChanged = setTileEnabled(
796 new ComponentName(packageName,
797 isDataPlanFeatureEnabled
798 ? Settings.DataPlanUsageSummaryActivity.class.getName()
799 : Settings.DataUsageSummaryActivity.class.getName()),
800 Utils.isBandwidthControlEnabled() /* enabled */,
801 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700802
Fan Zhangaeb94f02017-07-05 13:46:04 -0700803 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800804 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700805 Utils.showSimCardTile(this), isAdmin)
806 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700807
Fan Zhangaeb94f02017-07-05 13:46:04 -0700808 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800809 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700810 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400811
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400814 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700815 && !Utils.isMonkeyRunning(), isAdmin)
816 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400817
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800819 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700820 !UserManager.isDeviceInDemoMode(this), isAdmin)
821 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800822
Fan Zhangaeb94f02017-07-05 13:46:04 -0700823 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800824 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 !UserManager.isDeviceInDemoMode(this), isAdmin)
826 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700827
Fan Zhangaeb94f02017-07-05 13:46:04 -0700828 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700829 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700830 !UserManager.isDeviceInDemoMode(this), isAdmin)
831 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400832
Fan Zhangaeb94f02017-07-05 13:46:04 -0700833 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800834 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700835 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
836 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400837
Tony Mantler3d84d562017-07-31 10:48:55 -0700838 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Dan Sandler12c4ba42016-03-28 11:13:40 -0400839 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
jeffreyhuang142682c2017-10-17 11:05:15 -0700840 final boolean useDevOptionV1 = Settings.DevelopmentSettingsActivity.isEnabled();
Fan Zhang4c26da92017-09-08 15:29:54 -0700841 // Enable old Dev option if v2 is disabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700842 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400843 Settings.DevelopmentSettingsActivity.class.getName()),
jeffreyhuang142682c2017-10-17 11:05:15 -0700844 showDev && useDevOptionV1, isAdmin)
Fan Zhang4c26da92017-09-08 15:29:54 -0700845 || somethingChanged;
846 // Enable new Dev option if v2 is enable
847 somethingChanged = setTileEnabled(new ComponentName(packageName,
848 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuang142682c2017-10-17 11:05:15 -0700849 showDev && !useDevOptionV1, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700850 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400851
Fan Zhanga6986e72017-03-08 09:24:45 -0800852 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700853 somethingChanged = setTileEnabled(new ComponentName(packageName,
854 BackupSettingsActivity.class.getName()), true, isAdmin)
855 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800856
Fan Zhangaeb94f02017-07-05 13:46:04 -0700857 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800858 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700859 WifiDisplaySettings.isAvailable(this), isAdmin)
860 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800861
Jason Monk4da79e02015-09-10 10:54:36 -0400862 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800863
Jason Monk4da79e02015-09-10 10:54:36 -0400864 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800865 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800866 synchronized (categories) {
867 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700868 final int tileCount = category.getTilesCount();
869 for (int i = 0; i < tileCount; i++) {
870 final ComponentName component = category.getTile(i).intent.getComponent();
871
Fan Zhanga6986e72017-03-08 09:24:45 -0800872 final String name = component.getClassName();
873 final boolean isEnabledForRestricted = ArrayUtils.contains(
874 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
875 if (packageName.equals(component.getPackageName())
876 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700877 somethingChanged = setTileEnabled(component, false, isAdmin)
878 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800879 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700880 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700881 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700882 }
883 }
Doris Ling26bf0032016-06-15 18:16:09 -0700884
Fan Zhang224caad2017-01-06 11:42:48 -0800885 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700886 if (somethingChanged) {
887 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
888 updateCategories();
889 } else {
890 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
891 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500892 }
893
Fan Zhangaeb94f02017-07-05 13:46:04 -0700894 /**
895 * @return whether or not the enabled state actually changed.
896 */
897 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400898 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800899 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
900 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400901 enabled = false;
902 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700903 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500904 }
905
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800906 private void getMetaData() {
907 try {
908 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
909 PackageManager.GET_META_DATA);
910 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800911 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
912 } catch (NameNotFoundException nnfe) {
913 // No recovery
914 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
915 }
916 }
917
918 // give subclasses access to the Next button
919 public boolean hasNextButton() {
920 return mNextButton != null;
921 }
922
923 public Button getNextButton() {
924 return mNextButton;
925 }
926
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800927 @Override
928 public boolean shouldUpRecreateTask(Intent targetIntent) {
929 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
930 }
931
Jason Monkd4f03ec2016-01-07 16:25:34 -0500932 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700933 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700934 return;
935 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700936 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500937 startActivityForResult(intent, REQUEST_SUGGESTION);
938 }
939
940 @Override
941 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
942 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
943 && resultCode != RESULT_CANCELED) {
944 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
945 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
946 }
947 super.onActivityResult(requestCode, resultCode, data);
948 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800949
950 @VisibleForTesting
951 Bitmap getBitmapFromXmlResource(int drawableRes) {
952 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
953 Canvas canvas = new Canvas();
954 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
955 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
956 canvas.setBitmap(bitmap);
957 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
958 drawable.draw(canvas);
959
960 return bitmap;
961 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700962
963 @Override
964 public void onClick(View v) {
965 Intent intent = new Intent(this, SearchActivity.class);
966 startActivity(intent);
967 }
968}