blob: 0435d03fe03105bafb8b9ce4a7fd06eb54159765 [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;
Jim Miller0698a212014-10-16 19:49:07 -0700239
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800240 @VisibleForTesting
241 String mSearchQuery;
242
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700243 public SwitchBar getSwitchBar() {
244 return mSwitchBar;
245 }
246
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 @Override
248 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800249 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 null, 0);
251 return true;
252 }
253
254 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400255 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800256 return false;
257 }
258
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 public void onConfigurationChanged(Configuration newConfig) {
261 super.onConfigurationChanged(newConfig);
Matthew Fritze34c27602017-01-18 18:24:56 -0800262 if (!mSearchFeatureProvider.isEnabled(this)) {
263 mSearchFeatureProvider.updateIndex(getApplicationContext());
264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800265 }
266
267 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700268 protected void onStart() {
269 super.onStart();
270
271 if (mNeedToRevertToInitialFragment) {
272 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 }
275
276 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700277 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700278 if (!mDisplaySearch) {
279 return false;
280 }
281
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700282 MenuInflater inflater = getMenuInflater();
Matthew Fritze9955db62016-12-20 09:37:21 -0800283 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700284 mSearchFeatureProvider.setUpSearchMenu(menu, this);
285 return true;
286 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700287 inflater.inflate(R.menu.options_menu, menu);
288
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700289
290 // Cache the search query (can be overridden by the OnQueryTextListener)
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700291 final String query = mSearchQuery;
292
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700293 mSearchMenuItem = menu.findItem(R.id.search);
294 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700295
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700296 if (mSearchMenuItem == null || mSearchView == null) {
297 return false;
298 }
299
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700300 if (mSearchResultsFragment != null) {
301 mSearchResultsFragment.setSearchView(mSearchView);
302 }
303
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700304 mSearchMenuItem.setOnActionExpandListener(this);
Fan Zhangb9239182016-08-22 13:59:59 -0700305 mSearchView.setMaxWidth(Integer.MAX_VALUE);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700306 mSearchView.setOnQueryTextListener(this);
307 mSearchView.setOnCloseListener(this);
308
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700309 if (mSearchMenuItemExpanded) {
310 mSearchMenuItem.expandActionView();
311 }
312 mSearchView.setQuery(query, true /* submit */);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700313 return true;
314 }
315
Jason Monk31c7c322016-01-20 14:41:52 -0500316 @Override
317 public SharedPreferences getSharedPreferences(String name, int mode) {
318 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500319 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500320 }
321 return super.getSharedPreferences(name, mode);
322 }
323
Jason Monke4ebcd12016-02-21 09:37:41 -0500324 private String getMetricsTag() {
325 String tag = getClass().getName();
326 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
327 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
328 }
329 if (tag.startsWith("com.android.settings.")) {
330 tag = tag.replace("com.android.settings.", "");
331 }
332 return tag;
333 }
334
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700335 private static boolean isShortCutIntent(final Intent intent) {
336 Set<String> categories = intent.getCategories();
337 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
338 }
339
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700340 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700341 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700342 if (action == null) {
343 return false;
344 }
345 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
346 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
347 }
348 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700349 }
350
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700351 @Override
352 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700353 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500354 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700355
356 final FeatureFactory factory = FeatureFactory.getFactory(this);
357
358 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800359 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800360 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700361
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700362 // Should happen before any call to getIntent()
363 getMetaData();
364
365 final Intent intent = getIntent();
366 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
367 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 }
Udam Saini92779ce2016-03-28 14:29:48 -0700369 if (intent.getBooleanExtra(EXTRA_HIDE_DRAWER, false)) {
370 setIsDrawerPresent(false);
371 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800372
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
374 Context.MODE_PRIVATE);
375
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700376 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700377 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700378
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700379 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700380 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
381
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700382 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700383 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700384
Jason Monk4da79e02015-09-10 10:54:36 -0400385 mIsShowingDashboard = className.equals(Settings.class.getName())
386 || className.equals(Settings.WirelessSettings.class.getName())
387 || className.equals(Settings.DeviceSettings.class.getName())
388 || className.equals(Settings.PersonalSettings.class.getName())
389 || className.equals(Settings.WirelessSettings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700390
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700391 // This is a "Sub Settings" when:
392 // - this is a real SubSettings
393 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700394 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700395 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
396
Fan Zhang92f1e942017-01-21 10:07:26 -0800397 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
398 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700399 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800400 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700401 }
402
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700403 setContentView(mIsShowingDashboard ?
404 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700406 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700407
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 getFragmentManager().addOnBackStackChangedListener(this);
409
Matthew Fritze34c27602017-01-18 18:24:56 -0800410 if (mIsShowingDashboard && !mSearchFeatureProvider.isEnabled(this)) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800411 // Run the Index update only if we have some space
412 if (!Utils.isLowStorage(this)) {
Fan Zhanga8759242017-01-06 09:18:59 -0800413 mSearchFeatureProvider.updateIndex(getApplicationContext());
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800414 } else {
415 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
416 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700417 }
418
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700420 // We are restarting from a previous saved state; used that to initialize, instead
421 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700422 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
423 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700424 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800425
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700426 ArrayList<DashboardCategory> categories =
427 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
428 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700429 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700430 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700431 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800432 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700433
434 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700435 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Salvador Martinez2368e542016-07-25 18:16:47 -0700436
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800437 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700438 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400439 mDisplaySearch = false;
440 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700441 if (mIsShortcut) {
442 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700443 } else if (isSubSettings) {
444 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700445 } else {
446 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700447 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700448 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700449
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700450 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700451 switchToFragment(initialFragmentName, initialArguments, true, false,
452 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000453 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700454 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700455 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700456 // Show Search affordance
457 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700458 mInitialTitleResId = R.string.dashboard_title;
Salvador Martinez2368e542016-07-25 18:16:47 -0700459
460 // add argument to indicate which settings tab should be initially selected
461 final Bundle args = new Bundle();
462 final String extraName = DashboardContainerFragment.EXTRA_SELECT_SETTINGS_TAB;
463 args.putString(extraName, intent.getStringExtra(extraName));
Fan Zhanga96a2d82016-09-27 17:51:11 -0700464 if (isDashboardFeatureEnabled()) {
465 switchToFragment(DashboardSummary.class.getName(), args, false, false,
466 mInitialTitleResId, mInitialTitle, false);
467 } else {
468 switchToFragment(DashboardContainerFragment.class.getName(), args, false, false,
469 mInitialTitleResId, mInitialTitle, false);
470 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 }
472 }
473
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700474 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700475 if (mActionBar != null) {
476 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
477 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
478 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700479 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500480 if (mSwitchBar != null) {
481 mSwitchBar.setMetricsTag(getMetricsTag());
482 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700483
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800484 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800485 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
486
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700487 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488 if (buttonBar != null) {
489 buttonBar.setVisibility(View.VISIBLE);
490
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700491 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 backButton.setOnClickListener(new OnClickListener() {
493 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700494 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 finish();
496 }
497 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700498 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 skipButton.setOnClickListener(new OnClickListener() {
500 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700501 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502 finish();
503 }
504 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700505 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800506 mNextButton.setOnClickListener(new OnClickListener() {
507 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700508 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509 finish();
510 }
511 });
512
513 // set our various button parameters
514 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
515 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
516 if (TextUtils.isEmpty(buttonText)) {
517 mNextButton.setVisibility(View.GONE);
518 }
519 else {
520 mNextButton.setText(buttonText);
521 }
522 }
523 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
524 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
525 if (TextUtils.isEmpty(buttonText)) {
526 backButton.setVisibility(View.GONE);
527 }
528 else {
529 backButton.setText(buttonText);
530 }
531 }
532 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
533 skipButton.setVisibility(View.VISIBLE);
534 }
535 }
536 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700537
Fan Zhangefba6b42017-02-07 17:40:59 -0800538 if (DEBUG_TIMING) {
539 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
540 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700541 }
542
Fan Zhangece8ff62016-06-30 13:20:12 -0700543 public void setDisplaySearchMenu(boolean displaySearch) {
544 if (displaySearch != mDisplaySearch) {
545 mDisplaySearch = displaySearch;
546 invalidateOptionsMenu();
547 }
548 }
549
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700550 private void setTitleFromIntent(Intent intent) {
551 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
552 if (initialTitleResId > 0) {
553 mInitialTitle = null;
554 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100555
556 final String initialTitleResPackageName = intent.getStringExtra(
557 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
558 if (initialTitleResPackageName != null) {
559 try {
560 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
561 0 /* flags */, new UserHandle(UserHandle.myUserId()));
562 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
563 setTitle(mInitialTitle);
564 mInitialTitleResId = -1;
565 return;
566 } catch (NameNotFoundException e) {
567 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
568 }
569 } else {
570 setTitle(mInitialTitleResId);
571 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700572 } else {
573 mInitialTitleResId = -1;
574 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
575 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
576 setTitle(mInitialTitle);
577 }
578 }
579
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800580 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700582 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800583 }
584
Fan Zhang28691572016-03-23 15:31:08 -0700585 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800586 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700587
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800588 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700589 if (mInitialTitleResId > 0) {
590 setTitle(mInitialTitleResId);
591 } else {
592 setTitle(mInitialTitle);
593 }
Fan Zhang28691572016-03-23 15:31:08 -0700594 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800595 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700596
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800597 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
598 setTitleFromBackStackEntry(bse);
599 }
600
601 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
602 final CharSequence title;
603 final int titleRes = bse.getBreadCrumbTitleRes();
604 if (titleRes > 0) {
605 title = getText(titleRes);
606 } else {
607 title = bse.getBreadCrumbTitle();
608 }
609 if (title != null) {
610 setTitle(title);
611 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800612 }
613
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800614 @Override
615 protected void onSaveInstanceState(Bundle outState) {
616 super.onSaveInstanceState(outState);
617
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700618 if (mCategories.size() > 0) {
619 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800620 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700621
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700622 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700623 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700624
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700625 if (mDisplaySearch) {
626 // The option menus are created if the ActionBar is visible and they are also created
627 // asynchronously. If you launch Settings with an Intent action like
628 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
629 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
630 // menu item and search view are null.
631 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
632 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700633
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700634 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
635 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
636 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800637 }
638
639 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400640 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800641 super.onResume();
642
643 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
644 @Override
645 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400646 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 }
648 };
649 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
650 mDevelopmentPreferencesListener);
651
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Matthew Fritze34c27602017-01-18 18:24:56 -0800653 if (!mSearchFeatureProvider.isEnabled(this)) {
654 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_ADDED));
655 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED));
656 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800657 if (mDynamicIndexableContentMonitor == null) {
658 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
659 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800660 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700661
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700662 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700663 onQueryTextSubmit(mSearchQuery);
664 }
Jason Monk4da79e02015-09-10 10:54:36 -0400665 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 }
667
668 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400669 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 super.onPause();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800671 unregisterReceiver(mBatteryInfoReceiver);
Matthew Fritze34c27602017-01-18 18:24:56 -0800672 if (!mSearchFeatureProvider.isEnabled(this)) {
673 unregisterReceiver(mUserAddRemoveReceiver);
674 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800675 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900676 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800677 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800678 }
679
680 @Override
681 public void onDestroy() {
682 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700683
684 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
685 mDevelopmentPreferencesListener);
686 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800687 }
688
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800689 protected boolean isValidFragment(String fragmentName) {
690 // Almost all fragments are wrapped in this,
691 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800692 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
693 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800694 }
695 return false;
696 }
697
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800698 @Override
699 public Intent getIntent() {
700 Intent superIntent = super.getIntent();
701 String startingFragment = getStartingFragmentClass(superIntent);
702 // This is called from super.onCreate, isMultiPane() is not yet reliable
703 // Do not use onIsHidingHeaders either, which relies itself on this method
704 if (startingFragment != null) {
705 Intent modIntent = new Intent(superIntent);
706 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
707 Bundle args = superIntent.getExtras();
708 if (args != null) {
709 args = new Bundle(args);
710 } else {
711 args = new Bundle();
712 }
713 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100714 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800715 return modIntent;
716 }
717 return superIntent;
718 }
719
720 /**
721 * Checks if the component name in the intent is different from the Settings class and
722 * returns the class name to load as a fragment.
723 */
724 private String getStartingFragmentClass(Intent intent) {
725 if (mFragmentClass != null) return mFragmentClass;
726
727 String intentClass = intent.getComponent().getClassName();
728 if (intentClass.equals(getClass().getName())) return null;
729
730 if ("com.android.settings.ManageApplications".equals(intentClass)
731 || "com.android.settings.RunningServices".equals(intentClass)
732 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
733 // Old names of manage apps.
734 intentClass = com.android.settings.applications.ManageApplications.class.getName();
735 }
736
737 return intentClass;
738 }
739
740 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000741 * Start a new fragment containing a preference panel. If the preferences
742 * are being displayed in multi-pane mode, the given fragment class will
743 * be instantiated and placed in the appropriate pane. If running in
744 * single-pane mode, a new activity will be launched in which to show the
745 * fragment.
746 *
747 * @param fragmentClass Full name of the class implementing the fragment.
748 * @param args Any desired arguments to supply to the fragment.
749 * @param titleRes Optional resource identifier of the title of this
750 * fragment.
751 * @param titleText Optional text of the title of this fragment.
752 * @param resultTo Optional fragment that result data should be sent to.
753 * If non-null, resultTo.onActivityResult() will be called when this
754 * preference panel is done. The launched panel must use
755 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
756 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700757 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000758 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800759 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
760 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700761 String title = null;
762 if (titleRes < 0) {
763 if (titleText != null) {
764 title = titleText.toString();
765 } else {
766 // There not much we can do in that case
767 title = "";
768 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700769 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700770 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800771 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000772 }
773
774 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100775 * Start a new fragment in a new activity containing a preference panel for a given user. If the
776 * preferences are being displayed in multi-pane mode, the given fragment class will be
777 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
778 * activity will be launched in which to show the fragment.
779 *
780 * @param fragmentClass Full name of the class implementing the fragment.
781 * @param args Any desired arguments to supply to the fragment.
782 * @param titleRes Optional resource identifier of the title of this fragment.
783 * @param titleText Optional text of the title of this fragment.
784 * @param userHandle The user for which the panel has to be started.
785 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800786 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
787 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700788 // This is a workaround.
789 //
790 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
791 // starting the fragment could cause a native stack corruption. See b/17523189. However,
792 // adding that flag and start the preference panel with the same UserHandler will make it
793 // impossible to use back button to return to the previous screen. See b/20042570.
794 //
795 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
796 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
797 // when we're calling it as the same user.
798 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800799 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700800 } else {
801 String title = null;
802 if (titleRes < 0) {
803 if (titleText != null) {
804 title = titleText.toString();
805 } else {
806 // There not much we can do in that case
807 title = "";
808 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100809 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800810 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
811 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100812 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100813 }
814
815 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800816 * Called by a preference panel fragment to finish itself.
817 *
818 * @param caller The fragment that is asking to be finished.
819 * @param resultCode Optional result code to send back to the original
820 * launching fragment.
821 * @param resultData Optional result data to send back to the original
822 * launching fragment.
823 */
824 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
825 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700826 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800827 }
828
829 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000830 * Start a new fragment.
831 *
832 * @param fragment The fragment to start
833 * @param push If true, the current fragment will be pushed onto the back stack. If false,
834 * the current fragment will be replaced.
835 */
836 public void startPreferenceFragment(Fragment fragment, boolean push) {
837 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700838 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000839 if (push) {
840 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
841 transaction.addToBackStack(BACK_STACK_PREFS);
842 } else {
843 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
844 }
845 transaction.commitAllowingStateLoss();
846 }
847
848 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700849 * Switch to a specific Fragment with taking care of validation, Title and BackStack
850 */
851 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700852 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700853 if (validate && !isValidFragment(fragmentName)) {
854 throw new IllegalArgumentException("Invalid fragment for this activity: "
855 + fragmentName);
856 }
857 Fragment f = Fragment.instantiate(this, fragmentName, args);
858 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700859 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700860 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700861 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700862 }
863 if (addToBackStack) {
864 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
865 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700866 if (titleResId > 0) {
867 transaction.setBreadCrumbTitle(titleResId);
868 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700869 transaction.setBreadCrumbTitle(title);
870 }
871 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700872 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700873 return f;
874 }
875
Jason Monk4da79e02015-09-10 10:54:36 -0400876 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500877 // Generally the items that are will be changing from these updates will
878 // not be in the top list of tiles, so run it in the background and the
879 // SettingsDrawerActivity will pick up on the updates automatically.
880 AsyncTask.execute(new Runnable() {
881 @Override
882 public void run() {
883 doUpdateTilesList();
884 }
885 });
886 }
887
888 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400889 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700890 final UserManager um = UserManager.get(this);
891 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700892
Jason Monk4da79e02015-09-10 10:54:36 -0400893 String packageName = getPackageName();
894 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800895 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700896
Jason Monk4da79e02015-09-10 10:54:36 -0400897 setTileEnabled(new ComponentName(packageName,
898 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800899 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700900
Jason Monk4da79e02015-09-10 10:54:36 -0400901 setTileEnabled(new ComponentName(packageName,
902 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800903 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700904
Jason Monk4da79e02015-09-10 10:54:36 -0400905 setTileEnabled(new ComponentName(packageName,
906 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800907 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700908
Jason Monk4da79e02015-09-10 10:54:36 -0400909 setTileEnabled(new ComponentName(packageName,
910 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800911 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400912
913 setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400914 Settings.UserSettingsActivity.class.getName()),
915 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhang224caad2017-01-06 11:42:48 -0800916 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400917
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700918 setTileEnabled(new ComponentName(packageName,
919 Settings.WirelessSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800920 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700921
922 setTileEnabled(new ComponentName(packageName,
923 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800924 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400925 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
926 setTileEnabled(new ComponentName(packageName,
927 Settings.PaymentSettingsActivity.class.getName()),
928 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
929 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800930 && adapter != null && adapter.isEnabled(), isAdmin);
Fan Zhangffd328b2017-01-09 10:16:49 -0800931 setTileEnabled(new ComponentName(packageName,
932 "com.android.settings.PaymentSettingsDashboardAlias"),
933 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
934 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
935 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400936
937 setTileEnabled(new ComponentName(packageName,
938 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800939 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400940
941 final boolean showDev = mDevelopmentPreferences.getBoolean(
Dan Sandler12c4ba42016-03-28 11:13:40 -0400942 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
943 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400944 setTileEnabled(new ComponentName(packageName,
945 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800946 showDev, isAdmin);
Fan Zhangcc335d92016-09-29 14:37:14 -0700947 setTileEnabled(new ComponentName(packageName,
948 Settings.DevelopmentSettingsActivity.DASHBOARD_ALIAS),
Fan Zhang224caad2017-01-06 11:42:48 -0800949 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400950
Jason Monk4da79e02015-09-10 10:54:36 -0400951 if (UserHandle.MU_ENABLED && !isAdmin) {
952 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhanga96a2d82016-09-27 17:51:11 -0700953 List<DashboardCategory> categories;
954 if (isDashboardFeatureEnabled()) {
955 categories = mDashboardFeatureProvider.getAllCategories();
956 } else {
957 categories = getDashboardCategories();
958 }
959
Jason Monk4da79e02015-09-10 10:54:36 -0400960 for (DashboardCategory category : categories) {
Jason Monkd4f03ec2016-01-07 16:25:34 -0500961 for (Tile tile : category.tiles) {
Jason Monk4da79e02015-09-10 10:54:36 -0400962 ComponentName component = tile.intent.getComponent();
Fan Zhang9ec45362017-01-10 11:55:45 -0800963 final String name = component.getClassName();
964 final boolean isEnabledForRestricted = ArrayUtils.contains(
965 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
966 if (packageName.equals(component.getPackageName()) && !isEnabledForRestricted) {
Fan Zhang224caad2017-01-06 11:42:48 -0800967 setTileEnabled(component, false, isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700968 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700969 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700970 }
971 }
Doris Ling26bf0032016-06-15 18:16:09 -0700972
Anton Philippovadfec552017-01-25 20:37:36 +0000973 // Enable/disable backup settings depending on whether the user is admin.
Doris Ling26bf0032016-06-15 18:16:09 -0700974 setTileEnabled(new ComponentName(packageName,
Christine Franks14782222017-01-23 16:44:02 -0800975 BackupSettingsActivity.class.getName()), true,
976 isAdmin || Utils.isCarrierDemoUser(this));
Fan Zhang0e8d0d12016-11-02 09:15:33 -0700977 setTileEnabled(new ComponentName(packageName,
Anton Philippovadfec552017-01-25 20:37:36 +0000978 "com.android.settings.BackupResetDashboardAlias"), true, isAdmin);
Doris Ling26bf0032016-06-15 18:16:09 -0700979
Bartosz Fabianowski05061fc2016-11-14 12:04:49 +0100980 setTileEnabled(new ComponentName(packageName,
981 Settings.EnterprisePrivacySettingsActivity.class.getName()),
982 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800983 .hasDeviceOwner(), isAdmin);
Fan Zhang10dd4652016-11-16 10:24:26 -0800984 setTileEnabled(new ComponentName(packageName,
985 "com.android.settings.EnterprisePrivacyDashboardAlias"),
986 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800987 .hasDeviceOwner(), isAdmin);
988 // Final step, refresh categories.
989 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500990 }
991
Fan Zhang224caad2017-01-06 11:42:48 -0800992 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400993 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800994 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
995 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400996 enabled = false;
997 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400998 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500999 }
1000
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001001 private void getMetaData() {
1002 try {
1003 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1004 PackageManager.GET_META_DATA);
1005 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001006 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1007 } catch (NameNotFoundException nnfe) {
1008 // No recovery
1009 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1010 }
1011 }
1012
Fan Zhanga96a2d82016-09-27 17:51:11 -07001013 @Override
1014 protected boolean isDashboardFeatureEnabled() {
1015 if (mDashboardFeatureProvider == null) {
1016 mDashboardFeatureProvider =
1017 FeatureFactory.getFactory(this).getDashboardFeatureProvider(this);
1018 }
1019 return mDashboardFeatureProvider.isEnabled();
1020 }
1021
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001022 // give subclasses access to the Next button
1023 public boolean hasNextButton() {
1024 return mNextButton != null;
1025 }
1026
1027 public Button getNextButton() {
1028 return mNextButton;
1029 }
1030
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001031 @Override
1032 public boolean shouldUpRecreateTask(Intent targetIntent) {
1033 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1034 }
1035
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001036 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001037 @Override
1038 public boolean onQueryTextSubmit(String query) {
Matthew Fritze9955db62016-12-20 09:37:21 -08001039 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001040 return false;
1041 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001042 mSearchQuery = query;
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001043 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001044 return mSearchResultsFragment.onQueryTextSubmit(query);
1045 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001046
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001047 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001048 @Override
1049 public boolean onQueryTextChange(String newText) {
Fan Zhanga3e8f5c2016-12-01 09:52:10 -08001050 mSearchQuery = newText;
Matthew Fritze9955db62016-12-20 09:37:21 -08001051 if (mSearchFeatureProvider.isEnabled(this) || mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001052 return false;
1053 }
1054 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001055 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001056
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001057 @Override
1058 public boolean onClose() {
1059 return false;
1060 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001061
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001062 @Override
1063 public boolean onMenuItemActionExpand(MenuItem item) {
1064 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001065 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001066 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001067 return true;
1068 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001069
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001070 @Override
1071 public boolean onMenuItemActionCollapse(MenuItem item) {
1072 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001073 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001074 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001075 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001076 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001077 return true;
1078 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001079
Jason Monk4da79e02015-09-10 10:54:36 -04001080 @Override
Jason Monkd4f03ec2016-01-07 16:25:34 -05001081 protected void onTileClicked(Tile tile) {
Jason Monk4da79e02015-09-10 10:54:36 -04001082 if (mIsShowingDashboard) {
1083 // If on dashboard, don't finish so the back comes back to here.
1084 openTile(tile);
1085 } else {
1086 super.onTileClicked(tile);
1087 }
1088 }
1089
Jason Monkaf001092015-11-04 13:16:18 -05001090 @Override
1091 public void onProfileTileOpen() {
1092 if (!mIsShowingDashboard) {
1093 finish();
1094 }
1095 }
1096
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001097 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001098 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001099 if (mSearchResultsFragment != null) {
1100 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001101 }
Anna Galusza6b1e9db2016-03-30 17:25:36 -07001102 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001103 if (current != null && current instanceof SearchResultsSummary) {
1104 mSearchResultsFragment = (SearchResultsSummary) current;
1105 } else {
Fan Zhang28691572016-03-23 15:31:08 -07001106 setContentHeaderView(null);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001107 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001108 SearchResultsSummary.class.getName(), null, false, true,
1109 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001110 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001111 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001112 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001113 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001114
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001115 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001116 public void needToRevertToInitialFragment() {
1117 mNeedToRevertToInitialFragment = true;
1118 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001119
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001120 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001121 private void revertToInitialFragment() {
1122 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001123 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001124 mSearchMenuItemExpanded = false;
1125 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1126 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001127 if (mSearchMenuItem != null) {
1128 mSearchMenuItem.collapseActionView();
1129 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001130 }
Jim Miller0698a212014-10-16 19:49:07 -07001131
1132 public Intent getResultIntentData() {
1133 return mResultIntentData;
1134 }
1135
1136 public void setResultIntentData(Intent resultIntentData) {
1137 mResultIntentData = resultIntentData;
1138 }
Jason Monkd4f03ec2016-01-07 16:25:34 -05001139
1140 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -07001141 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -07001142 return;
1143 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -07001144 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -05001145 startActivityForResult(intent, REQUEST_SUGGESTION);
1146 }
1147
1148 @Override
1149 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1150 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
1151 && resultCode != RESULT_CANCELED) {
1152 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
1153 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
1154 }
1155 super.onActivityResult(requestCode, resultCode, data);
1156 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001157}