blob: d28eed6f225ba5bf6a97734968c4c3876ac4d609 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fan Zhang6013a952018-02-06 11:36:07 -080019import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20
Fan Zhang60f01072016-10-13 16:53:41 -070021import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070022import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080023import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080035import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080036import android.graphics.Canvas;
37import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050038import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.os.UserHandle;
41import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080042import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070044import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040045import android.support.v7.preference.Preference;
46import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070048import android.transition.TransitionManager;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080049import android.util.FeatureFlagUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.view.View;
52import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070055import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070056
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080057import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040058import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070059import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000060import com.android.settings.backup.BackupSettingsActivity;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080061import com.android.settings.core.FeatureFlags;
Fan Zhang615563d2018-02-15 15:19:33 -080062import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080063import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.dashboard.DashboardFeatureProvider;
65import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070066import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080067import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070068import com.android.settings.widget.SwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080069import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080070import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
71import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070072import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040073import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040074import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070075
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070078import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079
Jason Monk4da79e02015-09-10 10:54:36 -040080public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081 implements PreferenceManager.OnPreferenceTreeClickListener,
82 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080083 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084
85 private static final String LOG_TAG = "Settings";
86
87 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070088 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089
90 /**
91 * When starting this activity, the invoking Intent can contain this extra
92 * string to specify which fragment should be initially displayed.
93 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
94 * will call isValidFragment() to confirm that the fragment class name is valid for this
95 * activity.
96 */
97 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
98
99 /**
100 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
101 * this extra can also be specified to supply a Bundle of arguments to pass
102 * to that fragment when it is instantiated during the initial creation
103 * of the activity.
104 */
105 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
106
107 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700108 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
109 */
110 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
111
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800112 public static final String BACK_STACK_PREFS = ":settings:prefs";
113
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114 // extras that allow any preference activity to be launched as part of a wizard
115
116 // show Back and Next buttons? takes boolean parameter
117 // Back will then return RESULT_CANCELED and Next RESULT_OK
118 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
119
120 // add a Skip button?
121 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
122
123 // specify custom text for the Back or Next buttons, or cause a button to not appear
124 // at all by setting it to null
125 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
126 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
127
128 /**
129 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700130 * 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 -0800131 * that fragment.
132 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700133 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100134 /**
135 * The package name used to resolve the title resource id.
136 */
137 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
138 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700139 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
140 ":settings:show_fragment_title_resid";
141 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
142 ":settings:show_fragment_as_shortcut";
143
144 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
145 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800146
Fan Zhang78ab9f92017-02-27 16:10:05 -0800147 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700148 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
149
Jason Monk30695812015-11-17 09:01:08 -0500150 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800151 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
153 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
154
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800156
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800157 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700158 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800159
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700160 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
161 "android.settings.APPLICATION_DETAILS_SETTINGS"
162 };
163
Tony Mantler3d84d562017-07-31 10:48:55 -0700164 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 private boolean mBatteryPresent = true;
167 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 @Override
169 public void onReceive(Context context, Intent intent) {
170 String action = intent.getAction();
171 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
172 boolean batteryPresent = Utils.isBatteryPresent(intent);
173
174 if (mBatteryPresent != batteryPresent) {
175 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400176 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 }
178 }
179 }
180 };
181
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700182 private SwitchBar mSwitchBar;
183
184 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700185
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700186 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700187 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700188
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700189 private ViewGroup mContent;
190
Fan Zhangc6ca3142017-02-14 15:02:35 -0800191 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700192
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700193 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800194 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195
Fan Zhanga96a2d82016-09-27 17:51:11 -0700196 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700197
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700198 public SwitchBar getSwitchBar() {
199 return mSwitchBar;
200 }
201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 @Override
203 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800204 new SubSettingLauncher(this)
205 .setDestination(pref.getFragment())
206 .setArguments(pref.getExtras())
207 .setSourceMetricsCategory(caller instanceof Instrumentable
208 ? ((Instrumentable) caller).getMetricsCategory()
209 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
210 .setTitle(-1)
211 .launch();
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")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800223 return new SharedPreferencesLogger(this, getMetricsTag(),
224 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500225 }
226 return super.getSharedPreferences(name, mode);
227 }
228
Jason Monke4ebcd12016-02-21 09:37:41 -0500229 private String getMetricsTag() {
230 String tag = getClass().getName();
231 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
232 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
233 }
234 if (tag.startsWith("com.android.settings.")) {
235 tag = tag.replace("com.android.settings.", "");
236 }
237 return tag;
238 }
239
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700240 private static boolean isShortCutIntent(final Intent intent) {
241 Set<String> categories = intent.getCategories();
242 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
243 }
244
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700245 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700246 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700247 if (action == null) {
248 return false;
249 }
250 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
251 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
252 }
253 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700254 }
255
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700256 @Override
257 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700258 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500259 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700260
261 final FeatureFactory factory = FeatureFactory.getFactory(this);
262
263 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800264 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700265
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700266 // Should happen before any call to getIntent()
267 getMetaData();
268
269 final Intent intent = getIntent();
270 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
271 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700274 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700275 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700276
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700277 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700278 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
279
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700280 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700281 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700282
Doris Ling1694d4b2017-03-03 14:21:37 -0800283 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700284
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700285 // This is a "Sub Settings" when:
286 // - this is a real SubSettings
287 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700288 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700289 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
290
Fan Zhang92f1e942017-01-21 10:07:26 -0800291 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
292 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700293 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800294 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700295 }
296
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700297 setContentView(mIsShowingDashboard ?
298 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700300 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700301
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 getFragmentManager().addOnBackStackChangedListener(this);
303
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700304 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700305 // We are restarting from a previous saved state; used that to initialize, instead
306 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700307 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800308
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700309 ArrayList<DashboardCategory> categories =
310 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
311 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700312 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700313 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700314 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800317 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 }
319
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700320 if (mIsShowingDashboard) {
321 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
322 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800323 final Toolbar toolbar = findViewById(R.id.search_action_bar);
324 FeatureFactory.getFactory(this).getSearchFeatureProvider()
325 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700326 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700327
328 // Please forgive me for what I am about to do.
329 //
330 // Need to make the navigation icon non-clickable so that the entire card is clickable
331 // and goes to the search UI. Also set the background to null so there's no ripple.
332 View navView = toolbar.getNavigationView();
333 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800334 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700335 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700336 }
337
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700338 ActionBar actionBar = getActionBar();
339 if (actionBar != null) {
Fan Zhangf11c8852018-02-06 14:21:01 -0800340 actionBar.setDisplayHomeAsUpEnabled(true);
341 actionBar.setHomeButtonEnabled(true);
Fan Zhange096ae72017-11-30 14:41:04 -0800342 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700343 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700344 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500345 if (mSwitchBar != null) {
346 mSwitchBar.setMetricsTag(getMetricsTag());
347 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700348
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
351
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700352 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 if (buttonBar != null) {
354 buttonBar.setVisibility(View.VISIBLE);
355
Fan Zhangf11c8852018-02-06 14:21:01 -0800356 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 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800363 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 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800370 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);
Fan Zhangf11c8852018-02-06 14:21:01 -0800383 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800384 mNextButton.setText(buttonText);
385 }
386 }
387 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
388 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
389 if (TextUtils.isEmpty(buttonText)) {
390 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800391 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800392 backButton.setText(buttonText);
393 }
394 }
395 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
396 skipButton.setVisibility(View.VISIBLE);
397 }
398 }
399 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700400
Fan Zhangefba6b42017-02-07 17:40:59 -0800401 if (DEBUG_TIMING) {
402 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
403 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700404 }
405
Doris Lingcb69baf2017-02-23 17:50:03 -0800406 @VisibleForTesting
407 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
408 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800409 setTitleFromIntent(intent);
410
411 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
412 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800413 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800414 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700415 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800416 mInitialTitleResId = R.string.dashboard_title;
417
418 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800419 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800420 }
421 }
422
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700423 private void setTitleFromIntent(Intent intent) {
424 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
425 if (initialTitleResId > 0) {
426 mInitialTitle = null;
427 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100428
429 final String initialTitleResPackageName = intent.getStringExtra(
430 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
431 if (initialTitleResPackageName != null) {
432 try {
433 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
434 0 /* flags */, new UserHandle(UserHandle.myUserId()));
435 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
436 setTitle(mInitialTitle);
437 mInitialTitleResId = -1;
438 return;
439 } catch (NameNotFoundException e) {
440 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
441 }
442 } else {
443 setTitle(mInitialTitleResId);
444 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700445 } else {
446 mInitialTitleResId = -1;
447 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
448 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
449 setTitle(mInitialTitle);
450 }
451 }
452
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800453 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800454 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700455 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800456 }
457
Fan Zhang28691572016-03-23 15:31:08 -0700458 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800459 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700460
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800461 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700462 if (mInitialTitleResId > 0) {
463 setTitle(mInitialTitleResId);
464 } else {
465 setTitle(mInitialTitle);
466 }
Fan Zhang28691572016-03-23 15:31:08 -0700467 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800468 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700469
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800470 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
471 setTitleFromBackStackEntry(bse);
472 }
473
474 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
475 final CharSequence title;
476 final int titleRes = bse.getBreadCrumbTitleRes();
477 if (titleRes > 0) {
478 title = getText(titleRes);
479 } else {
480 title = bse.getBreadCrumbTitle();
481 }
482 if (title != null) {
483 setTitle(title);
484 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800485 }
486
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800487 @Override
488 protected void onSaveInstanceState(Bundle outState) {
489 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700490 saveState(outState);
491 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700493 /**
494 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
495 */
496 @VisibleForTesting
497 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700498 if (mCategories.size() > 0) {
499 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800500 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800501 }
502
503 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400504 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800505 super.onResume();
506
Tony Mantler3d84d562017-07-31 10:48:55 -0700507 mDevelopmentSettingsListener = new BroadcastReceiver() {
508 @Override
509 public void onReceive(Context context, Intent intent) {
510 updateTilesList();
511 }
512 };
513 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
514 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700517
Jason Monk4da79e02015-09-10 10:54:36 -0400518 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800519 }
520
521 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400522 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800523 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700524 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
525 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 }
528
529 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800530 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
531 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
532 taskDescription.setIcon(icon);
533 super.setTaskDescription(taskDescription);
534 }
535
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 protected boolean isValidFragment(String fragmentName) {
537 // Almost all fragments are wrapped in this,
538 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800539 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
540 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 }
542 return false;
543 }
544
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 @Override
546 public Intent getIntent() {
547 Intent superIntent = super.getIntent();
548 String startingFragment = getStartingFragmentClass(superIntent);
549 // This is called from super.onCreate, isMultiPane() is not yet reliable
550 // Do not use onIsHidingHeaders either, which relies itself on this method
551 if (startingFragment != null) {
552 Intent modIntent = new Intent(superIntent);
553 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
554 Bundle args = superIntent.getExtras();
555 if (args != null) {
556 args = new Bundle(args);
557 } else {
558 args = new Bundle();
559 }
560 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100561 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 return modIntent;
563 }
564 return superIntent;
565 }
566
567 /**
568 * Checks if the component name in the intent is different from the Settings class and
569 * returns the class name to load as a fragment.
570 */
571 private String getStartingFragmentClass(Intent intent) {
572 if (mFragmentClass != null) return mFragmentClass;
573
574 String intentClass = intent.getComponent().getClassName();
575 if (intentClass.equals(getClass().getName())) return null;
576
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700577 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800578 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
579 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700580 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 }
582
583 return intentClass;
584 }
585
586 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000587 * Start a new fragment containing a preference panel. If the preferences
588 * are being displayed in multi-pane mode, the given fragment class will
589 * be instantiated and placed in the appropriate pane. If running in
590 * single-pane mode, a new activity will be launched in which to show the
591 * fragment.
592 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800593 * @param fragmentClass Full name of the class implementing the fragment.
594 * @param args Any desired arguments to supply to the fragment.
595 * @param titleRes Optional resource identifier of the title of this
596 * fragment.
597 * @param titleText Optional text of the title of this fragment.
598 * @param resultTo Optional fragment that result data should be sent to.
599 * If non-null, resultTo.onActivityResult() will be called when this
600 * preference panel is done. The launched panel must use
601 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000602 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fan Zhangf11c8852018-02-06 14:21:01 -0800603 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000604 */
Fan Zhang7cf99f52018-02-16 10:37:37 -0800605 @Deprecated
Fan Zhangc6ca3142017-02-14 15:02:35 -0800606 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
607 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700608 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800609 if (titleRes < 0 && titleText != null) {
610 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700611 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700612 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800613 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000614 }
615
616 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100617 * Start a new fragment in a new activity containing a preference panel for a given user. If the
618 * preferences are being displayed in multi-pane mode, the given fragment class will be
619 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
620 * activity will be launched in which to show the fragment.
621 *
622 * @param fragmentClass Full name of the class implementing the fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800623 * @param args Any desired arguments to supply to the fragment.
624 * @param titleRes Optional resource identifier of the title of this fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800625 * @param userHandle The user for which the panel has to be started.
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100626 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800627 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
Fan Zhang615563d2018-02-15 15:19:33 -0800628 Bundle args, int titleRes, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700629 // This is a workaround.
630 //
631 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
632 // starting the fragment could cause a native stack corruption. See b/17523189. However,
633 // adding that flag and start the preference panel with the same UserHandler will make it
634 // impossible to use back button to return to the previous screen. See b/20042570.
635 //
636 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
637 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
638 // when we're calling it as the same user.
639 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhang615563d2018-02-15 15:19:33 -0800640 startPreferencePanel(caller, fragmentClass, args, titleRes, null /* titleText */,
641 null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700642 } else {
Fan Zhang615563d2018-02-15 15:19:33 -0800643 new SubSettingLauncher(this)
644 .setDestination(fragmentClass)
645 .setArguments(args)
646 .setTitle(titleRes)
647 .setIsShortCut(mIsShortcut)
648 .setSourceMetricsCategory(mMetricsFeatureProvider.getMetricsCategory(caller))
649 .setUserHandle(userHandle)
650 .launch();
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100651 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100652 }
653
654 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 * Called by a preference panel fragment to finish itself.
656 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800657 * @param caller The fragment that is asking to be finished.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800659 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800661 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800662 */
663 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
664 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700665 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 }
667
668 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000669 * Start a new fragment.
670 *
671 * @param fragment The fragment to start
Fan Zhangf11c8852018-02-06 14:21:01 -0800672 * @param push If true, the current fragment will be pushed onto the back stack. If false,
673 * the current fragment will be replaced.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000674 */
675 public void startPreferenceFragment(Fragment fragment, boolean push) {
676 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700677 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000678 if (push) {
679 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
680 transaction.addToBackStack(BACK_STACK_PREFS);
681 } else {
682 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
683 }
684 transaction.commitAllowingStateLoss();
685 }
686
687 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700688 * Switch to a specific Fragment with taking care of validation, Title and BackStack
689 */
690 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700691 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700692 if (validate && !isValidFragment(fragmentName)) {
693 throw new IllegalArgumentException("Invalid fragment for this activity: "
694 + fragmentName);
695 }
696 Fragment f = Fragment.instantiate(this, fragmentName, args);
697 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700698 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700699 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700700 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700701 }
702 if (addToBackStack) {
703 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
704 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700705 if (titleResId > 0) {
706 transaction.setBreadCrumbTitle(titleResId);
707 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700708 transaction.setBreadCrumbTitle(title);
709 }
710 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700711 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700712 return f;
713 }
714
Jason Monk4da79e02015-09-10 10:54:36 -0400715 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500716 // Generally the items that are will be changing from these updates will
717 // not be in the top list of tiles, so run it in the background and the
718 // SettingsDrawerActivity will pick up on the updates automatically.
719 AsyncTask.execute(new Runnable() {
720 @Override
721 public void run() {
722 doUpdateTilesList();
723 }
724 });
725 }
726
727 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400728 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700729 final UserManager um = UserManager.get(this);
730 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800731 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700732 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400733 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700734 somethingChanged = setTileEnabled(
735 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
736 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700737
Fan Zhangaeb94f02017-07-05 13:46:04 -0700738 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800739 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700740 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
741 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700742
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700743
744 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
745 // enable DataPlanUsageSummaryActivity.
746 somethingChanged = setTileEnabled(
Fan Zhang3ed55b12018-02-07 13:50:09 -0800747 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700748 Utils.isBandwidthControlEnabled() /* enabled */,
749 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700750
jackqdyulei76ba0f72017-11-17 14:14:51 -0800751 final boolean isConnectedDeviceV2Enabled =
752 Settings.ConnectedDeviceDashboardActivity.isEnabled();
753 // Enable new connected page if v2 enabled
754 somethingChanged = setTileEnabled(
755 new ComponentName(packageName,
756 Settings.ConnectedDeviceDashboardActivity.class.getName()),
757 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
758 isAdmin) || somethingChanged;
759 // Enable old connected page if v2 disabled
760 somethingChanged = setTileEnabled(
761 new ComponentName(packageName,
762 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
763 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
764 isAdmin) || somethingChanged;
765
Fan Zhangaeb94f02017-07-05 13:46:04 -0700766 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800767 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700768 Utils.showSimCardTile(this), isAdmin)
769 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700770
Fan Zhangf3144942017-12-22 09:59:37 -0800771 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800772 .getPowerUsageFeatureProvider(this)
773 .isBatteryV2Enabled();
774 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700775 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800776 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800777 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
778 // Enable legacy battery page if v2 disabled
779 somethingChanged = setTileEnabled(new ComponentName(packageName,
780 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
781 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400782
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800783 final boolean isDataUsageSettingsV2Enabled =
784 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
785 // Enable new data usage page if v2 enabled
786 somethingChanged = setTileEnabled(new ComponentName(packageName,
787 Settings.DataUsageSummaryActivity.class.getName()),
788 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
789 || somethingChanged;
790 // Enable legacy data usage page if v2 disabled
791 somethingChanged = setTileEnabled(new ComponentName(packageName,
792 Settings.DataUsageSummaryLegacyActivity.class.getName()),
793 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
794 || somethingChanged;
795
Fan Zhangaeb94f02017-07-05 13:46:04 -0700796 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800797 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400798 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700799 && !Utils.isMonkeyRunning(), isAdmin)
800 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400801
Fan Zhangaeb94f02017-07-05 13:46:04 -0700802 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800803 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700804 !UserManager.isDeviceInDemoMode(this), isAdmin)
805 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700808 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 !UserManager.isDeviceInDemoMode(this), isAdmin)
810 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400811
Tony Mantler3d84d562017-07-31 10:48:55 -0700812 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800813 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800814
Fan Zhang4c26da92017-09-08 15:29:54 -0700815 somethingChanged = setTileEnabled(new ComponentName(packageName,
816 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800817 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400819
Fan Zhanga6986e72017-03-08 09:24:45 -0800820 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700821 somethingChanged = setTileEnabled(new ComponentName(packageName,
822 BackupSettingsActivity.class.getName()), true, isAdmin)
823 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800824
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800826 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700827 WifiDisplaySettings.isAvailable(this), isAdmin)
828 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800829
Daniel Nishi1e620952017-12-19 10:15:14 -0800830 // Enable/disable the Me Card page.
831 final boolean isMeCardEnabled = featureFactory
832 .getAccountFeatureProvider()
833 .isMeCardEnabled(this);
834 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800835 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800836 isMeCardEnabled, isAdmin)
837 || somethingChanged;
838 somethingChanged = setTileEnabled(new ComponentName(packageName,
839 Settings.DeviceInfoSettingsActivity.class.getName()),
840 !isMeCardEnabled, isAdmin)
841 || somethingChanged;
842
Jason Monk4da79e02015-09-10 10:54:36 -0400843 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800844
Jason Monk4da79e02015-09-10 10:54:36 -0400845 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800846 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800847 synchronized (categories) {
848 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700849 final int tileCount = category.getTilesCount();
850 for (int i = 0; i < tileCount; i++) {
851 final ComponentName component = category.getTile(i).intent.getComponent();
852
Fan Zhanga6986e72017-03-08 09:24:45 -0800853 final String name = component.getClassName();
854 final boolean isEnabledForRestricted = ArrayUtils.contains(
855 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
856 if (packageName.equals(component.getPackageName())
857 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700858 somethingChanged = setTileEnabled(component, false, isAdmin)
859 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800860 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700861 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700862 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700863 }
864 }
Doris Ling26bf0032016-06-15 18:16:09 -0700865
Fan Zhang224caad2017-01-06 11:42:48 -0800866 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700867 if (somethingChanged) {
868 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
869 updateCategories();
870 } else {
871 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
872 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500873 }
874
Fan Zhangaeb94f02017-07-05 13:46:04 -0700875 /**
876 * @return whether or not the enabled state actually changed.
877 */
878 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400879 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800880 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
881 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400882 enabled = false;
883 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700884 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500885 }
886
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800887 private void getMetaData() {
888 try {
889 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
890 PackageManager.GET_META_DATA);
891 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800892 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
893 } catch (NameNotFoundException nnfe) {
894 // No recovery
895 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
896 }
897 }
898
899 // give subclasses access to the Next button
900 public boolean hasNextButton() {
901 return mNextButton != null;
902 }
903
904 public Button getNextButton() {
905 return mNextButton;
906 }
907
jackqdyulei6c355f52017-03-01 17:37:23 -0800908 @VisibleForTesting
909 Bitmap getBitmapFromXmlResource(int drawableRes) {
910 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
911 Canvas canvas = new Canvas();
912 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
913 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
914 canvas.setBitmap(bitmap);
915 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
916 drawable.draw(canvas);
917
918 return bitmap;
919 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700920}