blob: f120b7fdde6af20f8fb118f5bb389f4fece0d109 [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;
42import android.support.v7.preference.Preference;
43import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070045import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080046import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.view.View;
48import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070049import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070051import android.widget.Toolbar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040053import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000054import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080055import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080056import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070057import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070058import com.android.settings.dashboard.DashboardFeatureProvider;
59import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070060import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050062import com.android.settings.search.DynamicIndexableContentMonitor;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070063import com.android.settings.search.SearchActivity;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080064import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070065import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040066import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040067import com.android.settingslib.drawer.SettingsDrawerActivity;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080068import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080069import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070070import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071
Jason Monk4da79e02015-09-10 10:54:36 -040072public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073 implements PreferenceManager.OnPreferenceTreeClickListener,
74 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070075 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076
77 private static final String LOG_TAG = "Settings";
78
Matthew Fritzebe0989c2017-04-25 09:38:56 -070079 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080080
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070082 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070083 @VisibleForTesting
84 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085
86 /**
87 * When starting this activity, the invoking Intent can contain this extra
88 * string to specify which fragment should be initially displayed.
89 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
90 * will call isValidFragment() to confirm that the fragment class name is valid for this
91 * activity.
92 */
93 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
94
95 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080096 * The metrics category constant for logging source when a setting fragment is opened.
97 */
98 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
99
100 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800101 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
102 * this extra can also be specified to supply a Bundle of arguments to pass
103 * to that fragment when it is instantiated during the initial creation
104 * of the activity.
105 */
106 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
107
108 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700109 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
110 */
111 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
112
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800113 public static final String BACK_STACK_PREFS = ":settings:prefs";
114
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115 // extras that allow any preference activity to be launched as part of a wizard
116
117 // show Back and Next buttons? takes boolean parameter
118 // Back will then return RESULT_CANCELED and Next RESULT_OK
119 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
120
121 // add a Skip button?
122 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
123
124 // specify custom text for the Back or Next buttons, or cause a button to not appear
125 // at all by setting it to null
126 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
127 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
128
129 /**
130 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700131 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132 * that fragment.
133 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700134 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100135 /**
136 * The package name used to resolve the title resource id.
137 */
138 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
139 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700140 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
141 ":settings:show_fragment_title_resid";
142 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
143 ":settings:show_fragment_as_shortcut";
144
145 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
146 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800147
Fan Zhang78ab9f92017-02-27 16:10:05 -0800148 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700149 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
150
Jason Monk30695812015-11-17 09:01:08 -0500151 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152 "com.android.settings.FRAGMENT_CLASS";
153
154 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
155
Jason Monkd4f03ec2016-01-07 16:25:34 -0500156 private static final int REQUEST_SUGGESTION = 42;
157
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800158 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800159
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800160 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700161 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800162
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700163 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
164 "android.settings.APPLICATION_DETAILS_SETTINGS"
165 };
166
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 private SharedPreferences mDevelopmentPreferences;
168 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
169
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
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800186 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700187
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700188 private SwitchBar mSwitchBar;
189
190 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700191
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700192 @VisibleForTesting
193 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700194
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700195 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700196 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700197
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700198 private ViewGroup mContent;
199
Fan Zhangc6ca3142017-02-14 15:02:35 -0800200 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700201
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700202 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800203 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204
Fan Zhanga96a2d82016-09-27 17:51:11 -0700205 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500206 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700207
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700208 public SwitchBar getSwitchBar() {
209 return mSwitchBar;
210 }
211
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 @Override
213 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800214 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215 null, 0);
216 return true;
217 }
218
219 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400220 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 return false;
222 }
223
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500225 public SharedPreferences getSharedPreferences(String name, int mode) {
226 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500227 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500228 }
229 return super.getSharedPreferences(name, mode);
230 }
231
Jason Monke4ebcd12016-02-21 09:37:41 -0500232 private String getMetricsTag() {
233 String tag = getClass().getName();
234 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
235 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
236 }
237 if (tag.startsWith("com.android.settings.")) {
238 tag = tag.replace("com.android.settings.", "");
239 }
240 return tag;
241 }
242
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700243 private static boolean isShortCutIntent(final Intent intent) {
244 Set<String> categories = intent.getCategories();
245 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
246 }
247
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700248 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700249 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700250 if (action == null) {
251 return false;
252 }
253 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
254 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
255 }
256 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700257 }
258
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700259 @Override
260 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700261 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500262 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700263
264 final FeatureFactory factory = FeatureFactory.getFactory(this);
265
266 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800267 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700268
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700269 // Should happen before any call to getIntent()
270 getMetaData();
271
272 final Intent intent = getIntent();
273 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
274 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800277 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
278 Context.MODE_PRIVATE);
279
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700280 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700281 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700282
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700283 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700284 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
285
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700286 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700287 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700288
Doris Ling1694d4b2017-03-03 14:21:37 -0800289 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700290
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700291 // This is a "Sub Settings" when:
292 // - this is a real SubSettings
293 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700294 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700295 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
296
Fan Zhang92f1e942017-01-21 10:07:26 -0800297 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
298 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700299 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800300 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700301 }
302
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700303 setContentView(mIsShowingDashboard ?
304 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700306 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700307
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 getFragmentManager().addOnBackStackChangedListener(this);
309
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700310 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700311 // We are restarting from a previous saved state; used that to initialize, instead
312 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700313 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800314
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700315 ArrayList<DashboardCategory> categories =
316 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
317 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700318 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700319 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700320 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700322
323 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700324
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800326 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 }
328
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700329 if (mIsShowingDashboard) {
330 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
331 findViewById(R.id.action_bar).setVisibility(View.GONE);
332 Toolbar toolbar = findViewById(R.id.search_action_bar);
333 toolbar.setOnClickListener(this);
334 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700335
336 // Please forgive me for what I am about to do.
337 //
338 // Need to make the navigation icon non-clickable so that the entire card is clickable
339 // and goes to the search UI. Also set the background to null so there's no ripple.
340 View navView = toolbar.getNavigationView();
341 navView.setClickable(false);
342 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700343 }
344
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700345 ActionBar actionBar = getActionBar();
346 if (actionBar != null) {
347 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
348 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700349 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700350 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500351 if (mSwitchBar != null) {
352 mSwitchBar.setMetricsTag(getMetricsTag());
353 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700354
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
357
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700358 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 if (buttonBar != null) {
360 buttonBar.setVisibility(View.VISIBLE);
361
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 backButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700369 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 skipButton.setOnClickListener(new OnClickListener() {
371 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700372 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 finish();
374 }
375 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700376 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800377 mNextButton.setOnClickListener(new OnClickListener() {
378 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700379 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800380 finish();
381 }
382 });
383
384 // set our various button parameters
385 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
386 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
387 if (TextUtils.isEmpty(buttonText)) {
388 mNextButton.setVisibility(View.GONE);
389 }
390 else {
391 mNextButton.setText(buttonText);
392 }
393 }
394 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
395 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
396 if (TextUtils.isEmpty(buttonText)) {
397 backButton.setVisibility(View.GONE);
398 }
399 else {
400 backButton.setText(buttonText);
401 }
402 }
403 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
404 skipButton.setVisibility(View.VISIBLE);
405 }
406 }
407 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700408
Fan Zhangefba6b42017-02-07 17:40:59 -0800409 if (DEBUG_TIMING) {
410 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
411 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700412 }
413
Doris Lingcb69baf2017-02-23 17:50:03 -0800414 @VisibleForTesting
415 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
416 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800417 // UP will be shown only if it is a sub settings
418 if (mIsShortcut) {
419 mDisplayHomeAsUpEnabled = isSubSettings;
420 } else if (isSubSettings) {
421 mDisplayHomeAsUpEnabled = true;
422 } else {
423 mDisplayHomeAsUpEnabled = false;
424 }
425 setTitleFromIntent(intent);
426
427 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
428 switchToFragment(initialFragmentName, initialArguments, true, false,
429 mInitialTitleResId, mInitialTitle, false);
430 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700431 // Show search icon as up affordance if we are displaying the main Dashboard
432 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800433 mInitialTitleResId = R.string.dashboard_title;
434
435 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
436 mInitialTitleResId, mInitialTitle, false);
437 }
438 }
439
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700440 private void setTitleFromIntent(Intent intent) {
441 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
442 if (initialTitleResId > 0) {
443 mInitialTitle = null;
444 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100445
446 final String initialTitleResPackageName = intent.getStringExtra(
447 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
448 if (initialTitleResPackageName != null) {
449 try {
450 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
451 0 /* flags */, new UserHandle(UserHandle.myUserId()));
452 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
453 setTitle(mInitialTitle);
454 mInitialTitleResId = -1;
455 return;
456 } catch (NameNotFoundException e) {
457 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
458 }
459 } else {
460 setTitle(mInitialTitleResId);
461 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700462 } else {
463 mInitialTitleResId = -1;
464 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
465 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
466 setTitle(mInitialTitle);
467 }
468 }
469
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800470 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700472 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
474
Fan Zhang28691572016-03-23 15:31:08 -0700475 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800476 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700477
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800478 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700479 if (mInitialTitleResId > 0) {
480 setTitle(mInitialTitleResId);
481 } else {
482 setTitle(mInitialTitle);
483 }
Fan Zhang28691572016-03-23 15:31:08 -0700484 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800485 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700486
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800487 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
488 setTitleFromBackStackEntry(bse);
489 }
490
491 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
492 final CharSequence title;
493 final int titleRes = bse.getBreadCrumbTitleRes();
494 if (titleRes > 0) {
495 title = getText(titleRes);
496 } else {
497 title = bse.getBreadCrumbTitle();
498 }
499 if (title != null) {
500 setTitle(title);
501 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800502 }
503
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800504 @Override
505 protected void onSaveInstanceState(Bundle outState) {
506 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700507 saveState(outState);
508 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700510 /**
511 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
512 */
513 @VisibleForTesting
514 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700515 if (mCategories.size() > 0) {
516 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700518
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700519 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700520 }
521
522 @Override
523 protected void onRestoreInstanceState(Bundle savedInstanceState) {
524 super.onRestoreInstanceState(savedInstanceState);
525
526 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 }
528
529 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400530 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 super.onResume();
532
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700533 mDevelopmentPreferencesListener = (sharedPreferences, key) -> updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
535 mDevelopmentPreferencesListener);
536
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800538 if (mDynamicIndexableContentMonitor == null) {
539 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
540 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800541 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700542
Jason Monk4da79e02015-09-10 10:54:36 -0400543 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544 }
545
546 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400547 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800548 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800549 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
550 mDevelopmentPreferencesListener);
551 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800553 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900554 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800555 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800556 }
557
558 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800559 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
560 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
561 taskDescription.setIcon(icon);
562 super.setTaskDescription(taskDescription);
563 }
564
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565 protected boolean isValidFragment(String fragmentName) {
566 // Almost all fragments are wrapped in this,
567 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800568 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
569 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 }
571 return false;
572 }
573
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 @Override
575 public Intent getIntent() {
576 Intent superIntent = super.getIntent();
577 String startingFragment = getStartingFragmentClass(superIntent);
578 // This is called from super.onCreate, isMultiPane() is not yet reliable
579 // Do not use onIsHidingHeaders either, which relies itself on this method
580 if (startingFragment != null) {
581 Intent modIntent = new Intent(superIntent);
582 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
583 Bundle args = superIntent.getExtras();
584 if (args != null) {
585 args = new Bundle(args);
586 } else {
587 args = new Bundle();
588 }
589 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100590 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800591 return modIntent;
592 }
593 return superIntent;
594 }
595
596 /**
597 * Checks if the component name in the intent is different from the Settings class and
598 * returns the class name to load as a fragment.
599 */
600 private String getStartingFragmentClass(Intent intent) {
601 if (mFragmentClass != null) return mFragmentClass;
602
603 String intentClass = intent.getComponent().getClassName();
604 if (intentClass.equals(getClass().getName())) return null;
605
606 if ("com.android.settings.ManageApplications".equals(intentClass)
607 || "com.android.settings.RunningServices".equals(intentClass)
608 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
609 // Old names of manage apps.
610 intentClass = com.android.settings.applications.ManageApplications.class.getName();
611 }
612
613 return intentClass;
614 }
615
616 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000617 * Start a new fragment containing a preference panel. If the preferences
618 * are being displayed in multi-pane mode, the given fragment class will
619 * be instantiated and placed in the appropriate pane. If running in
620 * single-pane mode, a new activity will be launched in which to show the
621 * fragment.
622 *
623 * @param fragmentClass Full name of the class implementing the fragment.
624 * @param args Any desired arguments to supply to the fragment.
625 * @param titleRes Optional resource identifier of the title of this
626 * fragment.
627 * @param titleText Optional text of the title of this fragment.
628 * @param resultTo Optional fragment that result data should be sent to.
629 * If non-null, resultTo.onActivityResult() will be called when this
630 * preference panel is done. The launched panel must use
631 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
632 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700633 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000634 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800635 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
636 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700637 String title = null;
638 if (titleRes < 0) {
639 if (titleText != null) {
640 title = titleText.toString();
641 } else {
642 // There not much we can do in that case
643 title = "";
644 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700645 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700646 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800647 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000648 }
649
650 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100651 * Start a new fragment in a new activity containing a preference panel for a given user. If the
652 * preferences are being displayed in multi-pane mode, the given fragment class will be
653 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
654 * activity will be launched in which to show the fragment.
655 *
656 * @param fragmentClass Full name of the class implementing the fragment.
657 * @param args Any desired arguments to supply to the fragment.
658 * @param titleRes Optional resource identifier of the title of this fragment.
659 * @param titleText Optional text of the title of this fragment.
660 * @param userHandle The user for which the panel has to be started.
661 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800662 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
663 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700664 // This is a workaround.
665 //
666 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
667 // starting the fragment could cause a native stack corruption. See b/17523189. However,
668 // adding that flag and start the preference panel with the same UserHandler will make it
669 // impossible to use back button to return to the previous screen. See b/20042570.
670 //
671 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
672 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
673 // when we're calling it as the same user.
674 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800675 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700676 } else {
677 String title = null;
678 if (titleRes < 0) {
679 if (titleText != null) {
680 title = titleText.toString();
681 } else {
682 // There not much we can do in that case
683 title = "";
684 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100685 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800686 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
687 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100688 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100689 }
690
691 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800692 * Called by a preference panel fragment to finish itself.
693 *
694 * @param caller The fragment that is asking to be finished.
695 * @param resultCode Optional result code to send back to the original
696 * launching fragment.
697 * @param resultData Optional result data to send back to the original
698 * launching fragment.
699 */
700 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
701 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700702 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800703 }
704
705 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000706 * Start a new fragment.
707 *
708 * @param fragment The fragment to start
709 * @param push If true, the current fragment will be pushed onto the back stack. If false,
710 * the current fragment will be replaced.
711 */
712 public void startPreferenceFragment(Fragment fragment, boolean push) {
713 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700714 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000715 if (push) {
716 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
717 transaction.addToBackStack(BACK_STACK_PREFS);
718 } else {
719 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
720 }
721 transaction.commitAllowingStateLoss();
722 }
723
724 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700725 * Switch to a specific Fragment with taking care of validation, Title and BackStack
726 */
727 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700728 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700729 if (validate && !isValidFragment(fragmentName)) {
730 throw new IllegalArgumentException("Invalid fragment for this activity: "
731 + fragmentName);
732 }
733 Fragment f = Fragment.instantiate(this, fragmentName, args);
734 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700735 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700736 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700737 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700738 }
739 if (addToBackStack) {
740 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
741 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700742 if (titleResId > 0) {
743 transaction.setBreadCrumbTitle(titleResId);
744 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700745 transaction.setBreadCrumbTitle(title);
746 }
747 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700748 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700749 return f;
750 }
751
Jason Monk4da79e02015-09-10 10:54:36 -0400752 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500753 // Generally the items that are will be changing from these updates will
754 // not be in the top list of tiles, so run it in the background and the
755 // SettingsDrawerActivity will pick up on the updates automatically.
756 AsyncTask.execute(new Runnable() {
757 @Override
758 public void run() {
759 doUpdateTilesList();
760 }
761 });
762 }
763
764 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400765 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700766 final UserManager um = UserManager.get(this);
767 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700768 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400769 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700770 somethingChanged = setTileEnabled(
771 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
772 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700773
Fan Zhangaeb94f02017-07-05 13:46:04 -0700774 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800775 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700776 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
777 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700778
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700779 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
780 .getDataPlanFeatureProvider()
781 .isEnabled();
782
783 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
784 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
785 // reverse.
786
787 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
788 // disable DataPlanUsageSummaryActivity.
789 somethingChanged = setTileEnabled(
790 new ComponentName(packageName,
791 isDataPlanFeatureEnabled
792 ? Settings.DataUsageSummaryActivity.class.getName()
793 : Settings.DataPlanUsageSummaryActivity.class.getName()),
794 false /* enabled */,
795 isAdmin) || somethingChanged;
796
797 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
798 // enable DataPlanUsageSummaryActivity.
799 somethingChanged = setTileEnabled(
800 new ComponentName(packageName,
801 isDataPlanFeatureEnabled
802 ? Settings.DataPlanUsageSummaryActivity.class.getName()
803 : Settings.DataUsageSummaryActivity.class.getName()),
804 Utils.isBandwidthControlEnabled() /* enabled */,
805 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 Utils.showSimCardTile(this), isAdmin)
810 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700811
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400815
Fan Zhangaeb94f02017-07-05 13:46:04 -0700816 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800817 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400818 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700819 && !Utils.isMonkeyRunning(), isAdmin)
820 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400821
Fan Zhangaeb94f02017-07-05 13:46:04 -0700822 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800823 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700824 !UserManager.isDeviceInDemoMode(this), isAdmin)
825 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800826
Fan Zhangaeb94f02017-07-05 13:46:04 -0700827 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800828 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700829 !UserManager.isDeviceInDemoMode(this), isAdmin)
830 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700831
Fan Zhangaeb94f02017-07-05 13:46:04 -0700832 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700833 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700834 !UserManager.isDeviceInDemoMode(this), isAdmin)
835 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400836
Fan Zhangaeb94f02017-07-05 13:46:04 -0700837 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700839 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
840 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400841
842 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800843 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400844 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700845 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400846 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700847 showDev, isAdmin)
848 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400849
Fan Zhanga6986e72017-03-08 09:24:45 -0800850 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700851 somethingChanged = setTileEnabled(new ComponentName(packageName,
852 BackupSettingsActivity.class.getName()), true, isAdmin)
853 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800854
Fan Zhangaeb94f02017-07-05 13:46:04 -0700855 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800856 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700857 WifiDisplaySettings.isAvailable(this), isAdmin)
858 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800859
Jason Monk4da79e02015-09-10 10:54:36 -0400860 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800861
Jason Monk4da79e02015-09-10 10:54:36 -0400862 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800863 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800864 synchronized (categories) {
865 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700866 final int tileCount = category.getTilesCount();
867 for (int i = 0; i < tileCount; i++) {
868 final ComponentName component = category.getTile(i).intent.getComponent();
869
Fan Zhanga6986e72017-03-08 09:24:45 -0800870 final String name = component.getClassName();
871 final boolean isEnabledForRestricted = ArrayUtils.contains(
872 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
873 if (packageName.equals(component.getPackageName())
874 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700875 somethingChanged = setTileEnabled(component, false, isAdmin)
876 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800877 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700878 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700879 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700880 }
881 }
Doris Ling26bf0032016-06-15 18:16:09 -0700882
Fan Zhang224caad2017-01-06 11:42:48 -0800883 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700884 if (somethingChanged) {
885 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
886 updateCategories();
887 } else {
888 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
889 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500890 }
891
Fan Zhangaeb94f02017-07-05 13:46:04 -0700892 /**
893 * @return whether or not the enabled state actually changed.
894 */
895 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400896 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800897 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
898 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400899 enabled = false;
900 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700901 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500902 }
903
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800904 private void getMetaData() {
905 try {
906 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
907 PackageManager.GET_META_DATA);
908 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800909 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
910 } catch (NameNotFoundException nnfe) {
911 // No recovery
912 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
913 }
914 }
915
916 // give subclasses access to the Next button
917 public boolean hasNextButton() {
918 return mNextButton != null;
919 }
920
921 public Button getNextButton() {
922 return mNextButton;
923 }
924
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800925 @Override
926 public boolean shouldUpRecreateTask(Intent targetIntent) {
927 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
928 }
929
Jason Monkd4f03ec2016-01-07 16:25:34 -0500930 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700931 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700932 return;
933 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700934 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500935 startActivityForResult(intent, REQUEST_SUGGESTION);
936 }
937
938 @Override
939 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
940 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
941 && resultCode != RESULT_CANCELED) {
942 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
943 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
944 }
945 super.onActivityResult(requestCode, resultCode, data);
946 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800947
948 @VisibleForTesting
949 Bitmap getBitmapFromXmlResource(int drawableRes) {
950 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
951 Canvas canvas = new Canvas();
952 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
953 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
954 canvas.setBitmap(bitmap);
955 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
956 drawable.draw(canvas);
957
958 return bitmap;
959 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700960
961 @Override
962 public void onClick(View v) {
963 Intent intent = new Intent(this, SearchActivity.class);
964 startActivity(intent);
965 }
966}