blob: 556dbfb5b3c43b912ef96556ff8c7c3d103cc23c [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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080036import android.nfc.NfcAdapter;
Jason Monkfd2c7222015-12-02 15:38:38 -050037import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.UserHandle;
40import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080041import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040042import android.support.v14.preference.PreferenceFragment;
43import 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 Megliod25314d2014-03-21 19:24:43 -070048import android.view.Menu;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.view.View;
50import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070053import android.widget.Toolbar;
Fan Zhang28691572016-03-23 15:31:08 -070054
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040056import com.android.settings.Settings.WifiSettingsActivity;
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 Zhang11d2d682017-03-17 16:19:26 -070063import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050065import com.android.settings.search.DynamicIndexableContentMonitor;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070066import com.android.settings.search.SearchActivity;
Matthew Fritze2078bc22017-05-22 13:31:54 -070067import com.android.settings.search.SearchFeatureProvider;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080068import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070069import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040071import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050072
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070075import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076
Jason Monk4da79e02015-09-10 10:54:36 -040077public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078 implements PreferenceManager.OnPreferenceTreeClickListener,
79 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070080 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081
82 private static final String LOG_TAG = "Settings";
83
Matthew Fritzebe0989c2017-04-25 09:38:56 -070084 public static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080085
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080086 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070087 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070088 @VisibleForTesting
89 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090
91 /**
92 * When starting this activity, the invoking Intent can contain this extra
93 * string to specify which fragment should be initially displayed.
94 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
95 * will call isValidFragment() to confirm that the fragment class name is valid for this
96 * activity.
97 */
98 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
99
100 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800101 * The metrics category constant for logging source when a setting fragment is opened.
102 */
103 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
104
105 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800106 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
107 * this extra can also be specified to supply a Bundle of arguments to pass
108 * to that fragment when it is instantiated during the initial creation
109 * of the activity.
110 */
111 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
112
113 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700114 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
115 */
116 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
117
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800118 public static final String BACK_STACK_PREFS = ":settings:prefs";
119
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120 // extras that allow any preference activity to be launched as part of a wizard
121
122 // show Back and Next buttons? takes boolean parameter
123 // Back will then return RESULT_CANCELED and Next RESULT_OK
124 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
125
126 // add a Skip button?
127 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
128
129 // specify custom text for the Back or Next buttons, or cause a button to not appear
130 // at all by setting it to null
131 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
132 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
133
134 /**
135 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700136 * 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 -0800137 * that fragment.
138 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100140 /**
141 * The package name used to resolve the title resource id.
142 */
143 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
144 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700145 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
146 ":settings:show_fragment_title_resid";
147 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
148 ":settings:show_fragment_as_shortcut";
149
150 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
151 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
Fan Zhang78ab9f92017-02-27 16:10:05 -0800153 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700154 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
155
Jason Monk30695812015-11-17 09:01:08 -0500156 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800157 "com.android.settings.FRAGMENT_CLASS";
158
159 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
160
Jason Monkd4f03ec2016-01-07 16:25:34 -0500161 private static final int REQUEST_SUGGESTION = 42;
162
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800165 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700166 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800167
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700168 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
169 "android.settings.APPLICATION_DETAILS_SETTINGS"
170 };
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 private SharedPreferences mDevelopmentPreferences;
173 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
174
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 private boolean mBatteryPresent = true;
176 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 @Override
178 public void onReceive(Context context, Intent intent) {
179 String action = intent.getAction();
180 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
181 boolean batteryPresent = Utils.isBatteryPresent(intent);
182
183 if (mBatteryPresent != batteryPresent) {
184 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400185 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186 }
187 }
188 }
189 };
190
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800191 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700192
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700193 private SwitchBar mSwitchBar;
194
195 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700196
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700197 @VisibleForTesting
198 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700199
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700200 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700201 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700202
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700203 private ViewGroup mContent;
204
Fan Zhangc6ca3142017-02-14 15:02:35 -0800205 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700206
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700207 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800208 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209
Fan Zhanga96a2d82016-09-27 17:51:11 -0700210 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500211 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700212
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700213 public SwitchBar getSwitchBar() {
214 return mSwitchBar;
215 }
216
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 @Override
218 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800219 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 null, 0);
221 return true;
222 }
223
224 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400225 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800226 return false;
227 }
228
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500230 public SharedPreferences getSharedPreferences(String name, int mode) {
231 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500232 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500233 }
234 return super.getSharedPreferences(name, mode);
235 }
236
Jason Monke4ebcd12016-02-21 09:37:41 -0500237 private String getMetricsTag() {
238 String tag = getClass().getName();
239 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
240 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
241 }
242 if (tag.startsWith("com.android.settings.")) {
243 tag = tag.replace("com.android.settings.", "");
244 }
245 return tag;
246 }
247
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700248 private static boolean isShortCutIntent(final Intent intent) {
249 Set<String> categories = intent.getCategories();
250 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
251 }
252
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700253 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700254 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700255 if (action == null) {
256 return false;
257 }
258 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
259 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
260 }
261 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700262 }
263
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700264 @Override
265 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700266 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500267 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700268
269 final FeatureFactory factory = FeatureFactory.getFactory(this);
270
271 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800272 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700273
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700274 // Should happen before any call to getIntent()
275 getMetaData();
276
277 final Intent intent = getIntent();
278 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
279 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
283 Context.MODE_PRIVATE);
284
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700285 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700286 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700287
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700288 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700289 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
290
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700291 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700292 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700293
Doris Ling1694d4b2017-03-03 14:21:37 -0800294 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700295
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700296 // This is a "Sub Settings" when:
297 // - this is a real SubSettings
298 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700299 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700300 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
301
Fan Zhang92f1e942017-01-21 10:07:26 -0800302 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
303 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700304 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800305 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700306 }
307
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700308 setContentView(mIsShowingDashboard ?
309 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700311 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700312
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 getFragmentManager().addOnBackStackChangedListener(this);
314
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700315 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700316 // We are restarting from a previous saved state; used that to initialize, instead
317 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700318 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800319
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700320 ArrayList<DashboardCategory> categories =
321 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
322 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700323 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700324 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700325 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700327
328 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700329
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800330 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800331 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800332 }
333
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700334 if (mIsShowingDashboard) {
335 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
336 findViewById(R.id.action_bar).setVisibility(View.GONE);
337 Toolbar toolbar = findViewById(R.id.search_action_bar);
338 toolbar.setOnClickListener(this);
339 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700340
341 // Please forgive me for what I am about to do.
342 //
343 // Need to make the navigation icon non-clickable so that the entire card is clickable
344 // and goes to the search UI. Also set the background to null so there's no ripple.
345 View navView = toolbar.getNavigationView();
346 navView.setClickable(false);
347 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700348 }
349
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700350 ActionBar actionBar = getActionBar();
351 if (actionBar != null) {
352 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
353 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700354 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700355 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500356 if (mSwitchBar != null) {
357 mSwitchBar.setMetricsTag(getMetricsTag());
358 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700359
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
362
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700363 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 if (buttonBar != null) {
365 buttonBar.setVisibility(View.VISIBLE);
366
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700367 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 backButton.setOnClickListener(new OnClickListener() {
369 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700370 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 finish();
372 }
373 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700374 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 skipButton.setOnClickListener(new OnClickListener() {
376 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700377 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800378 finish();
379 }
380 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700381 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800382 mNextButton.setOnClickListener(new OnClickListener() {
383 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700384 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800385 finish();
386 }
387 });
388
389 // set our various button parameters
390 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
391 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
392 if (TextUtils.isEmpty(buttonText)) {
393 mNextButton.setVisibility(View.GONE);
394 }
395 else {
396 mNextButton.setText(buttonText);
397 }
398 }
399 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
400 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
401 if (TextUtils.isEmpty(buttonText)) {
402 backButton.setVisibility(View.GONE);
403 }
404 else {
405 backButton.setText(buttonText);
406 }
407 }
408 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
409 skipButton.setVisibility(View.VISIBLE);
410 }
411 }
412 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700413
Fan Zhangefba6b42017-02-07 17:40:59 -0800414 if (DEBUG_TIMING) {
415 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
416 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700417 }
418
Doris Lingcb69baf2017-02-23 17:50:03 -0800419 @VisibleForTesting
420 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
421 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800422 // UP will be shown only if it is a sub settings
423 if (mIsShortcut) {
424 mDisplayHomeAsUpEnabled = isSubSettings;
425 } else if (isSubSettings) {
426 mDisplayHomeAsUpEnabled = true;
427 } else {
428 mDisplayHomeAsUpEnabled = false;
429 }
430 setTitleFromIntent(intent);
431
432 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
433 switchToFragment(initialFragmentName, initialArguments, true, false,
434 mInitialTitleResId, mInitialTitle, false);
435 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700436 // Show search icon as up affordance if we are displaying the main Dashboard
437 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800438 mInitialTitleResId = R.string.dashboard_title;
439
440 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
441 mInitialTitleResId, mInitialTitle, false);
442 }
443 }
444
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700445 private void setTitleFromIntent(Intent intent) {
446 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
447 if (initialTitleResId > 0) {
448 mInitialTitle = null;
449 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100450
451 final String initialTitleResPackageName = intent.getStringExtra(
452 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
453 if (initialTitleResPackageName != null) {
454 try {
455 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
456 0 /* flags */, new UserHandle(UserHandle.myUserId()));
457 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
458 setTitle(mInitialTitle);
459 mInitialTitleResId = -1;
460 return;
461 } catch (NameNotFoundException e) {
462 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
463 }
464 } else {
465 setTitle(mInitialTitleResId);
466 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700467 } else {
468 mInitialTitleResId = -1;
469 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
470 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
471 setTitle(mInitialTitle);
472 }
473 }
474
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800475 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700477 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 }
479
Fan Zhang28691572016-03-23 15:31:08 -0700480 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800481 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700482
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800483 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700484 if (mInitialTitleResId > 0) {
485 setTitle(mInitialTitleResId);
486 } else {
487 setTitle(mInitialTitle);
488 }
Fan Zhang28691572016-03-23 15:31:08 -0700489 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800490 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700491
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800492 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
493 setTitleFromBackStackEntry(bse);
494 }
495
496 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
497 final CharSequence title;
498 final int titleRes = bse.getBreadCrumbTitleRes();
499 if (titleRes > 0) {
500 title = getText(titleRes);
501 } else {
502 title = bse.getBreadCrumbTitle();
503 }
504 if (title != null) {
505 setTitle(title);
506 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800507 }
508
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509 @Override
510 protected void onSaveInstanceState(Bundle outState) {
511 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700512 saveState(outState);
513 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700515 /**
516 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
517 */
518 @VisibleForTesting
519 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700520 if (mCategories.size() > 0) {
521 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800522 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700523
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700524 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700525 }
526
527 @Override
528 protected void onRestoreInstanceState(Bundle savedInstanceState) {
529 super.onRestoreInstanceState(savedInstanceState);
530
531 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 }
533
534 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400535 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 super.onResume();
537
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700538 mDevelopmentPreferencesListener = (sharedPreferences, key) -> updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
540 mDevelopmentPreferencesListener);
541
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800543 if (mDynamicIndexableContentMonitor == null) {
544 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
545 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800546 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700547
Jason Monk4da79e02015-09-10 10:54:36 -0400548 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 }
550
551 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400552 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800554 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
555 mDevelopmentPreferencesListener);
556 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800557 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800558 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900559 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800560 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800561 }
562
563 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800564 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
565 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
566 taskDescription.setIcon(icon);
567 super.setTaskDescription(taskDescription);
568 }
569
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 protected boolean isValidFragment(String fragmentName) {
571 // Almost all fragments are wrapped in this,
572 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800573 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
574 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 }
576 return false;
577 }
578
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 @Override
580 public Intent getIntent() {
581 Intent superIntent = super.getIntent();
582 String startingFragment = getStartingFragmentClass(superIntent);
583 // This is called from super.onCreate, isMultiPane() is not yet reliable
584 // Do not use onIsHidingHeaders either, which relies itself on this method
585 if (startingFragment != null) {
586 Intent modIntent = new Intent(superIntent);
587 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
588 Bundle args = superIntent.getExtras();
589 if (args != null) {
590 args = new Bundle(args);
591 } else {
592 args = new Bundle();
593 }
594 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100595 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800596 return modIntent;
597 }
598 return superIntent;
599 }
600
601 /**
602 * Checks if the component name in the intent is different from the Settings class and
603 * returns the class name to load as a fragment.
604 */
605 private String getStartingFragmentClass(Intent intent) {
606 if (mFragmentClass != null) return mFragmentClass;
607
608 String intentClass = intent.getComponent().getClassName();
609 if (intentClass.equals(getClass().getName())) return null;
610
611 if ("com.android.settings.ManageApplications".equals(intentClass)
612 || "com.android.settings.RunningServices".equals(intentClass)
613 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
614 // Old names of manage apps.
615 intentClass = com.android.settings.applications.ManageApplications.class.getName();
616 }
617
618 return intentClass;
619 }
620
621 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000622 * Start a new fragment containing a preference panel. If the preferences
623 * are being displayed in multi-pane mode, the given fragment class will
624 * be instantiated and placed in the appropriate pane. If running in
625 * single-pane mode, a new activity will be launched in which to show the
626 * fragment.
627 *
628 * @param fragmentClass Full name of the class implementing the fragment.
629 * @param args Any desired arguments to supply to the fragment.
630 * @param titleRes Optional resource identifier of the title of this
631 * fragment.
632 * @param titleText Optional text of the title of this fragment.
633 * @param resultTo Optional fragment that result data should be sent to.
634 * If non-null, resultTo.onActivityResult() will be called when this
635 * preference panel is done. The launched panel must use
636 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
637 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700638 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000639 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800640 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
641 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700642 String title = null;
643 if (titleRes < 0) {
644 if (titleText != null) {
645 title = titleText.toString();
646 } else {
647 // There not much we can do in that case
648 title = "";
649 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700650 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700651 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800652 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000653 }
654
655 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100656 * Start a new fragment in a new activity containing a preference panel for a given user. If the
657 * preferences are being displayed in multi-pane mode, the given fragment class will be
658 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
659 * activity will be launched in which to show the fragment.
660 *
661 * @param fragmentClass Full name of the class implementing the fragment.
662 * @param args Any desired arguments to supply to the fragment.
663 * @param titleRes Optional resource identifier of the title of this fragment.
664 * @param titleText Optional text of the title of this fragment.
665 * @param userHandle The user for which the panel has to be started.
666 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800667 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
668 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700669 // This is a workaround.
670 //
671 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
672 // starting the fragment could cause a native stack corruption. See b/17523189. However,
673 // adding that flag and start the preference panel with the same UserHandler will make it
674 // impossible to use back button to return to the previous screen. See b/20042570.
675 //
676 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
677 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
678 // when we're calling it as the same user.
679 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800680 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700681 } else {
682 String title = null;
683 if (titleRes < 0) {
684 if (titleText != null) {
685 title = titleText.toString();
686 } else {
687 // There not much we can do in that case
688 title = "";
689 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100690 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800691 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
692 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100693 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100694 }
695
696 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800697 * Called by a preference panel fragment to finish itself.
698 *
699 * @param caller The fragment that is asking to be finished.
700 * @param resultCode Optional result code to send back to the original
701 * launching fragment.
702 * @param resultData Optional result data to send back to the original
703 * launching fragment.
704 */
705 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
706 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700707 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800708 }
709
710 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000711 * Start a new fragment.
712 *
713 * @param fragment The fragment to start
714 * @param push If true, the current fragment will be pushed onto the back stack. If false,
715 * the current fragment will be replaced.
716 */
717 public void startPreferenceFragment(Fragment fragment, boolean push) {
718 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700719 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000720 if (push) {
721 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
722 transaction.addToBackStack(BACK_STACK_PREFS);
723 } else {
724 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
725 }
726 transaction.commitAllowingStateLoss();
727 }
728
729 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700730 * Switch to a specific Fragment with taking care of validation, Title and BackStack
731 */
732 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700733 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 if (validate && !isValidFragment(fragmentName)) {
735 throw new IllegalArgumentException("Invalid fragment for this activity: "
736 + fragmentName);
737 }
738 Fragment f = Fragment.instantiate(this, fragmentName, args);
739 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700740 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700742 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700743 }
744 if (addToBackStack) {
745 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
746 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700747 if (titleResId > 0) {
748 transaction.setBreadCrumbTitle(titleResId);
749 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700750 transaction.setBreadCrumbTitle(title);
751 }
752 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700753 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700754 return f;
755 }
756
Jason Monk4da79e02015-09-10 10:54:36 -0400757 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500758 // Generally the items that are will be changing from these updates will
759 // not be in the top list of tiles, so run it in the background and the
760 // SettingsDrawerActivity will pick up on the updates automatically.
761 AsyncTask.execute(new Runnable() {
762 @Override
763 public void run() {
764 doUpdateTilesList();
765 }
766 });
767 }
768
769 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400770 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700771 final UserManager um = UserManager.get(this);
772 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700773
Jason Monk4da79e02015-09-10 10:54:36 -0400774 String packageName = getPackageName();
775 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800776 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700777
Jason Monk4da79e02015-09-10 10:54:36 -0400778 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800779 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800780 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700781
Jason Monk4da79e02015-09-10 10:54:36 -0400782 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800783 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800784 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700785
Jason Monk4da79e02015-09-10 10:54:36 -0400786 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800787 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800788 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700789
Jason Monk4da79e02015-09-10 10:54:36 -0400790 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800791 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800792 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400793
794 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800795 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400796 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800797 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400798
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700799 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800800 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800801 !UserManager.isDeviceInDemoMode(this), isAdmin);
802
803 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800804 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800805 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700806
807 setTileEnabled(new ComponentName(packageName,
808 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800809 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400810 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
811 setTileEnabled(new ComponentName(packageName,
812 Settings.PaymentSettingsActivity.class.getName()),
813 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
814 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800815 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400816
817 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800818 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800819 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400820
821 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800822 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400823 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400824 setTileEnabled(new ComponentName(packageName,
825 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800826 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400827
Fan Zhanga6986e72017-03-08 09:24:45 -0800828 // Enable/disable backup settings depending on whether the user is admin.
829 setTileEnabled(new ComponentName(packageName,
Christine Franks2d029392017-05-26 18:23:38 -0700830 BackupSettingsActivity.class.getName()), true, isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800831
832 setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800833 Settings.WifiDisplaySettingsActivity.class.getName()),
834 WifiDisplaySettings.isAvailable(this), isAdmin);
835
Jason Monk4da79e02015-09-10 10:54:36 -0400836 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800837
Jason Monk4da79e02015-09-10 10:54:36 -0400838 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800839 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800840 synchronized (categories) {
841 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700842 final int tileCount = category.getTilesCount();
843 for (int i = 0; i < tileCount; i++) {
844 final ComponentName component = category.getTile(i).intent.getComponent();
845
Fan Zhanga6986e72017-03-08 09:24:45 -0800846 final String name = component.getClassName();
847 final boolean isEnabledForRestricted = ArrayUtils.contains(
848 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
849 if (packageName.equals(component.getPackageName())
850 && !isEnabledForRestricted) {
851 setTileEnabled(component, false, isAdmin);
852 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700853 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700854 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700855 }
856 }
Doris Ling26bf0032016-06-15 18:16:09 -0700857
Fan Zhang224caad2017-01-06 11:42:48 -0800858 // Final step, refresh categories.
859 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500860 }
861
Fan Zhang224caad2017-01-06 11:42:48 -0800862 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400863 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800864 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
865 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400866 enabled = false;
867 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400868 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500869 }
870
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800871 private void getMetaData() {
872 try {
873 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
874 PackageManager.GET_META_DATA);
875 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800876 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
877 } catch (NameNotFoundException nnfe) {
878 // No recovery
879 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
880 }
881 }
882
883 // give subclasses access to the Next button
884 public boolean hasNextButton() {
885 return mNextButton != null;
886 }
887
888 public Button getNextButton() {
889 return mNextButton;
890 }
891
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800892 @Override
893 public boolean shouldUpRecreateTask(Intent targetIntent) {
894 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
895 }
896
Jason Monkd4f03ec2016-01-07 16:25:34 -0500897 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700898 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700899 return;
900 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700901 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500902 startActivityForResult(intent, REQUEST_SUGGESTION);
903 }
904
905 @Override
906 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
907 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
908 && resultCode != RESULT_CANCELED) {
909 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
910 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
911 }
912 super.onActivityResult(requestCode, resultCode, data);
913 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800914
915 @VisibleForTesting
916 Bitmap getBitmapFromXmlResource(int drawableRes) {
917 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
918 Canvas canvas = new Canvas();
919 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
920 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
921 canvas.setBitmap(bitmap);
922 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
923 drawable.draw(canvas);
924
925 return bitmap;
926 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700927
928 @Override
929 public void onClick(View v) {
930 Intent intent = new Intent(this, SearchActivity.class);
931 startActivity(intent);
932 }
933}