blob: a8e28df15ab0fc1d6a1195b122601d1f0aa45972 [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;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700108import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.vpn2.VpnSettings;
110import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700111import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700113import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import com.android.settings.wifi.WifiSettings;
115import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400116
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import org.xmlpull.v1.XmlPullParser;
118import org.xmlpull.v1.XmlPullParserException;
119
120import java.io.IOException;
121import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700123import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700125import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700126
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127public class SettingsActivity extends Activity
128 implements PreferenceManager.OnPreferenceTreeClickListener,
129 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100130 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700131 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100132 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133
134 private static final String LOG_TAG = "Settings";
135
136 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700137 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
139 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700140 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700141 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700142 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800143
144 /**
145 * When starting this activity, the invoking Intent can contain this extra
146 * string to specify which fragment should be initially displayed.
147 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
148 * will call isValidFragment() to confirm that the fragment class name is valid for this
149 * activity.
150 */
151 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
152
153 /**
154 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
155 * this extra can also be specified to supply a Bundle of arguments to pass
156 * to that fragment when it is instantiated during the initial creation
157 * of the activity.
158 */
159 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
160
161 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700162 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
163 */
164 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
165
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800166 public static final String BACK_STACK_PREFS = ":settings:prefs";
167
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 // extras that allow any preference activity to be launched as part of a wizard
169
170 // show Back and Next buttons? takes boolean parameter
171 // Back will then return RESULT_CANCELED and Next RESULT_OK
172 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
173
174 // add a Skip button?
175 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
176
177 // specify custom text for the Back or Next buttons, or cause a button to not appear
178 // at all by setting it to null
179 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
180 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
181
182 /**
183 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700184 * 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 -0800185 * that fragment.
186 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700188 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700189 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800190
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 private static final String META_DATA_KEY_FRAGMENT_CLASS =
192 "com.android.settings.FRAGMENT_CLASS";
193
194 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
195
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700196 private static final String EMPTY_QUERY = "";
197
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198 private static boolean sShowNoHomeNotice = false;
199
200 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800202 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700203 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800204
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205 // Show only these settings for restricted users
206 private int[] SETTINGS_FOR_RESTRICTED = {
207 R.id.wireless_section,
208 R.id.wifi_settings,
209 R.id.bluetooth_settings,
210 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700211 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 R.id.wireless_settings,
213 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400214 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215 R.id.display_settings,
216 R.id.storage_settings,
217 R.id.application_settings,
218 R.id.battery_settings,
219 R.id.personal_section,
220 R.id.location_settings,
221 R.id.security_settings,
222 R.id.language_settings,
223 R.id.user_settings,
224 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800225 R.id.system_section,
226 R.id.date_time_settings,
227 R.id.about_settings,
228 R.id.accessibility_settings,
229 R.id.print_settings,
230 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800231 R.id.home_settings,
232 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800233 };
234
235 private static final String[] ENTRY_FRAGMENTS = {
236 WirelessSettings.class.getName(),
237 WifiSettings.class.getName(),
238 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700239 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700241 MessageAccessSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700242 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 TetherSettings.class.getName(),
244 WifiP2pSettings.class.getName(),
245 VpnSettings.class.getName(),
246 DateTimeSettings.class.getName(),
247 LocalePicker.class.getName(),
248 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700249 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 SpellCheckersSettings.class.getName(),
251 UserDictionaryList.class.getName(),
252 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700253 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 DisplaySettings.class.getName(),
255 DeviceInfoSettings.class.getName(),
256 ManageApplications.class.getName(),
257 ProcessStatsUi.class.getName(),
258 NotificationStation.class.getName(),
259 LocationSettings.class.getName(),
260 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700261 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262 PrivacySettings.class.getName(),
263 DeviceAdminSettings.class.getName(),
264 AccessibilitySettings.class.getName(),
265 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800266 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
267 TextToSpeechSettings.class.getName(),
268 Memory.class.getName(),
269 DevelopmentSettings.class.getName(),
270 UsbSettings.class.getName(),
271 AndroidBeam.class.getName(),
272 WifiDisplaySettings.class.getName(),
273 PowerUsageSummary.class.getName(),
274 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000275 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276 CryptKeeperSettings.class.getName(),
277 DataUsageSummary.class.getName(),
278 DreamSettings.class.getName(),
279 UserSettings.class.getName(),
280 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400281 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 PrintSettingsFragment.class.getName(),
283 PrintJobSettingsFragment.class.getName(),
284 TrustedCredentialsSettings.class.getName(),
285 PaymentSettings.class.getName(),
286 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700287 ZenModeSettings.class.getName(),
288 NotificationSettings.class.getName(),
289 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
290 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400291 InstalledAppDetails.class.getName(),
292 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400293 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700294 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700295 OtherSoundSettings.class.getName(),
296 QuickLaunchSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 };
298
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700299
300 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
301 "android.settings.APPLICATION_DETAILS_SETTINGS"
302 };
303
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 private SharedPreferences mDevelopmentPreferences;
305 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
306
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 private boolean mBatteryPresent = true;
308 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 @Override
310 public void onReceive(Context context, Intent intent) {
311 String action = intent.getAction();
312 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
313 boolean batteryPresent = Utils.isBatteryPresent(intent);
314
315 if (mBatteryPresent != batteryPresent) {
316 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700317 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 }
319 }
320 }
321 };
322
Svetoslav990159a2014-04-14 17:14:59 -0700323 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
324 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700325
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700327 private SwitchBar mSwitchBar;
328
329 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700330
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700331 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700332 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700333
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700334 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700335 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700336
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700337 private ViewGroup mContent;
338
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700339 private SearchView mSearchView;
340 private MenuItem mSearchMenuItem;
341 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700342 private SearchResultsSummary mSearchResultsFragment;
343 private String mSearchQuery;
344
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700345 // Categories
346 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700348 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700349 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 private Handler mHandler = new Handler() {
351 @Override
352 public void handleMessage(Message msg) {
353 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700354 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700355 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
356 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700357 buildDashboardCategories(mCategories);
358 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 } break;
360 }
361 }
362 };
363
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700364 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700365 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700366
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700367 public SwitchBar getSwitchBar() {
368 return mSwitchBar;
369 }
370
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700371 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
372 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700373 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700374 }
375 return mCategories;
376 }
377
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800378 @Override
379 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
380 // Override the fragment title for Wallpaper settings
381 int titleRes = pref.getTitleRes();
382 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
383 titleRes = R.string.wallpaper_settings_fragment_title;
384 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
385 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
386 if (UserManager.get(this).isLinkedUser()) {
387 titleRes = R.string.profile_info_settings_title;
388 } else {
389 titleRes = R.string.user_info_settings_title;
390 }
391 }
392 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
393 null, 0);
394 return true;
395 }
396
397 @Override
398 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
399 return false;
400 }
401
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700402 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700403 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700404 Message msg = new Message();
405 msg.what = MSG_BUILD_CATEGORIES;
406 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407 }
408 }
409
410 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 public void onConfigurationChanged(Configuration newConfig) {
412 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800413 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800414 }
415
416 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700417 protected void onStart() {
418 super.onStart();
419
420 if (mNeedToRevertToInitialFragment) {
421 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800422 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800423 }
424
425 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700427 if (!mDisplaySearch) {
428 return false;
429 }
430
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700431 MenuInflater inflater = getMenuInflater();
432 inflater.inflate(R.menu.options_menu, menu);
433
434 // Cache the search query (can be overriden by the OnQueryTextListener)
435 final String query = mSearchQuery;
436
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700437 mSearchMenuItem = menu.findItem(R.id.search);
438 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700439
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700440 if (mSearchMenuItem == null || mSearchView == null) {
441 return false;
442 }
443
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700444 if (mSearchResultsFragment != null) {
445 mSearchResultsFragment.setSearchView(mSearchView);
446 }
447
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700448 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700449 mSearchView.setOnQueryTextListener(this);
450 mSearchView.setOnCloseListener(this);
451
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700452 if (mSearchMenuItemExpanded) {
453 mSearchMenuItem.expandActionView();
454 }
455 mSearchView.setQuery(query, true /* submit */);
456
457 return true;
458 }
459
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700460 private static boolean isShortCutIntent(final Intent intent) {
461 Set<String> categories = intent.getCategories();
462 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
463 }
464
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700465 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700466 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700467 if (action == null) {
468 return false;
469 }
470 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
471 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
472 }
473 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700474 }
475
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700476 @Override
477 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700478 super.onCreate(savedState);
479
480 // Should happen before any call to getIntent()
481 getMetaData();
482
483 final Intent intent = getIntent();
484 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
485 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800486 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800487
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
489 Context.MODE_PRIVATE);
490
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700491 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700492 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700493
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700494 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700495 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
496
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700497 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700498 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700499
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700500 mIsShowingDashboard = className.equals(Settings.class.getName());
501 final boolean isSubSettings = className.equals(SubSettings.class.getName());
502
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700503 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
504 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700505 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700506 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
507 // Check also that we are not a Theme Dialog as we don't want to override them
508 final int themeResId = getThemeResId();
509 if (themeResId != R.style.Theme_DialogWhenLarge &&
510 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
511 setTheme(R.style.Theme_SubSettings);
512 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700513 }
514
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700515 setContentView(mIsShowingDashboard ?
516 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700518 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700519
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800520 getFragmentManager().addOnBackStackChangedListener(this);
521
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700522 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700523 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700524 }
525
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700526 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700527 // We are restarting from a previous saved state; used that to initialize, instead
528 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700529 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
530 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700532 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800533
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700534 ArrayList<DashboardCategory> categories =
535 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
536 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700537 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700538 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700539 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700541
542 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700543 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700544 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
545 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700547 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700548 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
549 // only if it is a sub settings
550 if (mIsShortcut) {
551 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700552 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553 } else if (isSubSettings) {
554 mDisplayHomeAsUpEnabled = true;
555 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700556 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700557 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700558
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700559 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700560 switchToFragment(initialFragmentName, initialArguments, true, false,
561 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000562 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700563 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700564 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700565 // Show Search affordance
566 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700567 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700568 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700569 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 }
571 }
572
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700573 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700574 if (mActionBar != null) {
575 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
576 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
577 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700578 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
579
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
582
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700583 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800584 if (buttonBar != null) {
585 buttonBar.setVisibility(View.VISIBLE);
586
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700587 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 backButton.setOnClickListener(new OnClickListener() {
589 public void onClick(View v) {
590 setResult(RESULT_CANCELED);
591 finish();
592 }
593 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700594 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 skipButton.setOnClickListener(new OnClickListener() {
596 public void onClick(View v) {
597 setResult(RESULT_OK);
598 finish();
599 }
600 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700601 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 mNextButton.setOnClickListener(new OnClickListener() {
603 public void onClick(View v) {
604 setResult(RESULT_OK);
605 finish();
606 }
607 });
608
609 // set our various button parameters
610 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
611 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
612 if (TextUtils.isEmpty(buttonText)) {
613 mNextButton.setVisibility(View.GONE);
614 }
615 else {
616 mNextButton.setText(buttonText);
617 }
618 }
619 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
620 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
621 if (TextUtils.isEmpty(buttonText)) {
622 backButton.setVisibility(View.GONE);
623 }
624 else {
625 backButton.setText(buttonText);
626 }
627 }
628 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
629 skipButton.setVisibility(View.VISIBLE);
630 }
631 }
632 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700633
634 mHomeActivitiesCount = getHomeActivitiesCount();
635 }
636
637 private int getHomeActivitiesCount() {
638 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
639 getPackageManager().getHomeActivities(homeApps);
640 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800641 }
642
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700643 private void setTitleFromIntent(Intent intent) {
644 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
645 if (initialTitleResId > 0) {
646 mInitialTitle = null;
647 mInitialTitleResId = initialTitleResId;
648 setTitle(mInitialTitleResId);
649 } else {
650 mInitialTitleResId = -1;
651 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
652 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
653 setTitle(mInitialTitle);
654 }
655 }
656
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800657 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700659 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 }
661
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700662 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800663 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700664
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800665 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700666 if (mInitialTitleResId > 0) {
667 setTitle(mInitialTitleResId);
668 } else {
669 setTitle(mInitialTitle);
670 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700671 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800672 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700673
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800674 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
675 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700676
677 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800678 }
679
680 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
681 final CharSequence title;
682 final int titleRes = bse.getBreadCrumbTitleRes();
683 if (titleRes > 0) {
684 title = getText(titleRes);
685 } else {
686 title = bse.getBreadCrumbTitle();
687 }
688 if (title != null) {
689 setTitle(title);
690 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800691 }
692
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 @Override
694 protected void onSaveInstanceState(Bundle outState) {
695 super.onSaveInstanceState(outState);
696
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700697 if (mCategories.size() > 0) {
698 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800699 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700700
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700701 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700702 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700703
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700704 if (mDisplaySearch) {
705 // The option menus are created if the ActionBar is visible and they are also created
706 // asynchronously. If you launch Settings with an Intent action like
707 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
708 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
709 // menu item and search view are null.
710 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
711 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700712
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700713 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
714 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
715 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700716
717 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800718 }
719
720 @Override
721 public void onResume() {
722 super.onResume();
723
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700724 final int newHomeActivityCount = getHomeActivitiesCount();
725 if (newHomeActivityCount != mHomeActivitiesCount) {
726 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700727 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700728 }
729
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800730 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
731 @Override
732 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700733 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800734 }
735 };
736 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
737 mDevelopmentPreferencesListener);
738
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800739 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700740
Svetoslav990159a2014-04-14 17:14:59 -0700741 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700742
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700743 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700744 onQueryTextSubmit(mSearchQuery);
745 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800746 }
747
748 @Override
749 public void onPause() {
750 super.onPause();
751
752 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700753 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800754 }
755
756 @Override
757 public void onDestroy() {
758 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700759
760 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
761 mDevelopmentPreferencesListener);
762 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800763 }
764
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800765 protected boolean isValidFragment(String fragmentName) {
766 // Almost all fragments are wrapped in this,
767 // except for a few that have their own activities.
768 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
769 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
770 }
771 return false;
772 }
773
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 @Override
775 public Intent getIntent() {
776 Intent superIntent = super.getIntent();
777 String startingFragment = getStartingFragmentClass(superIntent);
778 // This is called from super.onCreate, isMultiPane() is not yet reliable
779 // Do not use onIsHidingHeaders either, which relies itself on this method
780 if (startingFragment != null) {
781 Intent modIntent = new Intent(superIntent);
782 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
783 Bundle args = superIntent.getExtras();
784 if (args != null) {
785 args = new Bundle(args);
786 } else {
787 args = new Bundle();
788 }
789 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100790 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800791 return modIntent;
792 }
793 return superIntent;
794 }
795
796 /**
797 * Checks if the component name in the intent is different from the Settings class and
798 * returns the class name to load as a fragment.
799 */
800 private String getStartingFragmentClass(Intent intent) {
801 if (mFragmentClass != null) return mFragmentClass;
802
803 String intentClass = intent.getComponent().getClassName();
804 if (intentClass.equals(getClass().getName())) return null;
805
806 if ("com.android.settings.ManageApplications".equals(intentClass)
807 || "com.android.settings.RunningServices".equals(intentClass)
808 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
809 // Old names of manage apps.
810 intentClass = com.android.settings.applications.ManageApplications.class.getName();
811 }
812
813 return intentClass;
814 }
815
816 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000817 * Start a new fragment containing a preference panel. If the preferences
818 * are being displayed in multi-pane mode, the given fragment class will
819 * be instantiated and placed in the appropriate pane. If running in
820 * single-pane mode, a new activity will be launched in which to show the
821 * fragment.
822 *
823 * @param fragmentClass Full name of the class implementing the fragment.
824 * @param args Any desired arguments to supply to the fragment.
825 * @param titleRes Optional resource identifier of the title of this
826 * fragment.
827 * @param titleText Optional text of the title of this fragment.
828 * @param resultTo Optional fragment that result data should be sent to.
829 * If non-null, resultTo.onActivityResult() will be called when this
830 * preference panel is done. The launched panel must use
831 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
832 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700833 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000834 */
835 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700836 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700837 String title = null;
838 if (titleRes < 0) {
839 if (titleText != null) {
840 title = titleText.toString();
841 } else {
842 // There not much we can do in that case
843 title = "";
844 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700845 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700846 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700847 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000848 }
849
850 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800851 * Called by a preference panel fragment to finish itself.
852 *
853 * @param caller The fragment that is asking to be finished.
854 * @param resultCode Optional result code to send back to the original
855 * launching fragment.
856 * @param resultData Optional result data to send back to the original
857 * launching fragment.
858 */
859 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
860 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700861 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800862 }
863
864 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000865 * Start a new fragment.
866 *
867 * @param fragment The fragment to start
868 * @param push If true, the current fragment will be pushed onto the back stack. If false,
869 * the current fragment will be replaced.
870 */
871 public void startPreferenceFragment(Fragment fragment, boolean push) {
872 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700873 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000874 if (push) {
875 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
876 transaction.addToBackStack(BACK_STACK_PREFS);
877 } else {
878 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
879 }
880 transaction.commitAllowingStateLoss();
881 }
882
883 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700884 * Switch to a specific Fragment with taking care of validation, Title and BackStack
885 */
886 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700887 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700888 if (validate && !isValidFragment(fragmentName)) {
889 throw new IllegalArgumentException("Invalid fragment for this activity: "
890 + fragmentName);
891 }
892 Fragment f = Fragment.instantiate(this, fragmentName, args);
893 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700894 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700895 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700896 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700897 }
898 if (addToBackStack) {
899 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
900 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700901 if (titleResId > 0) {
902 transaction.setBreadCrumbTitle(titleResId);
903 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700904 transaction.setBreadCrumbTitle(title);
905 }
906 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700907 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700908 return f;
909 }
910
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700911 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700912 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700913 *
914 * @param categories The list in which to place the tiles categories.
915 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700916 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700917 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700918 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
919 updateTilesList(categories);
920 }
921
922 /**
923 * Parse the given XML file as a categories description, adding each
924 * parsed categories and tiles into the target list.
925 *
926 * @param resid The XML resource to load and parse.
927 * @param target The list in which the parsed categories and tiles should be placed.
928 */
929 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
930 XmlResourceParser parser = null;
931 try {
932 parser = getResources().getXml(resid);
933 AttributeSet attrs = Xml.asAttributeSet(parser);
934
935 int type;
936 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
937 && type != XmlPullParser.START_TAG) {
938 // Parse next until start tag is found
939 }
940
941 String nodeName = parser.getName();
942 if (!"dashboard-categories".equals(nodeName)) {
943 throw new RuntimeException(
944 "XML document must start with <preference-categories> tag; found"
945 + nodeName + " at " + parser.getPositionDescription());
946 }
947
948 Bundle curBundle = null;
949
950 final int outerDepth = parser.getDepth();
951 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
952 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
953 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
954 continue;
955 }
956
957 nodeName = parser.getName();
958 if ("dashboard-category".equals(nodeName)) {
959 DashboardCategory category = new DashboardCategory();
960
961 TypedArray sa = obtainStyledAttributes(
962 attrs, com.android.internal.R.styleable.PreferenceHeader);
963 category.id = sa.getResourceId(
964 com.android.internal.R.styleable.PreferenceHeader_id,
965 (int)DashboardCategory.CAT_ID_UNDEFINED);
966
967 TypedValue tv = sa.peekValue(
968 com.android.internal.R.styleable.PreferenceHeader_title);
969 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
970 if (tv.resourceId != 0) {
971 category.titleRes = tv.resourceId;
972 } else {
973 category.title = tv.string;
974 }
975 }
976 sa.recycle();
977
978 final int innerDepth = parser.getDepth();
979 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
980 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
981 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
982 continue;
983 }
984
985 String innerNodeName = parser.getName();
986 if (innerNodeName.equals("dashboard-tile")) {
987 DashboardTile tile = new DashboardTile();
988
989 sa = obtainStyledAttributes(
990 attrs, com.android.internal.R.styleable.PreferenceHeader);
991 tile.id = sa.getResourceId(
992 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700993 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700994 tv = sa.peekValue(
995 com.android.internal.R.styleable.PreferenceHeader_title);
996 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
997 if (tv.resourceId != 0) {
998 tile.titleRes = tv.resourceId;
999 } else {
1000 tile.title = tv.string;
1001 }
1002 }
1003 tv = sa.peekValue(
1004 com.android.internal.R.styleable.PreferenceHeader_summary);
1005 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1006 if (tv.resourceId != 0) {
1007 tile.summaryRes = tv.resourceId;
1008 } else {
1009 tile.summary = tv.string;
1010 }
1011 }
1012 tile.iconRes = sa.getResourceId(
1013 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1014 tile.fragment = sa.getString(
1015 com.android.internal.R.styleable.PreferenceHeader_fragment);
1016 sa.recycle();
1017
1018 if (curBundle == null) {
1019 curBundle = new Bundle();
1020 }
1021
1022 final int innerDepth2 = parser.getDepth();
1023 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1024 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1025 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1026 continue;
1027 }
1028
1029 String innerNodeName2 = parser.getName();
1030 if (innerNodeName2.equals("extra")) {
1031 getResources().parseBundleExtra("extra", attrs, curBundle);
1032 XmlUtils.skipCurrentTag(parser);
1033
1034 } else if (innerNodeName2.equals("intent")) {
1035 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1036
1037 } else {
1038 XmlUtils.skipCurrentTag(parser);
1039 }
1040 }
1041
1042 if (curBundle.size() > 0) {
1043 tile.fragmentArguments = curBundle;
1044 curBundle = null;
1045 }
1046
PauloftheWest38155612014-06-30 10:02:36 -07001047 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001048 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001049 category.addTile(tile);
1050 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001051
1052 } else {
1053 XmlUtils.skipCurrentTag(parser);
1054 }
1055 }
1056
1057 target.add(category);
1058 } else {
1059 XmlUtils.skipCurrentTag(parser);
1060 }
1061 }
1062
1063 } catch (XmlPullParserException e) {
1064 throw new RuntimeException("Error parsing categories", e);
1065 } catch (IOException e) {
1066 throw new RuntimeException("Error parsing categories", e);
1067 } finally {
1068 if (parser != null) parser.close();
1069 }
1070 }
1071
1072 private void updateTilesList(List<DashboardCategory> target) {
1073 final boolean showDev = mDevelopmentPreferences.getBoolean(
1074 DevelopmentSettings.PREF_SHOW,
1075 android.os.Build.TYPE.equals("eng"));
1076
1077 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1078
1079 final int size = target.size();
1080 for (int i = 0; i < size; i++) {
1081
1082 DashboardCategory category = target.get(i);
1083
1084 // Ids are integers, so downcasting is ok
1085 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001086 int n = category.getTilesCount() - 1;
1087 while (n >= 0) {
1088
1089 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001090 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001091 id = (int) tile.id;
1092 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001093 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1094 removeTile = true;
1095 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001096 } else if (id == R.id.wifi_settings) {
1097 // Remove WiFi Settings if WiFi service is not available.
1098 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001099 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001100 }
1101 } else if (id == R.id.bluetooth_settings) {
1102 // Remove Bluetooth Settings if Bluetooth service is not available.
1103 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001104 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001105 }
1106 } else if (id == R.id.data_usage_settings) {
1107 // Remove data usage when kernel module not enabled
1108 final INetworkManagementService netManager = INetworkManagementService.Stub
1109 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1110 try {
1111 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001112 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001113 }
1114 } catch (RemoteException e) {
1115 // ignored
1116 }
1117 } else if (id == R.id.battery_settings) {
1118 // Remove battery settings when battery is not available. (e.g. TV)
1119
1120 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001121 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001122 }
1123 } else if (id == R.id.home_settings) {
1124 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001125 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001126 }
1127 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001128 boolean hasMultipleUsers =
1129 ((UserManager) getSystemService(Context.USER_SERVICE))
1130 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001131 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001132 || (!UserManager.supportsMultipleUsers()
1133 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001134 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001135 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001136 }
1137 } else if (id == R.id.nfc_payment_settings) {
1138 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001139 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001140 } else {
1141 // Only show if NFC is on and we have the HCE feature
1142 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1143 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1144 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001145 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001146 }
1147 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001148 } else if (id == R.id.print_settings) {
1149 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1150 PackageManager.FEATURE_PRINTING);
1151 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001152 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001153 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001154 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001155 if (!showDev || um.hasUserRestriction(
1156 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001157 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001158 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001159 }
1160
1161 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001162 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1163 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001164 }
1165
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001166 if (removeTile && n < category.getTilesCount()) {
1167 category.removeTile(n);
1168 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001169 n--;
1170 }
1171 }
1172 }
1173
1174 private boolean updateHomeSettingTiles(DashboardTile tile) {
1175 // Once we decide to show Home settings, keep showing it forever
1176 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1177 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1178 return true;
1179 }
1180
1181 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001182 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001183 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001184 // When there's only one available home app, omit this settings
1185 // category entirely at the top level UI. If the user just
1186 // uninstalled the penultimate home app candidiate, we also
1187 // now tell them about why they aren't seeing 'Home' in the list.
1188 if (sShowNoHomeNotice) {
1189 sShowNoHomeNotice = false;
1190 NoHomeDialogFragment.show(this);
1191 }
1192 return false;
1193 } else {
1194 // Okay, we're allowing the Home settings category. Tell it, when
1195 // invoked via this front door, that we'll need to be told about the
1196 // case when the user uninstalls all but one home app.
1197 if (tile.fragmentArguments == null) {
1198 tile.fragmentArguments = new Bundle();
1199 }
1200 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1201 }
1202 } catch (Exception e) {
1203 // Can't look up the home activity; bail on configuring the icon
1204 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1205 }
1206
1207 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1208 return true;
1209 }
1210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001211 private void getMetaData() {
1212 try {
1213 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1214 PackageManager.GET_META_DATA);
1215 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001216 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1217 } catch (NameNotFoundException nnfe) {
1218 // No recovery
1219 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1220 }
1221 }
1222
1223 // give subclasses access to the Next button
1224 public boolean hasNextButton() {
1225 return mNextButton != null;
1226 }
1227
1228 public Button getNextButton() {
1229 return mNextButton;
1230 }
1231
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001232 @Override
1233 public boolean shouldUpRecreateTask(Intent targetIntent) {
1234 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1235 }
1236
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001237 public static void requestHomeNotice() {
1238 sShowNoHomeNotice = true;
1239 }
1240
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001241 @Override
1242 public boolean onQueryTextSubmit(String query) {
1243 switchToSearchResultsFragmentIfNeeded();
1244 mSearchQuery = query;
1245 return mSearchResultsFragment.onQueryTextSubmit(query);
1246 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001247
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001248 @Override
1249 public boolean onQueryTextChange(String newText) {
1250 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001251 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001252 return false;
1253 }
1254 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001255 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001256
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001257 @Override
1258 public boolean onClose() {
1259 return false;
1260 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001261
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001262 @Override
1263 public boolean onMenuItemActionExpand(MenuItem item) {
1264 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001265 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001266 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001267 return true;
1268 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001269
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001270 @Override
1271 public boolean onMenuItemActionCollapse(MenuItem item) {
1272 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001273 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001274 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001275 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001276 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001277 return true;
1278 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001279
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001280 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001281 if (mSearchResultsFragment != null) {
1282 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001283 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001284 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001285 if (current != null && current instanceof SearchResultsSummary) {
1286 mSearchResultsFragment = (SearchResultsSummary) current;
1287 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001288 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001289 SearchResultsSummary.class.getName(), null, false, true,
1290 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001291 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001292 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001293 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001295
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001296 public void needToRevertToInitialFragment() {
1297 mNeedToRevertToInitialFragment = true;
1298 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001299
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001300 private void revertToInitialFragment() {
1301 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001302 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001303 mSearchMenuItemExpanded = false;
1304 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1305 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001306 if (mSearchMenuItem != null) {
1307 mSearchMenuItem.collapseActionView();
1308 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001309 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001310}