blob: 222c18c77b91f151bf4ac41b68e7ab4df0ec0bf6 [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.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070022import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080023import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.SharedPreferences;
27import android.content.pm.ActivityInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.PackageManager.NameNotFoundException;
Jason Monkfd2c7222015-12-02 15:38:38 -050030import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080031import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080032import android.os.UserHandle;
33import android.os.UserManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080034import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070035import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080036import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.view.View;
38import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070039import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.widget.Button;
Fan Zhang4c26da92017-09-08 15:29:54 -070041
Fan Zhang60243e62018-08-03 09:13:53 -070042import androidx.annotation.Nullable;
43import androidx.annotation.VisibleForTesting;
44import androidx.fragment.app.Fragment;
45import androidx.fragment.app.FragmentManager;
46import androidx.fragment.app.FragmentTransaction;
47import androidx.localbroadcastmanager.content.LocalBroadcastManager;
48import androidx.preference.Preference;
49import androidx.preference.PreferenceFragmentCompat;
50import androidx.preference.PreferenceManager;
51
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040053import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070054import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000055import com.android.settings.backup.BackupSettingsActivity;
Fan Zhangaf32e722018-07-17 10:12:30 -070056import com.android.settings.core.OnActivityResultListener;
tmfangd97fba52018-06-14 14:48:35 +080057import com.android.settings.core.SettingsBaseActivity;
Fan Zhang615563d2018-02-15 15:19:33 -080058import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080059import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070060import com.android.settings.dashboard.DashboardFeatureProvider;
Fan Zhang21526c12018-09-06 15:44:26 -070061import com.android.settings.homepage.TopLevelSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070062import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080063import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070064import com.android.settings.widget.SwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080065import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080066import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070067import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.DashboardCategory;
Fan Zhang4c26da92017-09-08 15:29:54 -070069
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import java.util.List;
72
tmfangd97fba52018-06-14 14:48:35 +080073
74public class SettingsActivity extends SettingsBaseActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075 implements PreferenceManager.OnPreferenceTreeClickListener,
tmfang27c84de2018-06-28 11:39:05 +080076 PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080077 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078
Fan Zhang22230502018-03-27 14:14:13 -070079 private static final String LOG_TAG = "SettingsActivity";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070082 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083
84 /**
85 * When starting this activity, the invoking Intent can contain this extra
86 * string to specify which fragment should be initially displayed.
87 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
88 * will call isValidFragment() to confirm that the fragment class name is valid for this
89 * activity.
90 */
91 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
92
93 /**
94 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
95 * this extra can also be specified to supply a Bundle of arguments to pass
96 * to that fragment when it is instantiated during the initial creation
97 * of the activity.
98 */
99 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
100
101 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700102 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
103 */
104 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
105
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800106 public static final String BACK_STACK_PREFS = ":settings:prefs";
107
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108 // extras that allow any preference activity to be launched as part of a wizard
109
110 // show Back and Next buttons? takes boolean parameter
111 // Back will then return RESULT_CANCELED and Next RESULT_OK
112 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
113
114 // add a Skip button?
115 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
116
117 // specify custom text for the Back or Next buttons, or cause a button to not appear
118 // at all by setting it to null
119 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
120 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
121
122 /**
123 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700124 * 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 -0800125 * that fragment.
126 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700127 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100128 /**
129 * The package name used to resolve the title resource id.
130 */
131 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
132 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700133 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
134 ":settings:show_fragment_title_resid";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700135
136 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
137 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800138
Jason Monk30695812015-11-17 09:01:08 -0500139 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800140 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
143
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800144 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800145
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800146 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700147 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800148
Tony Mantler3d84d562017-07-31 10:48:55 -0700149 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151 private boolean mBatteryPresent = true;
152 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153 @Override
154 public void onReceive(Context context, Intent intent) {
155 String action = intent.getAction();
156 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
157 boolean batteryPresent = Utils.isBatteryPresent(intent);
158
159 if (mBatteryPresent != batteryPresent) {
160 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400161 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162 }
163 }
164 }
165 };
166
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700167 private SwitchBar mSwitchBar;
168
169 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700170
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700171 private ViewGroup mContent;
172
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700173 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800174 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175
Fan Zhanga96a2d82016-09-27 17:51:11 -0700176 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700177
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700178 public SwitchBar getSwitchBar() {
179 return mSwitchBar;
180 }
181
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 @Override
tmfang27c84de2018-06-28 11:39:05 +0800183 public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800184 new SubSettingLauncher(this)
185 .setDestination(pref.getFragment())
186 .setArguments(pref.getExtras())
187 .setSourceMetricsCategory(caller instanceof Instrumentable
188 ? ((Instrumentable) caller).getMetricsCategory()
189 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
hjchangliaoe86eec02018-05-02 13:01:07 +0800190 .setTitleRes(-1)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800191 .launch();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800192 return true;
193 }
194
195 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400196 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800197 return false;
198 }
199
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500201 public SharedPreferences getSharedPreferences(String name, int mode) {
202 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800203 return new SharedPreferencesLogger(this, getMetricsTag(),
204 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500205 }
206 return super.getSharedPreferences(name, mode);
207 }
208
Jason Monke4ebcd12016-02-21 09:37:41 -0500209 private String getMetricsTag() {
210 String tag = getClass().getName();
211 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
212 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
213 }
214 if (tag.startsWith("com.android.settings.")) {
215 tag = tag.replace("com.android.settings.", "");
216 }
217 return tag;
218 }
219
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700220 @Override
221 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700222 super.onCreate(savedState);
Fan Zhang22230502018-03-27 14:14:13 -0700223 Log.d(LOG_TAG, "Starting onCreate");
Jason Monkfd2c7222015-12-02 15:38:38 -0500224 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700225
226 final FeatureFactory factory = FeatureFactory.getFactory(this);
227
228 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700229
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700230 // Should happen before any call to getIntent()
231 getMetaData();
232
233 final Intent intent = getIntent();
234 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
235 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800236 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700238 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700239 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700240
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700241 // This is a "Sub Settings" when:
242 // - this is a real SubSettings
243 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700244 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700245 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
246
Fan Zhang92f1e942017-01-21 10:07:26 -0800247 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
248 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700249 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800250 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700251 }
252
Fan Zhang8e14ca02018-11-27 13:06:44 -0800253 setContentView(R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700255 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700256
tmfang27c84de2018-06-28 11:39:05 +0800257 getSupportFragmentManager().addOnBackStackChangedListener(this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700259 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700260 // We are restarting from a previous saved state; used that to initialize, instead
261 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700262 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800263
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700264 ArrayList<DashboardCategory> categories =
265 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
266 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700267 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700268 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700269 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 } else {
Fan Zhang8e14ca02018-11-27 13:06:44 -0800272 launchSettingFragment(initialFragmentName, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 }
274
Doris Ling882bef32018-07-31 14:04:44 -0700275 final boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700276
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700277 ActionBar actionBar = getActionBar();
278 if (actionBar != null) {
Tony Make226b382018-03-02 18:23:35 +0000279 actionBar.setDisplayHomeAsUpEnabled(deviceProvisioned);
280 actionBar.setHomeButtonEnabled(deviceProvisioned);
Fan Zhang8e14ca02018-11-27 13:06:44 -0800281 actionBar.setDisplayShowTitleEnabled(true);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700282 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700283 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500284 if (mSwitchBar != null) {
285 mSwitchBar.setMetricsTag(getMetricsTag());
286 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700287
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
290
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700291 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292 if (buttonBar != null) {
293 buttonBar.setVisibility(View.VISIBLE);
294
Fan Zhangf11c8852018-02-06 14:21:01 -0800295 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 backButton.setOnClickListener(new OnClickListener() {
297 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700298 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 finish();
300 }
301 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800302 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 skipButton.setOnClickListener(new OnClickListener() {
304 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700305 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 finish();
307 }
308 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800309 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 mNextButton.setOnClickListener(new OnClickListener() {
311 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700312 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 finish();
314 }
315 });
316
317 // set our various button parameters
318 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
319 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
320 if (TextUtils.isEmpty(buttonText)) {
321 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800322 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 mNextButton.setText(buttonText);
324 }
325 }
326 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
327 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
328 if (TextUtils.isEmpty(buttonText)) {
329 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800330 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 backButton.setText(buttonText);
332 }
333 }
334 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
335 skipButton.setVisibility(View.VISIBLE);
336 }
337 }
338 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700339
Fan Zhangefba6b42017-02-07 17:40:59 -0800340 if (DEBUG_TIMING) {
341 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
342 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700343 }
344
Fan Zhangaf32e722018-07-17 10:12:30 -0700345 @Override
346 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
347 super.onActivityResult(requestCode, resultCode, data);
348 final List<Fragment> fragments = getSupportFragmentManager().getFragments();
349 if (fragments != null) {
350 for (Fragment fragment : fragments) {
351 if (fragment instanceof OnActivityResultListener) {
352 fragment.onActivityResult(requestCode, resultCode, data);
353 }
354 }
355 }
356 }
357
Doris Lingcb69baf2017-02-23 17:50:03 -0800358 @VisibleForTesting
Fan Zhang8e14ca02018-11-27 13:06:44 -0800359 void launchSettingFragment(String initialFragmentName, Intent intent) {
360 if (initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800361 setTitleFromIntent(intent);
362
363 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
364 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800365 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800366 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700367 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800368 mInitialTitleResId = R.string.dashboard_title;
Fan Zhang130c4772018-10-01 12:14:06 -0700369 switchToFragment(TopLevelSettings.class.getName(), null /* args */, false, false,
370 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800371
Doris Lingcb69baf2017-02-23 17:50:03 -0800372 }
373 }
374
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700375 private void setTitleFromIntent(Intent intent) {
Fan Zhang22230502018-03-27 14:14:13 -0700376 Log.d(LOG_TAG, "Starting to set activity title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700377 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
378 if (initialTitleResId > 0) {
379 mInitialTitle = null;
380 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100381
382 final String initialTitleResPackageName = intent.getStringExtra(
383 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
384 if (initialTitleResPackageName != null) {
385 try {
386 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
387 0 /* flags */, new UserHandle(UserHandle.myUserId()));
388 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
389 setTitle(mInitialTitle);
390 mInitialTitleResId = -1;
391 return;
392 } catch (NameNotFoundException e) {
393 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
394 }
395 } else {
396 setTitle(mInitialTitleResId);
397 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700398 } else {
399 mInitialTitleResId = -1;
400 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
401 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
402 setTitle(mInitialTitle);
403 }
Fan Zhang22230502018-03-27 14:14:13 -0700404 Log.d(LOG_TAG, "Done setting title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700405 }
406
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800407 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700409 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 }
411
Fan Zhang28691572016-03-23 15:31:08 -0700412 private void setTitleFromBackStack() {
tmfang27c84de2018-06-28 11:39:05 +0800413 final int count = getSupportFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700414
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800415 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700416 if (mInitialTitleResId > 0) {
417 setTitle(mInitialTitleResId);
418 } else {
419 setTitle(mInitialTitle);
420 }
Fan Zhang28691572016-03-23 15:31:08 -0700421 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800422 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700423
tmfang27c84de2018-06-28 11:39:05 +0800424 FragmentManager.BackStackEntry bse = getSupportFragmentManager().
425 getBackStackEntryAt(count - 1);
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800426 setTitleFromBackStackEntry(bse);
427 }
428
429 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
430 final CharSequence title;
431 final int titleRes = bse.getBreadCrumbTitleRes();
432 if (titleRes > 0) {
433 title = getText(titleRes);
434 } else {
435 title = bse.getBreadCrumbTitle();
436 }
437 if (title != null) {
438 setTitle(title);
439 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800440 }
441
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800442 @Override
443 protected void onSaveInstanceState(Bundle outState) {
444 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700445 saveState(outState);
446 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800447
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700448 /**
449 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
450 */
451 @VisibleForTesting
452 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700453 if (mCategories.size() > 0) {
454 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800455 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800456 }
457
458 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400459 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800460 super.onResume();
461
Tony Mantler3d84d562017-07-31 10:48:55 -0700462 mDevelopmentSettingsListener = new BroadcastReceiver() {
463 @Override
464 public void onReceive(Context context, Intent intent) {
465 updateTilesList();
466 }
467 };
468 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
469 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700472
Jason Monk4da79e02015-09-10 10:54:36 -0400473 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 }
475
476 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400477 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700479 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
480 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800482 }
483
484 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800485 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Sunny Goyal3c846752018-07-03 11:39:00 -0700486 taskDescription.setIcon(R.drawable.ic_launcher_settings);
jackqdyulei6c355f52017-03-01 17:37:23 -0800487 super.setTaskDescription(taskDescription);
488 }
489
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490 protected boolean isValidFragment(String fragmentName) {
491 // Almost all fragments are wrapped in this,
492 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800493 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
494 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 }
496 return false;
497 }
498
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 @Override
500 public Intent getIntent() {
501 Intent superIntent = super.getIntent();
502 String startingFragment = getStartingFragmentClass(superIntent);
503 // This is called from super.onCreate, isMultiPane() is not yet reliable
504 // Do not use onIsHidingHeaders either, which relies itself on this method
505 if (startingFragment != null) {
506 Intent modIntent = new Intent(superIntent);
507 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Fan Zhange8666c62018-03-02 11:01:15 -0800508 Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509 if (args != null) {
510 args = new Bundle(args);
511 } else {
512 args = new Bundle();
513 }
514 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100515 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 return modIntent;
517 }
518 return superIntent;
519 }
520
521 /**
522 * Checks if the component name in the intent is different from the Settings class and
523 * returns the class name to load as a fragment.
524 */
525 private String getStartingFragmentClass(Intent intent) {
526 if (mFragmentClass != null) return mFragmentClass;
527
528 String intentClass = intent.getComponent().getClassName();
529 if (intentClass.equals(getClass().getName())) return null;
530
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700531 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
533 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700534 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 }
536
537 return intentClass;
538 }
539
540 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 * Called by a preference panel fragment to finish itself.
542 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800544 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800546 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 */
Fan Zhang47854c22018-02-19 13:54:09 -0800548 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700550 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800551 }
552
553 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700554 * Switch to a specific Fragment with taking care of validation, Title and BackStack
555 */
556 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700557 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fan Zhang22230502018-03-27 14:14:13 -0700558 Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700559 if (validate && !isValidFragment(fragmentName)) {
560 throw new IllegalArgumentException("Invalid fragment for this activity: "
561 + fragmentName);
562 }
563 Fragment f = Fragment.instantiate(this, fragmentName, args);
tmfang27c84de2018-06-28 11:39:05 +0800564 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700565 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700566 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700567 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700568 }
569 if (addToBackStack) {
570 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
571 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700572 if (titleResId > 0) {
573 transaction.setBreadCrumbTitle(titleResId);
574 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700575 transaction.setBreadCrumbTitle(title);
576 }
577 transaction.commitAllowingStateLoss();
tmfang27c84de2018-06-28 11:39:05 +0800578 getSupportFragmentManager().executePendingTransactions();
Fan Zhang22230502018-03-27 14:14:13 -0700579 Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700580 return f;
581 }
582
Jason Monk4da79e02015-09-10 10:54:36 -0400583 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500584 // Generally the items that are will be changing from these updates will
585 // not be in the top list of tiles, so run it in the background and the
tmfangd97fba52018-06-14 14:48:35 +0800586 // SettingsBaseActivity will pick up on the updates automatically.
Jason Monkfd2c7222015-12-02 15:38:38 -0500587 AsyncTask.execute(new Runnable() {
588 @Override
589 public void run() {
590 doUpdateTilesList();
591 }
592 });
593 }
594
595 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400596 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700597 final UserManager um = UserManager.get(this);
598 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700599 boolean somethingChanged = false;
Fan Zhang22230502018-03-27 14:14:13 -0700600 final String packageName = getPackageName();
601 final StringBuilder changedList = new StringBuilder();
602 somethingChanged = setTileEnabled(changedList,
Fan Zhangaeb94f02017-07-05 13:46:04 -0700603 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
604 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700605
Fan Zhang22230502018-03-27 14:14:13 -0700606 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800607 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700608 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
609 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700610
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700611 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
612 // enable DataPlanUsageSummaryActivity.
Fan Zhang22230502018-03-27 14:14:13 -0700613 somethingChanged = setTileEnabled(changedList,
Fan Zhang3ed55b12018-02-07 13:50:09 -0800614 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700615 Utils.isBandwidthControlEnabled() /* enabled */,
616 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700617
Fan Zhang22230502018-03-27 14:14:13 -0700618 somethingChanged = setTileEnabled(changedList,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800619 new ComponentName(packageName,
620 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Jerry Zhang40bd5702018-02-23 14:43:58 -0800621 !UserManager.isDeviceInDemoMode(this) /* enabled */,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800622 isAdmin) || somethingChanged;
623
Fan Zhang22230502018-03-27 14:14:13 -0700624 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800625 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700626 Utils.showSimCardTile(this), isAdmin)
627 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700628
Fan Zhang22230502018-03-27 14:14:13 -0700629 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800630 Settings.PowerUsageSummaryActivity.class.getName()),
Lei Yu68ffee12018-03-26 14:31:57 -0700631 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400632
Fan Zhang22230502018-03-27 14:14:13 -0700633 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800634 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhangcd21cb82018-06-11 09:28:02 -0700635 Utils.isBandwidthControlEnabled(), isAdmin)
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800636 || somethingChanged;
637
Fan Zhang22230502018-03-27 14:14:13 -0700638 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800639 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400640 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700641 && !Utils.isMonkeyRunning(), isAdmin)
642 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400643
Tony Mantler3d84d562017-07-31 10:48:55 -0700644 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800645 && !Utils.isMonkeyRunning();
Fan Zhang22230502018-03-27 14:14:13 -0700646 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4c26da92017-09-08 15:29:54 -0700647 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
Christine Franks986d02f2018-04-23 11:19:57 -0700648 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700649 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400650
Fan Zhanga6986e72017-03-08 09:24:45 -0800651 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhang22230502018-03-27 14:14:13 -0700652 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhangaeb94f02017-07-05 13:46:04 -0700653 BackupSettingsActivity.class.getName()), true, isAdmin)
654 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800655
Fan Zhang22230502018-03-27 14:14:13 -0700656 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800657 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700658 WifiDisplaySettings.isAvailable(this), isAdmin)
659 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800660
Jason Monk4da79e02015-09-10 10:54:36 -0400661 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800662
Jason Monk4da79e02015-09-10 10:54:36 -0400663 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800664 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800665 synchronized (categories) {
666 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700667 final int tileCount = category.getTilesCount();
668 for (int i = 0; i < tileCount; i++) {
Fan Zhang3506b742018-08-07 13:52:52 -0700669 final ComponentName component = category.getTile(i)
670 .getIntent().getComponent();
Fan Zhanga6986e72017-03-08 09:24:45 -0800671 final String name = component.getClassName();
672 final boolean isEnabledForRestricted = ArrayUtils.contains(
Christine Franks986d02f2018-04-23 11:19:57 -0700673 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
Fan Zhanga6986e72017-03-08 09:24:45 -0800674 if (packageName.equals(component.getPackageName())
675 && !isEnabledForRestricted) {
Fan Zhang22230502018-03-27 14:14:13 -0700676 somethingChanged =
677 setTileEnabled(changedList, component, false, isAdmin)
678 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800679 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700680 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700681 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700682 }
683 }
Doris Ling26bf0032016-06-15 18:16:09 -0700684
Fan Zhang224caad2017-01-06 11:42:48 -0800685 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700686 if (somethingChanged) {
Fan Zhang22230502018-03-27 14:14:13 -0700687 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories "
688 + changedList.toString());
Fan Zhangaeb94f02017-07-05 13:46:04 -0700689 updateCategories();
690 } else {
691 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
692 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500693 }
694
Fan Zhangaeb94f02017-07-05 13:46:04 -0700695 /**
696 * @return whether or not the enabled state actually changed.
697 */
Fan Zhang22230502018-03-27 14:14:13 -0700698 private boolean setTileEnabled(StringBuilder changedList, ComponentName component,
699 boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400700 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800701 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
702 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400703 enabled = false;
704 }
Fan Zhang22230502018-03-27 14:14:13 -0700705 boolean changed = setTileEnabled(component, enabled);
706 if (changed) {
707 changedList.append(component.toShortString()).append(",");
708 }
709 return changed;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500710 }
711
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800712 private void getMetaData() {
713 try {
714 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
715 PackageManager.GET_META_DATA);
716 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800717 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
718 } catch (NameNotFoundException nnfe) {
719 // No recovery
720 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
721 }
722 }
723
724 // give subclasses access to the Next button
725 public boolean hasNextButton() {
726 return mNextButton != null;
727 }
728
729 public Button getNextButton() {
730 return mNextButton;
731 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700732}