SettingsActivity.java revision 7e4855e8f644bdecf68ec65b8c1b7c08624a4876
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.accounts.Account;
20263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.accounts.AccountManager;
21263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.accounts.OnAccountsUpdateListener;
22263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.ActionBar;
23263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.Activity;
24263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.Fragment;
25263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.FragmentManager;
26263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.FragmentTransaction;
27263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.BroadcastReceiver;
28d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.content.ComponentName;
29263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Context;
30263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Intent;
31263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.IntentFilter;
32263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.SharedPreferences;
33263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ActivityInfo;
34263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager;
35263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager.NameNotFoundException;
36263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ResolveInfo;
37263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.Configuration;
38263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.TypedArray;
39263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.XmlResourceParser;
40263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.nfc.NfcAdapter;
41263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Bundle;
42263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Handler;
43263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.INetworkManagementService;
44263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Message;
45263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.RemoteException;
46263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.ServiceManager;
47263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserHandle;
48263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserManager;
49263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.Preference;
50263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceFragment;
51263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceManager;
52263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceScreen;
53263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.text.TextUtils;
54263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.AttributeSet;
55263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Log;
56263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.TypedValue;
57263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Xml;
58d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.Menu;
59d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.MenuInflater;
60263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.MenuItem;
61263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View;
62263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View.OnClickListener;
63263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.Button;
64263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
65d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.widget.SearchView;
66263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.ArrayUtils;
67263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.XmlUtils;
68263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.AccessibilitySettings;
69263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.CaptionPropertiesFragment;
70263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AccountSyncSettings;
71263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AuthenticatorHelper;
72263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.ManageAccountsSettings;
73d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.applications.InstalledAppDetails;
74263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ManageApplications;
75263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ProcessStatsUi;
76263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.bluetooth.BluetoothSettings;
7763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglioimport com.android.settings.dashboard.DashboardCategory;
78263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.dashboard.DashboardSummary;
7963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglioimport com.android.settings.dashboard.DashboardTile;
80d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.NoHomeDialogFragment;
81d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.SearchResultsSummary;
82263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.Memory;
83263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.UsbSettings;
84263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.fuelgauge.PowerUsageSummary;
857a6bfd132cac4dcff72e3adcdd68a7ef777cee12Fabrice Di Meglioimport com.android.settings.search.DynamicIndexableContentMonitor;
86fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglioimport com.android.settings.search.Index;
87263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.InputMethodAndLanguageSettings;
88263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.SpellCheckersSettings;
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.UserDictionaryList;
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.location.LocationSettings;
92263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.AndroidBeam;
93263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.PaymentSettings;
94c9afadb04b00598f5988101681089290cf14f79cJohn Spurlockimport com.android.settings.notification.ConditionProviderSettings;
954a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationAccessSettings;
964a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationSettings;
974a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.NotificationStation;
984a35051565b0ce6d29313c3e4ffe19e1dde78db0John Spurlockimport com.android.settings.notification.ZenModeSettings;
99263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintJobSettingsFragment;
100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintSettingsFragment;
101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.tts.TextToSpeechSettings;
102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.users.UserSettings;
103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.vpn2.VpnSettings;
104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wfd.WifiDisplaySettings;
10541937766981423c9252e12e3319b2e7532739627Fabrice Di Meglioimport com.android.settings.widget.SwitchBar;
106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.AdvancedWifiSettings;
107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiSettings;
108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.p2p.WifiP2pSettings;
109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParser;
110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParserException;
111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.io.IOException;
113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.ArrayList;
114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Collections;
115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Comparator;
116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.List;
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,
123d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
124d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        SearchView.OnQueryTextListener, SearchView.OnCloseListener,
125d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        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";
134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * string to specify which fragment should be initially displayed.
138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will call isValidFragment() to confirm that the fragment class name is valid for this
140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity.
141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specified to supply a Bundle of arguments to pass
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to that fragment when it is instantiated during the initial creation
148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * of the activity.
149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
153c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
154c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     */
155c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio    public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
156c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
1576f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio    public static final String BACK_STACK_PREFS = ":settings:prefs";
1586f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // extras that allow any preference activity to be launched as part of a wizard
160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // show Back and Next buttons? takes boolean parameter
162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Back will then return RESULT_CANCELED and Next RESULT_OK
163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // add a Skip button?
166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // specify custom text for the Back or Next buttons, or cause a button to not appear
169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // at all by setting it to null
170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specify to supply the title to be shown for
176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that fragment.
177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
178d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_FRAGMENT_CLASS =
181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.FRAGMENT_CLASS";
182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
185d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio    private static final String EMPTY_QUERY = "";
186d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static boolean sShowNoHomeNotice = false;
188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String mFragmentClass;
190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1918eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private CharSequence mInitialTitle;
1928eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Show only these settings for restricted users
194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int[] SETTINGS_FOR_RESTRICTED = {
195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_section,
196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wifi_settings,
197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.bluetooth_settings,
198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.data_usage_settings,
199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_settings,
200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.device_section,
201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.sound_settings,
202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.display_settings,
203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.storage_settings,
204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.application_settings,
205263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.battery_settings,
206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.personal_section,
207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.location_settings,
208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.security_settings,
209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.language_settings,
210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.user_settings,
211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_settings,
212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_add,
213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.system_section,
214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.date_time_settings,
215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.about_settings,
216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.accessibility_settings,
217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.print_settings,
218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.nfc_payment_settings,
2192858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.home_settings,
2202858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.dashboard
221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String[] ENTRY_FRAGMENTS = {
224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WirelessSettings.class.getName(),
225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiSettings.class.getName(),
226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AdvancedWifiSettings.class.getName(),
227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            BluetoothSettings.class.getName(),
228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TetherSettings.class.getName(),
229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiP2pSettings.class.getName(),
230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            VpnSettings.class.getName(),
231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DateTimeSettings.class.getName(),
232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocalePicker.class.getName(),
233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            InputMethodAndLanguageSettings.class.getName(),
234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SpellCheckersSettings.class.getName(),
235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionaryList.class.getName(),
236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionarySettings.class.getName(),
237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SoundSettings.class.getName(),
238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DisplaySettings.class.getName(),
239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceInfoSettings.class.getName(),
240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageApplications.class.getName(),
241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ProcessStatsUi.class.getName(),
242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationStation.class.getName(),
243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocationSettings.class.getName(),
244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SecuritySettings.class.getName(),
245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrivacySettings.class.getName(),
246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceAdminSettings.class.getName(),
247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccessibilitySettings.class.getName(),
248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CaptionPropertiesFragment.class.getName(),
249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextToSpeechSettings.class.getName(),
252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Memory.class.getName(),
253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DevelopmentSettings.class.getName(),
254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UsbSettings.class.getName(),
255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AndroidBeam.class.getName(),
256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiDisplaySettings.class.getName(),
257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PowerUsageSummary.class.getName(),
258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountSyncSettings.class.getName(),
259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CryptKeeperSettings.class.getName(),
260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DataUsageSummary.class.getName(),
261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DreamSettings.class.getName(),
262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserSettings.class.getName(),
263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationAccessSettings.class.getName(),
264c9afadb04b00598f5988101681089290cf14f79cJohn Spurlock            ConditionProviderSettings.class.getName(),
265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageAccountsSettings.class.getName(),
266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintSettingsFragment.class.getName(),
267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintJobSettingsFragment.class.getName(),
268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TrustedCredentialsSettings.class.getName(),
269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PaymentSettings.class.getName(),
270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            KeyboardLayoutPickerFragment.class.getName(),
271d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ZenModeSettings.class.getName(),
272d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            NotificationSettings.class.getName(),
273d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
274d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
275d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            InstalledAppDetails.class.getName()
276263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
277263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
278263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences mDevelopmentPreferences;
279263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
280263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
281263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private AuthenticatorHelper mAuthenticatorHelper;
282263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mListeningToAccountUpdates;
283263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
284263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mBatteryPresent = true;
285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
286263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
287263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
288263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onReceive(Context context, Intent intent) {
289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String action = intent.getAction();
290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                boolean batteryPresent = Utils.isBatteryPresent(intent);
292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mBatteryPresent != batteryPresent) {
294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mBatteryPresent = batteryPresent;
295769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    invalidateCategories();
296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
301990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav    private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
302990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav            new DynamicIndexableContentMonitor();
303853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
304d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private ActionBar mActionBar;
30541937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    private SwitchBar mSwitchBar;
30641937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
30741937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    private Button mNextButton;
308b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio    private boolean mDisplayHomeAsUpEnabled;
309d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
31035062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio    private boolean mIsShowingDashboard;
31135062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio
312d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchView mSearchView;
313d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private MenuItem mSearchMenuItem;
314d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mSearchMenuItemExpanded = false;
315d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchResultsSummary mSearchResultsFragment;
316d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private String mSearchQuery;
317d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
318769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    // Categories
319769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
320769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private boolean mNeedToRebuildCategories;
321263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
322769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private static final int MSG_BUILD_CATEGORIES = 1;
323263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Handler mHandler = new Handler() {
324263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
325263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void handleMessage(Message msg) {
326263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (msg.what) {
327769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                case MSG_BUILD_CATEGORIES: {
328769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    buildDashboardCategories(mCategories);
329263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } break;
330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
331263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
332263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
334d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mNeedToRevertToInitialFragment = false;
335d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
33641937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    public SwitchBar getSwitchBar() {
33741937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio        return mSwitchBar;
33841937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio    }
33941937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
340769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    public AuthenticatorHelper getAuthenticatorHelper() {
341769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        return mAuthenticatorHelper;
342769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
343769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
344769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    public List<DashboardCategory> getDashboardCategories() {
345769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (mNeedToRebuildCategories) {
346769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            buildDashboardCategories(mCategories);
347769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            mNeedToRebuildCategories = false;
348769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
349769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        return mCategories;
350769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
351769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
352263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
353263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
354263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Override the fragment title for Wallpaper settings
355263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int titleRes = pref.getTitleRes();
356263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
357263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = R.string.wallpaper_settings_fragment_title;
358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                && UserHandle.myUserId() != UserHandle.USER_OWNER) {
360263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (UserManager.get(this).isLinkedUser()) {
361263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.profile_info_settings_title;
362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.user_info_settings_title;
364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                null, 0);
368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
370263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
374263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
375263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
376769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void invalidateCategories() {
377769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
378769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
379263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
380263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
381263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
382263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
383263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onConfigurationChanged(Configuration newConfig) {
384263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onConfigurationChanged(newConfig);
3856f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio        Index.getInstance(this).update();
386263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
387263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
388263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
389d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onStart() {
390d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onStart();
391d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
392d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mNeedToRevertToInitialFragment) {
393d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
394263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
395263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
396263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
397263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
398d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onCreateOptionsMenu(Menu menu) {
399d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuInflater inflater = getMenuInflater();
400d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        inflater.inflate(R.menu.options_menu, menu);
401d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
402d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        // Cache the search query (can be overriden by the OnQueryTextListener)
403d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        final String query = mSearchQuery;
404d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
4059593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem = menu.findItem(R.id.search);
4069593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchView = (SearchView) mSearchMenuItem.getActionView();
407d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
40823ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem == null || mSearchView == null) {
40923ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            return false;
41023ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
41123ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio
4128c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio        if (mSearchResultsFragment != null) {
4138c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio            mSearchResultsFragment.setSearchView(mSearchView);
4148c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio        }
4158c3b0ce7120da882082eed533462090c4feadff8Fabrice Di Meglio
4169593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem.setOnActionExpandListener(this);
417d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnQueryTextListener(this);
418d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnCloseListener(this);
419d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
420d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mSearchMenuItemExpanded) {
421d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItem.expandActionView();
422d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
423d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setQuery(query, true /* submit */);
424d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
425d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
426d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
427d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
428d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
429d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onCreate(Bundle savedState) {
430263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
431263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
432263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
4335ebabfcd35dd00a1d5beb9bcaca03574ae09b4e8Fabrice Di Meglio
434263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper = new AuthenticatorHelper();
435263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
436263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, null);
437263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
438263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
439263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Context.MODE_PRIVATE);
440263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
441263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getMetaData();
442263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
443d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onCreate(savedState);
444263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
445263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setContentView(R.layout.settings_main);
446263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
447263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getFragmentManager().addOnBackStackChangedListener(this);
448263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
449b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        mDisplayHomeAsUpEnabled = true;
450263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
4515cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        // Getting Intent properties can only be done after the super.onCreate(...)
4525cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        final String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
4535cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio
45435062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio        mIsShowingDashboard = (initialFragmentName == null);
45535062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio
45635062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio        if (mIsShowingDashboard) {
4575cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio            Index.getInstance(this).update();
4585cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        }
4595cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio
460d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (savedState != null) {
4611800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // We are restarting from a previous saved state; used that to initialize, instead
4621800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // of starting fresh.
463d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
464d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
465263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
4661800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
4671800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
4681800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            setTitle(mInitialTitle);
4695529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
470769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            ArrayList<DashboardCategory> categories =
471769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
472769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            if (categories != null) {
4735f995727cc2e4bd27d26a50f3a565fb677fdb294Fabrice Di Meglio                mCategories.clear();
474769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                mCategories.addAll(categories);
475b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio                setTitleFromBackStack();
476263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
477b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio
478b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio            mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
479263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else {
48035062d6983e352f5cb6c4a6bbf3c1e4e214d44a9Fabrice Di Meglio            if (!mIsShowingDashboard) {
481d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final ComponentName cn = getIntent().getComponent();
482d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                // No UP is we are launched thru a Settings shortcut
483d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                if (!cn.getClassName().equals(SubSettings.class.getName())) {
484b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio                    mDisplayHomeAsUpEnabled = false;
485d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                }
486d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
487d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
488832e54618588fdd31397b1779411af06e58565d7Fabrice Di Meglio                setTitle(mInitialTitle);
489c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
4905ebabfcd35dd00a1d5beb9bcaca03574ae09b4e8Fabrice Di Meglio                Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
491d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                switchToFragment( initialFragmentName, initialArguments, true, false,
492d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                        mInitialTitle, false);
49310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            } else {
494680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio                // We need to build the Categories in all cases
495680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio                buildDashboardCategories(mCategories);
496680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio
497e9326d276135b657738d851e0a56df447f09567aFabrice Di Meglio                // No UP if we are displaying the main Dashboard
498b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio                mDisplayHomeAsUpEnabled = false;
499769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                if (mCategories.size() > 0) {
500d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    mInitialTitle = getText(R.string.dashboard_title);
501d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    switchToFragment(DashboardSummary.class.getName(), null, false, false,
502d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                            mInitialTitle, false);
50310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                }
504263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
505263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
506263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
507d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar = getActionBar();
508d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio        if (mActionBar != null) {
509d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio            mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
510d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio            mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
511d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio        }
51241937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio        mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
51341937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio
514263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // see if we should show Back/Next buttons
515263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent intent = getIntent();
516263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
518d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio            View buttonBar = findViewById(R.id.button_bar);
519263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (buttonBar != null) {
520263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                buttonBar.setVisibility(View.VISIBLE);
521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
522d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                Button backButton = (Button)findViewById(R.id.back_button);
523263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                backButton.setOnClickListener(new OnClickListener() {
524263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
525263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_CANCELED);
526263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
527263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
528263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
529d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                Button skipButton = (Button)findViewById(R.id.skip_button);
530263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                skipButton.setOnClickListener(new OnClickListener() {
531263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
532263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
533263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
534263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
535263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
536d2b64f339adb8f019e3c712e388e35830d8b68a9Fabrice Di Meglio                mNextButton = (Button)findViewById(R.id.next_button);
537263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton.setOnClickListener(new OnClickListener() {
538263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
539263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
540263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
541263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
542263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
543263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
544263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // set our various button parameters
545263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
546263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
547263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
548263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setVisibility(View.GONE);
549263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
550263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
551263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setText(buttonText);
552263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
553263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
554263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
555263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
556263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
557263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setVisibility(View.GONE);
558263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
559263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
560263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setText(buttonText);
561263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
562263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
563263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
564263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    skipButton.setVisibility(View.VISIBLE);
565263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
566263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
567263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
568c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    }
569c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio
570c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    @Override
571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onBackStackChanged() {
572d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        setTitleFromBackStack();
5738eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    }
5748eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
575b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio    private int setTitleFromBackStack() {
5768eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int count = getFragmentManager().getBackStackEntryCount();
577b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5788eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (count == 0) {
5798eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(mInitialTitle);
580b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio            return 0;
581263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
582b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5838eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
5848eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        setTitleFromBackStackEntry(bse);
585b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
586b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio        return count;
587263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
588263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
5898eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
5905529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        final CharSequence title;
5918eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int titleRes = bse.getBreadCrumbTitleRes();
5928eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (titleRes > 0) {
5938eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = getText(titleRes);
5945529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        } else {
5958eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = bse.getBreadCrumbTitle();
5968eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        }
5978eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (title != null) {
5988eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(title);
5995529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        }
6005529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio    }
6015529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
602263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
603263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onSaveInstanceState(Bundle outState) {
604263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onSaveInstanceState(outState);
605263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
606769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (mCategories.size() > 0) {
607769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
608263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
609d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
610b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
611b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio
612d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // The option menus are created if the ActionBar is visible and they are also created
613d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // asynchronously. If you launch Settings with an Intent action like
614d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
615d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
616d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // menu item and search view are null.
617d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
618d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
619d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
620d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
621d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putString(SAVE_KEY_SEARCH_QUERY, query);
622263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
623263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
624263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
625263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onResume() {
626263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onResume();
627263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
628263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
629263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
630263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
631769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                invalidateCategories();
632263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
633263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
634263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
635263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
636263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
637263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
638853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
639990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.register(this);
640a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio
641a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        if(!TextUtils.isEmpty(mSearchQuery)) {
642a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            onQueryTextSubmit(mSearchQuery);
643a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
644263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
645263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
646263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
647263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onPause() {
648263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPause();
649263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
650263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        unregisterReceiver(mBatteryInfoReceiver);
651263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
652990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.unregister();
653263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
654263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
655263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
656263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onDestroy() {
657263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onDestroy();
658680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio
659680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio        mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
660680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio                mDevelopmentPreferencesListener);
661680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio        mDevelopmentPreferencesListener = null;
662680b064b293888ea47a6211943b51bee3adfb70dFabrice Di Meglio
663263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mListeningToAccountUpdates) {
664263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).removeOnAccountsUpdatedListener(this);
665263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
666263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
667263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
668263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected boolean isValidFragment(String fragmentName) {
669263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Almost all fragments are wrapped in this,
670263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // except for a few that have their own activities.
671263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
672263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
673263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
674263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
675263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
676263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
677263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
678263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Intent getIntent() {
679263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent superIntent = super.getIntent();
680263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String startingFragment = getStartingFragmentClass(superIntent);
681263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // This is called from super.onCreate, isMultiPane() is not yet reliable
682263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Do not use onIsHidingHeaders either, which relies itself on this method
683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (startingFragment != null) {
684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Intent modIntent = new Intent(superIntent);
685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle args = superIntent.getExtras();
687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (args != null) {
688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle(args);
689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle();
691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
692263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            args.putParcelable("intent", superIntent);
693263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return modIntent;
695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return superIntent;
697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
700263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Checks if the component name in the intent is different from the Settings class and
701263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * returns the class name to load as a fragment.
702263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
703263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String getStartingFragmentClass(Intent intent) {
704263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mFragmentClass != null) return mFragmentClass;
705263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String intentClass = intent.getComponent().getClassName();
707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intentClass.equals(getClass().getName())) return null;
708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ("com.android.settings.ManageApplications".equals(intentClass)
710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.RunningServices".equals(intentClass)
711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.applications.StorageUse".equals(intentClass)) {
712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Old names of manage apps.
713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            intentClass = com.android.settings.applications.ManageApplications.class.getName();
714263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return intentClass;
717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
72010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment containing a preference panel.  If the preferences
72110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * are being displayed in multi-pane mode, the given fragment class will
72210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * be instantiated and placed in the appropriate pane.  If running in
72310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * single-pane mode, a new activity will be launched in which to show the
72410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
72510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
72610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentClass Full name of the class implementing the fragment.
72710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Any desired arguments to supply to the fragment.
72810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Optional resource identifier of the title of this
72910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
73010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText Optional text of the title of this fragment.
73110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Optional fragment that result data should be sent to.
73210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * If non-null, resultTo.onActivityResult() will be called when this
73310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference panel is done.  The launched panel must use
73410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
73510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the caller's
73610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * request code to be received with the resut.
73710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
73810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
739d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            CharSequence titleText, Fragment resultTo, int resultRequestCode) {
740911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        String title;
741911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        if (titleRes > 0) {
742911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio            title = getString(titleRes);
743821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio        } else if (titleText != null) {
744911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio            title = titleText.toString();
745821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio        } else {
746821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio            // There not much we can do in that case
747821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio            title = "";
748911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        }
749769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode, title);
75010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
75110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
75210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
753263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called by a preference panel fragment to finish itself.
754263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
755263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param caller The fragment that is asking to be finished.
756263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultCode Optional result code to send back to the original
757263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
758263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultData Optional result data to send back to the original
759263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
760263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
761263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
762263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setResult(resultCode, resultData);
763263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
764263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
765263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
76610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
76710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
76810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragment The fragment to start
76910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
77010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * the current fragment will be replaced.
77110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
77210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferenceFragment(Fragment fragment, boolean push) {
77310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
77410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, fragment);
77510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (push) {
77610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
77710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.addToBackStack(BACK_STACK_PREFS);
77810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
77910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
78010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
78110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
78210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
78310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
78410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
785d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Switch to a specific Fragment with taking care of validation, Title and BackStack
786d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
787d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
788d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            boolean addToBackStack, CharSequence title, boolean withTransition) {
789d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (validate && !isValidFragment(fragmentName)) {
790d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            throw new IllegalArgumentException("Invalid fragment for this activity: "
791d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    + fragmentName);
792d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
793d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
794d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
795d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.replace(R.id.prefs, f);
796d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (withTransition) {
797d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
798d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
799d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (addToBackStack) {
800d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
801d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
802d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (title != null) {
803d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setBreadCrumbTitle(title);
804d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
805d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.commitAllowingStateLoss();
806d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return f;
807d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
808d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
809769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    public void setNeedToRebuildCategories(boolean need) {
810769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        mNeedToRebuildCategories = need;
811263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
812263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
81363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    /**
814769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio     * Called when the activity needs its list of categories/tiles built.
81563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     *
81663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param categories The list in which to place the tiles categories.
81763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     */
818769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void buildDashboardCategories(List<DashboardCategory> categories) {
8195f995727cc2e4bd27d26a50f3a565fb677fdb294Fabrice Di Meglio        categories.clear();
82063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        loadCategoriesFromResource(R.xml.dashboard_categories, categories);
82163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        updateTilesList(categories);
82263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
82363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
82463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    /**
82563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * Parse the given XML file as a categories description, adding each
82663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * parsed categories and tiles into the target list.
82763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     *
82863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param resid The XML resource to load and parse.
82963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     * @param target The list in which the parsed categories and tiles should be placed.
83063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio     */
83163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
83263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        XmlResourceParser parser = null;
83363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        try {
83463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            parser = getResources().getXml(resid);
83563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            AttributeSet attrs = Xml.asAttributeSet(parser);
83663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
83763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int type;
83863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
83963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    && type != XmlPullParser.START_TAG) {
84063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // Parse next until start tag is found
84163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
84263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
84363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            String nodeName = parser.getName();
84463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (!"dashboard-categories".equals(nodeName)) {
84563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                throw new RuntimeException(
84663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        "XML document must start with <preference-categories> tag; found"
84763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                + nodeName + " at " + parser.getPositionDescription());
84863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
84963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
85063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            Bundle curBundle = null;
85163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
85263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            final int outerDepth = parser.getDepth();
85363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
85463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
85563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
85663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    continue;
85763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
85863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
85963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                nodeName = parser.getName();
86063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if ("dashboard-category".equals(nodeName)) {
86163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    DashboardCategory category = new DashboardCategory();
86263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
86363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    TypedArray sa = obtainStyledAttributes(
86463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            attrs, com.android.internal.R.styleable.PreferenceHeader);
86563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    category.id = sa.getResourceId(
86663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_id,
86763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            (int)DashboardCategory.CAT_ID_UNDEFINED);
86863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
86963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    TypedValue tv = sa.peekValue(
87063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_title);
87163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
87263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (tv.resourceId != 0) {
87363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.titleRes = tv.resourceId;
87463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        } else {
87563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.title = tv.string;
87663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
87763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
87863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    sa.recycle();
87963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
88063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    final int innerDepth = parser.getDepth();
88163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
88263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
88363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
88463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            continue;
88563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
88663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
88763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        String innerNodeName = parser.getName();
88863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (innerNodeName.equals("dashboard-tile")) {
88963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            DashboardTile tile = new DashboardTile();
89063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
89163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            sa = obtainStyledAttributes(
89263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    attrs, com.android.internal.R.styleable.PreferenceHeader);
89363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.id = sa.getResourceId(
89463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_id,
895e9326d276135b657738d851e0a56df447f09567aFabrice Di Meglio                                    (int)TILE_ID_UNDEFINED);
89663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tv = sa.peekValue(
89763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_title);
89863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (tv != null && tv.type == TypedValue.TYPE_STRING) {
89963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (tv.resourceId != 0) {
90063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.titleRes = tv.resourceId;
90163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
90263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.title = tv.string;
90363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
90463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
90563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tv = sa.peekValue(
90663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_summary);
90763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (tv != null && tv.type == TypedValue.TYPE_STRING) {
90863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (tv.resourceId != 0) {
90963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.summaryRes = tv.resourceId;
91063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
91163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.summary = tv.string;
91263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
91363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
91463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.iconRes = sa.getResourceId(
91563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_icon, 0);
91663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            tile.fragment = sa.getString(
91763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    com.android.internal.R.styleable.PreferenceHeader_fragment);
91863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            sa.recycle();
91963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
92063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (curBundle == null) {
92163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                curBundle = new Bundle();
92263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
92363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
92463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            final int innerDepth2 = parser.getDepth();
92563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
92663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
92763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
92863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    continue;
92963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
93063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
93163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                String innerNodeName2 = parser.getName();
93263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                if (innerNodeName2.equals("extra")) {
93363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    getResources().parseBundleExtra("extra", attrs, curBundle);
93463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    XmlUtils.skipCurrentTag(parser);
93563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
93663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else if (innerNodeName2.equals("intent")) {
93763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    tile.intent = Intent.parseIntent(getResources(), parser, attrs);
93863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
93963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                } else {
94063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                    XmlUtils.skipCurrentTag(parser);
94163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                }
94263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
94363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
94463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            if (curBundle.size() > 0) {
94563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                tile.fragmentArguments = curBundle;
94663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                curBundle = null;
94763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            }
94863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
94963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.addTile(tile);
95063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
95163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        } else {
95263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
95363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
95463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
95563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
95663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    target.add(category);
95763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else {
95863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    XmlUtils.skipCurrentTag(parser);
95963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
96063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
96163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
96263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (XmlPullParserException e) {
96363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            throw new RuntimeException("Error parsing categories", e);
96463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (IOException e) {
96563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            throw new RuntimeException("Error parsing categories", e);
96663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } finally {
96763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (parser != null) parser.close();
96863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
96963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
97063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
97163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private void updateTilesList(List<DashboardCategory> target) {
97263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final boolean showDev = mDevelopmentPreferences.getBoolean(
97363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                DevelopmentSettings.PREF_SHOW,
97463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                android.os.Build.TYPE.equals("eng"));
97563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
97663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
97763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
97863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        final int size = target.size();
97963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        for (int i = 0; i < size; i++) {
98063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
98163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            DashboardCategory category = target.get(i);
98263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
98363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            // Ids are integers, so downcasting is ok
98463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int id = (int) category.id;
98563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (id == R.id.account_settings) {
98663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                insertAccountsTiles(category);
98763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                continue;
98863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
98963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            int n = category.getTilesCount() - 1;
99063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            while (n >= 0) {
99163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
99263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                DashboardTile tile = category.getTile(n);
99363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
99463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                id = (int) tile.id;
99563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
99663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
99763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.wifi_settings) {
99863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove WiFi Settings if WiFi service is not available.
99963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
100063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
100163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
100263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.bluetooth_settings) {
100363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove Bluetooth Settings if Bluetooth service is not available.
100463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
100563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
100663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
100763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.data_usage_settings) {
100863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove data usage when kernel module not enabled
100963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    final INetworkManagementService netManager = INetworkManagementService.Stub
101063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
101163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    try {
101263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (!netManager.isBandwidthControlEnabled()) {
101363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.removeTile(n);
101463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
101563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    } catch (RemoteException e) {
101663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        // ignored
101763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
101863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.battery_settings) {
101963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    // Remove battery settings when battery is not available. (e.g. TV)
102063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
102163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!mBatteryPresent) {
102263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
102363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
102463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.home_settings) {
102563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!updateHomeSettingTiles(tile)) {
102663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
102763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
102863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.user_settings) {
102963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!UserHandle.MU_ENABLED
103063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            || !UserManager.supportsMultipleUsers()
103163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            || Utils.isMonkeyRunning()) {
103263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
103363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
103463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.nfc_payment_settings) {
103563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
103663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
103763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    } else {
103863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        // Only show if NFC is on and we have the HCE feature
103963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
104063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
104163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                                PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
104263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                            category.removeTile(n);
104363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        }
104463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
1045488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                } else if (id == R.id.print_settings) {
1046488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1047488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                            PackageManager.FEATURE_PRINTING);
1048488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    if (!hasPrintingSupport) {
1049488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                        category.removeTile(n);
1050488cae39dab2e8c298e8a1f1eec37bf0d96a83d2Fabrice Di Meglio                    }
105163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.development_settings) {
10526c088cb221bb43d47be065387231ffa7192ff211Julia Reynolds                    if (!showDev || um.hasUserRestriction(
10536c088cb221bb43d47be065387231ffa7192ff211Julia Reynolds                            UserManager.DISALLOW_DEBUGGING_FEATURES)) {
105463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
105563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
105663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                } else if (id == R.id.account_add) {
105763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
105863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        category.removeTile(n);
105963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    }
106063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
106163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
106263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
1063a97089d389338ca8e3d970b00dd13ffad732b5c1Amith Yamasani                        && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1064a97089d389338ca8e3d970b00dd13ffad732b5c1Amith Yamasani                        && n < category.getTilesCount()) {
106563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    category.removeTile(n);
106663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
106763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
106863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                n--;
106963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
107063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
107163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
107263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
107363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private boolean updateHomeSettingTiles(DashboardTile tile) {
107463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        // Once we decide to show Home settings, keep showing it forever
107563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
107663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
107763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            return true;
107863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
107963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
108063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        try {
108163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
108263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            getPackageManager().getHomeActivities(homeApps);
108363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (homeApps.size() < 2) {
108463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // When there's only one available home app, omit this settings
108563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // category entirely at the top level UI.  If the user just
108663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // uninstalled the penultimate home app candidiate, we also
108763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // now tell them about why they aren't seeing 'Home' in the list.
108863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (sShowNoHomeNotice) {
108963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    sShowNoHomeNotice = false;
109063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    NoHomeDialogFragment.show(this);
109163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
109263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                return false;
109363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            } else {
109463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // Okay, we're allowing the Home settings category.  Tell it, when
109563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // invoked via this front door, that we'll need to be told about the
109663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // case when the user uninstalls all but one home app.
109763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                if (tile.fragmentArguments == null) {
109863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    tile.fragmentArguments = new Bundle();
109963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                }
110063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
110163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
110263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        } catch (Exception e) {
110363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            // Can't look up the home activity; bail on configuring the icon
110463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            Log.w(LOG_TAG, "Problem looking up home activity!", e);
110563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
110663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
110763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
110863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        return true;
110963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
111063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
111163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    private void insertAccountsTiles(DashboardCategory target) {
111263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
111363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
111463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        for (String accountType : accountTypes) {
111563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
111663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (label == null) {
111763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                continue;
111863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
111963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
112063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
112163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            boolean skipToAccount = accounts.length == 1
112263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                    && !mAuthenticatorHelper.hasAccountPreferences(accountType);
112363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            DashboardTile accountTile = new DashboardTile();
112463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            accountTile.title = label;
112563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (accountTile.extras == null) {
112663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.extras = new Bundle();
112763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
112863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            if (skipToAccount) {
112963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragment = AccountSyncSettings.class.getName();
113063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragmentArguments = new Bundle();
113163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                // Need this for the icon
113263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
113363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
113463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
113563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        accounts[0]);
113663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            } else {
113763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragment = ManageAccountsSettings.class.getName();
113863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragmentArguments = new Bundle();
113963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
114063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
114163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        accountType);
114263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
114363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                        label.toString());
114463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
114563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            dashboardTiles.add(accountTile);
114663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            mAuthenticatorHelper.preloadDrawableForType(this, accountType);
114763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
114863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
114963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        // Sort by label
115063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
115163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            @Override
115263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            public int compare(DashboardTile t1, DashboardTile t2) {
115363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio                return t1.title.toString().compareTo(t2.title.toString());
115463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            }
115563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        });
115663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        int index = 0;
115763bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        for (DashboardTile tile : dashboardTiles) {
115863bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            target.addTile(index, tile);
115963bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            index++;
116063bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
116163bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        if (!mListeningToAccountUpdates) {
116263bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
116363bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio            mListeningToAccountUpdates = true;
116463bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio        }
116563bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio    }
116663bbb8e4fef5d0d0a5fa9c7656daa7ea160fd51bFabrice Di Meglio
1167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void getMetaData() {
1168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    PackageManager.GET_META_DATA);
1171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ai == null || ai.metaData == null) return;
1172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (NameNotFoundException nnfe) {
1174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // No recovery
1175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // give subclasses access to the Next button
1180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean hasNextButton() {
1181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton != null;
1182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Button getNextButton() {
1185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton;
1186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean shouldUpRecreateTask(Intent targetIntent) {
1190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onAccountsUpdated(Account[] accounts) {
1195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // TODO: watch for package upgrades to invalidate cache; see 7206643
1196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
1197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, accounts);
1198769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        invalidateCategories();
1199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static void requestHomeNotice() {
1202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sShowNoHomeNotice = true;
1203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1205d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1206d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextSubmit(String query) {
1207d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        switchToSearchResultsFragmentIfNeeded();
1208d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = query;
1209d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextSubmit(query);
1210d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1212d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1213d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextChange(String newText) {
1214d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = newText;
12157e4855e8f644bdecf68ec65b8c1b7c08624a4876Fabrice Di Meglio        if (mSearchResultsFragment == null) {
1216a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            return false;
1217a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
1218a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextChange(newText);
1219d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1221d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1222d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onClose() {
1223d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return false;
1224d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1226d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1227d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionExpand(MenuItem item) {
1228d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1229bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            switchToSearchResultsFragmentIfNeeded();
1230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1231d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1232d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1234d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1235d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionCollapse(MenuItem item) {
1236d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1237bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            if (mSearchMenuItemExpanded) {
1238d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                revertToInitialFragment();
1239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1241d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1242d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1244d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void switchToSearchResultsFragmentIfNeeded() {
1245bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (mSearchResultsFragment != null) {
1246bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            return;
1247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1248bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1249bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (current != null && current instanceof SearchResultsSummary) {
1250bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) current;
1251bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        } else {
1252d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio            final boolean isShowingSwitchBar =
1253d8aec08fa96c42a61bd6ead2aa9859b5b67a3f2cFabrice Di Meglio                    (mSwitchBar != null) ? mSwitchBar.isShowing() : false;
1254bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            String title = getString(R.string.search_results_title);
1255bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
125641937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio                    SearchResultsSummary.class.getName(), null, false, true, title,
125741937766981423c9252e12e3319b2e7532739627Fabrice Di Meglio                    !isShowingSwitchBar);
1258bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        }
1259d297a5840230b769a3c7ad8b85232968a7077f64Fabrice Di Meglio        mSearchResultsFragment.setSearchView(mSearchView);
1260bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = true;
1261d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1263d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void needToRevertToInitialFragment() {
1264d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = true;
1265d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1267d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void revertToInitialFragment() {
1268d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = false;
1269d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchResultsFragment = null;
1270bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = false;
1271bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1272bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio                FragmentManager.POP_BACK_STACK_INCLUSIVE);
127323ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem != null) {
127423ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            mSearchMenuItem.collapseActionView();
127523ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
1276263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1277263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
1278