blob: 0751b814c171f49f65f6f902cae0451cb78abe77 [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;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080057import com.android.settings.core.gateway.SettingsGateway;
Fan Zhang5f79ae92016-08-18 16:04:19 -070058import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhang28691572016-03-23 15:31:08 -070059import com.android.settings.dashboard.DashboardContainerFragment;
Fan Zhanga96a2d82016-09-27 17:51:11 -070060import com.android.settings.dashboard.DashboardFeatureProvider;
61import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070062import com.android.settings.dashboard.SearchResultsSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Sunny Goyalbf9f2d22016-11-02 11:20:17 -070064import com.android.settings.qstile.DevelopmentModeTile;
Jason Monk2ebc8a02015-02-13 15:23:19 -050065import com.android.settings.search.DynamicIndexableContentMonitor;
Matthew Fritze0ed37c32016-10-24 10:12:49 -070066import com.android.settings.search2.SearchFeatureProvider;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070067import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040068import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk7ea14c52016-01-22 14:28:02 -050070import com.android.settingslib.drawer.Tile;
Jason Monk2ebc8a02015-02-13 15:23:19 -050071
Doris Ling26bf0032016-06-15 18:16:09 -070072import java.net.URISyntaxException;
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 /**
105 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
106 * this extra can also be specified to supply a Bundle of arguments to pass
107 * to that fragment when it is instantiated during the initial creation
108 * of the activity.
109 */
110 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
111
112 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700113 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
114 */
115 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
116
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800117 public static final String BACK_STACK_PREFS = ":settings:prefs";
118
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119 // extras that allow any preference activity to be launched as part of a wizard
120
121 // show Back and Next buttons? takes boolean parameter
122 // Back will then return RESULT_CANCELED and Next RESULT_OK
123 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
124
125 // add a Skip button?
126 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
127
128 // specify custom text for the Back or Next buttons, or cause a button to not appear
129 // at all by setting it to null
130 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
131 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
132
133 /**
134 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700135 * 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 -0800136 * that fragment.
137 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100139 /**
140 * The package name used to resolve the title resource id.
141 */
142 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
143 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700144 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
145 ":settings:show_fragment_title_resid";
146 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
147 ":settings:show_fragment_as_shortcut";
148
149 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
150 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151
Udam Saini92779ce2016-03-28 14:29:48 -0700152 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
153
Jason Monk30695812015-11-17 09:01:08 -0500154 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 "com.android.settings.FRAGMENT_CLASS";
156
157 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
158
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700159 private static final String EMPTY_QUERY = "";
160
Jason Monkd4f03ec2016-01-07 16:25:34 -0500161 private static final int REQUEST_SUGGESTION = 42;
162
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800165 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700166 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800167
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700168 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
169 "android.settings.APPLICATION_DETAILS_SETTINGS"
170 };
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 private SharedPreferences mDevelopmentPreferences;
173 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
174
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 private boolean mBatteryPresent = true;
176 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 @Override
178 public void onReceive(Context context, Intent intent) {
179 String action = intent.getAction();
180 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
181 boolean batteryPresent = Utils.isBatteryPresent(intent);
182
183 if (mBatteryPresent != batteryPresent) {
184 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400185 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186 }
187 }
188 }
189 };
190
Clara Bayarri6934a042015-10-14 11:07:35 +0100191 private final BroadcastReceiver mUserAddRemoveReceiver = new BroadcastReceiver() {
192 @Override
193 public void onReceive(Context context, Intent intent) {
194 String action = intent.getAction();
195 if (action.equals(Intent.ACTION_USER_ADDED)
196 || action.equals(Intent.ACTION_USER_REMOVED)) {
Matthew Fritze18fcb082016-12-08 09:54:13 -0800197 mSearchFeatureProvider.updateIndex(getApplicationContext());
Clara Bayarri6934a042015-10-14 11:07:35 +0100198 }
199 }
200 };
201
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800202 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700203
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700204 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700205 private SwitchBar mSwitchBar;
206
207 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700208
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700209 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700210 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700211
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700212 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700213 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700214
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700215 private ViewGroup mContent;
216
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700217 private SearchView mSearchView;
218 private MenuItem mSearchMenuItem;
219 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700220 private SearchResultsSummary mSearchResultsFragment;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700221 private SearchFeatureProvider mSearchFeatureProvider;
222
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700223 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800224 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800225
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700226 private boolean mNeedToRevertToInitialFragment = false;
227
Fan Zhanga96a2d82016-09-27 17:51:11 -0700228 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700229 private Intent mResultIntentData;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500230 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700231
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800232 @VisibleForTesting
233 String mSearchQuery;
234
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700235 public SwitchBar getSwitchBar() {
236 return mSwitchBar;
237 }
238
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800239 @Override
240 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Sunny Goyal21ef89f2016-05-25 12:41:16 -0700241 startPreferencePanel(pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242 null, 0);
243 return true;
244 }
245
246 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400247 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 return false;
249 }
250
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 public void onConfigurationChanged(Configuration newConfig) {
253 super.onConfigurationChanged(newConfig);
Matthew Fritze18fcb082016-12-08 09:54:13 -0800254 mSearchFeatureProvider.updateIndex(getApplicationContext());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800255 }
256
257 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700258 protected void onStart() {
259 super.onStart();
260
261 if (mNeedToRevertToInitialFragment) {
262 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264 }
265
266 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700267 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700268 if (!mDisplaySearch) {
269 return false;
270 }
271
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700272 MenuInflater inflater = getMenuInflater();
Matthew Fritze9955db62016-12-20 09:37:21 -0800273 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700274 mSearchFeatureProvider.setUpSearchMenu(menu, this);
275 return true;
276 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700277 inflater.inflate(R.menu.options_menu, menu);
278
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700279
280 // Cache the search query (can be overridden by the OnQueryTextListener)
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700281 final String query = mSearchQuery;
282
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700283 mSearchMenuItem = menu.findItem(R.id.search);
284 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700285
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700286 if (mSearchMenuItem == null || mSearchView == null) {
287 return false;
288 }
289
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700290 if (mSearchResultsFragment != null) {
291 mSearchResultsFragment.setSearchView(mSearchView);
292 }
293
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700294 mSearchMenuItem.setOnActionExpandListener(this);
Fan Zhangb9239182016-08-22 13:59:59 -0700295 mSearchView.setMaxWidth(Integer.MAX_VALUE);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700296 mSearchView.setOnQueryTextListener(this);
297 mSearchView.setOnCloseListener(this);
298
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700299 if (mSearchMenuItemExpanded) {
300 mSearchMenuItem.expandActionView();
301 }
302 mSearchView.setQuery(query, true /* submit */);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700303 return true;
304 }
305
Jason Monk31c7c322016-01-20 14:41:52 -0500306 @Override
307 public SharedPreferences getSharedPreferences(String name, int mode) {
308 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500309 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500310 }
311 return super.getSharedPreferences(name, mode);
312 }
313
Jason Monke4ebcd12016-02-21 09:37:41 -0500314 private String getMetricsTag() {
315 String tag = getClass().getName();
316 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
317 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
318 }
319 if (tag.startsWith("com.android.settings.")) {
320 tag = tag.replace("com.android.settings.", "");
321 }
322 return tag;
323 }
324
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700325 private static boolean isShortCutIntent(final Intent intent) {
326 Set<String> categories = intent.getCategories();
327 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
328 }
329
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700330 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700331 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700332 if (action == null) {
333 return false;
334 }
335 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
336 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
337 }
338 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700339 }
340
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700341 @Override
342 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700343 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500344 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700345
346 final FeatureFactory factory = FeatureFactory.getFactory(this);
347
348 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800349 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700350
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700351 // Should happen before any call to getIntent()
352 getMetaData();
353
354 final Intent intent = getIntent();
355 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
356 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 }
Udam Saini92779ce2016-03-28 14:29:48 -0700358 if (intent.getBooleanExtra(EXTRA_HIDE_DRAWER, false)) {
359 setIsDrawerPresent(false);
360 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
363 Context.MODE_PRIVATE);
364
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700365 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700366 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700367
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700368 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700369 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
370
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700371 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700372 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700373
Jason Monk4da79e02015-09-10 10:54:36 -0400374 mIsShowingDashboard = className.equals(Settings.class.getName())
375 || className.equals(Settings.WirelessSettings.class.getName())
376 || className.equals(Settings.DeviceSettings.class.getName())
377 || className.equals(Settings.PersonalSettings.class.getName())
378 || className.equals(Settings.WirelessSettings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700379
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700380 // This is a "Sub Settings" when:
381 // - this is a real SubSettings
382 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700383 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700384 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
385
386 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
387 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700388 // Check also that we are not a Theme Dialog as we don't want to override them
389 final int themeResId = getThemeResId();
390 if (themeResId != R.style.Theme_DialogWhenLarge &&
391 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
392 setTheme(R.style.Theme_SubSettings);
393 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700394 }
395
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700396 setContentView(mIsShowingDashboard ?
397 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800398
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700399 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700400
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 getFragmentManager().addOnBackStackChangedListener(this);
402
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700403 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800404 // Run the Index update only if we have some space
405 if (!Utils.isLowStorage(this)) {
Fan Zhanga8759242017-01-06 09:18:59 -0800406 mSearchFeatureProvider.updateIndex(getApplicationContext());
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800407 } else {
408 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
409 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700410 }
411
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700412 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700413 // We are restarting from a previous saved state; used that to initialize, instead
414 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700415 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
416 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700417 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800418
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700419 ArrayList<DashboardCategory> categories =
420 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
421 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700422 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700423 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700424 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800425 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700426
427 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700428 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Salvador Martinez2368e542016-07-25 18:16:47 -0700429
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800430 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700431 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400432 mDisplaySearch = false;
433 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700434 if (mIsShortcut) {
435 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700436 } else if (isSubSettings) {
437 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700438 } else {
439 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700440 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700441 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700442
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700443 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700444 switchToFragment(initialFragmentName, initialArguments, true, false,
445 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000446 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700447 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700448 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700449 // Show Search affordance
450 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700451 mInitialTitleResId = R.string.dashboard_title;
Salvador Martinez2368e542016-07-25 18:16:47 -0700452
453 // add argument to indicate which settings tab should be initially selected
454 final Bundle args = new Bundle();
455 final String extraName = DashboardContainerFragment.EXTRA_SELECT_SETTINGS_TAB;
456 args.putString(extraName, intent.getStringExtra(extraName));
Fan Zhanga96a2d82016-09-27 17:51:11 -0700457 if (isDashboardFeatureEnabled()) {
458 switchToFragment(DashboardSummary.class.getName(), args, false, false,
459 mInitialTitleResId, mInitialTitle, false);
460 } else {
461 switchToFragment(DashboardContainerFragment.class.getName(), args, false, false,
462 mInitialTitleResId, mInitialTitle, false);
463 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 }
465 }
466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700467 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700468 if (mActionBar != null) {
469 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
470 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
471 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700472 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500473 if (mSwitchBar != null) {
474 mSwitchBar.setMetricsTag(getMetricsTag());
475 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700476
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
479
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700480 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 if (buttonBar != null) {
482 buttonBar.setVisibility(View.VISIBLE);
483
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700484 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800485 backButton.setOnClickListener(new OnClickListener() {
486 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700487 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488 finish();
489 }
490 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700491 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 skipButton.setOnClickListener(new OnClickListener() {
493 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700494 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 finish();
496 }
497 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700498 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 mNextButton.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 });
505
506 // set our various button parameters
507 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
508 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
509 if (TextUtils.isEmpty(buttonText)) {
510 mNextButton.setVisibility(View.GONE);
511 }
512 else {
513 mNextButton.setText(buttonText);
514 }
515 }
516 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
517 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
518 if (TextUtils.isEmpty(buttonText)) {
519 backButton.setVisibility(View.GONE);
520 }
521 else {
522 backButton.setText(buttonText);
523 }
524 }
525 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
526 skipButton.setVisibility(View.VISIBLE);
527 }
528 }
529 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700530
Jason Monkfd2c7222015-12-02 15:38:38 -0500531 if (DEBUG_TIMING) Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
532 + " ms");
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700533 }
534
Fan Zhangece8ff62016-06-30 13:20:12 -0700535 public void setDisplaySearchMenu(boolean displaySearch) {
536 if (displaySearch != mDisplaySearch) {
537 mDisplaySearch = displaySearch;
538 invalidateOptionsMenu();
539 }
540 }
541
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700542 private void setTitleFromIntent(Intent intent) {
543 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
544 if (initialTitleResId > 0) {
545 mInitialTitle = null;
546 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100547
548 final String initialTitleResPackageName = intent.getStringExtra(
549 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
550 if (initialTitleResPackageName != null) {
551 try {
552 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
553 0 /* flags */, new UserHandle(UserHandle.myUserId()));
554 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
555 setTitle(mInitialTitle);
556 mInitialTitleResId = -1;
557 return;
558 } catch (NameNotFoundException e) {
559 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
560 }
561 } else {
562 setTitle(mInitialTitleResId);
563 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700564 } else {
565 mInitialTitleResId = -1;
566 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
567 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
568 setTitle(mInitialTitle);
569 }
570 }
571
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800572 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700574 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 }
576
Fan Zhang28691572016-03-23 15:31:08 -0700577 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800578 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700579
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800580 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700581 if (mInitialTitleResId > 0) {
582 setTitle(mInitialTitleResId);
583 } else {
584 setTitle(mInitialTitle);
585 }
Fan Zhang28691572016-03-23 15:31:08 -0700586 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800587 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700588
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800589 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
590 setTitleFromBackStackEntry(bse);
591 }
592
593 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
594 final CharSequence title;
595 final int titleRes = bse.getBreadCrumbTitleRes();
596 if (titleRes > 0) {
597 title = getText(titleRes);
598 } else {
599 title = bse.getBreadCrumbTitle();
600 }
601 if (title != null) {
602 setTitle(title);
603 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800604 }
605
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800606 @Override
607 protected void onSaveInstanceState(Bundle outState) {
608 super.onSaveInstanceState(outState);
609
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700610 if (mCategories.size() > 0) {
611 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700613
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700614 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700615 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700616
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700617 if (mDisplaySearch) {
618 // The option menus are created if the ActionBar is visible and they are also created
619 // asynchronously. If you launch Settings with an Intent action like
620 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
621 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
622 // menu item and search view are null.
623 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
624 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700625
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700626 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
627 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
628 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800629 }
630
631 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400632 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800633 super.onResume();
634
635 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
636 @Override
637 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400638 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 }
640 };
641 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
642 mDevelopmentPreferencesListener);
643
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Clara Bayarri6934a042015-10-14 11:07:35 +0100645 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_ADDED));
646 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800647 if (mDynamicIndexableContentMonitor == null) {
648 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
649 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800650 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700651
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700652 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700653 onQueryTextSubmit(mSearchQuery);
654 }
Jason Monk4da79e02015-09-10 10:54:36 -0400655 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 }
657
658 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400659 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 super.onPause();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 unregisterReceiver(mBatteryInfoReceiver);
Clara Bayarri8c9521f2016-01-12 14:10:45 +0000662 unregisterReceiver(mUserAddRemoveReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800663 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900664 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800665 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 }
667
668 @Override
669 public void onDestroy() {
670 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700671
672 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
673 mDevelopmentPreferencesListener);
674 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800675 }
676
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800677 protected boolean isValidFragment(String fragmentName) {
678 // Almost all fragments are wrapped in this,
679 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800680 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
681 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800682 }
683 return false;
684 }
685
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800686 @Override
687 public Intent getIntent() {
688 Intent superIntent = super.getIntent();
689 String startingFragment = getStartingFragmentClass(superIntent);
690 // This is called from super.onCreate, isMultiPane() is not yet reliable
691 // Do not use onIsHidingHeaders either, which relies itself on this method
692 if (startingFragment != null) {
693 Intent modIntent = new Intent(superIntent);
694 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
695 Bundle args = superIntent.getExtras();
696 if (args != null) {
697 args = new Bundle(args);
698 } else {
699 args = new Bundle();
700 }
701 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100702 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800703 return modIntent;
704 }
705 return superIntent;
706 }
707
708 /**
709 * Checks if the component name in the intent is different from the Settings class and
710 * returns the class name to load as a fragment.
711 */
712 private String getStartingFragmentClass(Intent intent) {
713 if (mFragmentClass != null) return mFragmentClass;
714
715 String intentClass = intent.getComponent().getClassName();
716 if (intentClass.equals(getClass().getName())) return null;
717
718 if ("com.android.settings.ManageApplications".equals(intentClass)
719 || "com.android.settings.RunningServices".equals(intentClass)
720 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
721 // Old names of manage apps.
722 intentClass = com.android.settings.applications.ManageApplications.class.getName();
723 }
724
725 return intentClass;
726 }
727
728 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000729 * Start a new fragment containing a preference panel. If the preferences
730 * are being displayed in multi-pane mode, the given fragment class will
731 * be instantiated and placed in the appropriate pane. If running in
732 * single-pane mode, a new activity will be launched in which to show the
733 * fragment.
734 *
735 * @param fragmentClass Full name of the class implementing the fragment.
736 * @param args Any desired arguments to supply to the fragment.
737 * @param titleRes Optional resource identifier of the title of this
738 * fragment.
739 * @param titleText Optional text of the title of this fragment.
740 * @param resultTo Optional fragment that result data should be sent to.
741 * If non-null, resultTo.onActivityResult() will be called when this
742 * preference panel is done. The launched panel must use
743 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
744 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700745 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000746 */
747 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700748 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700749 String title = null;
750 if (titleRes < 0) {
751 if (titleText != null) {
752 title = titleText.toString();
753 } else {
754 // There not much we can do in that case
755 title = "";
756 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700757 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700758 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700759 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000760 }
761
762 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100763 * Start a new fragment in a new activity containing a preference panel for a given user. If the
764 * preferences are being displayed in multi-pane mode, the given fragment class will be
765 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
766 * activity will be launched in which to show the fragment.
767 *
768 * @param fragmentClass Full name of the class implementing the fragment.
769 * @param args Any desired arguments to supply to the fragment.
770 * @param titleRes Optional resource identifier of the title of this fragment.
771 * @param titleText Optional text of the title of this fragment.
772 * @param userHandle The user for which the panel has to be started.
773 */
774 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
775 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700776 // This is a workaround.
777 //
778 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
779 // starting the fragment could cause a native stack corruption. See b/17523189. However,
780 // adding that flag and start the preference panel with the same UserHandler will make it
781 // impossible to use back button to return to the previous screen. See b/20042570.
782 //
783 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
784 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
785 // when we're calling it as the same user.
786 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
787 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
788 } else {
789 String title = null;
790 if (titleRes < 0) {
791 if (titleText != null) {
792 title = titleText.toString();
793 } else {
794 // There not much we can do in that case
795 title = "";
796 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100797 }
Lifu Tangd0332852015-04-02 12:05:46 -0700798 Utils.startWithFragmentAsUser(this, fragmentClass, args,
799 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100800 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100801 }
802
803 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800804 * Called by a preference panel fragment to finish itself.
805 *
806 * @param caller The fragment that is asking to be finished.
807 * @param resultCode Optional result code to send back to the original
808 * launching fragment.
809 * @param resultData Optional result data to send back to the original
810 * launching fragment.
811 */
812 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
813 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700814 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800815 }
816
817 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000818 * Start a new fragment.
819 *
820 * @param fragment The fragment to start
821 * @param push If true, the current fragment will be pushed onto the back stack. If false,
822 * the current fragment will be replaced.
823 */
824 public void startPreferenceFragment(Fragment fragment, boolean push) {
825 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700826 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000827 if (push) {
828 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
829 transaction.addToBackStack(BACK_STACK_PREFS);
830 } else {
831 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
832 }
833 transaction.commitAllowingStateLoss();
834 }
835
836 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700837 * Switch to a specific Fragment with taking care of validation, Title and BackStack
838 */
839 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700840 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700841 if (validate && !isValidFragment(fragmentName)) {
842 throw new IllegalArgumentException("Invalid fragment for this activity: "
843 + fragmentName);
844 }
845 Fragment f = Fragment.instantiate(this, fragmentName, args);
846 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700847 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700848 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700849 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700850 }
851 if (addToBackStack) {
852 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
853 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700854 if (titleResId > 0) {
855 transaction.setBreadCrumbTitle(titleResId);
856 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700857 transaction.setBreadCrumbTitle(title);
858 }
859 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700860 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700861 return f;
862 }
863
Jason Monk4da79e02015-09-10 10:54:36 -0400864 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500865 // Generally the items that are will be changing from these updates will
866 // not be in the top list of tiles, so run it in the background and the
867 // SettingsDrawerActivity will pick up on the updates automatically.
868 AsyncTask.execute(new Runnable() {
869 @Override
870 public void run() {
871 doUpdateTilesList();
872 }
873 });
874 }
875
876 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400877 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700878 final UserManager um = UserManager.get(this);
879 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700880
Jason Monk4da79e02015-09-10 10:54:36 -0400881 String packageName = getPackageName();
882 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800883 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700884
Jason Monk4da79e02015-09-10 10:54:36 -0400885 setTileEnabled(new ComponentName(packageName,
886 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800887 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700888
Jason Monk4da79e02015-09-10 10:54:36 -0400889 setTileEnabled(new ComponentName(packageName,
890 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800891 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700892
Jason Monk4da79e02015-09-10 10:54:36 -0400893 setTileEnabled(new ComponentName(packageName,
894 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800895 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700896
Jason Monk4da79e02015-09-10 10:54:36 -0400897 setTileEnabled(new ComponentName(packageName,
898 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800899 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400900
901 setTileEnabled(new ComponentName(packageName,
Jason Monk4da79e02015-09-10 10:54:36 -0400902 Settings.UserSettingsActivity.class.getName()),
903 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhang224caad2017-01-06 11:42:48 -0800904 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400905
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700906 setTileEnabled(new ComponentName(packageName,
907 Settings.WirelessSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800908 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700909
910 setTileEnabled(new ComponentName(packageName,
911 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800912 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400913 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
914 setTileEnabled(new ComponentName(packageName,
915 Settings.PaymentSettingsActivity.class.getName()),
916 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
917 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800918 && adapter != null && adapter.isEnabled(), isAdmin);
Fan Zhangffd328b2017-01-09 10:16:49 -0800919 setTileEnabled(new ComponentName(packageName,
920 "com.android.settings.PaymentSettingsDashboardAlias"),
921 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
922 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
923 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400924
925 setTileEnabled(new ComponentName(packageName,
926 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800927 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400928
929 final boolean showDev = mDevelopmentPreferences.getBoolean(
Dan Sandler12c4ba42016-03-28 11:13:40 -0400930 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
931 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400932 setTileEnabled(new ComponentName(packageName,
933 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800934 showDev, isAdmin);
Fan Zhangcc335d92016-09-29 14:37:14 -0700935 setTileEnabled(new ComponentName(packageName,
936 Settings.DevelopmentSettingsActivity.DASHBOARD_ALIAS),
Fan Zhang224caad2017-01-06 11:42:48 -0800937 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400938
939 // Reveal development-only quick settings tiles
Sunny Goyalbf9f2d22016-11-02 11:20:17 -0700940 setTileEnabled(new ComponentName(this, DevelopmentModeTile.class), showDev);
Jason Monk4da79e02015-09-10 10:54:36 -0400941
942 if (UserHandle.MU_ENABLED && !isAdmin) {
943 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhanga96a2d82016-09-27 17:51:11 -0700944 List<DashboardCategory> categories;
945 if (isDashboardFeatureEnabled()) {
946 categories = mDashboardFeatureProvider.getAllCategories();
947 } else {
948 categories = getDashboardCategories();
949 }
950
Jason Monk4da79e02015-09-10 10:54:36 -0400951 for (DashboardCategory category : categories) {
Jason Monkd4f03ec2016-01-07 16:25:34 -0500952 for (Tile tile : category.tiles) {
Jason Monk4da79e02015-09-10 10:54:36 -0400953 ComponentName component = tile.intent.getComponent();
Fan Zhang9ec45362017-01-10 11:55:45 -0800954 final String name = component.getClassName();
955 final boolean isEnabledForRestricted = ArrayUtils.contains(
956 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
957 if (packageName.equals(component.getPackageName()) && !isEnabledForRestricted) {
Fan Zhang224caad2017-01-06 11:42:48 -0800958 setTileEnabled(component, false, isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700959 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700960 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700961 }
962 }
Doris Ling26bf0032016-06-15 18:16:09 -0700963
964 String backupIntent = getResources().getString(R.string.config_backup_settings_intent);
965 boolean useDefaultBackup = TextUtils.isEmpty(backupIntent);
966 setTileEnabled(new ComponentName(packageName,
Fan Zhang224caad2017-01-06 11:42:48 -0800967 Settings.PrivacySettingsActivity.class.getName()), useDefaultBackup, isAdmin);
Fan Zhang0e8d0d12016-11-02 09:15:33 -0700968 setTileEnabled(new ComponentName(packageName,
969 "com.android.settings.PrivacyDashboardAlias"),
Fan Zhang224caad2017-01-06 11:42:48 -0800970 useDefaultBackup, isAdmin);
Fan Zhang0e8d0d12016-11-02 09:15:33 -0700971
Doris Ling26bf0032016-06-15 18:16:09 -0700972 boolean hasBackupActivity = false;
973 if (!useDefaultBackup) {
974 try {
975 Intent intent = Intent.parseUri(backupIntent, 0);
976 hasBackupActivity = !getPackageManager().queryIntentActivities(intent, 0).isEmpty();
977 } catch (URISyntaxException e) {
978 Log.e(LOG_TAG, "Invalid backup intent URI!", e);
979 }
980 }
Fan Zhang10dd4652016-11-16 10:24:26 -0800981
Fan Zhang224caad2017-01-06 11:42:48 -0800982 // Enable/disable BackupSettingsActivity and its alias.
Doris Ling26bf0032016-06-15 18:16:09 -0700983 setTileEnabled(new ComponentName(packageName,
Fan Zhang224caad2017-01-06 11:42:48 -0800984 BackupSettingsActivity.class.getName()), hasBackupActivity, isAdmin);
Fan Zhang10dd4652016-11-16 10:24:26 -0800985 setTileEnabled(new ComponentName(packageName,
Fan Zhang224caad2017-01-06 11:42:48 -0800986 "com.android.settings.BackupResetDashboardAlias"), hasBackupActivity, isAdmin);
Doris Ling26bf0032016-06-15 18:16:09 -0700987
Bartosz Fabianowski05061fc2016-11-14 12:04:49 +0100988 setTileEnabled(new ComponentName(packageName,
989 Settings.EnterprisePrivacySettingsActivity.class.getName()),
990 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800991 .hasDeviceOwner(), isAdmin);
Fan Zhang10dd4652016-11-16 10:24:26 -0800992 setTileEnabled(new ComponentName(packageName,
993 "com.android.settings.EnterprisePrivacyDashboardAlias"),
994 FeatureFactory.getFactory(this).getEnterprisePrivacyFeatureProvider(this)
Fan Zhang224caad2017-01-06 11:42:48 -0800995 .hasDeviceOwner(), isAdmin);
996 // Final step, refresh categories.
997 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500998 }
999
Fan Zhang224caad2017-01-06 11:42:48 -08001000 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -04001001 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -08001002 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
1003 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -04001004 enabled = false;
1005 }
Jason Monk5862c1e2016-06-07 14:02:33 -04001006 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001007 }
1008
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001009 private void getMetaData() {
1010 try {
1011 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1012 PackageManager.GET_META_DATA);
1013 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001014 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1015 } catch (NameNotFoundException nnfe) {
1016 // No recovery
1017 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1018 }
1019 }
1020
Fan Zhanga96a2d82016-09-27 17:51:11 -07001021 @Override
1022 protected boolean isDashboardFeatureEnabled() {
1023 if (mDashboardFeatureProvider == null) {
1024 mDashboardFeatureProvider =
1025 FeatureFactory.getFactory(this).getDashboardFeatureProvider(this);
1026 }
1027 return mDashboardFeatureProvider.isEnabled();
1028 }
1029
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001030 // give subclasses access to the Next button
1031 public boolean hasNextButton() {
1032 return mNextButton != null;
1033 }
1034
1035 public Button getNextButton() {
1036 return mNextButton;
1037 }
1038
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001039 @Override
1040 public boolean shouldUpRecreateTask(Intent targetIntent) {
1041 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1042 }
1043
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001044 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001045 @Override
1046 public boolean onQueryTextSubmit(String query) {
Matthew Fritze9955db62016-12-20 09:37:21 -08001047 if (mSearchFeatureProvider.isEnabled(this)) {
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001048 return false;
1049 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001050 mSearchQuery = query;
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001051 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001052 return mSearchResultsFragment.onQueryTextSubmit(query);
1053 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001054
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001055 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001056 @Override
1057 public boolean onQueryTextChange(String newText) {
Fan Zhanga3e8f5c2016-12-01 09:52:10 -08001058 mSearchQuery = newText;
Matthew Fritze9955db62016-12-20 09:37:21 -08001059 if (mSearchFeatureProvider.isEnabled(this) || mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001060 return false;
1061 }
1062 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001063 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001064
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001065 @Override
1066 public boolean onClose() {
1067 return false;
1068 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001069
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001070 @Override
1071 public boolean onMenuItemActionExpand(MenuItem item) {
1072 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001073 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001074 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001075 return true;
1076 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001077
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001078 @Override
1079 public boolean onMenuItemActionCollapse(MenuItem item) {
1080 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001081 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001082 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001083 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001084 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001085 return true;
1086 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001087
Jason Monk4da79e02015-09-10 10:54:36 -04001088 @Override
Jason Monkd4f03ec2016-01-07 16:25:34 -05001089 protected void onTileClicked(Tile tile) {
Jason Monk4da79e02015-09-10 10:54:36 -04001090 if (mIsShowingDashboard) {
1091 // If on dashboard, don't finish so the back comes back to here.
1092 openTile(tile);
1093 } else {
1094 super.onTileClicked(tile);
1095 }
1096 }
1097
Jason Monkaf001092015-11-04 13:16:18 -05001098 @Override
1099 public void onProfileTileOpen() {
1100 if (!mIsShowingDashboard) {
1101 finish();
1102 }
1103 }
1104
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001105 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001106 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001107 if (mSearchResultsFragment != null) {
1108 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001109 }
Anna Galusza6b1e9db2016-03-30 17:25:36 -07001110 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001111 if (current != null && current instanceof SearchResultsSummary) {
1112 mSearchResultsFragment = (SearchResultsSummary) current;
1113 } else {
Fan Zhang28691572016-03-23 15:31:08 -07001114 setContentHeaderView(null);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001115 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001116 SearchResultsSummary.class.getName(), null, false, true,
1117 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001118 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001119 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001120 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001121 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001122
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001123 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001124 public void needToRevertToInitialFragment() {
1125 mNeedToRevertToInitialFragment = true;
1126 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001127
Matthew Fritze0ed37c32016-10-24 10:12:49 -07001128 @Deprecated
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001129 private void revertToInitialFragment() {
1130 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001131 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001132 mSearchMenuItemExpanded = false;
1133 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1134 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001135 if (mSearchMenuItem != null) {
1136 mSearchMenuItem.collapseActionView();
1137 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001138 }
Jim Miller0698a212014-10-16 19:49:07 -07001139
1140 public Intent getResultIntentData() {
1141 return mResultIntentData;
1142 }
1143
1144 public void setResultIntentData(Intent resultIntentData) {
1145 mResultIntentData = resultIntentData;
1146 }
Jason Monkd4f03ec2016-01-07 16:25:34 -05001147
1148 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -07001149 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -07001150 return;
1151 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -07001152 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -05001153 startActivityForResult(intent, REQUEST_SUGGESTION);
1154 }
1155
1156 @Override
1157 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1158 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
1159 && resultCode != RESULT_CANCELED) {
1160 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
1161 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
1162 }
1163 super.onActivityResult(requestCode, resultCode, data);
1164 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001165}