blob: f9b849eb79616695b208af6f07ea8eb84f302895 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fan Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050036import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.UserHandle;
39import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080040import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070042import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.view.View;
49import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070052import android.widget.Toolbar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040054import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000055import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080056import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080057import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070058import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070059import com.android.settings.dashboard.DashboardFeatureProvider;
60import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070061import com.android.settings.development.DevelopmentSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070062import com.android.settings.overlay.FeatureFactory;
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;
Tony Mantler3d84d562017-07-31 10:48:55 -070066import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040067import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.SettingsDrawerActivity;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080069import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070071import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072
Jason Monk4da79e02015-09-10 10:54:36 -040073public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074 implements PreferenceManager.OnPreferenceTreeClickListener,
75 PreferenceFragment.OnPreferenceStartFragmentCallback,
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070076 ButtonBarHandler, FragmentManager.OnBackStackChangedListener, OnClickListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077
78 private static final String LOG_TAG = "Settings";
79
80 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070081 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070082 @VisibleForTesting
83 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084
85 /**
86 * When starting this activity, the invoking Intent can contain this extra
87 * string to specify which fragment should be initially displayed.
88 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
89 * will call isValidFragment() to confirm that the fragment class name is valid for this
90 * activity.
91 */
92 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
93
94 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080095 * The metrics category constant for logging source when a setting fragment is opened.
96 */
97 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
98
99 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800100 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
101 * this extra can also be specified to supply a Bundle of arguments to pass
102 * to that fragment when it is instantiated during the initial creation
103 * of the activity.
104 */
105 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
106
107 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700108 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
109 */
110 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
111
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800112 public static final String BACK_STACK_PREFS = ":settings:prefs";
113
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114 // extras that allow any preference activity to be launched as part of a wizard
115
116 // show Back and Next buttons? takes boolean parameter
117 // Back will then return RESULT_CANCELED and Next RESULT_OK
118 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
119
120 // add a Skip button?
121 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
122
123 // specify custom text for the Back or Next buttons, or cause a button to not appear
124 // at all by setting it to null
125 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
126 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
127
128 /**
129 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700130 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131 * that fragment.
132 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700133 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100134 /**
135 * The package name used to resolve the title resource id.
136 */
137 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
138 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700139 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
140 ":settings:show_fragment_title_resid";
141 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
142 ":settings:show_fragment_as_shortcut";
143
144 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
145 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800146
Fan Zhang78ab9f92017-02-27 16:10:05 -0800147 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700148 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
149
Jason Monk30695812015-11-17 09:01:08 -0500150 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151 "com.android.settings.FRAGMENT_CLASS";
152
153 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
154
Jason Monkd4f03ec2016-01-07 16:25:34 -0500155 private static final int REQUEST_SUGGESTION = 42;
156
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800157 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800158
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800159 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700160 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800161
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700162 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
163 "android.settings.APPLICATION_DETAILS_SETTINGS"
164 };
165
Tony Mantler3d84d562017-07-31 10:48:55 -0700166 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 private boolean mBatteryPresent = true;
169 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 @Override
171 public void onReceive(Context context, Intent intent) {
172 String action = intent.getAction();
173 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
174 boolean batteryPresent = Utils.isBatteryPresent(intent);
175
176 if (mBatteryPresent != batteryPresent) {
177 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400178 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800179 }
180 }
181 }
182 };
183
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700184 private SwitchBar mSwitchBar;
185
186 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700187
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700188 @VisibleForTesting
189 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700190
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700191 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700192 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700193
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700194 private ViewGroup mContent;
195
Fan Zhangc6ca3142017-02-14 15:02:35 -0800196 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700197
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700198 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800199 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200
Fan Zhanga96a2d82016-09-27 17:51:11 -0700201 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500202 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700203
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700204 public SwitchBar getSwitchBar() {
205 return mSwitchBar;
206 }
207
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208 @Override
209 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800210 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 null, 0);
212 return true;
213 }
214
215 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400216 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 return false;
218 }
219
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500221 public SharedPreferences getSharedPreferences(String name, int mode) {
222 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500223 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500224 }
225 return super.getSharedPreferences(name, mode);
226 }
227
Jason Monke4ebcd12016-02-21 09:37:41 -0500228 private String getMetricsTag() {
229 String tag = getClass().getName();
230 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
231 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
232 }
233 if (tag.startsWith("com.android.settings.")) {
234 tag = tag.replace("com.android.settings.", "");
235 }
236 return tag;
237 }
238
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700239 private static boolean isShortCutIntent(final Intent intent) {
240 Set<String> categories = intent.getCategories();
241 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
242 }
243
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700244 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700245 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700246 if (action == null) {
247 return false;
248 }
249 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
250 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
251 }
252 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700253 }
254
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700255 @Override
256 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700257 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500258 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700259
260 final FeatureFactory factory = FeatureFactory.getFactory(this);
261
262 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800263 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700264
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700265 // Should happen before any call to getIntent()
266 getMetaData();
267
268 final Intent intent = getIntent();
269 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
270 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700273 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700274 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700275
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700276 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700277 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
278
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700279 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700280 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700281
Doris Ling1694d4b2017-03-03 14:21:37 -0800282 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700283
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700284 // This is a "Sub Settings" when:
285 // - this is a real SubSettings
286 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700287 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700288 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
289
Fan Zhang92f1e942017-01-21 10:07:26 -0800290 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
291 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700292 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800293 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700294 }
295
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700296 setContentView(mIsShowingDashboard ?
297 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700299 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700300
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 getFragmentManager().addOnBackStackChangedListener(this);
302
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700303 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700304 // We are restarting from a previous saved state; used that to initialize, instead
305 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700306 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800307
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700308 ArrayList<DashboardCategory> categories =
309 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
310 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700311 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700312 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700313 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700315
316 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700317
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800319 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 }
321
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700322 if (mIsShowingDashboard) {
323 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
324 findViewById(R.id.action_bar).setVisibility(View.GONE);
325 Toolbar toolbar = findViewById(R.id.search_action_bar);
326 toolbar.setOnClickListener(this);
327 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700328
329 // Please forgive me for what I am about to do.
330 //
331 // Need to make the navigation icon non-clickable so that the entire card is clickable
332 // and goes to the search UI. Also set the background to null so there's no ripple.
333 View navView = toolbar.getNavigationView();
334 navView.setClickable(false);
335 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700336 }
337
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700338 ActionBar actionBar = getActionBar();
339 if (actionBar != null) {
340 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
341 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700342 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700343 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500344 if (mSwitchBar != null) {
345 mSwitchBar.setMetricsTag(getMetricsTag());
346 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700347
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
350
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700351 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 if (buttonBar != null) {
353 buttonBar.setVisibility(View.VISIBLE);
354
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700355 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 backButton.setOnClickListener(new OnClickListener() {
357 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700358 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 finish();
360 }
361 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 skipButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700369 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 mNextButton.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 });
376
377 // set our various button parameters
378 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
379 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
380 if (TextUtils.isEmpty(buttonText)) {
381 mNextButton.setVisibility(View.GONE);
382 }
383 else {
384 mNextButton.setText(buttonText);
385 }
386 }
387 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
388 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
389 if (TextUtils.isEmpty(buttonText)) {
390 backButton.setVisibility(View.GONE);
391 }
392 else {
393 backButton.setText(buttonText);
394 }
395 }
396 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
397 skipButton.setVisibility(View.VISIBLE);
398 }
399 }
400 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700401
Fan Zhangefba6b42017-02-07 17:40:59 -0800402 if (DEBUG_TIMING) {
403 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
404 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700405 }
406
Doris Lingcb69baf2017-02-23 17:50:03 -0800407 @VisibleForTesting
408 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
409 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800410 // UP will be shown only if it is a sub settings
411 if (mIsShortcut) {
412 mDisplayHomeAsUpEnabled = isSubSettings;
413 } else if (isSubSettings) {
414 mDisplayHomeAsUpEnabled = true;
415 } else {
416 mDisplayHomeAsUpEnabled = false;
417 }
418 setTitleFromIntent(intent);
419
420 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
421 switchToFragment(initialFragmentName, initialArguments, true, false,
422 mInitialTitleResId, mInitialTitle, false);
423 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700424 // Show search icon as up affordance if we are displaying the main Dashboard
425 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800426 mInitialTitleResId = R.string.dashboard_title;
427
428 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
429 mInitialTitleResId, mInitialTitle, false);
430 }
431 }
432
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700433 private void setTitleFromIntent(Intent intent) {
434 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
435 if (initialTitleResId > 0) {
436 mInitialTitle = null;
437 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100438
439 final String initialTitleResPackageName = intent.getStringExtra(
440 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
441 if (initialTitleResPackageName != null) {
442 try {
443 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
444 0 /* flags */, new UserHandle(UserHandle.myUserId()));
445 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
446 setTitle(mInitialTitle);
447 mInitialTitleResId = -1;
448 return;
449 } catch (NameNotFoundException e) {
450 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
451 }
452 } else {
453 setTitle(mInitialTitleResId);
454 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700455 } else {
456 mInitialTitleResId = -1;
457 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
458 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
459 setTitle(mInitialTitle);
460 }
461 }
462
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800463 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700465 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 }
467
Fan Zhang28691572016-03-23 15:31:08 -0700468 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800469 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700470
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800471 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700472 if (mInitialTitleResId > 0) {
473 setTitle(mInitialTitleResId);
474 } else {
475 setTitle(mInitialTitle);
476 }
Fan Zhang28691572016-03-23 15:31:08 -0700477 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800478 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700479
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800480 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
481 setTitleFromBackStackEntry(bse);
482 }
483
484 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
485 final CharSequence title;
486 final int titleRes = bse.getBreadCrumbTitleRes();
487 if (titleRes > 0) {
488 title = getText(titleRes);
489 } else {
490 title = bse.getBreadCrumbTitle();
491 }
492 if (title != null) {
493 setTitle(title);
494 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800495 }
496
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 @Override
498 protected void onSaveInstanceState(Bundle outState) {
499 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700500 saveState(outState);
501 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700503 /**
504 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
505 */
506 @VisibleForTesting
507 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700508 if (mCategories.size() > 0) {
509 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700511
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700512 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700513 }
514
515 @Override
516 protected void onRestoreInstanceState(Bundle savedInstanceState) {
517 super.onRestoreInstanceState(savedInstanceState);
518
519 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800520 }
521
522 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400523 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800524 super.onResume();
525
Tony Mantler3d84d562017-07-31 10:48:55 -0700526 mDevelopmentSettingsListener = new BroadcastReceiver() {
527 @Override
528 public void onReceive(Context context, Intent intent) {
529 updateTilesList();
530 }
531 };
532 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
533 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700536
Jason Monk4da79e02015-09-10 10:54:36 -0400537 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538 }
539
540 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400541 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700543 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
544 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 }
547
548 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800549 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
550 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
551 taskDescription.setIcon(icon);
552 super.setTaskDescription(taskDescription);
553 }
554
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800555 protected boolean isValidFragment(String fragmentName) {
556 // Almost all fragments are wrapped in this,
557 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800558 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
559 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 }
561 return false;
562 }
563
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 @Override
565 public Intent getIntent() {
566 Intent superIntent = super.getIntent();
567 String startingFragment = getStartingFragmentClass(superIntent);
568 // This is called from super.onCreate, isMultiPane() is not yet reliable
569 // Do not use onIsHidingHeaders either, which relies itself on this method
570 if (startingFragment != null) {
571 Intent modIntent = new Intent(superIntent);
572 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
573 Bundle args = superIntent.getExtras();
574 if (args != null) {
575 args = new Bundle(args);
576 } else {
577 args = new Bundle();
578 }
579 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100580 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 return modIntent;
582 }
583 return superIntent;
584 }
585
586 /**
587 * Checks if the component name in the intent is different from the Settings class and
588 * returns the class name to load as a fragment.
589 */
590 private String getStartingFragmentClass(Intent intent) {
591 if (mFragmentClass != null) return mFragmentClass;
592
593 String intentClass = intent.getComponent().getClassName();
594 if (intentClass.equals(getClass().getName())) return null;
595
596 if ("com.android.settings.ManageApplications".equals(intentClass)
597 || "com.android.settings.RunningServices".equals(intentClass)
598 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
599 // Old names of manage apps.
600 intentClass = com.android.settings.applications.ManageApplications.class.getName();
601 }
602
603 return intentClass;
604 }
605
606 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000607 * Start a new fragment containing a preference panel. If the preferences
608 * are being displayed in multi-pane mode, the given fragment class will
609 * be instantiated and placed in the appropriate pane. If running in
610 * single-pane mode, a new activity will be launched in which to show the
611 * fragment.
612 *
613 * @param fragmentClass Full name of the class implementing the fragment.
614 * @param args Any desired arguments to supply to the fragment.
615 * @param titleRes Optional resource identifier of the title of this
616 * fragment.
617 * @param titleText Optional text of the title of this fragment.
618 * @param resultTo Optional fragment that result data should be sent to.
619 * If non-null, resultTo.onActivityResult() will be called when this
620 * preference panel is done. The launched panel must use
621 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
622 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700623 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000624 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800625 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
626 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700627 String title = null;
628 if (titleRes < 0) {
629 if (titleText != null) {
630 title = titleText.toString();
631 } else {
632 // There not much we can do in that case
633 title = "";
634 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700635 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700636 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800637 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000638 }
639
640 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100641 * Start a new fragment in a new activity containing a preference panel for a given user. If the
642 * preferences are being displayed in multi-pane mode, the given fragment class will be
643 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
644 * activity will be launched in which to show the fragment.
645 *
646 * @param fragmentClass Full name of the class implementing the fragment.
647 * @param args Any desired arguments to supply to the fragment.
648 * @param titleRes Optional resource identifier of the title of this fragment.
649 * @param titleText Optional text of the title of this fragment.
650 * @param userHandle The user for which the panel has to be started.
651 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800652 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
653 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700654 // This is a workaround.
655 //
656 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
657 // starting the fragment could cause a native stack corruption. See b/17523189. However,
658 // adding that flag and start the preference panel with the same UserHandler will make it
659 // impossible to use back button to return to the previous screen. See b/20042570.
660 //
661 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
662 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
663 // when we're calling it as the same user.
664 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800665 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700666 } else {
667 String title = null;
668 if (titleRes < 0) {
669 if (titleText != null) {
670 title = titleText.toString();
671 } else {
672 // There not much we can do in that case
673 title = "";
674 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100675 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800676 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
677 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100678 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100679 }
680
681 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800682 * Called by a preference panel fragment to finish itself.
683 *
684 * @param caller The fragment that is asking to be finished.
685 * @param resultCode Optional result code to send back to the original
686 * launching fragment.
687 * @param resultData Optional result data to send back to the original
688 * launching fragment.
689 */
690 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
691 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700692 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 }
694
695 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000696 * Start a new fragment.
697 *
698 * @param fragment The fragment to start
699 * @param push If true, the current fragment will be pushed onto the back stack. If false,
700 * the current fragment will be replaced.
701 */
702 public void startPreferenceFragment(Fragment fragment, boolean push) {
703 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700704 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000705 if (push) {
706 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
707 transaction.addToBackStack(BACK_STACK_PREFS);
708 } else {
709 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
710 }
711 transaction.commitAllowingStateLoss();
712 }
713
714 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700715 * Switch to a specific Fragment with taking care of validation, Title and BackStack
716 */
717 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700718 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700719 if (validate && !isValidFragment(fragmentName)) {
720 throw new IllegalArgumentException("Invalid fragment for this activity: "
721 + fragmentName);
722 }
723 Fragment f = Fragment.instantiate(this, fragmentName, args);
724 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700725 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700726 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700727 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700728 }
729 if (addToBackStack) {
730 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
731 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700732 if (titleResId > 0) {
733 transaction.setBreadCrumbTitle(titleResId);
734 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700735 transaction.setBreadCrumbTitle(title);
736 }
737 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700738 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700739 return f;
740 }
741
Jason Monk4da79e02015-09-10 10:54:36 -0400742 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500743 // Generally the items that are will be changing from these updates will
744 // not be in the top list of tiles, so run it in the background and the
745 // SettingsDrawerActivity will pick up on the updates automatically.
746 AsyncTask.execute(new Runnable() {
747 @Override
748 public void run() {
749 doUpdateTilesList();
750 }
751 });
752 }
753
754 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400755 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700756 final UserManager um = UserManager.get(this);
757 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700758 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400759 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700760 somethingChanged = setTileEnabled(
761 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
762 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700763
Fan Zhangaeb94f02017-07-05 13:46:04 -0700764 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800765 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700766 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
767 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700768
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700769 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
770 .getDataPlanFeatureProvider()
771 .isEnabled();
772
773 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
774 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
775 // reverse.
776
777 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
778 // disable DataPlanUsageSummaryActivity.
779 somethingChanged = setTileEnabled(
780 new ComponentName(packageName,
781 isDataPlanFeatureEnabled
782 ? Settings.DataUsageSummaryActivity.class.getName()
783 : Settings.DataPlanUsageSummaryActivity.class.getName()),
784 false /* enabled */,
785 isAdmin) || somethingChanged;
786
787 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
788 // enable DataPlanUsageSummaryActivity.
789 somethingChanged = setTileEnabled(
790 new ComponentName(packageName,
791 isDataPlanFeatureEnabled
792 ? Settings.DataPlanUsageSummaryActivity.class.getName()
793 : Settings.DataUsageSummaryActivity.class.getName()),
794 Utils.isBandwidthControlEnabled() /* enabled */,
795 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700796
Fan Zhangaeb94f02017-07-05 13:46:04 -0700797 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800798 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700799 Utils.showSimCardTile(this), isAdmin)
800 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700801
Fan Zhangaeb94f02017-07-05 13:46:04 -0700802 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800803 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700804 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400805
Fan Zhangaeb94f02017-07-05 13:46:04 -0700806 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800807 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400808 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 && !Utils.isMonkeyRunning(), isAdmin)
810 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400811
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 !UserManager.isDeviceInDemoMode(this), isAdmin)
815 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800816
Fan Zhangaeb94f02017-07-05 13:46:04 -0700817 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800818 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700819 !UserManager.isDeviceInDemoMode(this), isAdmin)
820 || somethingChanged;
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700821
Fan Zhangaeb94f02017-07-05 13:46:04 -0700822 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700823 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700824 !UserManager.isDeviceInDemoMode(this), isAdmin)
825 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400826
Fan Zhangaeb94f02017-07-05 13:46:04 -0700827 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800828 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700829 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
830 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400831
Tony Mantler3d84d562017-07-31 10:48:55 -0700832 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Dan Sandler12c4ba42016-03-28 11:13:40 -0400833 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700834 somethingChanged = setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400835 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700836 showDev, isAdmin)
837 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400838
Fan Zhanga6986e72017-03-08 09:24:45 -0800839 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700840 somethingChanged = setTileEnabled(new ComponentName(packageName,
841 BackupSettingsActivity.class.getName()), true, isAdmin)
842 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800843
Fan Zhangaeb94f02017-07-05 13:46:04 -0700844 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800845 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700846 WifiDisplaySettings.isAvailable(this), isAdmin)
847 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800848
Jason Monk4da79e02015-09-10 10:54:36 -0400849 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800850
Jason Monk4da79e02015-09-10 10:54:36 -0400851 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800852 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800853 synchronized (categories) {
854 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700855 final int tileCount = category.getTilesCount();
856 for (int i = 0; i < tileCount; i++) {
857 final ComponentName component = category.getTile(i).intent.getComponent();
858
Fan Zhanga6986e72017-03-08 09:24:45 -0800859 final String name = component.getClassName();
860 final boolean isEnabledForRestricted = ArrayUtils.contains(
861 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
862 if (packageName.equals(component.getPackageName())
863 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700864 somethingChanged = setTileEnabled(component, false, isAdmin)
865 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800866 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700867 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700868 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700869 }
870 }
Doris Ling26bf0032016-06-15 18:16:09 -0700871
Fan Zhang224caad2017-01-06 11:42:48 -0800872 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700873 if (somethingChanged) {
874 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
875 updateCategories();
876 } else {
877 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
878 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500879 }
880
Fan Zhangaeb94f02017-07-05 13:46:04 -0700881 /**
882 * @return whether or not the enabled state actually changed.
883 */
884 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400885 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800886 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
887 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400888 enabled = false;
889 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700890 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500891 }
892
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800893 private void getMetaData() {
894 try {
895 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
896 PackageManager.GET_META_DATA);
897 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800898 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
899 } catch (NameNotFoundException nnfe) {
900 // No recovery
901 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
902 }
903 }
904
905 // give subclasses access to the Next button
906 public boolean hasNextButton() {
907 return mNextButton != null;
908 }
909
910 public Button getNextButton() {
911 return mNextButton;
912 }
913
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800914 @Override
915 public boolean shouldUpRecreateTask(Intent targetIntent) {
916 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
917 }
918
Jason Monkd4f03ec2016-01-07 16:25:34 -0500919 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700920 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700921 return;
922 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700923 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500924 startActivityForResult(intent, REQUEST_SUGGESTION);
925 }
926
927 @Override
928 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
929 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
930 && resultCode != RESULT_CANCELED) {
931 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
932 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
933 }
934 super.onActivityResult(requestCode, resultCode, data);
935 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800936
937 @VisibleForTesting
938 Bitmap getBitmapFromXmlResource(int drawableRes) {
939 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
940 Canvas canvas = new Canvas();
941 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
942 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
943 canvas.setBitmap(bitmap);
944 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
945 drawable.draw(canvas);
946
947 return bitmap;
948 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700949
950 @Override
951 public void onClick(View v) {
952 Intent intent = new Intent(this, SearchActivity.class);
953 startActivity(intent);
954 }
955}