SettingsActivity.java revision 23ae00c1d87d64578cd62da1894ee25da531ca11
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.app.admin.DevicePolicyManager;
28263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.BroadcastReceiver;
29d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.content.ComponentName;
30263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Context;
31263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Intent;
32263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.IntentFilter;
33263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.SharedPreferences;
34263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ActivityInfo;
35263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager;
36263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager.NameNotFoundException;
37263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ResolveInfo;
38263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.Configuration;
39263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.TypedArray;
40263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.XmlResourceParser;
41263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.nfc.NfcAdapter;
42263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Bundle;
43263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Handler;
44263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.INetworkManagementService;
45263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Message;
46263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.RemoteException;
47263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.ServiceManager;
48263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserHandle;
49263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserManager;
50263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.Preference;
51263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceFragment;
52263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceManager;
53263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceScreen;
54263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.text.TextUtils;
55263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.AttributeSet;
56263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Log;
57263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.TypedValue;
58263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Xml;
59d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.Menu;
60d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.view.MenuInflater;
61263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.MenuItem;
62263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View;
63263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View.OnClickListener;
64263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.Button;
65263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.ListView;
66263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
67d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport android.widget.SearchView;
68263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.ArrayUtils;
69263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.XmlUtils;
70263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.AccessibilitySettings;
71263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.CaptionPropertiesFragment;
72263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AccountSyncSettings;
73263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AuthenticatorHelper;
74263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.ManageAccountsSettings;
75d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.applications.InstalledAppDetails;
76263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ManageApplications;
77263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ProcessStatsUi;
78263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.bluetooth.BluetoothSettings;
79263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.dashboard.DashboardSummary;
80d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.Header;
81d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.HeaderAdapter;
82d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.NoHomeDialogFragment;
83d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport com.android.settings.dashboard.SearchResultsSummary;
84263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.Memory;
85263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.UsbSettings;
86263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.fuelgauge.PowerUsageSummary;
877a6bfd132cac4dcff72e3adcdd68a7ef777cee12Fabrice Di Meglioimport com.android.settings.search.DynamicIndexableContentMonitor;
88fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglioimport com.android.settings.search.Index;
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.SpellCheckersSettings;
92263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.UserDictionaryList;
93263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.location.LocationSettings;
94263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.AndroidBeam;
95263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.PaymentSettings;
96263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintJobSettingsFragment;
97263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintSettingsFragment;
98263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.tts.TextToSpeechSettings;
99263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.users.UserSettings;
100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.vpn2.VpnSettings;
101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wfd.WifiDisplaySettings;
102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.AdvancedWifiSettings;
103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiSettings;
104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.p2p.WifiP2pSettings;
105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParser;
106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParserException;
107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.io.IOException;
109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.ArrayList;
110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Collections;
111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Comparator;
112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.HashMap;
113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.List;
114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
115d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport static com.android.settings.dashboard.Header.HEADER_ID_UNDEFINED;
116d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopublic class SettingsActivity extends Activity
118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        implements PreferenceManager.OnPreferenceTreeClickListener,
119263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        PreferenceFragment.OnPreferenceStartFragmentCallback,
120d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
121d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        SearchView.OnQueryTextListener, SearchView.OnCloseListener,
122d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuItem.OnActionExpandListener {
123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String LOG_TAG = "Settings";
125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Constants for state save/restore
127d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_HEADERS = ":settings:headers";
128d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
129d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
130b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio    private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * string to specify which fragment should be initially displayed.
135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will call isValidFragment() to confirm that the fragment class name is valid for this
137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity.
138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specified to supply a Bundle of arguments to pass
144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to that fragment when it is instantiated during the initial creation
145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * of the activity.
146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
150c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
151c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio     */
152c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio    public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
153c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
154c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio    /**
155263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
156263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * boolean that the header list should not be displayed.  This is most often
157263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch
158263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * the activity to display a specific fragment that the user has navigated
159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to.
160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_NO_HEADERS = ":settings:no_headers";
162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1636f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio    public static final String BACK_STACK_PREFS = ":settings:prefs";
1646f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // extras that allow any preference activity to be launched as part of a wizard
166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // show Back and Next buttons? takes boolean parameter
168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Back will then return RESULT_CANCELED and Next RESULT_OK
169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // add a Skip button?
172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // specify custom text for the Back or Next buttons, or cause a button to not appear
175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // at all by setting it to null
176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specify to supply the title to be shown for
182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that fragment.
183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
184d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_FRAGMENT_CLASS =
187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.FRAGMENT_CLASS";
188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
191d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio    private static final String EMPTY_QUERY = "";
192d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static boolean sShowNoHomeNotice = false;
194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String mFragmentClass;
1966502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio    private Header mSelectedHeader;
197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1988eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private CharSequence mInitialTitle;
1998eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Show only these settings for restricted users
201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int[] SETTINGS_FOR_RESTRICTED = {
202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_section,
203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wifi_settings,
204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.bluetooth_settings,
205263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.data_usage_settings,
206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_settings,
207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.device_section,
208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.sound_settings,
209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.display_settings,
210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.storage_settings,
211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.application_settings,
212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.battery_settings,
213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.personal_section,
214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.location_settings,
215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.security_settings,
216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.language_settings,
217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.user_settings,
218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_settings,
219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_add,
220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.system_section,
221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.date_time_settings,
222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.about_settings,
223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.accessibility_settings,
224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.print_settings,
225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.nfc_payment_settings,
2262858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.home_settings,
2272858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.dashboard
228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String[] ENTRY_FRAGMENTS = {
231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WirelessSettings.class.getName(),
232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiSettings.class.getName(),
233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AdvancedWifiSettings.class.getName(),
234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            BluetoothSettings.class.getName(),
235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TetherSettings.class.getName(),
236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiP2pSettings.class.getName(),
237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            VpnSettings.class.getName(),
238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DateTimeSettings.class.getName(),
239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocalePicker.class.getName(),
240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            InputMethodAndLanguageSettings.class.getName(),
241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SpellCheckersSettings.class.getName(),
242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionaryList.class.getName(),
243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionarySettings.class.getName(),
244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SoundSettings.class.getName(),
245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DisplaySettings.class.getName(),
246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceInfoSettings.class.getName(),
247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageApplications.class.getName(),
248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ProcessStatsUi.class.getName(),
249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationStation.class.getName(),
250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocationSettings.class.getName(),
251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SecuritySettings.class.getName(),
252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrivacySettings.class.getName(),
253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceAdminSettings.class.getName(),
254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccessibilitySettings.class.getName(),
255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CaptionPropertiesFragment.class.getName(),
256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleContrastPreferenceFragment.class.getName(),
258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextToSpeechSettings.class.getName(),
260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Memory.class.getName(),
261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DevelopmentSettings.class.getName(),
262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UsbSettings.class.getName(),
263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AndroidBeam.class.getName(),
264263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiDisplaySettings.class.getName(),
265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PowerUsageSummary.class.getName(),
266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountSyncSettings.class.getName(),
267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CryptKeeperSettings.class.getName(),
268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DataUsageSummary.class.getName(),
269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DreamSettings.class.getName(),
270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserSettings.class.getName(),
271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationAccessSettings.class.getName(),
272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageAccountsSettings.class.getName(),
273263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintSettingsFragment.class.getName(),
274263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintJobSettingsFragment.class.getName(),
275263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TrustedCredentialsSettings.class.getName(),
276263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PaymentSettings.class.getName(),
277263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            KeyboardLayoutPickerFragment.class.getName(),
278d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ZenModeSettings.class.getName(),
279d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            NotificationSettings.class.getName(),
280d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
281d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
282d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            InstalledAppDetails.class.getName()
283263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
284263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences mDevelopmentPreferences;
286263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
287263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
288263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private AuthenticatorHelper mAuthenticatorHelper;
289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mListeningToAccountUpdates;
290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mBatteryPresent = true;
292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onReceive(Context context, Intent intent) {
296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String action = intent.getAction();
297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                boolean batteryPresent = Utils.isBatteryPresent(intent);
299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mBatteryPresent != batteryPresent) {
301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mBatteryPresent = batteryPresent;
302263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    invalidateHeaders();
303263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
304263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
305263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
306263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
307263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
308990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav    private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
309990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav            new DynamicIndexableContentMonitor();
310853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
311d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Button mNextButton;
312d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private ActionBar mActionBar;
313b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio    private boolean mDisplayHomeAsUpEnabled;
314d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
315d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchView mSearchView;
316d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private MenuItem mSearchMenuItem;
317d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mSearchMenuItemExpanded = false;
318d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchResultsSummary mSearchResultsFragment;
319d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private String mSearchQuery;
320d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
321d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    // Headers
322d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
323263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private final ArrayList<Header> mHeaders = new ArrayList<Header>();
324263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private HeaderAdapter mHeaderAdapter;
325263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
326263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final int MSG_BUILD_HEADERS = 1;
327263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Handler mHandler = new Handler() {
328263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
329263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void handleMessage(Message msg) {
330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (msg.what) {
331263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case MSG_BUILD_HEADERS: {
332263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaders.clear();
333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    onBuildHeaders(mHeaders);
334263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaderAdapter.notifyDataSetChanged();
335263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } break;
336263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
338263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
339263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
340d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mNeedToRevertToInitialFragment = false;
341d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
342263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
343263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
344263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Override the fragment title for Wallpaper settings
345263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int titleRes = pref.getTitleRes();
346263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = R.string.wallpaper_settings_fragment_title;
348263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
349263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                && UserHandle.myUserId() != UserHandle.USER_OWNER) {
350263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (UserManager.get(this).isLinkedUser()) {
351263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.profile_info_settings_title;
352263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
353263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.user_info_settings_title;
354263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
355263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
356263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
357263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                null, 0);
358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
360263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
361263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void invalidateHeaders() {
367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) {
368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mHandler.sendEmptyMessage(MSG_BUILD_HEADERS);
369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
370263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onConfigurationChanged(Configuration newConfig) {
374263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onConfigurationChanged(newConfig);
3756f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio        Index.getInstance(this).update();
376263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
377263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
378263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
379d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onStart() {
380d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onStart();
381d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
382d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mNeedToRevertToInitialFragment) {
383d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
384263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
385263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
386263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
387263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
388d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onCreateOptionsMenu(Menu menu) {
389d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuInflater inflater = getMenuInflater();
390d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        inflater.inflate(R.menu.options_menu, menu);
391d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
392d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        // Cache the search query (can be overriden by the OnQueryTextListener)
393d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        final String query = mSearchQuery;
394d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
3959593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem = menu.findItem(R.id.search);
3969593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchView = (SearchView) mSearchMenuItem.getActionView();
397d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
39823ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem == null || mSearchView == null) {
39923ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            return false;
40023ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
40123ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio
4029593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem.setOnActionExpandListener(this);
403d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnQueryTextListener(this);
404d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnCloseListener(this);
405d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
406d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mSearchMenuItemExpanded) {
407d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItem.expandActionView();
408d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
409d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setQuery(query, true /* submit */);
410d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
411d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
412d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
413d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
414d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
415d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onCreate(Bundle savedState) {
416263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
417263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
418263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
4195ebabfcd35dd00a1d5beb9bcaca03574ae09b4e8Fabrice Di Meglio
420263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper = new AuthenticatorHelper();
421263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
422263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, null);
423263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
424263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        DevicePolicyManager dpm =
425263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
426dca2806a23d202384eb9b37a5026325f8fb08476Fabrice Di Meglio
427d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mHeaderAdapter = new HeaderAdapter(this, mHeaders, mAuthenticatorHelper, dpm);
428263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
429263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
430263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Context.MODE_PRIVATE);
431263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
432263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getMetaData();
433263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
434d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onCreate(savedState);
435263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
436263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setContentView(R.layout.settings_main);
437263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
438263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getFragmentManager().addOnBackStackChangedListener(this);
439263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
440b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        mDisplayHomeAsUpEnabled = true;
441263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
4425cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        // Getting Intent properties can only be done after the super.onCreate(...)
4435cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        final String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
4445cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio
4455cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        if (initialFragmentName == null) {
4465cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio            Index.getInstance(this).update();
4475cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio        }
4485cda21b449a56de42f7868bdcdaa31bb6f587c03Fabrice Di Meglio
449d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (savedState != null) {
4501800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // We are restarting from a previous saved state; used that to initialize, instead
4511800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            // of starting fresh.
452d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
453d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
454263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
4551800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
4561800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
4571800a9f3e7027e8ab8ffcab51715d39b126bad43Fabrice Di Meglio            setTitle(mInitialTitle);
4585529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
459d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ArrayList<Header> headers = savedState.getParcelableArrayList(SAVE_KEY_HEADERS);
460263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (headers != null) {
461263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaders.addAll(headers);
462b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio                setTitleFromBackStack();
463263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
464b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio
465b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio            mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
466263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else {
4676d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            // We need to build the Headers in all cases
4686d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            onBuildHeaders(mHeaders);
4696d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio
470d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            if (initialFragmentName != null) {
471d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final ComponentName cn = getIntent().getComponent();
472d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                // No UP is we are launched thru a Settings shortcut
473d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                if (!cn.getClassName().equals(SubSettings.class.getName())) {
474b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio                    mDisplayHomeAsUpEnabled = false;
475d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                }
476d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
477d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
478832e54618588fdd31397b1779411af06e58565d7Fabrice Di Meglio                setTitle(mInitialTitle);
479c1457323d271309d5d1955743cd806417c84b9d6Fabrice Di Meglio
4805ebabfcd35dd00a1d5beb9bcaca03574ae09b4e8Fabrice Di Meglio                Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
481d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                switchToFragment( initialFragmentName, initialArguments, true, false,
482d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                        mInitialTitle, false);
48310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            } else {
484d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                // No UP if we are displaying the Headers
485b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio                mDisplayHomeAsUpEnabled = false;
48610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                if (mHeaders.size() > 0) {
487d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    mInitialTitle = getText(R.string.dashboard_title);
488d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    switchToFragment(DashboardSummary.class.getName(), null, false, false,
489d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                            mInitialTitle, false);
49010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                }
491263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
492263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
493263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
494d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar = getActionBar();
495d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar.setHomeButtonEnabled(true);
496b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
497d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
498263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // see if we should show Back/Next buttons
499263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent intent = getIntent();
500263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
501263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
502263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            View buttonBar = findViewById(com.android.internal.R.id.button_bar);
503263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (buttonBar != null) {
504263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                buttonBar.setVisibility(View.VISIBLE);
505263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
506263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button backButton = (Button)findViewById(com.android.internal.R.id.back_button);
507263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                backButton.setOnClickListener(new OnClickListener() {
508263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
509263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_CANCELED);
510263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
511263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
512263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
513263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button);
514263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                skipButton.setOnClickListener(new OnClickListener() {
515263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
516263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
518263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
519263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
520263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton = (Button)findViewById(com.android.internal.R.id.next_button);
521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton.setOnClickListener(new OnClickListener() {
522263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
523263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
524263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
525263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
526263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
527263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
528263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // set our various button parameters
529263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
530263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
531263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
532263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setVisibility(View.GONE);
533263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
534263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
535263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setText(buttonText);
536263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
537263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
538263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
539263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
540263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
541263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setVisibility(View.GONE);
542263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
543263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
544263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setText(buttonText);
545263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
546263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
547263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
548263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    skipButton.setVisibility(View.VISIBLE);
549263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
550263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
551263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
552c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    }
553c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio
554c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    @Override
555263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onBackStackChanged() {
556d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        setTitleFromBackStack();
5578eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    }
5588eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
559b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio    private int setTitleFromBackStack() {
5608eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int count = getFragmentManager().getBackStackEntryCount();
561b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5628eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (count == 0) {
5638eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(mInitialTitle);
564b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio            return 0;
565263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
566b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5678eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
5688eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        setTitleFromBackStackEntry(bse);
569b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
570b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio        return count;
571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
572263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
5738eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
5745529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        final CharSequence title;
5758eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int titleRes = bse.getBreadCrumbTitleRes();
5768eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (titleRes > 0) {
5778eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = getText(titleRes);
5785529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        } else {
5798eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = bse.getBreadCrumbTitle();
5808eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        }
5818eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (title != null) {
5828eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(title);
5835529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        }
5845529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio    }
5855529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
586263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
587263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onSaveInstanceState(Bundle outState) {
588263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onSaveInstanceState(outState);
589263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
590263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mHeaders.size() > 0) {
591d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            outState.putParcelableArrayList(SAVE_KEY_HEADERS, mHeaders);
592263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
593d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
594b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio        outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
595b731dd0554744d91ca89d37ff7c82f8d6e5b502dFabrice Di Meglio
596d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // The option menus are created if the ActionBar is visible and they are also created
597d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // asynchronously. If you launch Settings with an Intent action like
598d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
599d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
600d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // menu item and search view are null.
601d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
602d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
603d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
604d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
605d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putString(SAVE_KEY_SEARCH_QUERY, query);
606263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
607263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
608263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
609263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onResume() {
610263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onResume();
611263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
612263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
613263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
614263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
615263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                invalidateHeaders();
616263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
617263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
618263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
619263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
620263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
621263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
622263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
623263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
624853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
625990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.register(this);
626a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio
627a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        if(!TextUtils.isEmpty(mSearchQuery)) {
628a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            onQueryTextSubmit(mSearchQuery);
629a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
630263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
631263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
632263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
633263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onPause() {
634263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPause();
635263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
636263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        unregisterReceiver(mBatteryInfoReceiver);
637263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
638263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
639263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
640263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
641263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = null;
642853e47181dc116ba95e91463735dc79ad4bdc95aSvetoslav
643990159abaad7f314fbe1f9eaf064a088806ffb19Svetoslav        mDynamicIndexableContentMonitor.unregister();
644263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
645263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
646263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
647263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onDestroy() {
648263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onDestroy();
649263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mListeningToAccountUpdates) {
650263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).removeOnAccountsUpdatedListener(this);
651263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
652263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
653263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
654263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected boolean isValidFragment(String fragmentName) {
655263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Almost all fragments are wrapped in this,
656263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // except for a few that have their own activities.
657263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
658263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
659263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
660263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
661263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
662263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
6636d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio    /**
6646d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * When in two-pane mode, switch to the fragment pane to show the given
6656d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * preference fragment.
6666d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     *
6676d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * @param header The new header to display.
668bbdada8fab2dcc28e869baca7eb7a57e8f0ec88bFabrice Di Meglio     * @param position The position of the Header in the list.
6696d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     */
670bbdada8fab2dcc28e869baca7eb7a57e8f0ec88bFabrice Di Meglio    private void onHeaderClick(Header header, int position) {
6716d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio        if (header == null) {
6726d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            return;
6736d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio        }
674d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (header.fragment != null) {
675d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startWithFragment(header.fragment, header.fragmentArguments, null, 0,
676d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    header.getTitle(getResources()));
677d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        } else if (header.intent != null) {
678d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startActivity(header.intent);
679263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
680263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
681263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
682263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called to determine whether the header list should be hidden.
684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * The default implementation returns the
685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied.
686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * This is set to false, for example, when the activity is being re-launched
687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to show a particular preference activity.
688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onIsHidingHeaders() {
690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
692263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
693263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Intent getIntent() {
695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent superIntent = super.getIntent();
696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String startingFragment = getStartingFragmentClass(superIntent);
697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // This is called from super.onCreate, isMultiPane() is not yet reliable
698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Do not use onIsHidingHeaders either, which relies itself on this method
699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (startingFragment != null) {
700263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Intent modIntent = new Intent(superIntent);
701263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
702263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle args = superIntent.getExtras();
703263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (args != null) {
704263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle(args);
705263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle();
707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            args.putParcelable("intent", superIntent);
709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return modIntent;
711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return superIntent;
713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
714263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Checks if the component name in the intent is different from the Settings class and
717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * returns the class name to load as a fragment.
718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String getStartingFragmentClass(Intent intent) {
720263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mFragmentClass != null) return mFragmentClass;
721263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
722263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String intentClass = intent.getComponent().getClassName();
723263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intentClass.equals(getClass().getName())) return null;
724263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
725263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ("com.android.settings.ManageApplications".equals(intentClass)
726263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.RunningServices".equals(intentClass)
727263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.applications.StorageUse".equals(intentClass)) {
728263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Old names of manage apps.
729263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            intentClass = com.android.settings.applications.ManageApplications.class.getName();
730263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
731263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
732263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return intentClass;
733263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
734263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
735263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
73610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment containing a preference panel.  If the preferences
73710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * are being displayed in multi-pane mode, the given fragment class will
73810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * be instantiated and placed in the appropriate pane.  If running in
73910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * single-pane mode, a new activity will be launched in which to show the
74010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
74110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
74210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentClass Full name of the class implementing the fragment.
74310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Any desired arguments to supply to the fragment.
74410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Optional resource identifier of the title of this
74510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
74610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText Optional text of the title of this fragment.
74710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Optional fragment that result data should be sent to.
74810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * If non-null, resultTo.onActivityResult() will be called when this
74910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference panel is done.  The launched panel must use
75010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
75110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the caller's
75210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * request code to be received with the resut.
75310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
75410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
755d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            CharSequence titleText, Fragment resultTo, int resultRequestCode) {
756911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        String title;
757911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        if (titleRes > 0) {
758911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio            title = getString(titleRes);
759821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio        } else if (titleText != null) {
760911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio            title = titleText.toString();
761821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio        } else {
762821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio            // There not much we can do in that case
763821a0729ad4060b54a8311c957534117b84bc881Fabrice Di Meglio            title = "";
764911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        }
765911fb2ad907d47a882c023b871c6457db32359f8Fabrice Di Meglio        startWithFragment(fragmentClass, args, resultTo, resultRequestCode, title);
76610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
76710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
76810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
769263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called by a preference panel fragment to finish itself.
770263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
771263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param caller The fragment that is asking to be finished.
772263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultCode Optional result code to send back to the original
773263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
774263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultData Optional result data to send back to the original
775263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
776263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
777263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
778263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setResult(resultCode, resultData);
779263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
780263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
781263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
78210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
78310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
78410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragment The fragment to start
78510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
78610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * the current fragment will be replaced.
78710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
78810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferenceFragment(Fragment fragment, boolean push) {
78910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
79010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, fragment);
79110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (push) {
79210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
79310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.addToBackStack(BACK_STACK_PREFS);
79410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
79510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
79610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
79710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
79810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
79910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
80010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
801d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Switch to a specific Fragment with taking care of validation, Title and BackStack
802d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
803d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
804d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            boolean addToBackStack, CharSequence title, boolean withTransition) {
805d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (validate && !isValidFragment(fragmentName)) {
806d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            throw new IllegalArgumentException("Invalid fragment for this activity: "
807d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    + fragmentName);
808d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
809d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
810d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
811d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.replace(R.id.prefs, f);
812d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (withTransition) {
813d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
814d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
815d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (addToBackStack) {
816d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
817d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
818d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (title != null) {
819d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setBreadCrumbTitle(title);
820d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
821d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.commitAllowingStateLoss();
822d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return f;
823d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
824d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
825d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
826d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Start a new instance of this activity, showing only the given fragment.
827d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * When launched in this mode, the given preference fragment will be instantiated and fill the
828d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * entire activity.
829d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     *
830d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param fragmentName The name of the fragment to display.
831d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
832d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param resultTo Option fragment that should receive the result of
833d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * the activity launch.
834d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the request
835d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * code in which to report the result.
836d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param title String to display for the title of this set of preferences.
837d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
838d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void startWithFragment(String fragmentName, Bundle args,
839d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                                  Fragment resultTo, int resultRequestCode, CharSequence title) {
840d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Intent intent = onBuildStartFragmentIntent(fragmentName, args, title);
841d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (resultTo == null) {
842d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startActivity(intent);
843d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        } else {
844d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            resultTo.startActivityForResult(intent, resultRequestCode);
845d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
846d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
847d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
848d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
849d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Build an Intent to launch a new activity showing the selected fragment.
850d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * The implementation constructs an Intent that re-launches the current activity with the
851d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * appropriate arguments to display the fragment.
852d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     *
853d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param fragmentName The name of the fragment to display.
854d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
855d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param title Optional title to show for this item.
856d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @return Returns an Intent that can be launched to display the given
857d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * fragment.
858d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
859d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, CharSequence title) {
860d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Intent intent = new Intent(Intent.ACTION_MAIN);
861d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.setClass(this, SubSettings.class);
862d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
863d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
864d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, title);
865d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_NO_HEADERS, true);
866d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return intent;
867d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
868d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
869d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
8706d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * Called when the activity needs its list of headers build.
871263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
872263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param headers The list in which to place the headers.
873263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
874263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void onBuildHeaders(List<Header> headers) {
875263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        loadHeadersFromResource(R.xml.settings_headers, headers);
876263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        updateHeaderList(headers);
877263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
878263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
879263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
880263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Parse the given XML file as a header description, adding each
881263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * parsed Header into the target list.
882263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
883263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resid The XML resource to load and parse.
884263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param target The list in which the parsed headers should be placed.
885263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
886263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void loadHeadersFromResource(int resid, List<Header> target) {
887263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        XmlResourceParser parser = null;
888263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
889263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            parser = getResources().getXml(resid);
890263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AttributeSet attrs = Xml.asAttributeSet(parser);
891263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
892263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int type;
893263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
894263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && type != XmlPullParser.START_TAG) {
895263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Parse next until start tag is found
896263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
897263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
898263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String nodeName = parser.getName();
899263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (!"preference-headers".equals(nodeName)) {
900263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                throw new RuntimeException(
901263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        "XML document must start with <preference-headers> tag; found"
902263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                + nodeName + " at " + parser.getPositionDescription());
903263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
904263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
905263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle curBundle = null;
906263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
907263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final int outerDepth = parser.getDepth();
908263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
909263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
910263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
911263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    continue;
912263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
913263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
914263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                nodeName = parser.getName();
915263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if ("header".equals(nodeName)) {
916263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    Header header = new Header();
917263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
918263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedArray sa = obtainStyledAttributes(
919263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            attrs, com.android.internal.R.styleable.PreferenceHeader);
920263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.id = sa.getResourceId(
921263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_id,
922263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            (int)HEADER_ID_UNDEFINED);
923263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedValue tv = sa.peekValue(
924263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_title);
925263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
926263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
927263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.titleRes = tv.resourceId;
928263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
929263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.title = tv.string;
930263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
931263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
932263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    tv = sa.peekValue(
933263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_summary);
934263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
935263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
936263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summaryRes = tv.resourceId;
937263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
938263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summary = tv.string;
939263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
940263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
941263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.iconRes = sa.getResourceId(
942263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_icon, 0);
943263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragment = sa.getString(
944263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_fragment);
945263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sa.recycle();
946263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
947263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle == null) {
948263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = new Bundle();
949263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
950263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
951263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    final int innerDepth = parser.getDepth();
952263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
953263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
954263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
955263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            continue;
956263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
957263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
958263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        String innerNodeName = parser.getName();
959263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (innerNodeName.equals("extra")) {
960263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            getResources().parseBundleExtra("extra", attrs, curBundle);
961263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
962263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
963263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else if (innerNodeName.equals("intent")) {
964263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.intent = Intent.parseIntent(getResources(), parser, attrs);
965263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
966263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
967263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
968263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
969263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
970263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
971263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle.size() > 0) {
972263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        header.fragmentArguments = curBundle;
973263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = null;
974263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
975263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
976263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.add(header);
977263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
978263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    XmlUtils.skipCurrentTag(parser);
979263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
980263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
981263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
982263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (XmlPullParserException e) {
983263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
984263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (IOException e) {
985263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
986263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } finally {
987263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (parser != null) parser.close();
988263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
989263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
990263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
991263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void updateHeaderList(List<Header> target) {
992263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final boolean showDev = mDevelopmentPreferences.getBoolean(
993263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                DevelopmentSettings.PREF_SHOW,
994263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                android.os.Build.TYPE.equals("eng"));
995263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int i = 0;
996263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
997263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
998263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderIndexMap.clear();
999263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        while (i < target.size()) {
1000263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = target.get(i);
1001263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Ids are integers, so downcasting
1002263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int id = (int) header.id;
1003263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1004263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
1005263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.wifi_settings) {
1006263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove WiFi Settings if WiFi service is not available.
1007263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1008263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1009263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1010263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.bluetooth_settings) {
1011263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove Bluetooth Settings if Bluetooth service is not available.
1012263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1013263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1014263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1015263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.data_usage_settings) {
1016263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove data usage when kernel module not enabled
1017263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                final INetworkManagementService netManager = INetworkManagementService.Stub
1018263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1019263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                try {
1020263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!netManager.isBandwidthControlEnabled()) {
1021263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1022263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1023263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } catch (RemoteException e) {
1024263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // ignored
1025263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1026263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.battery_settings) {
1027263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove battery settings when battery is not available. (e.g. TV)
1028263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1029263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!mBatteryPresent) {
1030263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1031263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1032263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_settings) {
1033263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                int headerIndex = i + 1;
1034263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i = insertAccountsHeaders(target, headerIndex);
1035263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.home_settings) {
1036263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!updateHomeSettingHeaders(header)) {
1037263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1038263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1039263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.user_settings) {
1040263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!UserHandle.MU_ENABLED
1041263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || !UserManager.supportsMultipleUsers()
1042263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || Utils.isMonkeyRunning()) {
1043263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1044263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1045263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.nfc_payment_settings) {
1046263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1047263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1048263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1049263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // Only show if NFC is on and we have the HCE feature
1050263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1051263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1052263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1053263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1054263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1055263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1056263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.development_settings) {
1057263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!showDev) {
1058263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1059263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1060263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_add) {
1061263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1062263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1063263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1064263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1065263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1066263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header
1067263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
1068263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1069263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                target.remove(i);
1070263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1071263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1072263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Increment if the current one wasn't removed by the Utils code.
1073263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header) {
1074263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaderIndexMap.put(id, i);
1075263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i++;
1076263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1077263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1078263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1079263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1080263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int insertAccountsHeaders(List<Header> target, int headerIndex) {
1081263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1082263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
1083263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (String accountType : accountTypes) {
1084263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1085263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (label == null) {
1086263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                continue;
1087263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1088263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1089263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1090263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            boolean skipToAccount = accounts.length == 1
1091263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1092263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header accHeader = new Header();
1093263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accHeader.title = label;
1094263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (accHeader.extras == null) {
1095263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras = new Bundle();
1096263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1097263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (skipToAccount) {
1098263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = AccountSyncSettings.class.getName();
1099263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Need this for the icon
1101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accounts[0]);
1105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = ManageAccountsSettings.class.getName();
1107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accountType);
1111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            label.toString());
1113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accountHeaders.add(accHeader);
1115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Sort by label
1119263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Collections.sort(accountHeaders, new Comparator<Header>() {
1120263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
1121263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public int compare(Header h1, Header h2) {
1122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return h1.title.toString().compareTo(h2.title.toString());
1123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        });
1125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (Header header : accountHeaders) {
1127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            target.add(headerIndex++, header);
1128263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1129263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mListeningToAccountUpdates) {
1130263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mListeningToAccountUpdates = true;
1132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return headerIndex;
1134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean updateHomeSettingHeaders(Header header) {
1137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Once we decide to show Home settings, keep showing it forever
1138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return true;
1141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getPackageManager().getHomeActivities(homeApps);
1146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (homeApps.size() < 2) {
1147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // When there's only one available home app, omit this settings
1148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // category entirely at the top level UI.  If the user just
1149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // uninstalled the penultimate home app candidiate, we also
1150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // now tell them about why they aren't seeing 'Home' in the list.
1151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (sShowNoHomeNotice) {
1152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sShowNoHomeNotice = false;
1153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NoHomeDialogFragment.show(this);
1154263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1155263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return false;
1156263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1157263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Okay, we're allowing the Home settings category.  Tell it, when
1158263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // invoked via this front door, that we'll need to be told about the
1159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // case when the user uninstalls all but one home app.
1160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (header.fragmentArguments == null) {
1161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragmentArguments = new Bundle();
1162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                header.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (Exception e) {
1166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Can't look up the home activity; bail on configuring the icon
1167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.w(LOG_TAG, "Problem looking up home activity!", e);
1168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
1172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void getMetaData() {
1175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    PackageManager.GET_META_DATA);
1178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ai == null || ai.metaData == null) return;
1179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (NameNotFoundException nnfe) {
1181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // No recovery
1182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // give subclasses access to the Next button
1187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean hasNextButton() {
1188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton != null;
1189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Button getNextButton() {
1192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton;
1193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1195d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public HeaderAdapter getHeaderAdapter() {
1196d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mHeaderAdapter;
1197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1199d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void onListItemClick(ListView l, View v, int position, long id) {
1200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!isResumed()) {
1201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return;
1202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Object item = mHeaderAdapter.getItem(position);
1204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (item instanceof Header) {
12056502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio            mSelectedHeader = (Header) item;
1206bbdada8fab2dcc28e869baca7eb7a57e8f0ec88bFabrice Di Meglio            onHeaderClick(mSelectedHeader, position);
1207d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
1208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean shouldUpRecreateTask(Intent targetIntent) {
1213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onAccountsUpdated(Account[] accounts) {
1218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // TODO: watch for package upgrades to invalidate cache; see 7206643
1219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
1220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, accounts);
1221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
1222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static void requestHomeNotice() {
1225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sShowNoHomeNotice = true;
1226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1228d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1229d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextSubmit(String query) {
1230d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        switchToSearchResultsFragmentIfNeeded();
1231d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = query;
1232d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextSubmit(query);
1233d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1235d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1236d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextChange(String newText) {
1237d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = newText;
1238a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        if (TextUtils.isEmpty(newText) && mSearchResultsFragment == null) {
1239a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio            return false;
1240a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        }
1241a32707664f85b77602d1fe45458e92fce1448ce2Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextChange(newText);
1242d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1244d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1245d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onClose() {
1246d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return false;
1247d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1249d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1250d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionExpand(MenuItem item) {
1251d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1252bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            switchToSearchResultsFragmentIfNeeded();
1253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1254d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1255d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1257d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1258d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionCollapse(MenuItem item) {
1259d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1260bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            if (mSearchMenuItemExpanded) {
1261d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                revertToInitialFragment();
1262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1264d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1265d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1267d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void switchToSearchResultsFragmentIfNeeded() {
1268bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (mSearchResultsFragment != null) {
1269bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            return;
1270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1271bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1272bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        if (current != null && current instanceof SearchResultsSummary) {
1273bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) current;
1274bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        } else {
1275bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            String title = getString(R.string.search_results_title);
1276bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio            mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
1277bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio                    SearchResultsSummary.class.getName(), null, false, true, title, true);
1278bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        }
1279bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = true;
1280d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1281263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1282d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void needToRevertToInitialFragment() {
1283d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = true;
1284d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1286d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void revertToInitialFragment() {
1287d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = false;
1288d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchResultsFragment = null;
1289bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        mSearchMenuItemExpanded = false;
1290bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio        getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1291bb16fd836a5347413e592acbffa189156b28bc53Fabrice Di Meglio                FragmentManager.POP_BACK_STACK_INCLUSIVE);
129223ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        if (mSearchMenuItem != null) {
129323ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio            mSearchMenuItem.collapseActionView();
129423ae00c1d87d64578cd62da1894ee25da531ca11Fabrice Di Meglio        }
1295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
1297