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