SettingsActivity.java revision d40dd45c18a55d574d65b3a0ac16b59d76027049
1263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio/*
2263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Copyright (C) 2014 The Android Open Source Project
3263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
4263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Licensed under the Apache License, Version 2.0 (the "License");
5263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * you may not use this file except in compliance with the License.
6263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * You may obtain a copy of the License at
7263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
8263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *      http://www.apache.org/licenses/LICENSE-2.0
9263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
10263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Unless required by applicable law or agreed to in writing, software
11263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * distributed under the License is distributed on an "AS IS" BASIS,
12263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * See the License for the specific language governing permissions and
14263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * limitations under the License.
15263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio */
16263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
17263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopackage com.android.settings;
18263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
19263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.ActionBar;
20263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.Activity;
21263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.Fragment;
22263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.FragmentManager;
23263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.FragmentTransaction;
24263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.BroadcastReceiver;
25d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.content.ComponentName;
26263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Context;
27263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Intent;
28263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.IntentFilter;
29263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.SharedPreferences;
30263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ActivityInfo;
31263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager;
32263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager.NameNotFoundException;
33263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ResolveInfo;
34263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.Configuration;
35263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.TypedArray;
36263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.XmlResourceParser;
37263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.nfc.NfcAdapter;
38263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Bundle;
39263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Handler;
40263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.INetworkManagementService;
41263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Message;
42263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.RemoteException;
43263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.ServiceManager;
44263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserHandle;
45263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserManager;
46263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.Preference;
47263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceFragment;
48263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceManager;
49263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceScreen;
50263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.text.TextUtils;
5159a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglioimport android.transition.TransitionManager;
52263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.AttributeSet;
53263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Log;
54263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.TypedValue;
55263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Xml;
56d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.Menu;
57d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.MenuInflater;
58263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.MenuItem;
59263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View;
60263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View.OnClickListener;
6159a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglioimport android.view.ViewGroup;
62263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.Button;
63d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.widget.SearchView;
64c7f8e8ce69e80ea495472aade5f8569c6583c5e8John Spurlock
65263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.ArrayUtils;
66263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.XmlUtils;
67263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.AccessibilitySettings;
68263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.CaptionPropertiesFragment;
69263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AccountSyncSettings;
70d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.applications.InstalledAppDetails;
71263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ManageApplications;
72263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ProcessStatsUi;
73263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.bluetooth.BluetoothSettings;
74c9dc66d6059a5bcc867ab2046c59f969a13c46d7PauloftheWestimport com.android.settings.bluetooth.MessageAccessSettings;
7563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglioimport com.android.settings.dashboard.DashboardCategory;
76263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.dashboard.DashboardSummary;
7763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglioimport com.android.settings.dashboard.DashboardTile;
78d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.NoHomeDialogFragment;
79d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.SearchResultsSummary;
80263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.Memory;
81263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.UsbSettings;
82c7f8e8ce69e80ea495472aade5f8569c6583c5e8John Spurlockimport com.android.settings.fuelgauge.BatterySaverSettings;
83263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.fuelgauge.PowerUsageSummary;
847a6bfd132cac4dcff72e3adcdd68a7ef777cee12Fabrice Di Meglioimport com.android.settings.search.DynamicIndexableContentMonitor;
85fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglioimport com.android.settings.search.Index;
86263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.InputMethodAndLanguageSettings;
87263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
88263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.SpellCheckersSettings;
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.UserDictionaryList;
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.location.LocationSettings;
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.AndroidBeam;
92263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.PaymentSettings;
93c9afadb04b00598f5988101681089290cf14f79cJohn Spurlockimport com.android.settings.notification.ConditionProviderSettings;
944a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationAccessSettings;
954a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationSettings;
964a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationStation;
974a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.ZenModeSettings;
98263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintJobSettingsFragment;
99263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintSettingsFragment;
100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.tts.TextToSpeechSettings;
101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.users.UserSettings;
102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.vpn2.VpnSettings;
103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wfd.WifiDisplaySettings;
10441937766981423c9252e12e3319b2e7532739627Fabrice Di Meglioimport com.android.settings.widget.SwitchBar;
105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.AdvancedWifiSettings;
1067837b999862cf341b0f124be7ace1dfc64d0a5a8PauloftheWestimport com.android.settings.wifi.SavedAccessPointsWifiSettings;
107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiSettings;
108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.p2p.WifiP2pSettings;
109c7f8e8ce69e80ea495472aade5f8569c6583c5e8John Spurlock
110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParser;
111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParserException;
112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.io.IOException;
114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.ArrayList;
115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.List;
1160d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglioimport java.util.Set;
117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
118e9326d276135b657738d851e0a56df447f09567aFabrice Di Meglioimport static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
119d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
120263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopublic class SettingsActivity extends Activity
121263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        implements PreferenceManager.OnPreferenceTreeClickListener,
122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        PreferenceFragment.OnPreferenceStartFragmentCallback,
123cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina        ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
124d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        SearchView.OnQueryTextListener, SearchView.OnCloseListener,
125cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina        MenuItem.OnActionExpandListener {
126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String LOG_TAG = "Settings";
128263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
129263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Constants for state save/restore
130769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
131d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
132d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
133b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio    private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
1343d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio    private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * string to specify which fragment should be initially displayed.
139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will call isValidFragment() to confirm that the fragment class name is valid for this
141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity.
142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specified to supply a Bundle of arguments to pass
148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to that fragment when it is instantiated during the initial creation
149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * of the activity.
150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
154c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
155c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     */
156c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio    public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
157c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
1586f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio    public static final String BACK_STACK_PREFS = ":settings:prefs";
1596f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // extras that allow any preference activity to be launched as part of a wizard
161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // show Back and Next buttons? takes boolean parameter
163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Back will then return RESULT_CANCELED and Next RESULT_OK
164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // add a Skip button?
167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // specify custom text for the Back or Next buttons, or cause a button to not appear
170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // at all by setting it to null
171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
176a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio     * those extra can also be specify to supply the title or title res id to be shown for
177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that fragment.
178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
179d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
180a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
1810d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_FRAGMENT_CLASS =
184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.FRAGMENT_CLASS";
185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
188d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio    private static final String EMPTY_QUERY = "";
189d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static boolean sShowNoHomeNotice = false;
191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String mFragmentClass;
193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1948eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private CharSequence mInitialTitle;
195a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio    private int mInitialTitleResId;
1968eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Show only these settings for restricted users
198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int[] SETTINGS_FOR_RESTRICTED = {
199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_section,
200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wifi_settings,
201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.bluetooth_settings,
202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.data_usage_settings,
203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_settings,
204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.device_section,
2054e4cdeffdb6087c953f44808f14f18554a749bdaJohn Spurlock            R.id.notification_settings,
206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.display_settings,
207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.storage_settings,
208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.application_settings,
209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.battery_settings,
210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.personal_section,
211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.location_settings,
212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.security_settings,
213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.language_settings,
214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.user_settings,
215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_settings,
216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.system_section,
217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.date_time_settings,
218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.about_settings,
219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.accessibility_settings,
220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.print_settings,
221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.nfc_payment_settings,
2222858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.home_settings,
2232858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.dashboard
224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String[] ENTRY_FRAGMENTS = {
227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WirelessSettings.class.getName(),
228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiSettings.class.getName(),
229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AdvancedWifiSettings.class.getName(),
2307837b999862cf341b0f124be7ace1dfc64d0a5a8PauloftheWest            SavedAccessPointsWifiSettings.class.getName(),
231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            BluetoothSettings.class.getName(),
232c9dc66d6059a5bcc867ab2046c59f969a13c46d7PauloftheWest            MessageAccessSettings.class.getName(),
233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TetherSettings.class.getName(),
234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiP2pSettings.class.getName(),
235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            VpnSettings.class.getName(),
236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DateTimeSettings.class.getName(),
237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocalePicker.class.getName(),
238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            InputMethodAndLanguageSettings.class.getName(),
239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SpellCheckersSettings.class.getName(),
240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionaryList.class.getName(),
241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionarySettings.class.getName(),
2423033ce0723103b55b1b5a53145c05e143b5bbfd6Amith Yamasani            HomeSettings.class.getName(),
243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DisplaySettings.class.getName(),
244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceInfoSettings.class.getName(),
245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageApplications.class.getName(),
246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ProcessStatsUi.class.getName(),
247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationStation.class.getName(),
248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocationSettings.class.getName(),
249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SecuritySettings.class.getName(),
250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrivacySettings.class.getName(),
251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceAdminSettings.class.getName(),
252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccessibilitySettings.class.getName(),
253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CaptionPropertiesFragment.class.getName(),
254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextToSpeechSettings.class.getName(),
257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Memory.class.getName(),
258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DevelopmentSettings.class.getName(),
259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UsbSettings.class.getName(),
260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AndroidBeam.class.getName(),
261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiDisplaySettings.class.getName(),
262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PowerUsageSummary.class.getName(),
263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountSyncSettings.class.getName(),
264263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CryptKeeperSettings.class.getName(),
265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DataUsageSummary.class.getName(),
266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DreamSettings.class.getName(),
267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserSettings.class.getName(),
268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationAccessSettings.class.getName(),
269c9afadb04b00598f5988101681089290cf14f79cJohn Spurlock            ConditionProviderSettings.class.getName(),
270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintSettingsFragment.class.getName(),
271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintJobSettingsFragment.class.getName(),
272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TrustedCredentialsSettings.class.getName(),
273263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PaymentSettings.class.getName(),
274263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            KeyboardLayoutPickerFragment.class.getName(),
275d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ZenModeSettings.class.getName(),
276d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            NotificationSettings.class.getName(),
277d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
278d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
279c7f8e8ce69e80ea495472aade5f8569c6583c5e8John Spurlock            InstalledAppDetails.class.getName(),
280c7f8e8ce69e80ea495472aade5f8569c6583c5e8John Spurlock            BatterySaverSettings.class.getName(),
281263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
282263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
2835a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio
2845a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio    private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
2855a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio            "android.settings.APPLICATION_DETAILS_SETTINGS"
2865a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio    };
2875a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio
288263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences mDevelopmentPreferences;
289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mBatteryPresent = true;
292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onReceive(Context context, Intent intent) {
296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String action = intent.getAction();
297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                boolean batteryPresent = Utils.isBatteryPresent(intent);
299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mBatteryPresent != batteryPresent) {
301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mBatteryPresent = batteryPresent;
302769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    invalidateCategories();
303263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
304263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
305263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
306263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
307263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
308990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav    private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
309990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav            new DynamicIndexableContentMonitor();
310853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
311d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private ActionBar mActionBar;
31241937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    private SwitchBar mSwitchBar;
31341937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
31441937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    private Button mNextButton;
3153d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio
316b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio    private boolean mDisplayHomeAsUpEnabled;
3173d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio    private boolean mDisplaySearch;
318d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
31935062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio    private boolean mIsShowingDashboard;
3200d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio    private boolean mIsShortcut;
32135062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio
32259a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglio    private ViewGroup mContent;
32359a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglio
324d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchView mSearchView;
325d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private MenuItem mSearchMenuItem;
326d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mSearchMenuItemExpanded = false;
327d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchResultsSummary mSearchResultsFragment;
328d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private String mSearchQuery;
329d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
330769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    // Categories
331769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
332769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private boolean mNeedToRebuildCategories;
333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
334769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private static final int MSG_BUILD_CATEGORIES = 1;
335263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Handler mHandler = new Handler() {
336263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void handleMessage(Message msg) {
338263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (msg.what) {
339769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                case MSG_BUILD_CATEGORIES: {
34042c4b0abe5578a7812844ad5205ffc3b87cf3072Fabrice Di Meglio                    if(mNeedToRebuildCategories) {
34142c4b0abe5578a7812844ad5205ffc3b87cf3072Fabrice Di Meglio                        buildDashboardCategories(mCategories);
34242c4b0abe5578a7812844ad5205ffc3b87cf3072Fabrice Di Meglio                    }
343263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } break;
344263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
345263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
346263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
348d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mNeedToRevertToInitialFragment = false;
349d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
35041937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    public SwitchBar getSwitchBar() {
35141937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio        return mSwitchBar;
35241937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    }
35341937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
354769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    public List<DashboardCategory> getDashboardCategories() {
35542c4b0abe5578a7812844ad5205ffc3b87cf3072Fabrice Di Meglio        if (mNeedToRebuildCategories || mCategories.size() == 0) {
356769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            buildDashboardCategories(mCategories);
357769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            mNeedToRebuildCategories = false;
358769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
359769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        return mCategories;
360769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
361769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Override the fragment title for Wallpaper settings
365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int titleRes = pref.getTitleRes();
366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = R.string.wallpaper_settings_fragment_title;
368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                && UserHandle.myUserId() != UserHandle.USER_OWNER) {
370263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (UserManager.get(this).isLinkedUser()) {
371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.profile_info_settings_title;
372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.user_info_settings_title;
374263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
375263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
376263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
377263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                null, 0);
378263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
379263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
380263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
381263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
382263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
383263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
384263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
385263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
386769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void invalidateCategories() {
387769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
388769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
389263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
390263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
391263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
392263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
393263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onConfigurationChanged(Configuration newConfig) {
394263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onConfigurationChanged(newConfig);
3956f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio        Index.getInstance(this).update();
396263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
397263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
398263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
399d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onStart() {
400d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onStart();
401d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
402d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mNeedToRevertToInitialFragment) {
403d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
404263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
405263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
406263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
407263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
408d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onCreateOptionsMenu(Menu menu) {
4093d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        if (!mDisplaySearch) {
4103d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            return false;
4113d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        }
4123d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio
413d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuInflater inflater = getMenuInflater();
414d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        inflater.inflate(R.menu.options_menu, menu);
415d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
416d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        // Cache the search query (can be overriden by the OnQueryTextListener)
417d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        final String query = mSearchQuery;
418d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
4199593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem = menu.findItem(R.id.search);
4209593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchView = (SearchView) mSearchMenuItem.getActionView();
421d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
42223ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem == null || mSearchView == null) {
42323ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            return false;
42423ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
42523ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio
4268c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio        if (mSearchResultsFragment != null) {
4278c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio            mSearchResultsFragment.setSearchView(mSearchView);
4288c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio        }
4298c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio
4309593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem.setOnActionExpandListener(this);
431d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnQueryTextListener(this);
432d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnCloseListener(this);
433d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
434d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mSearchMenuItemExpanded) {
435d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItem.expandActionView();
436d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
437d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setQuery(query, true /* submit */);
438d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
439d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
440d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
441d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
4420d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio    private static boolean isShortCutIntent(final Intent intent) {
4430d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        Set<String> categories = intent.getCategories();
4440d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
4450d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio    }
4460d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio
4475b3c3c00b0a280e29f3767878913f80274bd4993Fabrice Di Meglio    private static boolean isLikeShortCutIntent(final Intent intent) {
4485b7a100dbd9135b6b87ae4cc9ff3a5ece123bcf6Fabrice Di Meglio        String action = intent.getAction();
4495a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        if (action == null) {
4505a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio            return false;
4515a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        }
4525a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
4535a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio            if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
4545a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        }
4555a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        return false;
4565b7a100dbd9135b6b87ae4cc9ff3a5ece123bcf6Fabrice Di Meglio    }
4575b7a100dbd9135b6b87ae4cc9ff3a5ece123bcf6Fabrice Di Meglio
458d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
459d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onCreate(Bundle savedState) {
4600d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        super.onCreate(savedState);
4610d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio
4620d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        // Should happen before any call to getIntent()
4630d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        getMetaData();
4640d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio
4650d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        final Intent intent = getIntent();
4660d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
4670d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio            getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
468263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
469263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
470263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
471263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Context.MODE_PRIVATE);
472263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
473da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio        // Getting Intent properties can only be done after the super.onCreate(...)
4740d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio        final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
4750d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio
4765b3c3c00b0a280e29f3767878913f80274bd4993Fabrice Di Meglio        mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
4770d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
478da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio
479e817a66e83191c0ae74acd53a538a0965eba2683Fabrice Di Meglio        final ComponentName cn = intent.getComponent();
4805a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        final String className = cn.getClassName();
481da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio
4825a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        mIsShowingDashboard = className.equals(Settings.class.getName());
4835a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        final boolean isSubSettings = className.equals(SubSettings.class.getName());
4845a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio
4855a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        // If this is a sub settings or not the main Dashboard and not a Shortcut and not initial
4865a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        // Fragment then apply the correct theme for the ActionBar content inset
4875a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio        if (isSubSettings ||
4885a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName == null))) {
489da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio            setTheme(R.style.Theme_SubSettings);
490da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio        }
491da8babaedf45caf55eb77320f0c3ea873d195c4dFabrice Di Meglio
492d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio        setContentView(mIsShowingDashboard ?
493d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio                R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
494263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
495d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio        mContent = (ViewGroup) findViewById(R.id.main_content);
49659a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglio
497263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getFragmentManager().addOnBackStackChangedListener(this);
498263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
49935062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio        if (mIsShowingDashboard) {
500dba577f8f017baac0086c185579340db1c9281d9Fabrice Di Meglio            Index.getInstance(getApplicationContext()).update();
5015cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        }
5025cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio
503d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (savedState != null) {
5041800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // We are restarting from a previous saved state; used that to initialize, instead
5051800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // of starting fresh.
506d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
507d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
508263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
5090d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio            setTitleFromIntent(intent);
5105529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
511769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            ArrayList<DashboardCategory> categories =
512769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
513769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            if (categories != null) {
5145f995727cc2e4bd27d26a50f3a565fb677fdb294Fabrice Di Meglio                mCategories.clear();
515769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                mCategories.addAll(categories);
516b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio                setTitleFromBackStack();
517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
518b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio
519b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio            mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
5203d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else {
52235062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio            if (!mIsShowingDashboard) {
5230d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                // Search is shown we are launched thru a Settings "shortcut". UP will be shown
5240d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                // only if it is a sub settings
5250d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                if (mIsShortcut) {
5260d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                    mDisplayHomeAsUpEnabled = isSubSettings;
5273d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio                    mDisplaySearch = false;
5285a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                } else if (isSubSettings) {
5295a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                    mDisplayHomeAsUpEnabled = true;
5305a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                    mDisplaySearch = true;
531d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                }
5320d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                setTitleFromIntent(intent);
533c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
5340d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
535a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                switchToFragment(initialFragmentName, initialArguments, true, false,
536a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                        mInitialTitleResId, mInitialTitle, false);
53710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            } else {
5385a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                // No UP affordance if we are displaying the main Dashboard
539b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio                mDisplayHomeAsUpEnabled = false;
5405a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                // Show Search affordance
5415a62d94a2b1b5a93911e1f2cc9f3190e2f22622cFabrice Di Meglio                mDisplaySearch = true;
542a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                mInitialTitleResId = R.string.dashboard_title;
54342c4b0abe5578a7812844ad5205ffc3b87cf3072Fabrice Di Meglio                switchToFragment(DashboardSummary.class.getName(), null, false, false,
544a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                        mInitialTitleResId, mInitialTitle, false);
545263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
546263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
547263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
548d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar = getActionBar();
549d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio        if (mActionBar != null) {
550d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio            mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
551d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio            mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
552d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio        }
55341937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio        mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
55441937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
555263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // see if we should show Back/Next buttons
556263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
557263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
558d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio            View buttonBar = findViewById(R.id.button_bar);
559263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (buttonBar != null) {
560263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                buttonBar.setVisibility(View.VISIBLE);
561263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
562d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                Button backButton = (Button)findViewById(R.id.back_button);
563263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                backButton.setOnClickListener(new OnClickListener() {
564263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
565263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_CANCELED);
566263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
567263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
568263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
569d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                Button skipButton = (Button)findViewById(R.id.skip_button);
570263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                skipButton.setOnClickListener(new OnClickListener() {
571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
572263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
573263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
574263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
575263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
576d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                mNextButton = (Button)findViewById(R.id.next_button);
577263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton.setOnClickListener(new OnClickListener() {
578263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
579263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
580263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
581263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
582263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
583263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
584263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // set our various button parameters
585263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
586263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
587263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
588263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setVisibility(View.GONE);
589263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
590263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
591263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setText(buttonText);
592263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
593263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
594263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
595263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
596263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
597263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setVisibility(View.GONE);
598263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
599263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
600263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setText(buttonText);
601263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
602263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
603263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
604263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    skipButton.setVisibility(View.VISIBLE);
605263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
606263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
607263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
608c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    }
609c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio
610a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio    private void setTitleFromIntent(Intent intent) {
611a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
612a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        if (initialTitleResId > 0) {
613a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            mInitialTitle = null;
614a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            mInitialTitleResId = initialTitleResId;
615a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            setTitle(mInitialTitleResId);
616a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        } else {
617a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            mInitialTitleResId = -1;
618a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
619a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
620a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            setTitle(mInitialTitle);
621a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        }
622a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio    }
623a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio
624c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    @Override
625263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onBackStackChanged() {
626d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        setTitleFromBackStack();
6278eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    }
6288eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
629b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio    private int setTitleFromBackStack() {
6308eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int count = getFragmentManager().getBackStackEntryCount();
631b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
6328eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (count == 0) {
633a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            if (mInitialTitleResId > 0) {
634a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                setTitle(mInitialTitleResId);
635a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            } else {
636a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                setTitle(mInitialTitle);
637a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            }
638b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio            return 0;
639263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
640b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
6418eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
6428eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        setTitleFromBackStackEntry(bse);
643b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
644b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio        return count;
645263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
646263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
6478eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
6485529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        final CharSequence title;
6498eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int titleRes = bse.getBreadCrumbTitleRes();
6508eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (titleRes > 0) {
6518eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = getText(titleRes);
6525529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        } else {
6538eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = bse.getBreadCrumbTitle();
6548eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        }
6558eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (title != null) {
6568eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(title);
6575529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        }
6585529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio    }
6595529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
660263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
661263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onSaveInstanceState(Bundle outState) {
662263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onSaveInstanceState(outState);
663263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
664769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (mCategories.size() > 0) {
665769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
666263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
667d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
668b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
6693d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
6703d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio
6713d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        if (mDisplaySearch) {
6723d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            // The option menus are created if the ActionBar is visible and they are also created
6733d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            // asynchronously. If you launch Settings with an Intent action like
6743d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
6753d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
6763d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            // menu item and search view are null.
6773d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
6783d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
6793d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio
6803d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
6813d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio            outState.putString(SAVE_KEY_SEARCH_QUERY, query);
6823d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        }
683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onResume() {
687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onResume();
688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
692bb05178dba21509d5b26ae0bd718a471b8c6cd5eFabrice Di Meglio                setNeedToRebuildCategories(true);
693769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                invalidateCategories();
694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
700853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
701990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.register(this);
702a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio
7033d35ec729256d4a1e1796be3e9d858eefedda23dFabrice Di Meglio        if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
704a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            onQueryTextSubmit(mSearchQuery);
705a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onPause() {
710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPause();
711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        unregisterReceiver(mBatteryInfoReceiver);
713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
714990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.unregister();
715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onDestroy() {
719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onDestroy();
720680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio
721680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio        mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
722680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio                mDevelopmentPreferencesListener);
723680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio        mDevelopmentPreferencesListener = null;
724263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
725263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
726263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected boolean isValidFragment(String fragmentName) {
727263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Almost all fragments are wrapped in this,
728263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // except for a few that have their own activities.
729263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
730263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
731263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
732263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
733263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
734263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
735263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
736263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Intent getIntent() {
737263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent superIntent = super.getIntent();
738263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String startingFragment = getStartingFragmentClass(superIntent);
739263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // This is called from super.onCreate, isMultiPane() is not yet reliable
740263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Do not use onIsHidingHeaders either, which relies itself on this method
741263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (startingFragment != null) {
742263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Intent modIntent = new Intent(superIntent);
743263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
744263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle args = superIntent.getExtras();
745263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (args != null) {
746263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle(args);
747263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
748263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle();
749263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
750263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            args.putParcelable("intent", superIntent);
751ac1e20ebfc233a4894fa04a29b5d86602ee188eeKenny Guy            modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
752263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return modIntent;
753263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
754263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return superIntent;
755263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
756263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
757263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
758263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Checks if the component name in the intent is different from the Settings class and
759263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * returns the class name to load as a fragment.
760263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
761263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String getStartingFragmentClass(Intent intent) {
762263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mFragmentClass != null) return mFragmentClass;
763263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
764263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String intentClass = intent.getComponent().getClassName();
765263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intentClass.equals(getClass().getName())) return null;
766263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
767263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ("com.android.settings.ManageApplications".equals(intentClass)
768263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.RunningServices".equals(intentClass)
769263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.applications.StorageUse".equals(intentClass)) {
770263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Old names of manage apps.
771263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            intentClass = com.android.settings.applications.ManageApplications.class.getName();
772263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
773263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
774263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return intentClass;
775263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
776263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
777263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
77810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment containing a preference panel.  If the preferences
77910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * are being displayed in multi-pane mode, the given fragment class will
78010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * be instantiated and placed in the appropriate pane.  If running in
78110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * single-pane mode, a new activity will be launched in which to show the
78210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
78310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
78410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentClass Full name of the class implementing the fragment.
78510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Any desired arguments to supply to the fragment.
78610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Optional resource identifier of the title of this
78710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
78810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText Optional text of the title of this fragment.
78910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Optional fragment that result data should be sent to.
79010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * If non-null, resultTo.onActivityResult() will be called when this
79110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference panel is done.  The launched panel must use
79210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
79310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the caller's
79458146c2b2fa24d774840a07cf697ad315baf4de0Fabrice Di Meglio     * request code to be received with the result.
79510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
79610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
797d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            CharSequence titleText, Fragment resultTo, int resultRequestCode) {
798a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        String title = null;
799a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        if (titleRes < 0) {
800a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            if (titleText != null) {
801a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                title = titleText.toString();
802a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            } else {
803a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                // There not much we can do in that case
804a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                title = "";
805a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            }
806911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        }
807a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
8080d643fd182d37d2e8ee5d547b80d80423a5ed98dFabrice Di Meglio                titleRes, title, mIsShortcut);
80910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
81010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
81110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
812263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called by a preference panel fragment to finish itself.
813263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
814263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param caller The fragment that is asking to be finished.
815263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultCode Optional result code to send back to the original
816263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
817263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultData Optional result data to send back to the original
818263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
819263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
820263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
821263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setResult(resultCode, resultData);
82258146c2b2fa24d774840a07cf697ad315baf4de0Fabrice Di Meglio        finish();
823263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
824263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
825263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
82610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
82710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
82810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragment The fragment to start
82910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
83010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * the current fragment will be replaced.
83110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
83210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferenceFragment(Fragment fragment, boolean push) {
83310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
834d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio        transaction.replace(R.id.main_content, fragment);
83510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (push) {
83610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
83710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.addToBackStack(BACK_STACK_PREFS);
83810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
83910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
84010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
84110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
84210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
84310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
84410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
845d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Switch to a specific Fragment with taking care of validation, Title and BackStack
846d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
847d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
848a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
849d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (validate && !isValidFragment(fragmentName)) {
850d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            throw new IllegalArgumentException("Invalid fragment for this activity: "
851d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    + fragmentName);
852d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
853d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
854d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
855d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio        transaction.replace(R.id.main_content, f);
856d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (withTransition) {
85759a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglio            TransitionManager.beginDelayedTransition(mContent);
858d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
859d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (addToBackStack) {
860d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
861d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
862a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        if (titleResId > 0) {
863a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio            transaction.setBreadCrumbTitle(titleResId);
864a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio        } else if (title != null) {
865d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setBreadCrumbTitle(title);
866d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
867d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.commitAllowingStateLoss();
86859a4055cbb8f5a7b1b0c904bbc4ffc03d8a1239fFabrice Di Meglio        getFragmentManager().executePendingTransactions();
869d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return f;
870d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
871d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
872769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    public void setNeedToRebuildCategories(boolean need) {
873769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        mNeedToRebuildCategories = need;
874263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
875263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
87663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    /**
877769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio     * Called when the activity needs its list of categories/tiles built.
87863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     *
87963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param categories The list in which to place the tiles categories.
88063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     */
881769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void buildDashboardCategories(List<DashboardCategory> categories) {
8825f995727cc2e4bd27d26a50f3a565fb677fdb294Fabrice Di Meglio        categories.clear();
88363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        loadCategoriesFromResource(R.xml.dashboard_categories, categories);
88463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        updateTilesList(categories);
88563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
88663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
88763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    /**
88863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * Parse the given XML file as a categories description, adding each
88963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * parsed categories and tiles into the target list.
89063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     *
89163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param resid The XML resource to load and parse.
89263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param target The list in which the parsed categories and tiles should be placed.
89363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     */
89463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
89563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        XmlResourceParser parser = null;
89663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        try {
89763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            parser = getResources().getXml(resid);
89863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            AttributeSet attrs = Xml.asAttributeSet(parser);
89963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
90063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int type;
90163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
90263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    && type != XmlPullParser.START_TAG) {
90363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // Parse next until start tag is found
90463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
90563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
90663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            String nodeName = parser.getName();
90763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (!"dashboard-categories".equals(nodeName)) {
90863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                throw new RuntimeException(
90963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        "XML document must start with <preference-categories> tag; found"
91063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                + nodeName + " at " + parser.getPositionDescription());
91163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
91263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
91363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            Bundle curBundle = null;
91463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
91563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            final int outerDepth = parser.getDepth();
91663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
91763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
91863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
91963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    continue;
92063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
92163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
92263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                nodeName = parser.getName();
92363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if ("dashboard-category".equals(nodeName)) {
92463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    DashboardCategory category = new DashboardCategory();
92563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
92663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    TypedArray sa = obtainStyledAttributes(
92763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            attrs, com.android.internal.R.styleable.PreferenceHeader);
92863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    category.id = sa.getResourceId(
92963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_id,
93063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            (int)DashboardCategory.CAT_ID_UNDEFINED);
93163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
93263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    TypedValue tv = sa.peekValue(
93363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_title);
93463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
93563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (tv.resourceId != 0) {
93663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.titleRes = tv.resourceId;
93763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        } else {
93863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.title = tv.string;
93963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
94063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
94163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    sa.recycle();
94263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
94363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    final int innerDepth = parser.getDepth();
94463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
94563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
94663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
94763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            continue;
94863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
94963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
95063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        String innerNodeName = parser.getName();
95163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (innerNodeName.equals("dashboard-tile")) {
95263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            DashboardTile tile = new DashboardTile();
95363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
95463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            sa = obtainStyledAttributes(
95563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    attrs, com.android.internal.R.styleable.PreferenceHeader);
95663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.id = sa.getResourceId(
95763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_id,
958e9326d276135b657738d851e0a56df447f09567aFabrice Di Meglio                                    (int)TILE_ID_UNDEFINED);
95963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tv = sa.peekValue(
96063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_title);
96163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (tv != null && tv.type == TypedValue.TYPE_STRING) {
96263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (tv.resourceId != 0) {
96363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.titleRes = tv.resourceId;
96463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
96563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.title = tv.string;
96663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
96763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
96863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tv = sa.peekValue(
96963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_summary);
97063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (tv != null && tv.type == TypedValue.TYPE_STRING) {
97163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (tv.resourceId != 0) {
97263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.summaryRes = tv.resourceId;
97363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
97463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.summary = tv.string;
97563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
97663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
97763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.iconRes = sa.getResourceId(
97863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_icon, 0);
97963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.fragment = sa.getString(
98063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_fragment);
98163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            sa.recycle();
98263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
98363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (curBundle == null) {
98463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                curBundle = new Bundle();
98563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
98663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
98763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            final int innerDepth2 = parser.getDepth();
98863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
98963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
99063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
99163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    continue;
99263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
99363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
99463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                String innerNodeName2 = parser.getName();
99563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (innerNodeName2.equals("extra")) {
99663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    getResources().parseBundleExtra("extra", attrs, curBundle);
99763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    XmlUtils.skipCurrentTag(parser);
99863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
99963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else if (innerNodeName2.equals("intent")) {
100063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.intent = Intent.parseIntent(getResources(), parser, attrs);
100163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
100263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
100363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    XmlUtils.skipCurrentTag(parser);
100463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
100563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
100663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
100763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (curBundle.size() > 0) {
100863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                tile.fragmentArguments = curBundle;
100963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                curBundle = null;
101063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
101163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
101263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.addTile(tile);
101363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
101463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        } else {
101563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
101663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
101763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
101863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
101963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    target.add(category);
102063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else {
102163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    XmlUtils.skipCurrentTag(parser);
102263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
102363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
102463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
102563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (XmlPullParserException e) {
102663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            throw new RuntimeException("Error parsing categories", e);
102763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (IOException e) {
102863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            throw new RuntimeException("Error parsing categories", e);
102963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } finally {
103063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (parser != null) parser.close();
103163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
103263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
103363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
103463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private void updateTilesList(List<DashboardCategory> target) {
103563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final boolean showDev = mDevelopmentPreferences.getBoolean(
103663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                DevelopmentSettings.PREF_SHOW,
103763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                android.os.Build.TYPE.equals("eng"));
103863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
103963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
104063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
104163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final int size = target.size();
104263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        for (int i = 0; i < size; i++) {
104363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
104463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            DashboardCategory category = target.get(i);
104563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
104663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            // Ids are integers, so downcasting is ok
104763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int id = (int) category.id;
104863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int n = category.getTilesCount() - 1;
104963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while (n >= 0) {
105063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
105163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                DashboardTile tile = category.getTile(n);
105263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
105363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                id = (int) tile.id;
105463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
105563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
105663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.wifi_settings) {
105763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove WiFi Settings if WiFi service is not available.
105863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
105963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
106063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
106163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.bluetooth_settings) {
106263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove Bluetooth Settings if Bluetooth service is not available.
106363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
106463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
106563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
106663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.data_usage_settings) {
106763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove data usage when kernel module not enabled
106863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    final INetworkManagementService netManager = INetworkManagementService.Stub
106963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
107063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    try {
107163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (!netManager.isBandwidthControlEnabled()) {
107263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.removeTile(n);
107363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
107463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    } catch (RemoteException e) {
107563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        // ignored
107663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
107763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.battery_settings) {
107863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove battery settings when battery is not available. (e.g. TV)
107963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
108063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!mBatteryPresent) {
108163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
108263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
108363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.home_settings) {
108463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!updateHomeSettingTiles(tile)) {
108563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
108663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
108763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.user_settings) {
10884093e40e18f2e8d395ca467f54aef6e763c394f6Amith Yamasani                    boolean hasMultipleUsers =
10894093e40e18f2e8d395ca467f54aef6e763c394f6Amith Yamasani                            ((UserManager) getSystemService(Context.USER_SERVICE))
10904093e40e18f2e8d395ca467f54aef6e763c394f6Amith Yamasani                                    .getUserCount() > 1;
109163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!UserHandle.MU_ENABLED
10924093e40e18f2e8d395ca467f54aef6e763c394f6Amith Yamasani                            || (!UserManager.supportsMultipleUsers()
10934093e40e18f2e8d395ca467f54aef6e763c394f6Amith Yamasani                                    && !hasMultipleUsers)
109463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            || Utils.isMonkeyRunning()) {
109563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
109663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
109763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.nfc_payment_settings) {
109863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
109963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
110063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    } else {
110163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        // Only show if NFC is on and we have the HCE feature
110263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
110363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
110463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
110563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.removeTile(n);
110663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
110763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
1108488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                } else if (id == R.id.print_settings) {
1109488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1110488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                            PackageManager.FEATURE_PRINTING);
1111488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    if (!hasPrintingSupport) {
1112488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                        category.removeTile(n);
1113488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    }
111463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.development_settings) {
11156c088cb221bb43d47be065387231ffa7192ff211Julia Reynolds                    if (!showDev || um.hasUserRestriction(
11166c088cb221bb43d47be065387231ffa7192ff211Julia Reynolds                            UserManager.DISALLOW_DEBUGGING_FEATURES)) {
111763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
111863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
111963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
112063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
112163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
1122a97089d389338ca8e3d970b00dd13ffad732b5c1Amith Yamasani                        && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1123a97089d389338ca8e3d970b00dd13ffad732b5c1Amith Yamasani                        && n < category.getTilesCount()) {
112463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    category.removeTile(n);
112563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
112663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
112763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                n--;
112863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
112963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
113063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
113163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
113263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private boolean updateHomeSettingTiles(DashboardTile tile) {
113363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        // Once we decide to show Home settings, keep showing it forever
113463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
113563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
113663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            return true;
113763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
113863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
113963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        try {
114063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
114163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            getPackageManager().getHomeActivities(homeApps);
114263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (homeApps.size() < 2) {
114363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // When there's only one available home app, omit this settings
114463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // category entirely at the top level UI.  If the user just
114563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // uninstalled the penultimate home app candidiate, we also
114663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // now tell them about why they aren't seeing 'Home' in the list.
114763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (sShowNoHomeNotice) {
114863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    sShowNoHomeNotice = false;
114963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    NoHomeDialogFragment.show(this);
115063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
115163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                return false;
115263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            } else {
115363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // Okay, we're allowing the Home settings category.  Tell it, when
115463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // invoked via this front door, that we'll need to be told about the
115563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // case when the user uninstalls all but one home app.
115663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (tile.fragmentArguments == null) {
115763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    tile.fragmentArguments = new Bundle();
115863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
115963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
116063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
116163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (Exception e) {
116263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            // Can't look up the home activity; bail on configuring the icon
116363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            Log.w(LOG_TAG, "Problem looking up home activity!", e);
116463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
116563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
116663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
116763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        return true;
116863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
116963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
1170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void getMetaData() {
1171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    PackageManager.GET_META_DATA);
1174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ai == null || ai.metaData == null) return;
1175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (NameNotFoundException nnfe) {
1177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // No recovery
1178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // give subclasses access to the Next button
1183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean hasNextButton() {
1184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton != null;
1185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Button getNextButton() {
1188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton;
1189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean shouldUpRecreateTask(Intent targetIntent) {
1193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static void requestHomeNotice() {
1197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sShowNoHomeNotice = true;
1198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1200d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1201d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextSubmit(String query) {
1202d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        switchToSearchResultsFragmentIfNeeded();
1203d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = query;
1204d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextSubmit(query);
1205d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1207d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1208d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextChange(String newText) {
1209d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = newText;
12107e4855e8f644bdecf68ec65b8c1b7c08624a4876Fabrice Di Meglio        if (mSearchResultsFragment == null) {
1211a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            return false;
1212a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
1213a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextChange(newText);
1214d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1216d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1217d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onClose() {
1218d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return false;
1219d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1221d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1222d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionExpand(MenuItem item) {
1223d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1224bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            switchToSearchResultsFragmentIfNeeded();
1225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1226d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1227d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1229d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1230d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionCollapse(MenuItem item) {
1231d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1232bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            if (mSearchMenuItemExpanded) {
1233d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                revertToInitialFragment();
1234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1236d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1237d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1239d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void switchToSearchResultsFragmentIfNeeded() {
1240bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (mSearchResultsFragment != null) {
1241bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            return;
1242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1243d40dd45c18a55d574d65b3a0ac16b59d76027049Fabrice Di Meglio        Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
1244bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (current != null && current instanceof SearchResultsSummary) {
1245bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) current;
1246bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        } else {
1247bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
1248a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                    SearchResultsSummary.class.getName(), null, false, true,
1249a9e77993d1f82a230ff381134d4a22eff4a907cdFabrice Di Meglio                    R.string.search_results_title, null, true);
1250bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        }
1251d297a5840230b769a3c7ad8b85232968a7077f64Fabrice Di Meglio        mSearchResultsFragment.setSearchView(mSearchView);
1252bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = true;
1253d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1255d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void needToRevertToInitialFragment() {
1256d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = true;
1257d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1259d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void revertToInitialFragment() {
1260d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = false;
1261d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchResultsFragment = null;
1262bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = false;
1263bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1264bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio                FragmentManager.POP_BACK_STACK_INCLUSIVE);
126523ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem != null) {
126623ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            mSearchMenuItem.collapseActionView();
126723ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
1268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
1270