blob: 1202d1301af9464ecd8885b36485f8f31f825f5d [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fan Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050036import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.UserHandle;
39import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080040import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070042import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.view.View;
49import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070052import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070053
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040055import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070056import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000057import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080058import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080059import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070060import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070064import com.android.settings.search.SearchActivity;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080065import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070066import com.android.settings.widget.SwitchBar;
Tony Mantler3d84d562017-07-31 10:48:55 -070067import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070070
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070073import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074
Jason Monk4da79e02015-09-10 10:54:36 -040075public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076 implements PreferenceManager.OnPreferenceTreeClickListener,
77 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070078 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079
80 private static final String LOG_TAG = "Settings";
81
82 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070083 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070084 @VisibleForTesting
85 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080086
87 /**
88 * When starting this activity, the invoking Intent can contain this extra
89 * string to specify which fragment should be initially displayed.
90 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
91 * will call isValidFragment() to confirm that the fragment class name is valid for this
92 * activity.
93 */
94 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
95
96 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080097 * The metrics category constant for logging source when a setting fragment is opened.
98 */
99 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
100
101 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
103 * this extra can also be specified to supply a Bundle of arguments to pass
104 * to that fragment when it is instantiated during the initial creation
105 * of the activity.
106 */
107 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
108
109 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700110 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
111 */
112 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
113
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800114 public static final String BACK_STACK_PREFS = ":settings:prefs";
115
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116 // extras that allow any preference activity to be launched as part of a wizard
117
118 // show Back and Next buttons? takes boolean parameter
119 // Back will then return RESULT_CANCELED and Next RESULT_OK
120 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
121
122 // add a Skip button?
123 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
124
125 // specify custom text for the Back or Next buttons, or cause a button to not appear
126 // at all by setting it to null
127 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
128 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
129
130 /**
131 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700132 * 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 -0800133 * that fragment.
134 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700135 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100136 /**
137 * The package name used to resolve the title resource id.
138 */
139 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
140 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700141 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
142 ":settings:show_fragment_title_resid";
143 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
144 ":settings:show_fragment_as_shortcut";
145
146 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
147 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800148
Fan Zhang78ab9f92017-02-27 16:10:05 -0800149 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700150 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
151
Jason Monk30695812015-11-17 09:01:08 -0500152 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153 "com.android.settings.FRAGMENT_CLASS";
154
155 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
156
Jason Monkd4f03ec2016-01-07 16:25:34 -0500157 private static final int REQUEST_SUGGESTION = 42;
158
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800159 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800161 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700162 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800163
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700164 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
165 "android.settings.APPLICATION_DETAILS_SETTINGS"
166 };
167
Tony Mantler3d84d562017-07-31 10:48:55 -0700168 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 private boolean mBatteryPresent = true;
171 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 @Override
173 public void onReceive(Context context, Intent intent) {
174 String action = intent.getAction();
175 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
176 boolean batteryPresent = Utils.isBatteryPresent(intent);
177
178 if (mBatteryPresent != batteryPresent) {
179 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400180 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800181 }
182 }
183 }
184 };
185
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700186 private SwitchBar mSwitchBar;
187
188 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700189
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700190 @VisibleForTesting
191 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700192
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700193 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700194 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700195
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700196 private ViewGroup mContent;
197
Fan Zhangc6ca3142017-02-14 15:02:35 -0800198 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700199
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700200 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800201 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202
Fan Zhanga96a2d82016-09-27 17:51:11 -0700203 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500204 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700205
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700206 public SwitchBar getSwitchBar() {
207 return mSwitchBar;
208 }
209
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210 @Override
211 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800212 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213 null, 0);
214 return true;
215 }
216
217 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400218 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 return false;
220 }
221
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800222 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500223 public SharedPreferences getSharedPreferences(String name, int mode) {
224 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500225 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500226 }
227 return super.getSharedPreferences(name, mode);
228 }
229
Jason Monke4ebcd12016-02-21 09:37:41 -0500230 private String getMetricsTag() {
231 String tag = getClass().getName();
232 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
233 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
234 }
235 if (tag.startsWith("com.android.settings.")) {
236 tag = tag.replace("com.android.settings.", "");
237 }
238 return tag;
239 }
240
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700241 private static boolean isShortCutIntent(final Intent intent) {
242 Set<String> categories = intent.getCategories();
243 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
244 }
245
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700246 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700247 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700248 if (action == null) {
249 return false;
250 }
251 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
252 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
253 }
254 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700255 }
256
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700257 @Override
258 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700259 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500260 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700261
262 final FeatureFactory factory = FeatureFactory.getFactory(this);
263
264 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800265 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700266
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700267 // Should happen before any call to getIntent()
268 getMetaData();
269
270 final Intent intent = getIntent();
271 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
272 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700275 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700276 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700277
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700278 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700279 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
280
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700281 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700282 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700283
Doris Ling1694d4b2017-03-03 14:21:37 -0800284 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700285
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700286 // This is a "Sub Settings" when:
287 // - this is a real SubSettings
288 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700289 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700290 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
291
Fan Zhang92f1e942017-01-21 10:07:26 -0800292 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
293 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700294 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800295 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700296 }
297
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700298 setContentView(mIsShowingDashboard ?
299 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700301 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700302
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 getFragmentManager().addOnBackStackChangedListener(this);
304
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700305 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700306 // We are restarting from a previous saved state; used that to initialize, instead
307 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700308 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800309
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700310 ArrayList<DashboardCategory> categories =
311 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
312 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700313 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700314 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700315 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700317
318 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700319
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800321 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 }
323
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700324 if (mIsShowingDashboard) {
325 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
326 findViewById(R.id.action_bar).setVisibility(View.GONE);
327 Toolbar toolbar = findViewById(R.id.search_action_bar);
328 toolbar.setOnClickListener(this);
329 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700330
331 // Please forgive me for what I am about to do.
332 //
333 // Need to make the navigation icon non-clickable so that the entire card is clickable
334 // and goes to the search UI. Also set the background to null so there's no ripple.
335 View navView = toolbar.getNavigationView();
336 navView.setClickable(false);
337 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700338 }
339
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700340 ActionBar actionBar = getActionBar();
341 if (actionBar != null) {
342 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
343 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700344 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700345 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500346 if (mSwitchBar != null) {
347 mSwitchBar.setMetricsTag(getMetricsTag());
348 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700349
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
352
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700353 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 if (buttonBar != null) {
355 buttonBar.setVisibility(View.VISIBLE);
356
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700357 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 backButton.setOnClickListener(new OnClickListener() {
359 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700360 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 finish();
362 }
363 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700364 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800365 skipButton.setOnClickListener(new OnClickListener() {
366 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700367 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 finish();
369 }
370 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700371 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800372 mNextButton.setOnClickListener(new OnClickListener() {
373 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700374 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 finish();
376 }
377 });
378
379 // set our various button parameters
380 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
381 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
382 if (TextUtils.isEmpty(buttonText)) {
383 mNextButton.setVisibility(View.GONE);
384 }
385 else {
386 mNextButton.setText(buttonText);
387 }
388 }
389 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
390 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
391 if (TextUtils.isEmpty(buttonText)) {
392 backButton.setVisibility(View.GONE);
393 }
394 else {
395 backButton.setText(buttonText);
396 }
397 }
398 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
399 skipButton.setVisibility(View.VISIBLE);
400 }
401 }
402 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700403
Fan Zhangefba6b42017-02-07 17:40:59 -0800404 if (DEBUG_TIMING) {
405 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
406 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700407 }
408
Doris Lingcb69baf2017-02-23 17:50:03 -0800409 @VisibleForTesting
410 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
411 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800412 // UP will be shown only if it is a sub settings
413 if (mIsShortcut) {
414 mDisplayHomeAsUpEnabled = isSubSettings;
415 } else if (isSubSettings) {
416 mDisplayHomeAsUpEnabled = true;
417 } else {
418 mDisplayHomeAsUpEnabled = false;
419 }
420 setTitleFromIntent(intent);
421
422 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
423 switchToFragment(initialFragmentName, initialArguments, true, false,
424 mInitialTitleResId, mInitialTitle, false);
425 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700426 // Show search icon as up affordance if we are displaying the main Dashboard
427 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800428 mInitialTitleResId = R.string.dashboard_title;
429
430 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
431 mInitialTitleResId, mInitialTitle, false);
432 }
433 }
434
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700435 private void setTitleFromIntent(Intent intent) {
436 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
437 if (initialTitleResId > 0) {
438 mInitialTitle = null;
439 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100440
441 final String initialTitleResPackageName = intent.getStringExtra(
442 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
443 if (initialTitleResPackageName != null) {
444 try {
445 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
446 0 /* flags */, new UserHandle(UserHandle.myUserId()));
447 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
448 setTitle(mInitialTitle);
449 mInitialTitleResId = -1;
450 return;
451 } catch (NameNotFoundException e) {
452 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
453 }
454 } else {
455 setTitle(mInitialTitleResId);
456 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700457 } else {
458 mInitialTitleResId = -1;
459 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
460 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
461 setTitle(mInitialTitle);
462 }
463 }
464
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800465 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700467 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 }
469
Fan Zhang28691572016-03-23 15:31:08 -0700470 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800471 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700472
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800473 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700474 if (mInitialTitleResId > 0) {
475 setTitle(mInitialTitleResId);
476 } else {
477 setTitle(mInitialTitle);
478 }
Fan Zhang28691572016-03-23 15:31:08 -0700479 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800480 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700481
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800482 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
483 setTitleFromBackStackEntry(bse);
484 }
485
486 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
487 final CharSequence title;
488 final int titleRes = bse.getBreadCrumbTitleRes();
489 if (titleRes > 0) {
490 title = getText(titleRes);
491 } else {
492 title = bse.getBreadCrumbTitle();
493 }
494 if (title != null) {
495 setTitle(title);
496 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800497 }
498
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 @Override
500 protected void onSaveInstanceState(Bundle outState) {
501 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700502 saveState(outState);
503 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800504
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700505 /**
506 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
507 */
508 @VisibleForTesting
509 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700510 if (mCategories.size() > 0) {
511 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700513
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700514 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700515 }
516
517 @Override
518 protected void onRestoreInstanceState(Bundle savedInstanceState) {
519 super.onRestoreInstanceState(savedInstanceState);
520
521 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800522 }
523
524 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400525 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526 super.onResume();
527
Tony Mantler3d84d562017-07-31 10:48:55 -0700528 mDevelopmentSettingsListener = new BroadcastReceiver() {
529 @Override
530 public void onReceive(Context context, Intent intent) {
531 updateTilesList();
532 }
533 };
534 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
535 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700538
Jason Monk4da79e02015-09-10 10:54:36 -0400539 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 }
541
542 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400543 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700545 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
546 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800548 }
549
550 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800551 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
552 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
553 taskDescription.setIcon(icon);
554 super.setTaskDescription(taskDescription);
555 }
556
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800557 protected boolean isValidFragment(String fragmentName) {
558 // Almost all fragments are wrapped in this,
559 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800560 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
561 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 }
563 return false;
564 }
565
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 @Override
567 public Intent getIntent() {
568 Intent superIntent = super.getIntent();
569 String startingFragment = getStartingFragmentClass(superIntent);
570 // This is called from super.onCreate, isMultiPane() is not yet reliable
571 // Do not use onIsHidingHeaders either, which relies itself on this method
572 if (startingFragment != null) {
573 Intent modIntent = new Intent(superIntent);
574 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
575 Bundle args = superIntent.getExtras();
576 if (args != null) {
577 args = new Bundle(args);
578 } else {
579 args = new Bundle();
580 }
581 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100582 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800583 return modIntent;
584 }
585 return superIntent;
586 }
587
588 /**
589 * Checks if the component name in the intent is different from the Settings class and
590 * returns the class name to load as a fragment.
591 */
592 private String getStartingFragmentClass(Intent intent) {
593 if (mFragmentClass != null) return mFragmentClass;
594
595 String intentClass = intent.getComponent().getClassName();
596 if (intentClass.equals(getClass().getName())) return null;
597
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700598 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
600 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700601 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 }
603
604 return intentClass;
605 }
606
607 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000608 * Start a new fragment containing a preference panel. If the preferences
609 * are being displayed in multi-pane mode, the given fragment class will
610 * be instantiated and placed in the appropriate pane. If running in
611 * single-pane mode, a new activity will be launched in which to show the
612 * fragment.
613 *
614 * @param fragmentClass Full name of the class implementing the fragment.
615 * @param args Any desired arguments to supply to the fragment.
616 * @param titleRes Optional resource identifier of the title of this
617 * fragment.
618 * @param titleText Optional text of the title of this fragment.
619 * @param resultTo Optional fragment that result data should be sent to.
620 * If non-null, resultTo.onActivityResult() will be called when this
621 * preference panel is done. The launched panel must use
622 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
623 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700624 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000625 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800626 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
627 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 String title = null;
629 if (titleRes < 0) {
630 if (titleText != null) {
631 title = titleText.toString();
632 } else {
633 // There not much we can do in that case
634 title = "";
635 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700636 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700637 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800638 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000639 }
640
641 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100642 * Start a new fragment in a new activity containing a preference panel for a given user. If the
643 * preferences are being displayed in multi-pane mode, the given fragment class will be
644 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
645 * activity will be launched in which to show the fragment.
646 *
647 * @param fragmentClass Full name of the class implementing the fragment.
648 * @param args Any desired arguments to supply to the fragment.
649 * @param titleRes Optional resource identifier of the title of this fragment.
650 * @param titleText Optional text of the title of this fragment.
651 * @param userHandle The user for which the panel has to be started.
652 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800653 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
654 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700655 // This is a workaround.
656 //
657 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
658 // starting the fragment could cause a native stack corruption. See b/17523189. However,
659 // adding that flag and start the preference panel with the same UserHandler will make it
660 // impossible to use back button to return to the previous screen. See b/20042570.
661 //
662 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
663 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
664 // when we're calling it as the same user.
665 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800666 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700667 } else {
668 String title = null;
669 if (titleRes < 0) {
670 if (titleText != null) {
671 title = titleText.toString();
672 } else {
673 // There not much we can do in that case
674 title = "";
675 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100676 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800677 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
678 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100679 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100680 }
681
682 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800683 * Called by a preference panel fragment to finish itself.
684 *
685 * @param caller The fragment that is asking to be finished.
686 * @param resultCode Optional result code to send back to the original
687 * launching fragment.
688 * @param resultData Optional result data to send back to the original
689 * launching fragment.
690 */
691 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
692 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700693 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800694 }
695
696 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000697 * Start a new fragment.
698 *
699 * @param fragment The fragment to start
700 * @param push If true, the current fragment will be pushed onto the back stack. If false,
701 * the current fragment will be replaced.
702 */
703 public void startPreferenceFragment(Fragment fragment, boolean push) {
704 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700705 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000706 if (push) {
707 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
708 transaction.addToBackStack(BACK_STACK_PREFS);
709 } else {
710 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
711 }
712 transaction.commitAllowingStateLoss();
713 }
714
715 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700716 * Switch to a specific Fragment with taking care of validation, Title and BackStack
717 */
718 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700719 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700720 if (validate && !isValidFragment(fragmentName)) {
721 throw new IllegalArgumentException("Invalid fragment for this activity: "
722 + fragmentName);
723 }
724 Fragment f = Fragment.instantiate(this, fragmentName, args);
725 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700726 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700727 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700728 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700729 }
730 if (addToBackStack) {
731 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
732 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700733 if (titleResId > 0) {
734 transaction.setBreadCrumbTitle(titleResId);
735 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700736 transaction.setBreadCrumbTitle(title);
737 }
738 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700739 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700740 return f;
741 }
742
Jason Monk4da79e02015-09-10 10:54:36 -0400743 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500744 // Generally the items that are will be changing from these updates will
745 // not be in the top list of tiles, so run it in the background and the
746 // SettingsDrawerActivity will pick up on the updates automatically.
747 AsyncTask.execute(new Runnable() {
748 @Override
749 public void run() {
750 doUpdateTilesList();
751 }
752 });
753 }
754
755 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400756 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700757 final UserManager um = UserManager.get(this);
758 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400760 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700761 somethingChanged = setTileEnabled(
762 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
763 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700764
Fan Zhangaeb94f02017-07-05 13:46:04 -0700765 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800766 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700767 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
768 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700769
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700770 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
771 .getDataPlanFeatureProvider()
772 .isEnabled();
773
774 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
775 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
776 // reverse.
777
778 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
779 // disable DataPlanUsageSummaryActivity.
780 somethingChanged = setTileEnabled(
781 new ComponentName(packageName,
782 isDataPlanFeatureEnabled
783 ? Settings.DataUsageSummaryActivity.class.getName()
784 : Settings.DataPlanUsageSummaryActivity.class.getName()),
785 false /* enabled */,
786 isAdmin) || somethingChanged;
787
788 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
789 // enable DataPlanUsageSummaryActivity.
790 somethingChanged = setTileEnabled(
791 new ComponentName(packageName,
792 isDataPlanFeatureEnabled
793 ? Settings.DataPlanUsageSummaryActivity.class.getName()
794 : Settings.DataUsageSummaryActivity.class.getName()),
795 Utils.isBandwidthControlEnabled() /* enabled */,
796 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700797
Fan Zhangaeb94f02017-07-05 13:46:04 -0700798 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800799 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700800 Utils.showSimCardTile(this), isAdmin)
801 || 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.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700805 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400809 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700810 && !Utils.isMonkeyRunning(), isAdmin)
811 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400812
Fan Zhangaeb94f02017-07-05 13:46:04 -0700813 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800814 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700815 !UserManager.isDeviceInDemoMode(this), isAdmin)
816 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800817
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800819 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700820 !UserManager.isDeviceInDemoMode(this), isAdmin)
821 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700822
Fan Zhangaeb94f02017-07-05 13:46:04 -0700823 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700824 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 !UserManager.isDeviceInDemoMode(this), isAdmin)
826 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400827
Fan Zhangaeb94f02017-07-05 13:46:04 -0700828 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800829 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700830 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
831 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400832
Tony Mantler3d84d562017-07-31 10:48:55 -0700833 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Dan Sandler12c4ba42016-03-28 11:13:40 -0400834 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
jeffreyhuang142682c2017-10-17 11:05:15 -0700835 final boolean useDevOptionV1 = Settings.DevelopmentSettingsActivity.isEnabled();
Fan Zhang4c26da92017-09-08 15:29:54 -0700836 // Enable old Dev option if v2 is disabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700837 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400838 Settings.DevelopmentSettingsActivity.class.getName()),
jeffreyhuang142682c2017-10-17 11:05:15 -0700839 showDev && useDevOptionV1, isAdmin)
Fan Zhang4c26da92017-09-08 15:29:54 -0700840 || somethingChanged;
841 // Enable new Dev option if v2 is enable
842 somethingChanged = setTileEnabled(new ComponentName(packageName,
843 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuang142682c2017-10-17 11:05:15 -0700844 showDev && !useDevOptionV1, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700845 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400846
Fan Zhanga6986e72017-03-08 09:24:45 -0800847 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700848 somethingChanged = setTileEnabled(new ComponentName(packageName,
849 BackupSettingsActivity.class.getName()), true, isAdmin)
850 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800851
Fan Zhangaeb94f02017-07-05 13:46:04 -0700852 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800853 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700854 WifiDisplaySettings.isAvailable(this), isAdmin)
855 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800856
Jason Monk4da79e02015-09-10 10:54:36 -0400857 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800858
Jason Monk4da79e02015-09-10 10:54:36 -0400859 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800860 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800861 synchronized (categories) {
862 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700863 final int tileCount = category.getTilesCount();
864 for (int i = 0; i < tileCount; i++) {
865 final ComponentName component = category.getTile(i).intent.getComponent();
866
Fan Zhanga6986e72017-03-08 09:24:45 -0800867 final String name = component.getClassName();
868 final boolean isEnabledForRestricted = ArrayUtils.contains(
869 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
870 if (packageName.equals(component.getPackageName())
871 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700872 somethingChanged = setTileEnabled(component, false, isAdmin)
873 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800874 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700875 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700876 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700877 }
878 }
Doris Ling26bf0032016-06-15 18:16:09 -0700879
Fan Zhang224caad2017-01-06 11:42:48 -0800880 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700881 if (somethingChanged) {
882 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
883 updateCategories();
884 } else {
885 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
886 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500887 }
888
Fan Zhangaeb94f02017-07-05 13:46:04 -0700889 /**
890 * @return whether or not the enabled state actually changed.
891 */
892 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400893 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800894 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
895 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400896 enabled = false;
897 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700898 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500899 }
900
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800901 private void getMetaData() {
902 try {
903 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
904 PackageManager.GET_META_DATA);
905 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800906 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
907 } catch (NameNotFoundException nnfe) {
908 // No recovery
909 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
910 }
911 }
912
913 // give subclasses access to the Next button
914 public boolean hasNextButton() {
915 return mNextButton != null;
916 }
917
918 public Button getNextButton() {
919 return mNextButton;
920 }
921
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800922 @Override
923 public boolean shouldUpRecreateTask(Intent targetIntent) {
924 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
925 }
926
Jason Monkd4f03ec2016-01-07 16:25:34 -0500927 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700928 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700929 return;
930 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700931 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500932 startActivityForResult(intent, REQUEST_SUGGESTION);
933 }
934
935 @Override
936 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
937 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
938 && resultCode != RESULT_CANCELED) {
939 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
940 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
941 }
942 super.onActivityResult(requestCode, resultCode, data);
943 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800944
945 @VisibleForTesting
946 Bitmap getBitmapFromXmlResource(int drawableRes) {
947 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
948 Canvas canvas = new Canvas();
949 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
950 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
951 canvas.setBitmap(bitmap);
952 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
953 drawable.draw(canvas);
954
955 return bitmap;
956 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700957
958 @Override
959 public void onClick(View v) {
960 Intent intent = new Intent(this, SearchActivity.class);
961 startActivity(intent);
962 }
963}