blob: 00bdf5780d74abc60509e8728bd04c36fdabee9e [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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080033import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080034import android.nfc.NfcAdapter;
Jason Monkfd2c7222015-12-02 15:38:38 -050035import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080036import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.UserHandle;
38import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080039import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040040import android.support.v14.preference.PreferenceFragment;
41import android.support.v7.preference.Preference;
42import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080043import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070044import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.util.Log;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070046import android.view.Menu;
47import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.view.MenuItem;
49import android.view.View;
50import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070053import android.widget.SearchView;
Fan Zhang28691572016-03-23 15:31:08 -070054
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040056import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000057import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080058import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080059import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070060import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhang28691572016-03-23 15:31:08 -070061import com.android.settings.dashboard.DashboardContainerFragment;
Fan Zhanga96a2d82016-09-27 17:51:11 -070062import com.android.settings.dashboard.DashboardFeatureProvider;
63import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070064import com.android.settings.dashboard.SearchResultsSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070065import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050066import com.android.settings.search.DynamicIndexableContentMonitor;
Matthew Fritze0ed37c32016-10-24 10:12:49 -070067import com.android.settings.search2.SearchFeatureProvider;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070068import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk7ea14c52016-01-22 14:28:02 -050071import com.android.settingslib.drawer.Tile;
Jason Monk2ebc8a02015-02-13 15:23:19 -050072
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070075import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076
Jason Monk4da79e02015-09-10 10:54:36 -040077public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078 implements PreferenceManager.OnPreferenceTreeClickListener,
79 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +010080 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070081 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +010082 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083
84 private static final String LOG_TAG = "Settings";
85
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080086 private static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
87
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070089 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070090 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
91 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -070092 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -070093 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080094
95 /**
96 * When starting this activity, the invoking Intent can contain this extra
97 * string to specify which fragment should be initially displayed.
98 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
99 * will call isValidFragment() to confirm that the fragment class name is valid for this
100 * activity.
101 */
102 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
103
104 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800105 * The metrics category constant for logging source when a setting fragment is opened.
106 */
107 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
108
109 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
111 * this extra can also be specified to supply a Bundle of arguments to pass
112 * to that fragment when it is instantiated during the initial creation
113 * of the activity.
114 */
115 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
116
117 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700118 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
119 */
120 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
121
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800122 public static final String BACK_STACK_PREFS = ":settings:prefs";
123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124 // extras that allow any preference activity to be launched as part of a wizard
125
126 // show Back and Next buttons? takes boolean parameter
127 // Back will then return RESULT_CANCELED and Next RESULT_OK
128 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
129
130 // add a Skip button?
131 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
132
133 // specify custom text for the Back or Next buttons, or cause a button to not appear
134 // at all by setting it to null
135 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
136 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
137
138 /**
139 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700140 * 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 -0800141 * that fragment.
142 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700143 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100144 /**
145 * The package name used to resolve the title resource id.
146 */
147 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
148 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700149 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
150 ":settings:show_fragment_title_resid";
151 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
152 ":settings:show_fragment_as_shortcut";
153
154 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
155 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800156
Udam Saini92779ce2016-03-28 14:29:48 -0700157 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
158
Jason Monk30695812015-11-17 09:01:08 -0500159 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160 "com.android.settings.FRAGMENT_CLASS";
161
162 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
163
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700164 private static final String EMPTY_QUERY = "";
165
Jason Monkd4f03ec2016-01-07 16:25:34 -0500166 private static final int REQUEST_SUGGESTION = 42;
167
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800169
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800170 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700171 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800172
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700173 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
174 "android.settings.APPLICATION_DETAILS_SETTINGS"
175 };
176
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 private SharedPreferences mDevelopmentPreferences;
178 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
179
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800180 private boolean mBatteryPresent = true;
181 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 @Override
183 public void onReceive(Context context, Intent intent) {
184 String action = intent.getAction();
185 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
186 boolean batteryPresent = Utils.isBatteryPresent(intent);
187
188 if (mBatteryPresent != batteryPresent) {
189 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400190 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 }
192 }
193 }
194 };
195
Clara Bayarri6934a042015-10-14 11:07:35 +0100196 private final BroadcastReceiver mUserAddRemoveReceiver = new BroadcastReceiver() {
197 @Override
198 public void onReceive(Context context, Intent intent) {
Matthew Fritze34c27602017-01-18 18:24:56 -0800199 if (mSearchFeatureProvider != null && !mSearchFeatureProvider.isEnabled(context)) {
200 String action = intent.getAction();
201 if (action.equals(Intent.ACTION_USER_ADDED)
202 || action.equals(Intent.ACTION_USER_REMOVED)) {
203 mSearchFeatureProvider.updateIndex(getApplicationContext());
204 }
Clara Bayarri6934a042015-10-14 11:07:35 +0100205 }
206 }
207 };
208
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800209 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700210
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700211 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700212 private SwitchBar mSwitchBar;
213
214 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700215
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700216 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700217 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700218
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700219 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700220 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700221
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700222 private ViewGroup mContent;
223
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700224 private SearchView mSearchView;
225 private MenuItem mSearchMenuItem;
226 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700227 private SearchResultsSummary mSearchResultsFragment;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700228 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800229 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700230
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700231 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800232 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800233
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700234 private boolean mNeedToRevertToInitialFragment = false;
235
Fan Zhanga96a2d82016-09-27 17:51:11 -0700236 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700237 private Intent mResultIntentData;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500238 private ComponentName mCurrentSuggestion;
Doris Ling15ae3292017-02-27 11:39:05 -0800239 private final StringBuffer mDebugData = new StringBuffer();
Jim Miller0698a212014-10-16 19:49:07 -0700240
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800241 @VisibleForTesting
242 String mSearchQuery;
243
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700244 public SwitchBar getSwitchBar() {
245 return mSwitchBar;
246 }
247
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 @Override
249 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800250 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 null, 0);
252 return true;
253 }
254
255 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400256 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 return false;
258 }
259
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 public void onConfigurationChanged(Configuration newConfig) {
262 super.onConfigurationChanged(newConfig);
Matthew Fritze34c27602017-01-18 18:24:56 -0800263 if (!mSearchFeatureProvider.isEnabled(this)) {
264 mSearchFeatureProvider.updateIndex(getApplicationContext());
265 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800266 }
267
268 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700269 protected void onStart() {
270 super.onStart();
271
272 if (mNeedToRevertToInitialFragment) {
273 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 }
276
277 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700278 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700279 if (!mDisplaySearch) {
280 return false;
281 }
282
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700283 MenuInflater inflater = getMenuInflater();
Matthew Fritze9955db62016-12-20 09:37:21 -0800284 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700285 mSearchFeatureProvider.setUpSearchMenu(menu, this);
286 return true;
287 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700288 inflater.inflate(R.menu.options_menu, menu);
289
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700290
291 // Cache the search query (can be overridden by the OnQueryTextListener)
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700292 final String query = mSearchQuery;
293
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700294 mSearchMenuItem = menu.findItem(R.id.search);
295 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700296
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700297 if (mSearchMenuItem == null || mSearchView == null) {
298 return false;
299 }
300
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700301 if (mSearchResultsFragment != null) {
302 mSearchResultsFragment.setSearchView(mSearchView);
303 }
304
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700305 mSearchMenuItem.setOnActionExpandListener(this);
Fan Zhangb9239182016-08-22 13:59:59 -0700306 mSearchView.setMaxWidth(Integer.MAX_VALUE);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700307 mSearchView.setOnQueryTextListener(this);
308 mSearchView.setOnCloseListener(this);
309
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700310 if (mSearchMenuItemExpanded) {
311 mSearchMenuItem.expandActionView();
312 }
313 mSearchView.setQuery(query, true /* submit */);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700314 return true;
315 }
316
Jason Monk31c7c322016-01-20 14:41:52 -0500317 @Override
318 public SharedPreferences getSharedPreferences(String name, int mode) {
319 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500320 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500321 }
322 return super.getSharedPreferences(name, mode);
323 }
324
Jason Monke4ebcd12016-02-21 09:37:41 -0500325 private String getMetricsTag() {
326 String tag = getClass().getName();
327 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
328 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
329 }
330 if (tag.startsWith("com.android.settings.")) {
331 tag = tag.replace("com.android.settings.", "");
332 }
333 return tag;
334 }
335
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700336 private static boolean isShortCutIntent(final Intent intent) {
337 Set<String> categories = intent.getCategories();
338 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
339 }
340
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700341 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700342 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700343 if (action == null) {
344 return false;
345 }
346 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
347 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
348 }
349 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700350 }
351
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700352 @Override
353 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700354 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500355 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700356
357 final FeatureFactory factory = FeatureFactory.getFactory(this);
358
359 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800360 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800361 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700362
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700363 // Should happen before any call to getIntent()
364 getMetaData();
365
366 final Intent intent = getIntent();
367 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
368 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 }
Udam Saini92779ce2016-03-28 14:29:48 -0700370 if (intent.getBooleanExtra(EXTRA_HIDE_DRAWER, false)) {
371 setIsDrawerPresent(false);
372 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
375 Context.MODE_PRIVATE);
376
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700377 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700378 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700379
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700380 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700381 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
382
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700383 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700384 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700385
Jason Monk4da79e02015-09-10 10:54:36 -0400386 mIsShowingDashboard = className.equals(Settings.class.getName())
387 || className.equals(Settings.WirelessSettings.class.getName())
388 || className.equals(Settings.DeviceSettings.class.getName())
389 || className.equals(Settings.PersonalSettings.class.getName())
390 || className.equals(Settings.WirelessSettings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700391
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700392 // This is a "Sub Settings" when:
393 // - this is a real SubSettings
394 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700395 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700396 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
397
Fan Zhang92f1e942017-01-21 10:07:26 -0800398 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
399 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700400 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800401 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700402 }
403
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700404 setContentView(mIsShowingDashboard ?
405 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800406
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700407 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700408
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800409 getFragmentManager().addOnBackStackChangedListener(this);
410
Matthew Fritze34c27602017-01-18 18:24:56 -0800411 if (mIsShowingDashboard && !mSearchFeatureProvider.isEnabled(this)) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800412 // Run the Index update only if we have some space
413 if (!Utils.isLowStorage(this)) {
Fan Zhanga8759242017-01-06 09:18:59 -0800414 mSearchFeatureProvider.updateIndex(getApplicationContext());
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800415 } else {
416 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
417 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700418 }
419
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700421 // We are restarting from a previous saved state; used that to initialize, instead
422 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700423 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
424 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700425 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800426
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700427 ArrayList<DashboardCategory> categories =
428 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
429 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700430 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700431 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700432 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800433 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700434
435 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700436 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Salvador Martinez2368e542016-07-25 18:16:47 -0700437
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800438 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700439 if (!mIsShowingDashboard) {
Doris Ling15ae3292017-02-27 11:39:05 -0800440 if (initialFragmentName == null) {
441 logFragmentData(intent, className, isSubSettings);
442 }
Jason Monkea8b1a72015-06-02 16:46:34 -0400443 mDisplaySearch = false;
444 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700445 if (mIsShortcut) {
446 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700447 } else if (isSubSettings) {
448 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700449 } else {
450 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700451 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700452 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700453
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700454 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700455 switchToFragment(initialFragmentName, initialArguments, true, false,
456 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000457 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700458 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700459 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700460 // Show Search affordance
461 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700462 mInitialTitleResId = R.string.dashboard_title;
Salvador Martinez2368e542016-07-25 18:16:47 -0700463
464 // add argument to indicate which settings tab should be initially selected
465 final Bundle args = new Bundle();
466 final String extraName = DashboardContainerFragment.EXTRA_SELECT_SETTINGS_TAB;
467 args.putString(extraName, intent.getStringExtra(extraName));
Fan Zhanga96a2d82016-09-27 17:51:11 -0700468 if (isDashboardFeatureEnabled()) {
469 switchToFragment(DashboardSummary.class.getName(), args, false, false,
470 mInitialTitleResId, mInitialTitle, false);
471 } else {
472 switchToFragment(DashboardContainerFragment.class.getName(), args, false, false,
473 mInitialTitleResId, mInitialTitle, false);
474 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 }
476 }
477
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700478 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700479 if (mActionBar != null) {
480 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
481 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
482 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700483 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500484 if (mSwitchBar != null) {
485 mSwitchBar.setMetricsTag(getMetricsTag());
486 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700487
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800489 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
490
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700491 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 if (buttonBar != null) {
493 buttonBar.setVisibility(View.VISIBLE);
494
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700495 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800496 backButton.setOnClickListener(new OnClickListener() {
497 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700498 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 finish();
500 }
501 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700502 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800503 skipButton.setOnClickListener(new OnClickListener() {
504 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700505 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800506 finish();
507 }
508 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700509 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510 mNextButton.setOnClickListener(new OnClickListener() {
511 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700512 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800513 finish();
514 }
515 });
516
517 // set our various button parameters
518 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
519 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
520 if (TextUtils.isEmpty(buttonText)) {
521 mNextButton.setVisibility(View.GONE);
522 }
523 else {
524 mNextButton.setText(buttonText);
525 }
526 }
527 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
528 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
529 if (TextUtils.isEmpty(buttonText)) {
530 backButton.setVisibility(View.GONE);
531 }
532 else {
533 backButton.setText(buttonText);
534 }
535 }
536 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
537 skipButton.setVisibility(View.VISIBLE);
538 }
539 }
540 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700541
Fan Zhangefba6b42017-02-07 17:40:59 -0800542 if (DEBUG_TIMING) {
543 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
544 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700545 }
546
Fan Zhangece8ff62016-06-30 13:20:12 -0700547 public void setDisplaySearchMenu(boolean displaySearch) {
548 if (displaySearch != mDisplaySearch) {
549 mDisplaySearch = displaySearch;
550 invalidateOptionsMenu();
551 }
552 }
553
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700554 private void setTitleFromIntent(Intent intent) {
555 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
556 if (initialTitleResId > 0) {
557 mInitialTitle = null;
558 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100559
560 final String initialTitleResPackageName = intent.getStringExtra(
561 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
562 if (initialTitleResPackageName != null) {
563 try {
564 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
565 0 /* flags */, new UserHandle(UserHandle.myUserId()));
566 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
567 setTitle(mInitialTitle);
568 mInitialTitleResId = -1;
569 return;
570 } catch (NameNotFoundException e) {
571 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
572 }
573 } else {
574 setTitle(mInitialTitleResId);
575 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700576 } else {
577 mInitialTitleResId = -1;
578 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
579 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
580 setTitle(mInitialTitle);
581 }
582 }
583
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800584 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700586 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800587 }
588
Fan Zhang28691572016-03-23 15:31:08 -0700589 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800590 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700591
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800592 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700593 if (mInitialTitleResId > 0) {
594 setTitle(mInitialTitleResId);
595 } else {
596 setTitle(mInitialTitle);
597 }
Fan Zhang28691572016-03-23 15:31:08 -0700598 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800599 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700600
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800601 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
602 setTitleFromBackStackEntry(bse);
603 }
604
605 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
606 final CharSequence title;
607 final int titleRes = bse.getBreadCrumbTitleRes();
608 if (titleRes > 0) {
609 title = getText(titleRes);
610 } else {
611 title = bse.getBreadCrumbTitle();
612 }
613 if (title != null) {
614 setTitle(title);
615 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800616 }
617
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800618 @Override
619 protected void onSaveInstanceState(Bundle outState) {
620 super.onSaveInstanceState(outState);
621
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700622 if (mCategories.size() > 0) {
623 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800624 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700625
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700626 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700627 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700628
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700629 if (mDisplaySearch) {
630 // The option menus are created if the ActionBar is visible and they are also created
631 // asynchronously. If you launch Settings with an Intent action like
632 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
633 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
634 // menu item and search view are null.
635 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
636 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700637
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700638 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
639 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
640 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800641 }
642
643 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400644 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 super.onResume();
646
647 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
648 @Override
649 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400650 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 }
652 };
653 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
654 mDevelopmentPreferencesListener);
655
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Matthew Fritze34c27602017-01-18 18:24:56 -0800657 if (!mSearchFeatureProvider.isEnabled(this)) {
658 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_ADDED));
659 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED));
660 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800661 if (mDynamicIndexableContentMonitor == null) {
662 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
663 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800664 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700665
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700666 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700667 onQueryTextSubmit(mSearchQuery);
668 }
Jason Monk4da79e02015-09-10 10:54:36 -0400669 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 }
671
672 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400673 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800674 super.onPause();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800675 unregisterReceiver(mBatteryInfoReceiver);
Matthew Fritze34c27602017-01-18 18:24:56 -0800676 if (!mSearchFeatureProvider.isEnabled(this)) {
677 unregisterReceiver(mUserAddRemoveReceiver);
678 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800679 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900680 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800681 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800682 }
683
684 @Override
685 public void onDestroy() {
686 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700687
688 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
689 mDevelopmentPreferencesListener);
690 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800691 }
692
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 protected boolean isValidFragment(String fragmentName) {
694 // Almost all fragments are wrapped in this,
695 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800696 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
697 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800698 }
699 return false;
700 }
701
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800702 @Override
703 public Intent getIntent() {
704 Intent superIntent = super.getIntent();
705 String startingFragment = getStartingFragmentClass(superIntent);
706 // This is called from super.onCreate, isMultiPane() is not yet reliable
707 // Do not use onIsHidingHeaders either, which relies itself on this method
Doris Ling15ae3292017-02-27 11:39:05 -0800708 log("getIntent() startingFragment", startingFragment);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 if (startingFragment != null) {
710 Intent modIntent = new Intent(superIntent);
711 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
712 Bundle args = superIntent.getExtras();
713 if (args != null) {
714 args = new Bundle(args);
715 } else {
716 args = new Bundle();
717 }
718 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100719 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800720 return modIntent;
721 }
722 return superIntent;
723 }
724
725 /**
726 * Checks if the component name in the intent is different from the Settings class and
727 * returns the class name to load as a fragment.
728 */
729 private String getStartingFragmentClass(Intent intent) {
Doris Ling15ae3292017-02-27 11:39:05 -0800730 log("getStartingFragmentClass() mFragmentClass", mFragmentClass);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800731 if (mFragmentClass != null) return mFragmentClass;
732
733 String intentClass = intent.getComponent().getClassName();
Doris Ling15ae3292017-02-27 11:39:05 -0800734 log("getStartingFragmentClass() intentClass", intentClass);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 if (intentClass.equals(getClass().getName())) return null;
736
737 if ("com.android.settings.ManageApplications".equals(intentClass)
738 || "com.android.settings.RunningServices".equals(intentClass)
739 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
740 // Old names of manage apps.
741 intentClass = com.android.settings.applications.ManageApplications.class.getName();
742 }
743
744 return intentClass;
745 }
746
747 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000748 * Start a new fragment containing a preference panel. If the preferences
749 * are being displayed in multi-pane mode, the given fragment class will
750 * be instantiated and placed in the appropriate pane. If running in
751 * single-pane mode, a new activity will be launched in which to show the
752 * fragment.
753 *
754 * @param fragmentClass Full name of the class implementing the fragment.
755 * @param args Any desired arguments to supply to the fragment.
756 * @param titleRes Optional resource identifier of the title of this
757 * fragment.
758 * @param titleText Optional text of the title of this fragment.
759 * @param resultTo Optional fragment that result data should be sent to.
760 * If non-null, resultTo.onActivityResult() will be called when this
761 * preference panel is done. The launched panel must use
762 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
763 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700764 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000765 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800766 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
767 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700768 String title = null;
769 if (titleRes < 0) {
770 if (titleText != null) {
771 title = titleText.toString();
772 } else {
773 // There not much we can do in that case
774 title = "";
775 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700776 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700777 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800778 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000779 }
780
781 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100782 * Start a new fragment in a new activity containing a preference panel for a given user. If the
783 * preferences are being displayed in multi-pane mode, the given fragment class will be
784 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
785 * activity will be launched in which to show the fragment.
786 *
787 * @param fragmentClass Full name of the class implementing the fragment.
788 * @param args Any desired arguments to supply to the fragment.
789 * @param titleRes Optional resource identifier of the title of this fragment.
790 * @param titleText Optional text of the title of this fragment.
791 * @param userHandle The user for which the panel has to be started.
792 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800793 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
794 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700795 // This is a workaround.
796 //
797 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
798 // starting the fragment could cause a native stack corruption. See b/17523189. However,
799 // adding that flag and start the preference panel with the same UserHandler will make it
800 // impossible to use back button to return to the previous screen. See b/20042570.
801 //
802 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
803 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
804 // when we're calling it as the same user.
805 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800806 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700807 } else {
808 String title = null;
809 if (titleRes < 0) {
810 if (titleText != null) {
811 title = titleText.toString();
812 } else {
813 // There not much we can do in that case
814 title = "";
815 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100816 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800817 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
818 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100819 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100820 }
821
822 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800823 * Called by a preference panel fragment to finish itself.
824 *
825 * @param caller The fragment that is asking to be finished.
826 * @param resultCode Optional result code to send back to the original
827 * launching fragment.
828 * @param resultData Optional result data to send back to the original
829 * launching fragment.
830 */
831 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
832 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700833 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800834 }
835
836 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000837 * Start a new fragment.
838 *
839 * @param fragment The fragment to start
840 * @param push If true, the current fragment will be pushed onto the back stack. If false,
841 * the current fragment will be replaced.
842 */
843 public void startPreferenceFragment(Fragment fragment, boolean push) {
844 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700845 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000846 if (push) {
847 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
848 transaction.addToBackStack(BACK_STACK_PREFS);
849 } else {
850 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
851 }
852 transaction.commitAllowingStateLoss();
853 }
854
855 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700856 * Switch to a specific Fragment with taking care of validation, Title and BackStack
857 */
858 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700859 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700860 if (validate && !isValidFragment(fragmentName)) {
861 throw new IllegalArgumentException("Invalid fragment for this activity: "
862 + fragmentName);
863 }
864 Fragment f = Fragment.instantiate(this, fragmentName, args);
865 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700866 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700867 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700868 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700869 }
870 if (addToBackStack) {
871 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
872 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700873 if (titleResId > 0) {
874 transaction.setBreadCrumbTitle(titleResId);
875 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700876 transaction.setBreadCrumbTitle(title);
877 }
878 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700879 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700880 return f;
881 }
882
Jason Monk4da79e02015-09-10 10:54:36 -0400883 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500884 // Generally the items that are will be changing from these updates will
885 // not be in the top list of tiles, so run it in the background and the
886 // SettingsDrawerActivity will pick up on the updates automatically.
887 AsyncTask.execute(new Runnable() {
888 @Override
889 public void run() {
890 doUpdateTilesList();
891 }
892 });
893 }
894
895 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400896 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700897 final UserManager um = UserManager.get(this);
898 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700899
Jason Monk4da79e02015-09-10 10:54:36 -0400900 String packageName = getPackageName();
901 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800902 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700903
Jason Monk4da79e02015-09-10 10:54:36 -0400904 setTileEnabled(new ComponentName(packageName,
905 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800906 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700907
Jason Monk4da79e02015-09-10 10:54:36 -0400908 setTileEnabled(new ComponentName(packageName,
909 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800910 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700911
Jason Monk4da79e02015-09-10 10:54:36 -0400912 setTileEnabled(new ComponentName(packageName,
913 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800914 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700915
Jason Monk4da79e02015-09-10 10:54:36 -0400916 setTileEnabled(new ComponentName(packageName,
917 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800918 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400919
920 setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400921 Settings.UserSettingsActivity.class.getName()),
922 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhang224caad2017-01-06 11:42:48 -0800923 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400924
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700925 setTileEnabled(new ComponentName(packageName,
926 Settings.WirelessSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800927 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700928
929 setTileEnabled(new ComponentName(packageName,
930 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800931 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400932 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
933 setTileEnabled(new ComponentName(packageName,
934 Settings.PaymentSettingsActivity.class.getName()),
935 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
936 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800937 && adapter != null && adapter.isEnabled(), isAdmin);
Fan Zhangffd328b2017-01-09 10:16:49 -0800938 setTileEnabled(new ComponentName(packageName,
939 "com.android.settings.PaymentSettingsDashboardAlias"),
940 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
941 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
942 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400943
944 setTileEnabled(new ComponentName(packageName,
945 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800946 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400947
948 final boolean showDev = mDevelopmentPreferences.getBoolean(
Dan Sandler12c4ba42016-03-28 11:13:40 -0400949 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
950 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400951 setTileEnabled(new ComponentName(packageName,
952 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800953 showDev, isAdmin);
Fan Zhangcc335d92016-09-29 14:37:14 -0700954 setTileEnabled(new ComponentName(packageName,
955 Settings.DevelopmentSettingsActivity.DASHBOARD_ALIAS),
Fan Zhang224caad2017-01-06 11:42:48 -0800956 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400957
Jason Monk4da79e02015-09-10 10:54:36 -0400958 if (UserHandle.MU_ENABLED && !isAdmin) {
959 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhanga96a2d82016-09-27 17:51:11 -0700960 List<DashboardCategory> categories;
961 if (isDashboardFeatureEnabled()) {
962 categories = mDashboardFeatureProvider.getAllCategories();
963 } else {
964 categories = getDashboardCategories();
965 }
966
Jason Monk4da79e02015-09-10 10:54:36 -0400967 for (DashboardCategory category : categories) {
Jason Monkd4f03ec2016-01-07 16:25:34 -0500968 for (Tile tile : category.tiles) {
Jason Monk4da79e02015-09-10 10:54:36 -0400969 ComponentName component = tile.intent.getComponent();
Fan Zhang9ec45362017-01-10 11:55:45 -0800970 final String name = component.getClassName();
971 final boolean isEnabledForRestricted = ArrayUtils.contains(
972 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
973 if (packageName.equals(component.getPackageName()) && !isEnabledForRestricted) {
Fan Zhang224caad2017-01-06 11:42:48 -0800974 setTileEnabled(component, false, isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700975 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700976 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700977 }
978 }
Doris Ling26bf0032016-06-15 18:16:09 -0700979
Anton Philippovadfec552017-01-25 20:37:36 +0000980 // Enable/disable backup settings depending on whether the user is admin.
Doris Ling26bf0032016-06-15 18:16:09 -0700981 setTileEnabled(new ComponentName(packageName,
Christine Franks14782222017-01-23 16:44:02 -0800982 BackupSettingsActivity.class.getName()), true,
983 isAdmin || Utils.isCarrierDemoUser(this));
Fan Zhang0e8d0d12016-11-02 09:15:33 -0700984 setTileEnabled(new ComponentName(packageName,
Anton Philippovadfec552017-01-25 20:37:36 +0000985 "com.android.settings.BackupResetDashboardAlias"), true, isAdmin);
Doris Ling26bf0032016-06-15 18:16:09 -0700986
Bartosz Fabianowski05061fc2016-11-14 12:04:49 +0100987 setTileEnabled(new ComponentName(packageName,
988 Settings.EnterprisePrivacySettingsActivity.class.getName()),
989 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800990 .hasDeviceOwner(), isAdmin);
Fan Zhang10dd4652016-11-16 10:24:26 -0800991 setTileEnabled(new ComponentName(packageName,
992 "com.android.settings.EnterprisePrivacyDashboardAlias"),
993 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800994 .hasDeviceOwner(), isAdmin);
995 // Final step, refresh categories.
996 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500997 }
998
Fan Zhang224caad2017-01-06 11:42:48 -0800999 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -04001000 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -08001001 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
1002 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -04001003 enabled = false;
1004 }
Jason Monk5862c1e2016-06-07 14:02:33 -04001005 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001006 }
1007
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001008 private void getMetaData() {
1009 try {
1010 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1011 PackageManager.GET_META_DATA);
1012 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001013 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1014 } catch (NameNotFoundException nnfe) {
1015 // No recovery
1016 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1017 }
1018 }
1019
Fan Zhanga96a2d82016-09-27 17:51:11 -07001020 @Override
1021 protected boolean isDashboardFeatureEnabled() {
1022 if (mDashboardFeatureProvider == null) {
1023 mDashboardFeatureProvider =
1024 FeatureFactory.getFactory(this).getDashboardFeatureProvider(this);
1025 }
1026 return mDashboardFeatureProvider.isEnabled();
1027 }
1028
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001029 // give subclasses access to the Next button
1030 public boolean hasNextButton() {
1031 return mNextButton != null;
1032 }
1033
1034 public Button getNextButton() {
1035 return mNextButton;
1036 }
1037
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001038 @Override
1039 public boolean shouldUpRecreateTask(Intent targetIntent) {
1040 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1041 }
1042
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001043 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001044 @Override
1045 public boolean onQueryTextSubmit(String query) {
Matthew Fritze9955db62016-12-20 09:37:21 -08001046 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001047 return false;
1048 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001049 mSearchQuery = query;
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001050 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001051 return mSearchResultsFragment.onQueryTextSubmit(query);
1052 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001053
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001054 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001055 @Override
1056 public boolean onQueryTextChange(String newText) {
Fan Zhanga3e8f5c2016-12-01 09:52:10 -08001057 mSearchQuery = newText;
Matthew Fritze9955db62016-12-20 09:37:21 -08001058 if (mSearchFeatureProvider.isEnabled(this) || mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001059 return false;
1060 }
1061 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001062 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001063
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001064 @Override
1065 public boolean onClose() {
1066 return false;
1067 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001068
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001069 @Override
1070 public boolean onMenuItemActionExpand(MenuItem item) {
1071 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001072 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001073 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001074 return true;
1075 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001076
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001077 @Override
1078 public boolean onMenuItemActionCollapse(MenuItem item) {
1079 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001080 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001081 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001082 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001083 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001084 return true;
1085 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001086
Jason Monk4da79e02015-09-10 10:54:36 -04001087 @Override
Jason Monkd4f03ec2016-01-07 16:25:34 -05001088 protected void onTileClicked(Tile tile) {
Jason Monk4da79e02015-09-10 10:54:36 -04001089 if (mIsShowingDashboard) {
1090 // If on dashboard, don't finish so the back comes back to here.
1091 openTile(tile);
1092 } else {
1093 super.onTileClicked(tile);
1094 }
1095 }
1096
Jason Monkaf001092015-11-04 13:16:18 -05001097 @Override
1098 public void onProfileTileOpen() {
1099 if (!mIsShowingDashboard) {
1100 finish();
1101 }
1102 }
1103
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001104 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001105 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001106 if (mSearchResultsFragment != null) {
1107 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001108 }
Anna Galusza6b1e9db2016-03-30 17:25:36 -07001109 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001110 if (current != null && current instanceof SearchResultsSummary) {
1111 mSearchResultsFragment = (SearchResultsSummary) current;
1112 } else {
Fan Zhang28691572016-03-23 15:31:08 -07001113 setContentHeaderView(null);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001114 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001115 SearchResultsSummary.class.getName(), null, false, true,
1116 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001117 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001118 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001119 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001120 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001121
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001122 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001123 public void needToRevertToInitialFragment() {
1124 mNeedToRevertToInitialFragment = true;
1125 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001126
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001127 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001128 private void revertToInitialFragment() {
1129 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001130 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001131 mSearchMenuItemExpanded = false;
1132 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1133 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001134 if (mSearchMenuItem != null) {
1135 mSearchMenuItem.collapseActionView();
1136 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001137 }
Jim Miller0698a212014-10-16 19:49:07 -07001138
1139 public Intent getResultIntentData() {
1140 return mResultIntentData;
1141 }
1142
1143 public void setResultIntentData(Intent resultIntentData) {
1144 mResultIntentData = resultIntentData;
1145 }
Jason Monkd4f03ec2016-01-07 16:25:34 -05001146
1147 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -07001148 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -07001149 return;
1150 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -07001151 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -05001152 startActivityForResult(intent, REQUEST_SUGGESTION);
1153 }
1154
1155 @Override
1156 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1157 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
1158 && resultCode != RESULT_CANCELED) {
1159 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
1160 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
1161 }
1162 super.onActivityResult(requestCode, resultCode, data);
1163 }
Doris Ling15ae3292017-02-27 11:39:05 -08001164
1165 private void logFragmentData(Intent intent, String className, boolean isSubSettings) {
1166 if (intent != null) {
1167 logBundleData(intent.getExtras(), "Intent extra");
1168 logBundleData(intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS), "Fragment args");
1169 } else {
1170 log("Intent data", "NULL");
1171 }
1172 log("Fragment", mFragmentClass);
1173 log("Shortcut", mIsShortcut);
1174 log("Class Name", className);
1175 log("Show dashboard", mIsShowingDashboard);
1176 log("Sub setting", isSubSettings);
1177 log("Title", mInitialTitle);
1178 Log.d(LOG_TAG, mDebugData.toString());
1179 mDebugData.delete(0, mDebugData.length());
1180 }
1181
1182 private void logBundleData(Bundle data, String name) {
1183 if (data != null) {
1184 final Set<String> keys = data.keySet();
1185 mDebugData.append(name).append(": ");
1186 for (String key : keys) {
1187 log(key, data.get(key));
1188 }
1189 } else {
1190 log(name, "NULL");
1191 }
1192 }
1193
1194 private void log(String key, Object data) {
1195 mDebugData.append(key).append("=").append(data).append(", ");
1196 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001197}