SettingsActivity.java revision d6985df419549fbb7d420eced8e89f0429e49d71
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;
87fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglioimport com.android.settings.search.Index;
88263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.InputMethodAndLanguageSettings;
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.SpellCheckersSettings;
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.UserDictionaryList;
92263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.location.LocationSettings;
93263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.AndroidBeam;
94263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.PaymentSettings;
95263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintJobSettingsFragment;
96263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintSettingsFragment;
97263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.tts.TextToSpeechSettings;
98263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.users.UserSettings;
99263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.vpn2.VpnSettings;
100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wfd.WifiDisplaySettings;
101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.AdvancedWifiSettings;
102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiSettings;
103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.p2p.WifiP2pSettings;
104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParser;
105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParserException;
106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.io.IOException;
108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.ArrayList;
109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Collections;
110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Comparator;
111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.HashMap;
112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.List;
113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
114d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglioimport static com.android.settings.dashboard.Header.HEADER_ID_UNDEFINED;
115d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopublic class SettingsActivity extends Activity
117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        implements PreferenceManager.OnPreferenceTreeClickListener,
118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        PreferenceFragment.OnPreferenceStartFragmentCallback,
119d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
120d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        SearchView.OnQueryTextListener, SearchView.OnCloseListener,
121d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuItem.OnActionExpandListener {
122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String LOG_TAG = "Settings";
124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Constants for state save/restore
126d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_HEADERS = ":settings:headers";
127d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private static final String SAVE_KEY_CURRENT_HEADER = ":settings:cur_header";
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";
130263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * string to specify which fragment should be initially displayed.
134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will call isValidFragment() to confirm that the fragment class name is valid for this
136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity.
137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specified to supply a Bundle of arguments to pass
143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to that fragment when it is instantiated during the initial creation
144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * of the activity.
145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * boolean that the header list should not be displayed.  This is most often
151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch
152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * the activity to display a specific fragment that the user has navigated
153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to.
154263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
155263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_NO_HEADERS = ":settings:no_headers";
156263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1576f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio    public static final String BACK_STACK_PREFS = ":settings:prefs";
1586f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // extras that allow any preference activity to be launched as part of a wizard
160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // show Back and Next buttons? takes boolean parameter
162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Back will then return RESULT_CANCELED and Next RESULT_OK
163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // add a Skip button?
166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // specify custom text for the Back or Next buttons, or cause a button to not appear
169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // at all by setting it to null
170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specify to supply the title to be shown for
176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that fragment.
177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
178d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_HEADER_ID =
181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.TOP_LEVEL_HEADER_ID";
182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_FRAGMENT_CLASS =
184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.FRAGMENT_CLASS";
185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
188d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio    private static final String EMPTY_QUERY = "";
189d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static boolean sShowNoHomeNotice = false;
191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String mFragmentClass;
1936502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio    private Header mSelectedHeader;
194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Header mCurrentHeader;
195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1968eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private CharSequence mInitialTitle;
1978eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Show only these settings for restricted users
199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int[] SETTINGS_FOR_RESTRICTED = {
200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_section,
201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wifi_settings,
202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.bluetooth_settings,
203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.data_usage_settings,
204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_settings,
205263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.device_section,
206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.sound_settings,
207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.display_settings,
208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.storage_settings,
209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.application_settings,
210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.battery_settings,
211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.personal_section,
212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.location_settings,
213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.security_settings,
214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.language_settings,
215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.user_settings,
216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_settings,
217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_add,
218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.system_section,
219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.date_time_settings,
220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.about_settings,
221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.accessibility_settings,
222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.print_settings,
223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.nfc_payment_settings,
2242858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.home_settings,
2252858b790ad92bb39e26150d696432f0b717bcddcFabrice Di Meglio            R.id.dashboard
226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String[] ENTRY_FRAGMENTS = {
229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WirelessSettings.class.getName(),
230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiSettings.class.getName(),
231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AdvancedWifiSettings.class.getName(),
232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            BluetoothSettings.class.getName(),
233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TetherSettings.class.getName(),
234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiP2pSettings.class.getName(),
235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            VpnSettings.class.getName(),
236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DateTimeSettings.class.getName(),
237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocalePicker.class.getName(),
238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            InputMethodAndLanguageSettings.class.getName(),
239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SpellCheckersSettings.class.getName(),
240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionaryList.class.getName(),
241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionarySettings.class.getName(),
242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SoundSettings.class.getName(),
243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DisplaySettings.class.getName(),
244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceInfoSettings.class.getName(),
245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageApplications.class.getName(),
246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ProcessStatsUi.class.getName(),
247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationStation.class.getName(),
248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocationSettings.class.getName(),
249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SecuritySettings.class.getName(),
250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrivacySettings.class.getName(),
251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceAdminSettings.class.getName(),
252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccessibilitySettings.class.getName(),
253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CaptionPropertiesFragment.class.getName(),
254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleContrastPreferenceFragment.class.getName(),
256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextToSpeechSettings.class.getName(),
258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Memory.class.getName(),
259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DevelopmentSettings.class.getName(),
260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UsbSettings.class.getName(),
261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AndroidBeam.class.getName(),
262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiDisplaySettings.class.getName(),
263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PowerUsageSummary.class.getName(),
264263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountSyncSettings.class.getName(),
265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CryptKeeperSettings.class.getName(),
266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DataUsageSummary.class.getName(),
267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DreamSettings.class.getName(),
268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserSettings.class.getName(),
269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationAccessSettings.class.getName(),
270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageAccountsSettings.class.getName(),
271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintSettingsFragment.class.getName(),
272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintJobSettingsFragment.class.getName(),
273263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TrustedCredentialsSettings.class.getName(),
274263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PaymentSettings.class.getName(),
275263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            KeyboardLayoutPickerFragment.class.getName(),
276d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ZenModeSettings.class.getName(),
277d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            NotificationSettings.class.getName(),
278d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
279d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
280d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            InstalledAppDetails.class.getName()
281263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
282263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
283263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences mDevelopmentPreferences;
284263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
286263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private AuthenticatorHelper mAuthenticatorHelper;
287263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mListeningToAccountUpdates;
288263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mBatteryPresent = true;
290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onReceive(Context context, Intent intent) {
294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String action = intent.getAction();
295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                boolean batteryPresent = Utils.isBatteryPresent(intent);
297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mBatteryPresent != batteryPresent) {
299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mBatteryPresent = batteryPresent;
300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    invalidateHeaders();
301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
302263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
303263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
304263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
305263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
306d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Button mNextButton;
307d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private ActionBar mActionBar;
308d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
309d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchView mSearchView;
310d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private MenuItem mSearchMenuItem;
311d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mSearchMenuItemExpanded = false;
312d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mIsShowingSearchResults = false;
313d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private SearchResultsSummary mSearchResultsFragment;
314d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private String mSearchQuery;
315d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
316d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    // Headers
317d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
318263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private final ArrayList<Header> mHeaders = new ArrayList<Header>();
319263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private HeaderAdapter mHeaderAdapter;
320263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
321263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final int MSG_BUILD_HEADERS = 1;
322263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Handler mHandler = new Handler() {
323263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
324263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void handleMessage(Message msg) {
325263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (msg.what) {
326263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case MSG_BUILD_HEADERS: {
327263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaders.clear();
328263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    onBuildHeaders(mHeaders);
329263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaderAdapter.notifyDataSetChanged();
330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } break;
331263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
332263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
334263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
335d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private boolean mNeedToRevertToInitialFragment = false;
336d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
338263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
339263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Override the fragment title for Wallpaper settings
340263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int titleRes = pref.getTitleRes();
341263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
342263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = R.string.wallpaper_settings_fragment_title;
343263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
344263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                && UserHandle.myUserId() != UserHandle.USER_OWNER) {
345263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (UserManager.get(this).isLinkedUser()) {
346263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.profile_info_settings_title;
347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
348263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.user_info_settings_title;
349263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
350263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
351263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
352263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                null, 0);
353263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
354263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
355263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
356263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
357263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
360263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
361263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void invalidateHeaders() {
362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) {
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mHandler.sendEmptyMessage(MSG_BUILD_HEADERS);
364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onConfigurationChanged(Configuration newConfig) {
369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onConfigurationChanged(newConfig);
3706f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio        Index.getInstance(this).update();
371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
374d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onStart() {
375d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onStart();
376d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
377d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mNeedToRevertToInitialFragment) {
378d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
379263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
380263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
381263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
382263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
383d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onCreateOptionsMenu(Menu menu) {
384d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        MenuInflater inflater = getMenuInflater();
385d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        inflater.inflate(R.menu.options_menu, menu);
386d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
387d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        // Cache the search query (can be overriden by the OnQueryTextListener)
388d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        final String query = mSearchQuery;
389d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
3909593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem = menu.findItem(R.id.search);
3919593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchView = (SearchView) mSearchMenuItem.getActionView();
392d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
3939593782d154f378a0c911cd015d410c18fe74a92Fabrice Di Meglio        mSearchMenuItem.setOnActionExpandListener(this);
394d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnQueryTextListener(this);
395d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setOnCloseListener(this);
396d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
397d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (mSearchMenuItemExpanded) {
398d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItem.expandActionView();
399d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
400d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchView.setQuery(query, true /* submit */);
401d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
402d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
403d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
404d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
405d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
406d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    protected void onCreate(Bundle savedState) {
407263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
408263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
409263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
410263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
4116f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio        Index.getInstance(this).update();
4126f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
413263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper = new AuthenticatorHelper();
414263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
415263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, null);
416263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
417263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        DevicePolicyManager dpm =
418263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
419dca2806a23d202384eb9b37a5026325f8fb08476Fabrice Di Meglio
420d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mHeaderAdapter = new HeaderAdapter(this, mHeaders, mAuthenticatorHelper, dpm);
421263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
422263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
423263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Context.MODE_PRIVATE);
424263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
425263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getMetaData();
426263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
427d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        super.onCreate(savedState);
428263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
429263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setContentView(R.layout.settings_main);
430263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
431263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getFragmentManager().addOnBackStackChangedListener(this);
432263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
433d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        boolean displayHomeAsUpEnabled = true;
434263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
435d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
436d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
437263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
438d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (savedState != null) {
439d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
440d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
441263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
442d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    // We are restarting from a previous saved state; used that to
443263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // initialize, instead of starting fresh.
4448eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            mInitialTitle = getTitle();
4455529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
446d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            ArrayList<Header> headers = savedState.getParcelableArrayList(SAVE_KEY_HEADERS);
447263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (headers != null) {
448263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaders.addAll(headers);
449b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio                setTitleFromBackStack();
450263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
451263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else {
4526d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            // We need to build the Headers in all cases
4536d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            onBuildHeaders(mHeaders);
4546d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio
455d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            if (initialFragmentName != null) {
456d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final ComponentName cn = getIntent().getComponent();
457d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                // No UP is we are launched thru a Settings shortcut
458d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                if (!cn.getClassName().equals(SubSettings.class.getName())) {
459d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    displayHomeAsUpEnabled = false;
460d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                }
461d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
462d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
463832e54618588fdd31397b1779411af06e58565d7Fabrice Di Meglio                setTitle(mInitialTitle);
464d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                switchToFragment( initialFragmentName, initialArguments, true, false,
465d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                        mInitialTitle, false);
46610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            } else {
467d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                // No UP if we are displaying the Headers
468d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                displayHomeAsUpEnabled = false;
46910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                if (mHeaders.size() > 0) {
470d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    mInitialTitle = getText(R.string.dashboard_title);
471d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    switchToFragment(DashboardSummary.class.getName(), null, false, false,
472d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                            mInitialTitle, false);
47310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                }
474263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
475263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
476263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
477d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar = getActionBar();
478d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar.setHomeButtonEnabled(true);
479d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mActionBar.setDisplayHomeAsUpEnabled(displayHomeAsUpEnabled);
480d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
481263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // see if we should show Back/Next buttons
482263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent intent = getIntent();
483263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
484263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
485263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            View buttonBar = findViewById(com.android.internal.R.id.button_bar);
486263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (buttonBar != null) {
487263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                buttonBar.setVisibility(View.VISIBLE);
488263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
489263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button backButton = (Button)findViewById(com.android.internal.R.id.back_button);
490263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                backButton.setOnClickListener(new OnClickListener() {
491263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
492263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_CANCELED);
493263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
494263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
495263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
496263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button);
497263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                skipButton.setOnClickListener(new OnClickListener() {
498263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
499263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
500263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
501263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
502263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
503263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton = (Button)findViewById(com.android.internal.R.id.next_button);
504263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton.setOnClickListener(new OnClickListener() {
505263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
506263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
507263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
508263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
509263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
510263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
511263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // set our various button parameters
512263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
513263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
514263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
515263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setVisibility(View.GONE);
516263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
518263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setText(buttonText);
519263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
520263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
522263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
523263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
524263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setVisibility(View.GONE);
525263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
526263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
527263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setText(buttonText);
528263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
529263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
530263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
531263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    skipButton.setVisibility(View.VISIBLE);
532263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
533263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
534263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
535c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    }
536c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio
537c95be4fbb3ec437517a2190d88fbda5068dba8dfFabrice Di Meglio    @Override
538263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onBackStackChanged() {
539d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        setTitleFromBackStack();
5408eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    }
5418eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
542b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio    private int setTitleFromBackStack() {
5438eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int count = getFragmentManager().getBackStackEntryCount();
544b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5458eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (count == 0) {
5468eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(mInitialTitle);
547b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio            return 0;
548263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
549b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
5508eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
5518eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        setTitleFromBackStackEntry(bse);
552b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio
553b643cbf6d60bb4cd43d3191106d68680fffe97b5Fabrice Di Meglio        return count;
554263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
555263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
5568eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio    private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
5575529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        final CharSequence title;
5588eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final int titleRes = bse.getBreadCrumbTitleRes();
5598eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (titleRes > 0) {
5608eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = getText(titleRes);
5615529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        } else {
5628eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            title = bse.getBreadCrumbTitle();
5638eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        }
5648eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (title != null) {
5658eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            setTitle(title);
5665529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio        }
5675529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio    }
5685529d29f6c22e9f28444ca1c0aa9ea2534264220Fabrice Di Meglio
569263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
570263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onSaveInstanceState(Bundle outState) {
571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onSaveInstanceState(outState);
572263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
573263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mHeaders.size() > 0) {
574d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            outState.putParcelableArrayList(SAVE_KEY_HEADERS, mHeaders);
5756502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio            if (mCurrentHeader != null) {
5766502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio                int index = mHeaders.indexOf(mCurrentHeader);
577263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (index >= 0) {
578d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    outState.putInt(SAVE_KEY_CURRENT_HEADER, index);
579263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
580263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
581263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
582d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
583d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // The option menus are created if the ActionBar is visible and they are also created
584d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // asynchronously. If you launch Settings with an Intent action like
585d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
586d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
587d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        // menu item and search view are null.
588d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
589d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
590d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio
591d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
592d6985df419549fbb7d420eced8e89f0429e49d71Fabrice Di Meglio        outState.putString(SAVE_KEY_SEARCH_QUERY, query);
593263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
594263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
595263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
596263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onResume() {
597263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onResume();
598263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
599263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
600263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
601263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
602263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                invalidateHeaders();
603263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
604263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
605263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
606263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
607263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
608a504c4dcdf18be1be9f67cf83b506638f19077b8Matthew Xie        mHeaderAdapter.resume(this);
609263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
610263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
611263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
612263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
613263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
614263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
615263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onPause() {
616263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPause();
617263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
618263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        unregisterReceiver(mBatteryInfoReceiver);
619263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
620263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderAdapter.pause();
621263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
622263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
623263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
624263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
625263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = null;
626263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
627263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
628263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
629263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onDestroy() {
630263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onDestroy();
631263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mListeningToAccountUpdates) {
632263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).removeOnAccountsUpdatedListener(this);
633263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
634263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
635263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
636263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected boolean isValidFragment(String fragmentName) {
637263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Almost all fragments are wrapped in this,
638263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // except for a few that have their own activities.
639263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
640263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
641263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
642263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
643263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
644263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
6456d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio    /**
6466d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * When in two-pane mode, switch to the fragment pane to show the given
6476d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * preference fragment.
6486d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     *
6496d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * @param header The new header to display.
6506d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     */
651d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void onHeaderClick(Header header) {
6526d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio        if (header == null) {
6536d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio            return;
6546d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio        }
655d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (header.fragment != null) {
656d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startWithFragment(header.fragment, header.fragmentArguments, null, 0,
657d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    header.getTitle(getResources()));
658d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        } else if (header.intent != null) {
659d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startActivity(header.intent);
660d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        } else {
661d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            throw new IllegalStateException(
662d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    "Can't switch to header that has no Fragment nor Intent");
663263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
664263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
665263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
666263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
667263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called to determine whether the header list should be hidden.
668263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * The default implementation returns the
669263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied.
670263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * This is set to false, for example, when the activity is being re-launched
671263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to show a particular preference activity.
672263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
673263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onIsHidingHeaders() {
674263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
675263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
676263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
677263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
678263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Intent getIntent() {
679263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent superIntent = super.getIntent();
680263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String startingFragment = getStartingFragmentClass(superIntent);
681263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // This is called from super.onCreate, isMultiPane() is not yet reliable
682263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Do not use onIsHidingHeaders either, which relies itself on this method
683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (startingFragment != null) {
684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Intent modIntent = new Intent(superIntent);
685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle args = superIntent.getExtras();
687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (args != null) {
688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle(args);
689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle();
691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
692263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            args.putParcelable("intent", superIntent);
693263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return modIntent;
695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return superIntent;
697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
700263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Checks if the component name in the intent is different from the Settings class and
701263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * returns the class name to load as a fragment.
702263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
703263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String getStartingFragmentClass(Intent intent) {
704263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mFragmentClass != null) return mFragmentClass;
705263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String intentClass = intent.getComponent().getClassName();
707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intentClass.equals(getClass().getName())) return null;
708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ("com.android.settings.ManageApplications".equals(intentClass)
710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.RunningServices".equals(intentClass)
711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.applications.StorageUse".equals(intentClass)) {
712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Old names of manage apps.
713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            intentClass = com.android.settings.applications.ManageApplications.class.getName();
714263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return intentClass;
717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
72010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment containing a preference panel.  If the preferences
72110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * are being displayed in multi-pane mode, the given fragment class will
72210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * be instantiated and placed in the appropriate pane.  If running in
72310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * single-pane mode, a new activity will be launched in which to show the
72410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
72510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
72610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentClass Full name of the class implementing the fragment.
72710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Any desired arguments to supply to the fragment.
72810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Optional resource identifier of the title of this
72910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
73010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText Optional text of the title of this fragment.
73110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Optional fragment that result data should be sent to.
73210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * If non-null, resultTo.onActivityResult() will be called when this
73310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference panel is done.  The launched panel must use
73410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
73510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the caller's
73610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * request code to be received with the resut.
73710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
73810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
739d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            CharSequence titleText, Fragment resultTo, int resultRequestCode) {
740d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        switchToFragment(fragmentClass, args, resultTo, resultRequestCode, titleRes, titleText);
74110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
74210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
74310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
744263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called by a preference panel fragment to finish itself.
745263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
746263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param caller The fragment that is asking to be finished.
747263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultCode Optional result code to send back to the original
748263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
749263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultData Optional result data to send back to the original
750263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
751263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
752263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
753263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setResult(resultCode, resultData);
754263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
755263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
756263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
75710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
75810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
75910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragment The fragment to start
76010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
76110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * the current fragment will be replaced.
76210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
76310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferenceFragment(Fragment fragment, boolean push) {
76410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
76510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, fragment);
76610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (push) {
76710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
76810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.addToBackStack(BACK_STACK_PREFS);
76910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
77010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
77110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
77210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
77310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
77410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
77510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
776d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Start a new fragment. Used by #startPreferencePanel.
77710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
77810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentName The name of the fragment to display.
77910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
78010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Option fragment that should receive the result of
78110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 the activity launch.
78210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the request code in which to
78310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                          report the result.
78410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Resource ID of string to display for the title of. If the Resource ID is a
78510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 valid one then it will be used to get the title. Otherwise the titleText
78610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 argument will be used as the title.
78710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText string to display for the title of.
78810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
789d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void switchToFragment(String fragmentName, Bundle args, Fragment resultTo,
790d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            int resultRequestCode, int titleRes, CharSequence titleText) {
7918eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        final CharSequence cs;
7928eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        if (titleRes != 0) {
7938eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            cs = getText(titleRes);
7948eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        } else {
7958eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio            cs = titleText;
7968eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        }
7978eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio
79810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
79910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (resultTo != null) {
80010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            f.setTargetFragment(resultTo, resultRequestCode);
80110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
80210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
80310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, f);
80410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
80510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.addToBackStack(BACK_STACK_PREFS);
8068eb3f0fbf2ced7291167755fe74ce50164eb3a9eFabrice Di Meglio        transaction.setBreadCrumbTitle(cs);
80710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
80810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
80910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
81010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
811d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Switch to a specific Fragment with taking care of validation, Title and BackStack
812d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
813d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
814d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            boolean addToBackStack, CharSequence title, boolean withTransition) {
815d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (validate && !isValidFragment(fragmentName)) {
816d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            throw new IllegalArgumentException("Invalid fragment for this activity: "
817d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                    + fragmentName);
818d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
819d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
820d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
821d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.replace(R.id.prefs, f);
822d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (withTransition) {
823d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
824d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
825d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (addToBackStack) {
826d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
827d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
828d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (title != null) {
829d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            transaction.setBreadCrumbTitle(title);
830d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
831d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        transaction.commitAllowingStateLoss();
832d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return f;
833d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
834d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
835d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
836d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Start a new instance of this activity, showing only the given fragment.
837d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * When launched in this mode, the given preference fragment will be instantiated and fill the
838d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * entire activity.
839d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     *
840d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param fragmentName The name of the fragment to display.
841d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
842d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param resultTo Option fragment that should receive the result of
843d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * the activity launch.
844d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the request
845d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * code in which to report the result.
846d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param title String to display for the title of this set of preferences.
847d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
848d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void startWithFragment(String fragmentName, Bundle args,
849d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                                  Fragment resultTo, int resultRequestCode, CharSequence title) {
850d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Intent intent = onBuildStartFragmentIntent(fragmentName, args, title);
851d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (resultTo == null) {
852d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            startActivity(intent);
853d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        } else {
854d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            resultTo.startActivityForResult(intent, resultRequestCode);
855d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        }
856d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
857d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
858d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
859d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * Build an Intent to launch a new activity showing the selected fragment.
860d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * The implementation constructs an Intent that re-launches the current activity with the
861d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * appropriate arguments to display the fragment.
862d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     *
863d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param fragmentName The name of the fragment to display.
864d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
865d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @param title Optional title to show for this item.
866d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * @return Returns an Intent that can be launched to display the given
867d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     * fragment.
868d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio     */
869d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, CharSequence title) {
870d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        Intent intent = new Intent(Intent.ACTION_MAIN);
871d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.setClass(this, SubSettings.class);
872d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
873d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
874d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, title);
875d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        intent.putExtra(EXTRA_NO_HEADERS, true);
876d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return intent;
877d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
878d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio
879d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    /**
8806d534a1da52667c6c41b723d4c203fe0d59a2676Fabrice Di Meglio     * Called when the activity needs its list of headers build.
881263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
882263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param headers The list in which to place the headers.
883263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
884263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void onBuildHeaders(List<Header> headers) {
885263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        loadHeadersFromResource(R.xml.settings_headers, headers);
886263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        updateHeaderList(headers);
887263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
888263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
889263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
890263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Parse the given XML file as a header description, adding each
891263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * parsed Header into the target list.
892263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
893263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resid The XML resource to load and parse.
894263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param target The list in which the parsed headers should be placed.
895263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
896263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void loadHeadersFromResource(int resid, List<Header> target) {
897263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        XmlResourceParser parser = null;
898263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
899263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            parser = getResources().getXml(resid);
900263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AttributeSet attrs = Xml.asAttributeSet(parser);
901263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
902263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int type;
903263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
904263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && type != XmlPullParser.START_TAG) {
905263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Parse next until start tag is found
906263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
907263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
908263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String nodeName = parser.getName();
909263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (!"preference-headers".equals(nodeName)) {
910263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                throw new RuntimeException(
911263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        "XML document must start with <preference-headers> tag; found"
912263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                + nodeName + " at " + parser.getPositionDescription());
913263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
914263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
915263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle curBundle = null;
916263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
917263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final int outerDepth = parser.getDepth();
918263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
919263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
920263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
921263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    continue;
922263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
923263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
924263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                nodeName = parser.getName();
925263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if ("header".equals(nodeName)) {
926263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    Header header = new Header();
927263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
928263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedArray sa = obtainStyledAttributes(
929263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            attrs, com.android.internal.R.styleable.PreferenceHeader);
930263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.id = sa.getResourceId(
931263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_id,
932263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            (int)HEADER_ID_UNDEFINED);
933263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedValue tv = sa.peekValue(
934263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_title);
935263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
936263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
937263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.titleRes = tv.resourceId;
938263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
939263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.title = tv.string;
940263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
941263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
942263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    tv = sa.peekValue(
943263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_summary);
944263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
945263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
946263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summaryRes = tv.resourceId;
947263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
948263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summary = tv.string;
949263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
950263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
951263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.iconRes = sa.getResourceId(
952263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_icon, 0);
953263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragment = sa.getString(
954263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_fragment);
955263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sa.recycle();
956263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
957263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle == null) {
958263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = new Bundle();
959263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
960263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
961263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    final int innerDepth = parser.getDepth();
962263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
963263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
964263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
965263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            continue;
966263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
967263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
968263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        String innerNodeName = parser.getName();
969263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (innerNodeName.equals("extra")) {
970263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            getResources().parseBundleExtra("extra", attrs, curBundle);
971263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
972263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
973263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else if (innerNodeName.equals("intent")) {
974263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.intent = Intent.parseIntent(getResources(), parser, attrs);
975263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
976263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
977263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
978263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
979263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
980263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
981263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle.size() > 0) {
982263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        header.fragmentArguments = curBundle;
983263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = null;
984263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
985263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
986263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.add(header);
987263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
988263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    XmlUtils.skipCurrentTag(parser);
989263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
990263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
991263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
992263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (XmlPullParserException e) {
993263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
994263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (IOException e) {
995263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
996263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } finally {
997263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (parser != null) parser.close();
998263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
999263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1000263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1001263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void updateHeaderList(List<Header> target) {
1002263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final boolean showDev = mDevelopmentPreferences.getBoolean(
1003263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                DevelopmentSettings.PREF_SHOW,
1004263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                android.os.Build.TYPE.equals("eng"));
1005263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int i = 0;
1006263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1007263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1008263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderIndexMap.clear();
1009263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        while (i < target.size()) {
1010263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = target.get(i);
1011263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Ids are integers, so downcasting
1012263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int id = (int) header.id;
1013263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1014263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
1015263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.wifi_settings) {
1016263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove WiFi Settings if WiFi service is not available.
1017263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1018263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1019263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1020263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.bluetooth_settings) {
1021263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove Bluetooth Settings if Bluetooth service is not available.
1022263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1023263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1024263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1025263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.data_usage_settings) {
1026263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove data usage when kernel module not enabled
1027263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                final INetworkManagementService netManager = INetworkManagementService.Stub
1028263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1029263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                try {
1030263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!netManager.isBandwidthControlEnabled()) {
1031263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1032263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1033263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } catch (RemoteException e) {
1034263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // ignored
1035263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1036263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.battery_settings) {
1037263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove battery settings when battery is not available. (e.g. TV)
1038263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1039263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!mBatteryPresent) {
1040263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1041263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1042263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_settings) {
1043263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                int headerIndex = i + 1;
1044263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i = insertAccountsHeaders(target, headerIndex);
1045263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.home_settings) {
1046263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!updateHomeSettingHeaders(header)) {
1047263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1048263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1049263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.user_settings) {
1050263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!UserHandle.MU_ENABLED
1051263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || !UserManager.supportsMultipleUsers()
1052263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || Utils.isMonkeyRunning()) {
1053263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1054263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1055263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.nfc_payment_settings) {
1056263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1057263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1058263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1059263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // Only show if NFC is on and we have the HCE feature
1060263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1061263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1062263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1063263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1064263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1065263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1066263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.development_settings) {
1067263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!showDev) {
1068263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1069263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1070263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_add) {
1071263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1072263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1073263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1074263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1075263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1076263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header
1077263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
1078263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1079263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                target.remove(i);
1080263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1081263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1082263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Increment if the current one wasn't removed by the Utils code.
1083263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header) {
1084263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaderIndexMap.put(id, i);
1085263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i++;
1086263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1087263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1088263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1089263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1090263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int insertAccountsHeaders(List<Header> target, int headerIndex) {
1091263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1092263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
1093263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (String accountType : accountTypes) {
1094263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1095263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (label == null) {
1096263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                continue;
1097263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1098263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1099263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            boolean skipToAccount = accounts.length == 1
1101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header accHeader = new Header();
1103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accHeader.title = label;
1104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (accHeader.extras == null) {
1105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras = new Bundle();
1106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (skipToAccount) {
1108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = AccountSyncSettings.class.getName();
1109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Need this for the icon
1111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accounts[0]);
1115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = ManageAccountsSettings.class.getName();
1117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1119263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1120263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accountType);
1121263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            label.toString());
1123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accountHeaders.add(accHeader);
1125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1128263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Sort by label
1129263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Collections.sort(accountHeaders, new Comparator<Header>() {
1130263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
1131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public int compare(Header h1, Header h2) {
1132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return h1.title.toString().compareTo(h2.title.toString());
1133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        });
1135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (Header header : accountHeaders) {
1137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            target.add(headerIndex++, header);
1138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mListeningToAccountUpdates) {
1140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mListeningToAccountUpdates = true;
1142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return headerIndex;
1144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean updateHomeSettingHeaders(Header header) {
1147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Once we decide to show Home settings, keep showing it forever
1148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return true;
1151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1154263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1155263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getPackageManager().getHomeActivities(homeApps);
1156263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (homeApps.size() < 2) {
1157263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // When there's only one available home app, omit this settings
1158263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // category entirely at the top level UI.  If the user just
1159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // uninstalled the penultimate home app candidiate, we also
1160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // now tell them about why they aren't seeing 'Home' in the list.
1161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (sShowNoHomeNotice) {
1162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sShowNoHomeNotice = false;
1163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NoHomeDialogFragment.show(this);
1164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return false;
1166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Okay, we're allowing the Home settings category.  Tell it, when
1168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // invoked via this front door, that we'll need to be told about the
1169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // case when the user uninstalls all but one home app.
1170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (header.fragmentArguments == null) {
1171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragmentArguments = new Bundle();
1172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                header.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (Exception e) {
1176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Can't look up the home activity; bail on configuring the icon
1177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.w(LOG_TAG, "Problem looking up home activity!", e);
1178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
1182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void getMetaData() {
1185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    PackageManager.GET_META_DATA);
1188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ai == null || ai.metaData == null) return;
1189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (NameNotFoundException nnfe) {
1191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // No recovery
1192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // give subclasses access to the Next button
1197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean hasNextButton() {
1198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton != null;
1199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Button getNextButton() {
1202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton;
1203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1205d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public HeaderAdapter getHeaderAdapter() {
1206d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mHeaderAdapter;
1207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1209d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void onListItemClick(ListView l, View v, int position, long id) {
1210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!isResumed()) {
1211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return;
1212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Object item = mHeaderAdapter.getItem(position);
1214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (item instanceof Header) {
12156502720566595543b1fbb79370319a0dd700036fFabrice Di Meglio            mSelectedHeader = (Header) item;
1216d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            onHeaderClick(mSelectedHeader);
1217d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            revertToInitialFragment();
1218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean shouldUpRecreateTask(Intent targetIntent) {
1223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onAccountsUpdated(Account[] accounts) {
1228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // TODO: watch for package upgrades to invalidate cache; see 7206643
1229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
1230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, accounts);
1231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
1232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static void requestHomeNotice() {
1235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sShowNoHomeNotice = true;
1236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1238d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1239d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextSubmit(String query) {
1240d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        switchToSearchResultsFragmentIfNeeded();
1241d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = query;
1242d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return mSearchResultsFragment.onQueryTextSubmit(query);
1243d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1245d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1246d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onQueryTextChange(String newText) {
1247d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchQuery = newText;
1248d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return false;
1249d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1251d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1252d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onClose() {
1253d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return false;
1254d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1256d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1257d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionExpand(MenuItem item) {
1258d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1259d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            if (mSearchResultsFragment == null) {
1260d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                switchToSearchResultsFragmentIfNeeded();
1261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1263d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1264d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1266d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    @Override
1267d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public boolean onMenuItemActionCollapse(MenuItem item) {
1268d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (item.getItemId() == mSearchMenuItem.getItemId()) {
1269d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            if (mIsShowingSearchResults) {
1270d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                revertToInitialFragment();
1271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1273d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        return true;
1274d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1275263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1276d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void switchToSearchResultsFragmentIfNeeded() {
1277d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        if (!mIsShowingSearchResults) {
1278d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1279d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            if (current != null && current instanceof SearchResultsSummary) {
1280d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                mSearchResultsFragment = (SearchResultsSummary) current;
1281d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            } else {
1282d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                String title = getString(R.string.search_results_title);
1283d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
1284d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                        SearchResultsSummary.class.getName(), null, false, true, title, true);
1285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1286d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mIsShowingSearchResults = true;
1287263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1288d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1290d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    public void needToRevertToInitialFragment() {
1291d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = true;
1292d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    }
1293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1294d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio    private void revertToInitialFragment() {
1295d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mNeedToRevertToInitialFragment = false;
1296d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        getFragmentManager().popBackStack(SettingsActivity.BACK_STACK_PREFS,
1297d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio                FragmentManager.POP_BACK_STACK_INCLUSIVE);
1298d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchResultsFragment = null;
1299d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mIsShowingSearchResults = false;
1300d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        mSearchMenuItem.collapseActionView();
1301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1302263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
1303