blob: 9f1600bf08a884716ecad85112b5751e5728159e [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
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080019import android.app.ActionBar;
20import android.app.Activity;
21import 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;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
38import android.os.Bundle;
39import android.os.Handler;
40import android.os.INetworkManagementService;
41import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.UserHandle;
45import android.os.UserManager;
46import android.preference.Preference;
47import android.preference.PreferenceFragment;
48import android.preference.PreferenceManager;
49import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.util.TypedValue;
55import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070056import android.view.Menu;
57import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.view.MenuItem;
59import android.view.View;
60import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070061import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070063import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040064
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080065import com.android.internal.util.ArrayUtils;
66import com.android.internal.util.XmlUtils;
67import com.android.settings.accessibility.AccessibilitySettings;
68import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000069import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070071import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.applications.ManageApplications;
73import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.bluetooth.BluetoothSettings;
PauloftheWestc9dc66d2014-07-01 05:19:58 -070075import com.android.settings.bluetooth.MessageAccessSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070076import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070078import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070079import com.android.settings.dashboard.NoHomeDialogFragment;
80import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.deviceinfo.Memory;
82import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040083import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.fuelgauge.PowerUsageSummary;
John Spurlock802ddf92014-07-18 11:51:13 -040085import com.android.settings.notification.NotificationAppList;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070086import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -070087import com.android.settings.quicklaunch.QuickLaunchSettings;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070088import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070089import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
91import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
92import com.android.settings.inputmethod.SpellCheckersSettings;
93import com.android.settings.inputmethod.UserDictionaryList;
94import com.android.settings.location.LocationSettings;
95import com.android.settings.nfc.AndroidBeam;
96import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040097import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040098import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040099import com.android.settings.notification.NotificationAccessSettings;
100import com.android.settings.notification.NotificationSettings;
101import com.android.settings.notification.NotificationStation;
102import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103import com.android.settings.print.PrintJobSettingsFragment;
104import com.android.settings.print.PrintSettingsFragment;
PauloftheWest38155612014-06-30 10:02:36 -0700105import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800106import com.android.settings.tts.TextToSpeechSettings;
107import com.android.settings.users.UserSettings;
108import com.android.settings.vpn2.VpnSettings;
109import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700110import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700112import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.wifi.WifiSettings;
114import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400115
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import org.xmlpull.v1.XmlPullParser;
117import org.xmlpull.v1.XmlPullParserException;
118
119import java.io.IOException;
120import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700122import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700124import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126public class SettingsActivity extends Activity
127 implements PreferenceManager.OnPreferenceTreeClickListener,
128 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100129 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700130 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100131 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133 private static final String LOG_TAG = "Settings";
134
135 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700136 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
138 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700139 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700140 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700141 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 /**
144 * When starting this activity, the invoking Intent can contain this extra
145 * string to specify which fragment should be initially displayed.
146 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
147 * will call isValidFragment() to confirm that the fragment class name is valid for this
148 * activity.
149 */
150 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
151
152 /**
153 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
154 * this extra can also be specified to supply a Bundle of arguments to pass
155 * to that fragment when it is instantiated during the initial creation
156 * of the activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
159
160 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
162 */
163 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
164
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800165 public static final String BACK_STACK_PREFS = ":settings:prefs";
166
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 // extras that allow any preference activity to be launched as part of a wizard
168
169 // show Back and Next buttons? takes boolean parameter
170 // Back will then return RESULT_CANCELED and Next RESULT_OK
171 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
172
173 // add a Skip button?
174 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
175
176 // specify custom text for the Back or Next buttons, or cause a button to not appear
177 // at all by setting it to null
178 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
179 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
180
181 /**
182 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700183 * 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 -0800184 * that fragment.
185 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
188 ":settings:show_fragment_title_resid";
189 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
190 ":settings:show_fragment_as_shortcut";
191
192 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
193 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 private static final String META_DATA_KEY_FRAGMENT_CLASS =
196 "com.android.settings.FRAGMENT_CLASS";
197
198 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
199
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700200 private static final String EMPTY_QUERY = "";
201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 private static boolean sShowNoHomeNotice = false;
203
204 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800206 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700207 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 // Show only these settings for restricted users
210 private int[] SETTINGS_FOR_RESTRICTED = {
211 R.id.wireless_section,
212 R.id.wifi_settings,
213 R.id.bluetooth_settings,
214 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700215 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800216 R.id.wireless_settings,
217 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400218 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 R.id.display_settings,
220 R.id.storage_settings,
221 R.id.application_settings,
222 R.id.battery_settings,
223 R.id.personal_section,
224 R.id.location_settings,
225 R.id.security_settings,
226 R.id.language_settings,
227 R.id.user_settings,
228 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 R.id.system_section,
230 R.id.date_time_settings,
231 R.id.about_settings,
232 R.id.accessibility_settings,
233 R.id.print_settings,
234 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800235 R.id.home_settings,
236 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237 };
238
239 private static final String[] ENTRY_FRAGMENTS = {
240 WirelessSettings.class.getName(),
241 WifiSettings.class.getName(),
242 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700243 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700245 MessageAccessSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700246 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 TetherSettings.class.getName(),
248 WifiP2pSettings.class.getName(),
249 VpnSettings.class.getName(),
250 DateTimeSettings.class.getName(),
251 LocalePicker.class.getName(),
252 InputMethodAndLanguageSettings.class.getName(),
253 SpellCheckersSettings.class.getName(),
254 UserDictionaryList.class.getName(),
255 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700256 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 DisplaySettings.class.getName(),
258 DeviceInfoSettings.class.getName(),
259 ManageApplications.class.getName(),
260 ProcessStatsUi.class.getName(),
261 NotificationStation.class.getName(),
262 LocationSettings.class.getName(),
263 SecuritySettings.class.getName(),
264 PrivacySettings.class.getName(),
265 DeviceAdminSettings.class.getName(),
266 AccessibilitySettings.class.getName(),
267 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800268 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
269 TextToSpeechSettings.class.getName(),
270 Memory.class.getName(),
271 DevelopmentSettings.class.getName(),
272 UsbSettings.class.getName(),
273 AndroidBeam.class.getName(),
274 WifiDisplaySettings.class.getName(),
275 PowerUsageSummary.class.getName(),
276 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000277 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 CryptKeeperSettings.class.getName(),
279 DataUsageSummary.class.getName(),
280 DreamSettings.class.getName(),
281 UserSettings.class.getName(),
282 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400283 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800284 PrintSettingsFragment.class.getName(),
285 PrintJobSettingsFragment.class.getName(),
286 TrustedCredentialsSettings.class.getName(),
287 PaymentSettings.class.getName(),
288 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700289 ZenModeSettings.class.getName(),
290 NotificationSettings.class.getName(),
291 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
292 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400293 InstalledAppDetails.class.getName(),
294 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400295 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700296 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700297 OtherSoundSettings.class.getName(),
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700298 QuickLaunchSettings.class.getName(),
299 ApnSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 };
301
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700302
303 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
304 "android.settings.APPLICATION_DETAILS_SETTINGS"
305 };
306
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 private SharedPreferences mDevelopmentPreferences;
308 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
309
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 private boolean mBatteryPresent = true;
311 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 @Override
313 public void onReceive(Context context, Intent intent) {
314 String action = intent.getAction();
315 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
316 boolean batteryPresent = Utils.isBatteryPresent(intent);
317
318 if (mBatteryPresent != batteryPresent) {
319 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700320 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 }
322 }
323 }
324 };
325
Svetoslav990159a2014-04-14 17:14:59 -0700326 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
327 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700328
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700329 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700330 private SwitchBar mSwitchBar;
331
332 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700333
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700334 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700335 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700336
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700337 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700338 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700339
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700340 private ViewGroup mContent;
341
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700342 private SearchView mSearchView;
343 private MenuItem mSearchMenuItem;
344 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700345 private SearchResultsSummary mSearchResultsFragment;
346 private String mSearchQuery;
347
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700348 // Categories
349 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700351 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700352 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 private Handler mHandler = new Handler() {
354 @Override
355 public void handleMessage(Message msg) {
356 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700357 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700358 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
359 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700360 buildDashboardCategories(mCategories);
361 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 } break;
363 }
364 }
365 };
366
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700367 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700368 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700369
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700370 public SwitchBar getSwitchBar() {
371 return mSwitchBar;
372 }
373
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700374 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
375 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700376 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700377 }
378 return mCategories;
379 }
380
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800381 @Override
382 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
383 // Override the fragment title for Wallpaper settings
384 int titleRes = pref.getTitleRes();
385 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
386 titleRes = R.string.wallpaper_settings_fragment_title;
387 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
388 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
389 if (UserManager.get(this).isLinkedUser()) {
390 titleRes = R.string.profile_info_settings_title;
391 } else {
392 titleRes = R.string.user_info_settings_title;
393 }
394 }
395 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
396 null, 0);
397 return true;
398 }
399
400 @Override
401 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
402 return false;
403 }
404
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700405 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700406 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700407 Message msg = new Message();
408 msg.what = MSG_BUILD_CATEGORIES;
409 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 }
411 }
412
413 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800414 public void onConfigurationChanged(Configuration newConfig) {
415 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800416 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800417 }
418
419 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420 protected void onStart() {
421 super.onStart();
422
423 if (mNeedToRevertToInitialFragment) {
424 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800425 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800426 }
427
428 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700429 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700430 if (!mDisplaySearch) {
431 return false;
432 }
433
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700434 MenuInflater inflater = getMenuInflater();
435 inflater.inflate(R.menu.options_menu, menu);
436
437 // Cache the search query (can be overriden by the OnQueryTextListener)
438 final String query = mSearchQuery;
439
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700440 mSearchMenuItem = menu.findItem(R.id.search);
441 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700442
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700443 if (mSearchMenuItem == null || mSearchView == null) {
444 return false;
445 }
446
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700447 if (mSearchResultsFragment != null) {
448 mSearchResultsFragment.setSearchView(mSearchView);
449 }
450
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700451 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700452 mSearchView.setOnQueryTextListener(this);
453 mSearchView.setOnCloseListener(this);
454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700455 if (mSearchMenuItemExpanded) {
456 mSearchMenuItem.expandActionView();
457 }
458 mSearchView.setQuery(query, true /* submit */);
459
460 return true;
461 }
462
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700463 private static boolean isShortCutIntent(final Intent intent) {
464 Set<String> categories = intent.getCategories();
465 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
466 }
467
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700468 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700469 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700470 if (action == null) {
471 return false;
472 }
473 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
474 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
475 }
476 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700477 }
478
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479 @Override
480 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700481 super.onCreate(savedState);
482
483 // Should happen before any call to getIntent()
484 getMetaData();
485
486 final Intent intent = getIntent();
487 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
488 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800489 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800491 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
492 Context.MODE_PRIVATE);
493
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700494 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700495 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700496
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700497 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700498 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
499
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700500 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700501 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700502
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700503 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700504
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700505 // This is a "Sub Settings" when:
506 // - this is a real SubSettings
507 // - or :settings:show_fragment_as_subsetting is passed to the Intent
508 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
509 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
510
511 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
512 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700513 // Check also that we are not a Theme Dialog as we don't want to override them
514 final int themeResId = getThemeResId();
515 if (themeResId != R.style.Theme_DialogWhenLarge &&
516 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
517 setTheme(R.style.Theme_SubSettings);
518 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700519 }
520
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700521 setContentView(mIsShowingDashboard ?
522 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800523
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700524 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700525
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526 getFragmentManager().addOnBackStackChangedListener(this);
527
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700528 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700529 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700530 }
531
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700532 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700533 // We are restarting from a previous saved state; used that to initialize, instead
534 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700535 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
536 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700538 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800539
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700540 ArrayList<DashboardCategory> categories =
541 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
542 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700543 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700544 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700545 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700547
548 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700549 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700550 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
551 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700553 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
555 // only if it is a sub settings
556 if (mIsShortcut) {
557 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700558 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700559 } else if (isSubSettings) {
560 mDisplayHomeAsUpEnabled = true;
561 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700562 } else {
563 mDisplayHomeAsUpEnabled = false;
564 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700565 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700566 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700567
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700568 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700569 switchToFragment(initialFragmentName, initialArguments, true, false,
570 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000571 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700572 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700573 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700574 // Show Search affordance
575 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700576 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700577 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700578 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 }
580 }
581
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700582 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700583 if (mActionBar != null) {
584 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
585 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
586 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700587 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
588
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800589 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
591
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700592 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593 if (buttonBar != null) {
594 buttonBar.setVisibility(View.VISIBLE);
595
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700596 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 backButton.setOnClickListener(new OnClickListener() {
598 public void onClick(View v) {
599 setResult(RESULT_CANCELED);
600 finish();
601 }
602 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700603 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 skipButton.setOnClickListener(new OnClickListener() {
605 public void onClick(View v) {
606 setResult(RESULT_OK);
607 finish();
608 }
609 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700610 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800611 mNextButton.setOnClickListener(new OnClickListener() {
612 public void onClick(View v) {
613 setResult(RESULT_OK);
614 finish();
615 }
616 });
617
618 // set our various button parameters
619 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
620 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
621 if (TextUtils.isEmpty(buttonText)) {
622 mNextButton.setVisibility(View.GONE);
623 }
624 else {
625 mNextButton.setText(buttonText);
626 }
627 }
628 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
629 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
630 if (TextUtils.isEmpty(buttonText)) {
631 backButton.setVisibility(View.GONE);
632 }
633 else {
634 backButton.setText(buttonText);
635 }
636 }
637 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
638 skipButton.setVisibility(View.VISIBLE);
639 }
640 }
641 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700642
643 mHomeActivitiesCount = getHomeActivitiesCount();
644 }
645
646 private int getHomeActivitiesCount() {
647 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
648 getPackageManager().getHomeActivities(homeApps);
649 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800650 }
651
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700652 private void setTitleFromIntent(Intent intent) {
653 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
654 if (initialTitleResId > 0) {
655 mInitialTitle = null;
656 mInitialTitleResId = initialTitleResId;
657 setTitle(mInitialTitleResId);
658 } else {
659 mInitialTitleResId = -1;
660 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
661 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
662 setTitle(mInitialTitle);
663 }
664 }
665
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800666 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800667 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700668 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 }
670
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700671 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800672 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700673
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800674 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700675 if (mInitialTitleResId > 0) {
676 setTitle(mInitialTitleResId);
677 } else {
678 setTitle(mInitialTitle);
679 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700680 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800681 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700682
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800683 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
684 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700685
686 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800687 }
688
689 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
690 final CharSequence title;
691 final int titleRes = bse.getBreadCrumbTitleRes();
692 if (titleRes > 0) {
693 title = getText(titleRes);
694 } else {
695 title = bse.getBreadCrumbTitle();
696 }
697 if (title != null) {
698 setTitle(title);
699 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800700 }
701
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800702 @Override
703 protected void onSaveInstanceState(Bundle outState) {
704 super.onSaveInstanceState(outState);
705
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700706 if (mCategories.size() > 0) {
707 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800708 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700709
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700710 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700711 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700712
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700713 if (mDisplaySearch) {
714 // The option menus are created if the ActionBar is visible and they are also created
715 // asynchronously. If you launch Settings with an Intent action like
716 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
717 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
718 // menu item and search view are null.
719 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
720 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700721
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700722 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
723 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
724 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700725
726 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800727 }
728
729 @Override
730 public void onResume() {
731 super.onResume();
732
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700733 final int newHomeActivityCount = getHomeActivitiesCount();
734 if (newHomeActivityCount != mHomeActivitiesCount) {
735 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700736 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700737 }
738
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800739 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
740 @Override
741 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700742 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800743 }
744 };
745 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
746 mDevelopmentPreferencesListener);
747
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800748 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700749
Svetoslav990159a2014-04-14 17:14:59 -0700750 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700751
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700752 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700753 onQueryTextSubmit(mSearchQuery);
754 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 }
756
757 @Override
758 public void onPause() {
759 super.onPause();
760
761 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700762 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800763 }
764
765 @Override
766 public void onDestroy() {
767 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700768
769 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
770 mDevelopmentPreferencesListener);
771 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800772 }
773
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 protected boolean isValidFragment(String fragmentName) {
775 // Almost all fragments are wrapped in this,
776 // except for a few that have their own activities.
777 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
778 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
779 }
780 return false;
781 }
782
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800783 @Override
784 public Intent getIntent() {
785 Intent superIntent = super.getIntent();
786 String startingFragment = getStartingFragmentClass(superIntent);
787 // This is called from super.onCreate, isMultiPane() is not yet reliable
788 // Do not use onIsHidingHeaders either, which relies itself on this method
789 if (startingFragment != null) {
790 Intent modIntent = new Intent(superIntent);
791 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
792 Bundle args = superIntent.getExtras();
793 if (args != null) {
794 args = new Bundle(args);
795 } else {
796 args = new Bundle();
797 }
798 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100799 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800800 return modIntent;
801 }
802 return superIntent;
803 }
804
805 /**
806 * Checks if the component name in the intent is different from the Settings class and
807 * returns the class name to load as a fragment.
808 */
809 private String getStartingFragmentClass(Intent intent) {
810 if (mFragmentClass != null) return mFragmentClass;
811
812 String intentClass = intent.getComponent().getClassName();
813 if (intentClass.equals(getClass().getName())) return null;
814
815 if ("com.android.settings.ManageApplications".equals(intentClass)
816 || "com.android.settings.RunningServices".equals(intentClass)
817 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
818 // Old names of manage apps.
819 intentClass = com.android.settings.applications.ManageApplications.class.getName();
820 }
821
822 return intentClass;
823 }
824
825 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000826 * Start a new fragment containing a preference panel. If the preferences
827 * are being displayed in multi-pane mode, the given fragment class will
828 * be instantiated and placed in the appropriate pane. If running in
829 * single-pane mode, a new activity will be launched in which to show the
830 * fragment.
831 *
832 * @param fragmentClass Full name of the class implementing the fragment.
833 * @param args Any desired arguments to supply to the fragment.
834 * @param titleRes Optional resource identifier of the title of this
835 * fragment.
836 * @param titleText Optional text of the title of this fragment.
837 * @param resultTo Optional fragment that result data should be sent to.
838 * If non-null, resultTo.onActivityResult() will be called when this
839 * preference panel is done. The launched panel must use
840 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
841 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700842 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000843 */
844 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700845 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700846 String title = null;
847 if (titleRes < 0) {
848 if (titleText != null) {
849 title = titleText.toString();
850 } else {
851 // There not much we can do in that case
852 title = "";
853 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700854 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700855 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700856 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000857 }
858
859 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800860 * Called by a preference panel fragment to finish itself.
861 *
862 * @param caller The fragment that is asking to be finished.
863 * @param resultCode Optional result code to send back to the original
864 * launching fragment.
865 * @param resultData Optional result data to send back to the original
866 * launching fragment.
867 */
868 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
869 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700870 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800871 }
872
873 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000874 * Start a new fragment.
875 *
876 * @param fragment The fragment to start
877 * @param push If true, the current fragment will be pushed onto the back stack. If false,
878 * the current fragment will be replaced.
879 */
880 public void startPreferenceFragment(Fragment fragment, boolean push) {
881 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700882 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000883 if (push) {
884 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
885 transaction.addToBackStack(BACK_STACK_PREFS);
886 } else {
887 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
888 }
889 transaction.commitAllowingStateLoss();
890 }
891
892 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700893 * Switch to a specific Fragment with taking care of validation, Title and BackStack
894 */
895 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700896 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700897 if (validate && !isValidFragment(fragmentName)) {
898 throw new IllegalArgumentException("Invalid fragment for this activity: "
899 + fragmentName);
900 }
901 Fragment f = Fragment.instantiate(this, fragmentName, args);
902 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700903 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700904 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700905 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700906 }
907 if (addToBackStack) {
908 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
909 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700910 if (titleResId > 0) {
911 transaction.setBreadCrumbTitle(titleResId);
912 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700913 transaction.setBreadCrumbTitle(title);
914 }
915 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700916 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700917 return f;
918 }
919
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700920 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700921 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700922 *
923 * @param categories The list in which to place the tiles categories.
924 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700925 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700926 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700927 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
928 updateTilesList(categories);
929 }
930
931 /**
932 * Parse the given XML file as a categories description, adding each
933 * parsed categories and tiles into the target list.
934 *
935 * @param resid The XML resource to load and parse.
936 * @param target The list in which the parsed categories and tiles should be placed.
937 */
938 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
939 XmlResourceParser parser = null;
940 try {
941 parser = getResources().getXml(resid);
942 AttributeSet attrs = Xml.asAttributeSet(parser);
943
944 int type;
945 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
946 && type != XmlPullParser.START_TAG) {
947 // Parse next until start tag is found
948 }
949
950 String nodeName = parser.getName();
951 if (!"dashboard-categories".equals(nodeName)) {
952 throw new RuntimeException(
953 "XML document must start with <preference-categories> tag; found"
954 + nodeName + " at " + parser.getPositionDescription());
955 }
956
957 Bundle curBundle = null;
958
959 final int outerDepth = parser.getDepth();
960 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
961 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
962 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
963 continue;
964 }
965
966 nodeName = parser.getName();
967 if ("dashboard-category".equals(nodeName)) {
968 DashboardCategory category = new DashboardCategory();
969
970 TypedArray sa = obtainStyledAttributes(
971 attrs, com.android.internal.R.styleable.PreferenceHeader);
972 category.id = sa.getResourceId(
973 com.android.internal.R.styleable.PreferenceHeader_id,
974 (int)DashboardCategory.CAT_ID_UNDEFINED);
975
976 TypedValue tv = sa.peekValue(
977 com.android.internal.R.styleable.PreferenceHeader_title);
978 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
979 if (tv.resourceId != 0) {
980 category.titleRes = tv.resourceId;
981 } else {
982 category.title = tv.string;
983 }
984 }
985 sa.recycle();
986
987 final int innerDepth = parser.getDepth();
988 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
989 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
990 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
991 continue;
992 }
993
994 String innerNodeName = parser.getName();
995 if (innerNodeName.equals("dashboard-tile")) {
996 DashboardTile tile = new DashboardTile();
997
998 sa = obtainStyledAttributes(
999 attrs, com.android.internal.R.styleable.PreferenceHeader);
1000 tile.id = sa.getResourceId(
1001 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001002 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001003 tv = sa.peekValue(
1004 com.android.internal.R.styleable.PreferenceHeader_title);
1005 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1006 if (tv.resourceId != 0) {
1007 tile.titleRes = tv.resourceId;
1008 } else {
1009 tile.title = tv.string;
1010 }
1011 }
1012 tv = sa.peekValue(
1013 com.android.internal.R.styleable.PreferenceHeader_summary);
1014 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1015 if (tv.resourceId != 0) {
1016 tile.summaryRes = tv.resourceId;
1017 } else {
1018 tile.summary = tv.string;
1019 }
1020 }
1021 tile.iconRes = sa.getResourceId(
1022 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1023 tile.fragment = sa.getString(
1024 com.android.internal.R.styleable.PreferenceHeader_fragment);
1025 sa.recycle();
1026
1027 if (curBundle == null) {
1028 curBundle = new Bundle();
1029 }
1030
1031 final int innerDepth2 = parser.getDepth();
1032 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1033 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1034 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1035 continue;
1036 }
1037
1038 String innerNodeName2 = parser.getName();
1039 if (innerNodeName2.equals("extra")) {
1040 getResources().parseBundleExtra("extra", attrs, curBundle);
1041 XmlUtils.skipCurrentTag(parser);
1042
1043 } else if (innerNodeName2.equals("intent")) {
1044 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1045
1046 } else {
1047 XmlUtils.skipCurrentTag(parser);
1048 }
1049 }
1050
1051 if (curBundle.size() > 0) {
1052 tile.fragmentArguments = curBundle;
1053 curBundle = null;
1054 }
1055
PauloftheWest38155612014-06-30 10:02:36 -07001056 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001057 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001058 category.addTile(tile);
1059 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001060
1061 } else {
1062 XmlUtils.skipCurrentTag(parser);
1063 }
1064 }
1065
1066 target.add(category);
1067 } else {
1068 XmlUtils.skipCurrentTag(parser);
1069 }
1070 }
1071
1072 } catch (XmlPullParserException e) {
1073 throw new RuntimeException("Error parsing categories", e);
1074 } catch (IOException e) {
1075 throw new RuntimeException("Error parsing categories", e);
1076 } finally {
1077 if (parser != null) parser.close();
1078 }
1079 }
1080
1081 private void updateTilesList(List<DashboardCategory> target) {
1082 final boolean showDev = mDevelopmentPreferences.getBoolean(
1083 DevelopmentSettings.PREF_SHOW,
1084 android.os.Build.TYPE.equals("eng"));
1085
1086 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1087
1088 final int size = target.size();
1089 for (int i = 0; i < size; i++) {
1090
1091 DashboardCategory category = target.get(i);
1092
1093 // Ids are integers, so downcasting is ok
1094 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001095 int n = category.getTilesCount() - 1;
1096 while (n >= 0) {
1097
1098 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001099 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001100 id = (int) tile.id;
1101 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001102 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1103 removeTile = true;
1104 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001105 } else if (id == R.id.wifi_settings) {
1106 // Remove WiFi Settings if WiFi service is not available.
1107 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001108 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001109 }
1110 } else if (id == R.id.bluetooth_settings) {
1111 // Remove Bluetooth Settings if Bluetooth service is not available.
1112 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001113 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001114 }
1115 } else if (id == R.id.data_usage_settings) {
1116 // Remove data usage when kernel module not enabled
1117 final INetworkManagementService netManager = INetworkManagementService.Stub
1118 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1119 try {
1120 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001121 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001122 }
1123 } catch (RemoteException e) {
1124 // ignored
1125 }
1126 } else if (id == R.id.battery_settings) {
1127 // Remove battery settings when battery is not available. (e.g. TV)
1128
1129 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001130 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001131 }
1132 } else if (id == R.id.home_settings) {
1133 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001134 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001135 }
1136 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001137 boolean hasMultipleUsers =
1138 ((UserManager) getSystemService(Context.USER_SERVICE))
1139 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001140 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001141 || (!UserManager.supportsMultipleUsers()
1142 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001143 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001144 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001145 }
1146 } else if (id == R.id.nfc_payment_settings) {
1147 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001148 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001149 } else {
1150 // Only show if NFC is on and we have the HCE feature
1151 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1152 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1153 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001154 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001155 }
1156 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001157 } else if (id == R.id.print_settings) {
1158 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1159 PackageManager.FEATURE_PRINTING);
1160 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001161 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001162 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001163 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001164 if (!showDev || um.hasUserRestriction(
1165 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001166 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001167 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001168 }
1169
1170 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001171 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1172 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001173 }
1174
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001175 if (removeTile && n < category.getTilesCount()) {
1176 category.removeTile(n);
1177 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001178 n--;
1179 }
1180 }
1181 }
1182
1183 private boolean updateHomeSettingTiles(DashboardTile tile) {
1184 // Once we decide to show Home settings, keep showing it forever
1185 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1186 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1187 return true;
1188 }
1189
1190 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001191 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001192 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001193 // When there's only one available home app, omit this settings
1194 // category entirely at the top level UI. If the user just
1195 // uninstalled the penultimate home app candidiate, we also
1196 // now tell them about why they aren't seeing 'Home' in the list.
1197 if (sShowNoHomeNotice) {
1198 sShowNoHomeNotice = false;
1199 NoHomeDialogFragment.show(this);
1200 }
1201 return false;
1202 } else {
1203 // Okay, we're allowing the Home settings category. Tell it, when
1204 // invoked via this front door, that we'll need to be told about the
1205 // case when the user uninstalls all but one home app.
1206 if (tile.fragmentArguments == null) {
1207 tile.fragmentArguments = new Bundle();
1208 }
1209 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1210 }
1211 } catch (Exception e) {
1212 // Can't look up the home activity; bail on configuring the icon
1213 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1214 }
1215
1216 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1217 return true;
1218 }
1219
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001220 private void getMetaData() {
1221 try {
1222 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1223 PackageManager.GET_META_DATA);
1224 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001225 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1226 } catch (NameNotFoundException nnfe) {
1227 // No recovery
1228 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1229 }
1230 }
1231
1232 // give subclasses access to the Next button
1233 public boolean hasNextButton() {
1234 return mNextButton != null;
1235 }
1236
1237 public Button getNextButton() {
1238 return mNextButton;
1239 }
1240
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001241 @Override
1242 public boolean shouldUpRecreateTask(Intent targetIntent) {
1243 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1244 }
1245
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001246 public static void requestHomeNotice() {
1247 sShowNoHomeNotice = true;
1248 }
1249
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001250 @Override
1251 public boolean onQueryTextSubmit(String query) {
1252 switchToSearchResultsFragmentIfNeeded();
1253 mSearchQuery = query;
1254 return mSearchResultsFragment.onQueryTextSubmit(query);
1255 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001256
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001257 @Override
1258 public boolean onQueryTextChange(String newText) {
1259 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001260 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001261 return false;
1262 }
1263 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001265
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001266 @Override
1267 public boolean onClose() {
1268 return false;
1269 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001270
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001271 @Override
1272 public boolean onMenuItemActionExpand(MenuItem item) {
1273 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001274 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001275 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001276 return true;
1277 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001278
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001279 @Override
1280 public boolean onMenuItemActionCollapse(MenuItem item) {
1281 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001282 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001283 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001284 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001285 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001286 return true;
1287 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001288
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001289 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001290 if (mSearchResultsFragment != null) {
1291 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001292 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001293 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001294 if (current != null && current instanceof SearchResultsSummary) {
1295 mSearchResultsFragment = (SearchResultsSummary) current;
1296 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001297 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001298 SearchResultsSummary.class.getName(), null, false, true,
1299 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001300 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001301 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001302 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001303 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001304
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001305 public void needToRevertToInitialFragment() {
1306 mNeedToRevertToInitialFragment = true;
1307 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001308
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001309 private void revertToInitialFragment() {
1310 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001311 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001312 mSearchMenuItemExpanded = false;
1313 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1314 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001315 if (mSearchMenuItem != null) {
1316 mSearchMenuItem.collapseActionView();
1317 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001318 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001319}