SettingsActivity.java revision 10afdb82aca7c6345d2aace30c7b66238e6bfb3c
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.AlertDialog;
28263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.Dialog;
29263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.DialogFragment;
30263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.app.admin.DevicePolicyManager;
31263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.BroadcastReceiver;
32263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Context;
33263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.Intent;
34263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.IntentFilter;
35263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.SharedPreferences;
36263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ActivityInfo;
37263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager;
38263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.PackageManager.NameNotFoundException;
39263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.pm.ResolveInfo;
40263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.Configuration;
41263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.Resources;
42263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.TypedArray;
43263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.content.res.XmlResourceParser;
44263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.graphics.drawable.Drawable;
45263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.nfc.NfcAdapter;
46263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Bundle;
47263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Handler;
48263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.INetworkManagementService;
49263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Message;
50263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Parcel;
51263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Parcelable;
52263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.RemoteException;
53263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.ServiceManager;
54263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserHandle;
55263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.UserManager;
56263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.Preference;
57263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceFragment;
58263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceManager;
59263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.preference.PreferenceScreen;
60263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.support.v4.app.ActionBarDrawerToggle;
61263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.support.v4.widget.DrawerLayout;
62263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.text.TextUtils;
63263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.AttributeSet;
64263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Log;
65263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Pair;
66263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.TypedValue;
67263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.util.Xml;
68263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.LayoutInflater;
69263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.MenuItem;
70263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View;
71263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View.OnClickListener;
72263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.ViewGroup;
7310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglioimport android.widget.AbsListView;
74263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.AdapterView;
75263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.ArrayAdapter;
76263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.Button;
77263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.ImageButton;
78263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.ImageView;
79263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.ListView;
80263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.Switch;
81263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.widget.TextView;
82263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
83263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.ArrayUtils;
84263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.internal.util.XmlUtils;
85263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.AccessibilitySettings;
86263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accessibility.CaptionPropertiesFragment;
87263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AccountSyncSettings;
88263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.AuthenticatorHelper;
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.ChooseAccountFragment;
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.accounts.ManageAccountsSettings;
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ManageApplications;
92263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.applications.ProcessStatsUi;
93263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.bluetooth.BluetoothEnabler;
94263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.bluetooth.BluetoothSettings;
95263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.dashboard.DashboardSummary;
96263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.Memory;
97263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.deviceinfo.UsbSettings;
98263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.fuelgauge.PowerUsageSummary;
99263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.InputMethodAndLanguageSettings;
100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.SpellCheckersSettings;
102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.inputmethod.UserDictionaryList;
103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.location.LocationSettings;
104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.AndroidBeam;
105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.nfc.PaymentSettings;
106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintJobSettingsFragment;
107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.print.PrintSettingsFragment;
108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.tts.TextToSpeechSettings;
109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.users.UserSettings;
110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.vpn2.VpnSettings;
111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wfd.WifiDisplaySettings;
112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.AdvancedWifiSettings;
113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiEnabler;
114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.WifiSettings;
115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.wifi.p2p.WifiP2pSettings;
116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParser;
117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport org.xmlpull.v1.XmlPullParserException;
118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
119263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.io.IOException;
120263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.ArrayList;
121263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Collections;
122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.Comparator;
123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.HashMap;
124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport java.util.List;
125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopublic class SettingsActivity extends Activity
127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        implements PreferenceManager.OnPreferenceTreeClickListener,
128263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        PreferenceFragment.OnPreferenceStartFragmentCallback,
129263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener {
130263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String LOG_TAG = "Settings";
132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Constants for state save/restore
134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String HEADERS_TAG = ":settings:headers";
135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String CUR_HEADER_TAG = ":settings:cur_header";
136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity, the invoking Intent can contain this extra
139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * string to specify which fragment should be initially displayed.
140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will call isValidFragment() to confirm that the fragment class name is valid for this
142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity.
143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specified to supply a Bundle of arguments to pass
149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to that fragment when it is instantiated during the initial creation
150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * of the activity.
151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
154263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice 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
163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // extras that allow any preference activity to be launched as part of a wizard
164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // show Back and Next buttons? takes boolean parameter
166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Back will then return RESULT_CANCELED and Next RESULT_OK
167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // add a Skip button?
170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // specify custom text for the Back or Next buttons, or cause a button to not appear
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // at all by setting it to null
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * this extra can also be specify to supply the title to be shown for
180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that fragment.
181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String BACK_STACK_PREFS = ":settings:prefs";
185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_HEADER_ID =
187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.TOP_LEVEL_HEADER_ID";
188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String META_DATA_KEY_FRAGMENT_CLASS =
190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        "com.android.settings.FRAGMENT_CLASS";
191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static boolean sShowNoHomeNotice = false;
197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String mFragmentClass;
199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int mTopLevelHeaderId;
200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Header mFirstHeader;
201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Header mCurrentHeader;
202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // Show only these settings for restricted users
204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int[] SETTINGS_FOR_RESTRICTED = {
205263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_section,
206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wifi_settings,
207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.bluetooth_settings,
208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.data_usage_settings,
209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.wireless_settings,
210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.device_section,
211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.sound_settings,
212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.display_settings,
213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.storage_settings,
214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.application_settings,
215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.battery_settings,
216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.personal_section,
217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.location_settings,
218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.security_settings,
219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.language_settings,
220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.user_settings,
221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_settings,
222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.account_add,
223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.system_section,
224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.date_time_settings,
225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.about_settings,
226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.accessibility_settings,
227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.print_settings,
228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.nfc_payment_settings,
229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            R.id.home_settings
230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final String[] ENTRY_FRAGMENTS = {
233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WirelessSettings.class.getName(),
234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiSettings.class.getName(),
235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AdvancedWifiSettings.class.getName(),
236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            BluetoothSettings.class.getName(),
237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TetherSettings.class.getName(),
238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiP2pSettings.class.getName(),
239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            VpnSettings.class.getName(),
240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DateTimeSettings.class.getName(),
241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocalePicker.class.getName(),
242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            InputMethodAndLanguageSettings.class.getName(),
243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SpellCheckersSettings.class.getName(),
244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionaryList.class.getName(),
245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserDictionarySettings.class.getName(),
246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SoundSettings.class.getName(),
247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DisplaySettings.class.getName(),
248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceInfoSettings.class.getName(),
249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageApplications.class.getName(),
250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ProcessStatsUi.class.getName(),
251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationStation.class.getName(),
252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            LocationSettings.class.getName(),
253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            SecuritySettings.class.getName(),
254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrivacySettings.class.getName(),
255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DeviceAdminSettings.class.getName(),
256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccessibilitySettings.class.getName(),
257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CaptionPropertiesFragment.class.getName(),
258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleContrastPreferenceFragment.class.getName(),
260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextToSpeechSettings.class.getName(),
262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Memory.class.getName(),
263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DevelopmentSettings.class.getName(),
264263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UsbSettings.class.getName(),
265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AndroidBeam.class.getName(),
266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            WifiDisplaySettings.class.getName(),
267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PowerUsageSummary.class.getName(),
268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountSyncSettings.class.getName(),
269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CryptKeeperSettings.class.getName(),
270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DataUsageSummary.class.getName(),
271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            DreamSettings.class.getName(),
272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            UserSettings.class.getName(),
273263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            NotificationAccessSettings.class.getName(),
274263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ManageAccountsSettings.class.getName(),
275263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintSettingsFragment.class.getName(),
276263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PrintJobSettingsFragment.class.getName(),
277263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TrustedCredentialsSettings.class.getName(),
278263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            PaymentSettings.class.getName(),
279263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            KeyboardLayoutPickerFragment.class.getName(),
280263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ChooseAccountFragment.class.getName(),
281ca915668af1dfea973c2789d9ae48c958ca8362dFabrice Di Meglio            DashboardSummary.class.getName(),
282ca915668af1dfea973c2789d9ae48c958ca8362dFabrice Di Meglio            ApnSettings.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    // TODO: Update Call Settings based on airplane mode state.
289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private AuthenticatorHelper mAuthenticatorHelper;
293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mListeningToAccountUpdates;
294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Button mNextButton;
296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean mBatteryPresent = true;
298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onReceive(Context context, Intent intent) {
302263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String action = intent.getAction();
303263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
304263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                boolean batteryPresent = Utils.isBatteryPresent(intent);
305263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
306263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mBatteryPresent != batteryPresent) {
307263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mBatteryPresent = batteryPresent;
308263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    invalidateHeaders();
309263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
310263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
311263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
312263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
313263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
314263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private final ArrayList<Header> mHeaders = new ArrayList<Header>();
315263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Header mCurHeader;
316263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private HeaderAdapter mHeaderAdapter;
317263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
318263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private class TitlePair extends Pair<Integer, CharSequence> {
319263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
320263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public TitlePair(Integer first, CharSequence second) {
321263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            super(first, second);
322263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
323263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
324263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
325263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private final ArrayList<TitlePair> mTitleStack = new ArrayList<TitlePair>();
326263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
327263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private DrawerLayout mDrawerLayout;
328263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private ListView mDrawer;
329263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private ActionBarDrawerToggle mDrawerToggle;
330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private ActionBar mActionBar;
331263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
332263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final int MSG_BUILD_HEADERS = 1;
333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Handler mHandler = new Handler() {
334263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
335263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void handleMessage(Message msg) {
336263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (msg.what) {
337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case MSG_BUILD_HEADERS: {
338263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaders.clear();
339263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    onBuildHeaders(mHeaders);
340263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mHeaderAdapter.notifyDataSetChanged();
341263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (mCurHeader != null) {
342263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        Header mappedHeader = findBestMatchingHeader(mCurHeader, mHeaders);
343263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (mappedHeader != null) {
344263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            setSelectedHeader(mappedHeader);
345263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
346263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } break;
348263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
349263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
350263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    };
351263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
352263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
353263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
354263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Override the fragment title for Wallpaper settings
355263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int titleRes = pref.getTitleRes();
356263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
357263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = R.string.wallpaper_settings_fragment_title;
358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                && UserHandle.myUserId() != UserHandle.USER_OWNER) {
360263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (UserManager.get(this).isLinkedUser()) {
361263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.profile_info_settings_title;
362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                titleRes = R.string.user_info_settings_title;
364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                null, 0);
368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
370263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
374263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
375263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
376263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private class DrawerListener implements DrawerLayout.DrawerListener {
377263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
378263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onDrawerOpened(View drawerView) {
379263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.onDrawerOpened(drawerView);
380263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
381263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
382263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
383263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onDrawerClosed(View drawerView) {
384263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.onDrawerClosed(drawerView);
385263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            onHeaderClick(mCurrentHeader);
386263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
387263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
388263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
389263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onDrawerSlide(View drawerView, float slideOffset) {
390263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
391263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
392263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
393263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
394263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onDrawerStateChanged(int newState) {
395263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.onDrawerStateChanged(newState);
396263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
397263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
398263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
399263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private class DrawerItemClickListener implements ListView.OnItemClickListener {
400263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
401263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
402263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerLayout.closeDrawer(mDrawer);
40310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            onListItemClick((ListView)parent, view, position, id);
404263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
405263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
406263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
407263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private Header findBestMatchingHeader(Header cur, ArrayList<Header> from) {
408263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        ArrayList<Header> matches = new ArrayList<Header>();
409263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int j=0; j<from.size(); j++) {
410263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header oh = from.get(j);
411263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (cur == oh || (cur.id != HEADER_ID_UNDEFINED && cur.id == oh.id)) {
412263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Must be this one.
413263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                matches.clear();
414263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                matches.add(oh);
415263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                break;
416263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
417263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (cur.fragment != null) {
418263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.fragment.equals(oh.fragment)) {
419263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    matches.add(oh);
420263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
421263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (cur.intent != null) {
422263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.intent.equals(oh.intent)) {
423263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    matches.add(oh);
424263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
425263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (cur.title != null) {
426263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.title.equals(oh.title)) {
427263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    matches.add(oh);
428263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
429263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
430263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
431263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final int NM = matches.size();
432263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (NM == 1) {
433263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return matches.get(0);
434263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (NM > 1) {
435263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            for (int j=0; j<NM; j++) {
436263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Header oh = matches.get(j);
437263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.fragmentArguments != null &&
438263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        cur.fragmentArguments.equals(oh.fragmentArguments)) {
439263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    return oh;
440263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
441263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.extras != null && cur.extras.equals(oh.extras)) {
442263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    return oh;
443263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
444263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (cur.title != null && cur.title.equals(oh.title)) {
445263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    return oh;
446263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
447263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
448263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
449263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return null;
450263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
451263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
452263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void invalidateHeaders() {
453263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) {
454263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mHandler.sendEmptyMessage(MSG_BUILD_HEADERS);
455263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
456263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
457263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
458263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
459263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onPostCreate(Bundle savedInstanceState) {
460263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPostCreate(savedInstanceState);
461263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
462263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Sync the toggle state after onRestoreInstanceState has occurred.
463263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mDrawerToggle != null) {
464263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.syncState();
465263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
466263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
467263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
468263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
469263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onConfigurationChanged(Configuration newConfig) {
470263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onConfigurationChanged(newConfig);
471263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mDrawerToggle != null) {
472263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle.onConfigurationChanged(newConfig);
473263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
474263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
475263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
476263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
477263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onOptionsItemSelected(MenuItem item) {
478263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mDrawerToggle != null && mDrawerToggle.onOptionsItemSelected(item)) {
479263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return true;
480263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
481263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.onOptionsItemSelected(item);
482263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
483263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
484263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
485263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onCreate(Bundle savedInstanceState) {
486263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
487263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
488263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
489263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
490263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper = new AuthenticatorHelper();
491263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
492263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, null);
493263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
494263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        DevicePolicyManager dpm =
495263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
496263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderAdapter= new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper, dpm);
497263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
498263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
499263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Context.MODE_PRIVATE);
500263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
501263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getMetaData();
502263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
503263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onCreate(savedInstanceState);
504263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
505263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setContentView(R.layout.settings_main);
506263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
507263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        getFragmentManager().addOnBackStackChangedListener(this);
508263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
509263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mActionBar = getActionBar();
510263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mActionBar != null) {
511263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mActionBar.setDisplayHomeAsUpEnabled(true);
512263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mActionBar.setHomeButtonEnabled(true);
513263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
514263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
515263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerLayout.setDrawerListener(new DrawerListener());
516263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawer = (ListView) findViewById(R.id.headers_drawer);
518263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawer.setAdapter(mHeaderAdapter);
519263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawer.setOnItemClickListener(new DrawerItemClickListener());
52010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            mDrawer.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
522263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
523263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
524263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
525263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
526263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
527263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
528263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
529263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (savedInstanceState != null) {
530263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // We are restarting from a previous saved state; used that to
531263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // initialize, instead of starting fresh.
532263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ArrayList<Header> headers = savedInstanceState.getParcelableArrayList(HEADERS_TAG);
533263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (headers != null) {
534263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaders.addAll(headers);
535263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                int curHeader = savedInstanceState.getInt(CUR_HEADER_TAG,
536263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        (int) HEADER_ID_UNDEFINED);
537263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (curHeader >= 0 && curHeader < mHeaders.size()) {
538263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    setSelectedHeader(mHeaders.get(curHeader));
539263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
540263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
541263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
542263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else {
543263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (initialFragment != null) {
54410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // If we are just showing a fragment, we want to run in
54510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // new fragment mode, but don't need to compute and show
54610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // the headers.
54710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                switchToHeader(initialFragment, initialArguments, true);
54810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
54910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                final int initialTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE, 0);
55010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                if (initialTitle != 0) {
55110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    setTitle(getText(initialTitle));
55210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                }
55310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            } else {
55410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // We need to try to build the headers.
55510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                onBuildHeaders(mHeaders);
55610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
55710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // If there are headers, then at this point we need to show
55810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // them and, depending on the screen, we may also show in-line
55910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                // the currently selected preference fragment.
56010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                if (mHeaders.size() > 0) {
56110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    if (initialFragment == null) {
56210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                        Header h = onGetInitialHeader();
56310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                        switchToHeader(h, false);
56410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    } else {
56510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                        switchToHeader(initialFragment, initialArguments, false);
56610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    }
56710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                }
568263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
569263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
570263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // see if we should show Back/Next buttons
572263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent intent = getIntent();
573263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
574263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
575263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            View buttonBar = findViewById(com.android.internal.R.id.button_bar);
576263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (buttonBar != null) {
577263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                buttonBar.setVisibility(View.VISIBLE);
578263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
579263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button backButton = (Button)findViewById(com.android.internal.R.id.back_button);
580263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                backButton.setOnClickListener(new OnClickListener() {
581263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
582263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_CANCELED);
583263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
584263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
585263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
586263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button);
587263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                skipButton.setOnClickListener(new OnClickListener() {
588263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
589263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
590263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
591263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
592263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
593263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton = (Button)findViewById(com.android.internal.R.id.next_button);
594263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mNextButton.setOnClickListener(new OnClickListener() {
595263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    public void onClick(View v) {
596263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        setResult(RESULT_OK);
597263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        finish();
598263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
599263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                });
600263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
601263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // set our various button parameters
602263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
603263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
604263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
605263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setVisibility(View.GONE);
606263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
607263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
608263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mNextButton.setText(buttonText);
609263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
610263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
611263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
612263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
613263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (TextUtils.isEmpty(buttonText)) {
614263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setVisibility(View.GONE);
615263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
616263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    else {
617263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        backButton.setText(buttonText);
618263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
619263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
620263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
621263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    skipButton.setVisibility(View.VISIBLE);
622263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
623263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
624263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
625263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
626263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!onIsHidingHeaders()) {
627263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            highlightHeader(mTopLevelHeaderId);
628263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
629263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
630263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Retrieve any saved state
631263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (savedInstanceState != null) {
632263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
633263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
634263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
635263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
636263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
637263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onBackStackChanged() {
63810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        final int count = getFragmentManager().getBackStackEntryCount() + 1;
639263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        TitlePair pair = null;
640263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int last;
641263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        while (mTitleStack.size() > count) {
642263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            last = mTitleStack.size() - 1;
643263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            pair = mTitleStack.remove(last);
644263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
645263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Check if we go back
646263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (pair != null) {
647263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int size = mTitleStack.size();
648263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (size > 0) {
649263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                last = mTitleStack.size() - 1;
650263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                pair = mTitleStack.get(last);
651263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (pair != null) {
652263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    final CharSequence title;
653263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (pair.first > 0) {
654263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        title = getText(pair.first);
655263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    } else {
656263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        title = pair.second;
657263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
658263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    setTitle(title);
659263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
660263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
661263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
662263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
663263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
664263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
665263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Returns the Header list
666263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
667263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private List<Header> getHeaders() {
668263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mHeaders;
669263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
670263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
671263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
672263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected void onSaveInstanceState(Bundle outState) {
673263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onSaveInstanceState(outState);
674263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
675263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mHeaders.size() > 0) {
676263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            outState.putParcelableArrayList(HEADERS_TAG, mHeaders);
677263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (mCurHeader != null) {
678263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                int index = mHeaders.indexOf(mCurHeader);
679263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (index >= 0) {
680263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    outState.putInt(CUR_HEADER_TAG, index);
681263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
682263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Save the current fragment, if it is the same as originally launched
686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mCurrentHeader != null) {
687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
692263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onResume() {
693263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onResume();
694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                invalidateHeaders();
699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
700263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
701263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
702263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
703263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
704263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderAdapter.resume();
705263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onPause() {
712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onPause();
713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
714263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        unregisterReceiver(mBatteryInfoReceiver);
715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderAdapter.pause();
717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDevelopmentPreferencesListener);
720263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
721263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mDevelopmentPreferencesListener = null;
722263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
723263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
724263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
725263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onDestroy() {
726263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onDestroy();
727263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mListeningToAccountUpdates) {
728263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).removeOnAccountsUpdatedListener(this);
729263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
730263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
731263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
732263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
733263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @hide
734263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
735263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    protected boolean isValidFragment(String fragmentName) {
736263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Almost all fragments are wrapped in this,
737263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // except for a few that have their own activities.
738263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
739263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
740263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
741263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return false;
742263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
743263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
744263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
745263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * When in two-pane mode, switch to the fragment pane to show the given
746263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * preference fragment.
747263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
748263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param header The new header to display.
74910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param validate true means that the fragment's Header needs to be validated
750263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
75110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    private void switchToHeader(Header header, boolean validate) {
75210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (mCurHeader == header) {
75310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            // This is the header we are currently displaying.  Just make sure
75410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            // to pop the stack up to its root state.
75510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            getFragmentManager().popBackStack(BACK_STACK_PREFS,
75610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
75710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
75810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            mTitleStack.clear();
759263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (header.fragment == null) {
760263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                throw new IllegalStateException("can't switch to header that has no fragment");
761263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
76210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            switchToHeaderInner(header.fragment, header.fragmentArguments, validate);
763263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            setSelectedHeader(header);
764263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final CharSequence title;
765263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (header.fragment.equals("com.android.settings.dashboard.DashboardSummary")) {
766263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                title = getResources().getString(R.string.settings_label);
767263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
768263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                title = header.getTitle(getResources());
769263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
770263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final TitlePair pair = new TitlePair(0, title);
771263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mTitleStack.add(pair);
772263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            setTitle(title);
773263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
774263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
775263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
776263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void setSelectedHeader(Header header) {
777263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mCurHeader = header;
778263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int index = mHeaders.indexOf(header);
779263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mDrawer != null) {
780263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (index >= 0) {
781263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDrawer.setItemChecked(index, true);
782263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
783263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDrawer.clearChoices();
784263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
785263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
786263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
787263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
78810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public Header onGetInitialHeader() {
789263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String fragmentClass = getStartingFragmentClass(super.getIntent());
790263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (fragmentClass != null) {
791263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = new Header();
792263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            header.fragment = fragmentClass;
79310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            header.title = getTitle();
79410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            header.fragmentArguments = getIntent().getExtras();
795263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mCurrentHeader = header;
796263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return header;
797263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
798263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
79910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        return mFirstHeader;
8004cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio    }
8014cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio
8024cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio    /**
80310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * When in two-pane mode, switch the fragment pane to show the given
80410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference fragment.
8054cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio     *
8064cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio     * @param fragmentName The name of the fragment to display.
8074cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
80810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param validate true means that the fragment's Header needs to be validated
8094cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio     */
81010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    private void switchToHeader(String fragmentName, Bundle args, boolean validate) {
81110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        setSelectedHeader(null);
81210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        switchToHeaderInner(fragmentName, args, validate);
8134cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio    }
8144cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio
81510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    private void switchToHeaderInner(String fragmentName, Bundle args, boolean validate) {
81610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        getFragmentManager().popBackStack(BACK_STACK_PREFS,
81710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                FragmentManager.POP_BACK_STACK_INCLUSIVE);
81810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (validate && !isValidFragment(fragmentName)) {
81910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            throw new IllegalArgumentException("Invalid fragment for this activity: "
82010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                    + fragmentName);
8214cc95a53c2d9080095c4e1e06f815b8bc7a2cdfeFabrice Di Meglio        }
82210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
82310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
82410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
82510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, f);
826263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        transaction.commitAllowingStateLoss();
827263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
828263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
829263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
830263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onNewIntent(Intent intent) {
831263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onNewIntent(intent);
832263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
833263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // If it is not launched from history, then reset to top-level
834263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
835263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (mDrawer != null) {
836263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDrawer.setSelectionFromTop(0, 0);
837263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
838263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
839263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
840263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
841263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
842263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called to determine whether the header list should be hidden.
843263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * The default implementation returns the
844263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied.
845263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * This is set to false, for example, when the activity is being re-launched
846263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to show a particular preference activity.
847263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
848263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean onIsHidingHeaders() {
849263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
850263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
851263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
852263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void highlightHeader(int id) {
853263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (id != 0) {
854263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Integer index = mHeaderIndexMap.get(id);
855263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (index != null && mDrawer != null) {
856263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mDrawer.setItemChecked(index, true);
857263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mDrawer.getVisibility() == View.VISIBLE) {
858263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mDrawer.smoothScrollToPosition(index);
859263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
860263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
861263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
862263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
863263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
864263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
865263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Intent getIntent() {
866263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Intent superIntent = super.getIntent();
867263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String startingFragment = getStartingFragmentClass(superIntent);
868263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // This is called from super.onCreate, isMultiPane() is not yet reliable
869263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Do not use onIsHidingHeaders either, which relies itself on this method
870263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (startingFragment != null) {
871263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Intent modIntent = new Intent(superIntent);
872263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
873263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle args = superIntent.getExtras();
874263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (args != null) {
875263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle(args);
876263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
877263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                args = new Bundle();
878263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
879263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            args.putParcelable("intent", superIntent);
880263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
881263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return modIntent;
882263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
883263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return superIntent;
884263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
885263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
886263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
887263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Checks if the component name in the intent is different from the Settings class and
888263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * returns the class name to load as a fragment.
889263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
890263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private String getStartingFragmentClass(Intent intent) {
891263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (mFragmentClass != null) return mFragmentClass;
892263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
893263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String intentClass = intent.getComponent().getClassName();
894263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (intentClass.equals(getClass().getName())) return null;
895263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
896263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if ("com.android.settings.ManageApplications".equals(intentClass)
897263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.RunningServices".equals(intentClass)
898263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                || "com.android.settings.applications.StorageUse".equals(intentClass)) {
899263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Old names of manage apps.
900263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            intentClass = com.android.settings.applications.ManageApplications.class.getName();
901263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
902263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
903263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return intentClass;
904263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
905263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
906263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
90710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment containing a preference panel.  If the preferences
90810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * are being displayed in multi-pane mode, the given fragment class will
90910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * be instantiated and placed in the appropriate pane.  If running in
91010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * single-pane mode, a new activity will be launched in which to show the
91110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
91210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
91310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentClass Full name of the class implementing the fragment.
91410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Any desired arguments to supply to the fragment.
91510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Optional resource identifier of the title of this
91610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * fragment.
91710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText Optional text of the title of this fragment.
91810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Optional fragment that result data should be sent to.
91910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * If non-null, resultTo.onActivityResult() will be called when this
92010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * preference panel is done.  The launched panel must use
92110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
92210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the caller's
92310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * request code to be received with the resut.
92410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
92510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
92610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                                     CharSequence titleText, Fragment resultTo,
92710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                                     int resultRequestCode) {
92810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        startWithFragment(fragmentClass, args, resultTo, resultRequestCode, titleRes, titleText);
92910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
93010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
93110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
932263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called by a preference panel fragment to finish itself.
933263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
934263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param caller The fragment that is asking to be finished.
935263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultCode Optional result code to send back to the original
936263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
937263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resultData Optional result data to send back to the original
938263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * launching fragment.
939263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
940263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
941263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setResult(resultCode, resultData);
942263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
943263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
944263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
94510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
94610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
94710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragment The fragment to start
94810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
94910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * the current fragment will be replaced.
95010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
95110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    public void startPreferenceFragment(Fragment fragment, boolean push) {
95210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
95310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, fragment);
95410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (push) {
95510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
95610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.addToBackStack(BACK_STACK_PREFS);
95710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
95810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
95910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
96010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
96110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
96210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
96310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
96410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * Start a new fragment.
96510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *
96610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param fragmentName The name of the fragment to display.
96710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param args Optional arguments to supply to the fragment.
96810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultTo Option fragment that should receive the result of
96910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 the activity launch.
97010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param resultRequestCode If resultTo is non-null, this is the request code in which to
97110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                          report the result.
97210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleRes Resource ID of string to display for the title of. If the Resource ID is a
97310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 valid one then it will be used to get the title. Otherwise the titleText
97410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     *                 argument will be used as the title.
97510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * @param titleText string to display for the title of.
97610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     */
97710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    private void startWithFragment(String fragmentName, Bundle args, Fragment resultTo,
97810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio                                   int resultRequestCode, int titleRes, CharSequence titleText) {
97910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        Fragment f = Fragment.instantiate(this, fragmentName, args);
98010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (resultTo != null) {
98110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            f.setTargetFragment(resultTo, resultRequestCode);
98210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
98310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        FragmentTransaction transaction = getFragmentManager().beginTransaction();
98410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.replace(R.id.prefs, f);
98510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
98610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.addToBackStack(BACK_STACK_PREFS);
98710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        transaction.commitAllowingStateLoss();
98810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
98910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        final TitlePair pair;
99010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        final CharSequence cs;
99110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        if (titleRes != 0) {
99210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            pair = new TitlePair(titleRes, null);
99310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            cs = getText(titleRes);
99410afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        } else {
99510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            pair = new TitlePair(0, titleText);
99610afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            cs = titleText;
99710afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        }
99810afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        setTitle(cs);
99910afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio        mTitleStack.add(pair);
100010afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    }
100110afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio
100210afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio    /**
1003263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called when the activity needs its list of headers build.  By
1004263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * implementing this and adding at least one item to the list, you
1005263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * will cause the activity to run in its modern fragment mode.  Note
1006263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * that this function may not always be called; for example, if the
1007263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * activity has been asked to display a particular fragment without
1008263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * the header list, there is no need to build the headers.
1009263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
1010263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * <p>Typical implementations will use {@link #loadHeadersFromResource}
1011263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * to fill in the list from a resource.
1012263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
1013263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param headers The list in which to place the headers.
1014263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
1015263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void onBuildHeaders(List<Header> headers) {
1016263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        loadHeadersFromResource(R.xml.settings_headers, headers);
1017263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        updateHeaderList(headers);
1018263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1019263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1020263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
1021263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Parse the given XML file as a header description, adding each
1022263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * parsed Header into the target list.
1023263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
1024263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param resid The XML resource to load and parse.
1025263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param target The list in which the parsed headers should be placed.
1026263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
1027263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void loadHeadersFromResource(int resid, List<Header> target) {
1028263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        XmlResourceParser parser = null;
1029263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1030263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            parser = getResources().getXml(resid);
1031263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AttributeSet attrs = Xml.asAttributeSet(parser);
1032263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1033263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int type;
1034263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1035263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && type != XmlPullParser.START_TAG) {
1036263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Parse next until start tag is found
1037263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1038263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1039263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            String nodeName = parser.getName();
1040263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (!"preference-headers".equals(nodeName)) {
1041263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                throw new RuntimeException(
1042263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        "XML document must start with <preference-headers> tag; found"
1043263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                + nodeName + " at " + parser.getPositionDescription());
1044263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1045263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1046263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Bundle curBundle = null;
1047263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1048263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final int outerDepth = parser.getDepth();
1049263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1050263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1051263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1052263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    continue;
1053263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1054263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1055263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                nodeName = parser.getName();
1056263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if ("header".equals(nodeName)) {
1057263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    Header header = new Header();
1058263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1059263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedArray sa = obtainStyledAttributes(
1060263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            attrs, com.android.internal.R.styleable.PreferenceHeader);
1061263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.id = sa.getResourceId(
1062263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_id,
1063263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            (int)HEADER_ID_UNDEFINED);
1064263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    TypedValue tv = sa.peekValue(
1065263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_title);
1066263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1067263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
1068263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.titleRes = tv.resourceId;
1069263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
1070263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.title = tv.string;
1071263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
1072263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1073263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    tv = sa.peekValue(
1074263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_summary);
1075263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1076263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (tv.resourceId != 0) {
1077263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summaryRes = tv.resourceId;
1078263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
1079263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.summary = tv.string;
1080263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
1081263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1082263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.iconRes = sa.getResourceId(
1083263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1084263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragment = sa.getString(
1085263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            com.android.internal.R.styleable.PreferenceHeader_fragment);
1086263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sa.recycle();
1087263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1088263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle == null) {
1089263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = new Bundle();
1090263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1091263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1092263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    final int innerDepth = parser.getDepth();
1093263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1094263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1095263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1096263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            continue;
1097263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
1098263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1099263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        String innerNodeName = parser.getName();
1100263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (innerNodeName.equals("extra")) {
1101263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            getResources().parseBundleExtra("extra", attrs, curBundle);
1102263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
1103263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1104263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else if (innerNodeName.equals("intent")) {
1105263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            header.intent = Intent.parseIntent(getResources(), parser, attrs);
1106263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1107263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
1108263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            XmlUtils.skipCurrentTag(parser);
1109263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
1110263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1111263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1112263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (curBundle.size() > 0) {
1113263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        header.fragmentArguments = curBundle;
1114263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        curBundle = null;
1115263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1116263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1117263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.add(header);
1118263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1119263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    XmlUtils.skipCurrentTag(parser);
1120263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1121263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1122263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1123263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (XmlPullParserException e) {
1124263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
1125263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (IOException e) {
1126263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            throw new RuntimeException("Error parsing headers", e);
1127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } finally {
1128263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (parser != null) parser.close();
1129263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1130263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1131263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1132263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void updateHeaderList(List<Header> target) {
1133263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final boolean showDev = mDevelopmentPreferences.getBoolean(
1134263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                DevelopmentSettings.PREF_SHOW,
1135263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                android.os.Build.TYPE.equals("eng"));
1136263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        int i = 0;
1137263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1138263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1139263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mHeaderIndexMap.clear();
1140263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        while (i < target.size()) {
1141263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = target.get(i);
1142263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Ids are integers, so downcasting
1143263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int id = (int) header.id;
1144263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1145263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
1146263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.wifi_settings) {
1147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove WiFi Settings if WiFi service is not available.
1148263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1149263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1150263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1151263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.bluetooth_settings) {
1152263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove Bluetooth Settings if Bluetooth service is not available.
1153263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1154263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1155263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1156263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.data_usage_settings) {
1157263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove data usage when kernel module not enabled
1158263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                final INetworkManagementService netManager = INetworkManagementService.Stub
1159263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1160263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                try {
1161263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!netManager.isBandwidthControlEnabled()) {
1162263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1163263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1164263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } catch (RemoteException e) {
1165263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // ignored
1166263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1167263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.battery_settings) {
1168263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Remove battery settings when battery is not available. (e.g. TV)
1169263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1170263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!mBatteryPresent) {
1171263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1172263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_settings) {
1174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                int headerIndex = i + 1;
1175263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i = insertAccountsHeaders(target, headerIndex);
1176263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.home_settings) {
1177263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!updateHomeSettingHeaders(header)) {
1178263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1179263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1180263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.user_settings) {
1181263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!UserHandle.MU_ENABLED
1182263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || !UserManager.supportsMultipleUsers()
1183263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        || Utils.isMonkeyRunning()) {
1184263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1185263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1186263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.nfc_payment_settings) {
1187263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1188263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1189263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1190263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // Only show if NFC is on and we have the HCE feature
1191263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1192263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1193263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1194263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        target.remove(i);
1195263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1196263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1197263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.development_settings) {
1198263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!showDev) {
1199263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1200263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1201263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (id == R.id.account_add) {
1202263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1203263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    target.remove(i);
1204263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1205263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1206263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1207263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header
1208263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
1209263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1210263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                target.remove(i);
1211263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1212263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1213263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Increment if the current one wasn't removed by the Utils code.
1214263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (i < target.size() && target.get(i) == header) {
1215263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Hold on to the first header, when we need to reset to the top-level
1216263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (mFirstHeader == null &&
1217263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
1218263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    mFirstHeader = header;
1219263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1220263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                mHeaderIndexMap.put(id, i);
1221263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                i++;
1222263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1223263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1224263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1225263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1226263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private int insertAccountsHeaders(List<Header> target, int headerIndex) {
1227263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1228263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
1229263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (String accountType : accountTypes) {
1230263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1231263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (label == null) {
1232263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                continue;
1233263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1234263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1235263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1236263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            boolean skipToAccount = accounts.length == 1
1237263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1238263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header accHeader = new Header();
1239263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accHeader.title = label;
1240263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (accHeader.extras == null) {
1241263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras = new Bundle();
1242263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1243263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (skipToAccount) {
1244263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = AccountSyncSettings.class.getName();
1245263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1246263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Need this for the icon
1247263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1248263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1249263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1250263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accounts[0]);
1251263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1252263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragment = ManageAccountsSettings.class.getName();
1253263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments = new Bundle();
1254263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1255263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1256263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        accountType);
1257263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1258263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            label.toString());
1259263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1260263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            accountHeaders.add(accHeader);
1261263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1262263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1263263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1264263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Sort by label
1265263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Collections.sort(accountHeaders, new Comparator<Header>() {
1266263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            @Override
1267263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public int compare(Header h1, Header h2) {
1268263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return h1.title.toString().compareTo(h2.title.toString());
1269263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1270263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        });
1271263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1272263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        for (Header header : accountHeaders) {
1273263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            target.add(headerIndex++, header);
1274263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1275263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!mListeningToAccountUpdates) {
1276263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1277263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mListeningToAccountUpdates = true;
1278263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1279263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return headerIndex;
1280263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1281263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1282263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private boolean updateHomeSettingHeaders(Header header) {
1283263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // Once we decide to show Home settings, keep showing it forever
1284263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1285263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1286263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return true;
1287263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1288263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1289263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1290263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1291263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            getPackageManager().getHomeActivities(homeApps);
1292263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (homeApps.size() < 2) {
1293263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // When there's only one available home app, omit this settings
1294263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // category entirely at the top level UI.  If the user just
1295263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // uninstalled the penultimate home app candidiate, we also
1296263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // now tell them about why they aren't seeing 'Home' in the list.
1297263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (sShowNoHomeNotice) {
1298263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    sShowNoHomeNotice = false;
1299263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    NoHomeDialogFragment.show(this);
1300263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1301263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return false;
1302263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1303263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // Okay, we're allowing the Home settings category.  Tell it, when
1304263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // invoked via this front door, that we'll need to be told about the
1305263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                // case when the user uninstalls all but one home app.
1306263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (header.fragmentArguments == null) {
1307263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    header.fragmentArguments = new Bundle();
1308263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1309263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                header.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1310263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1311263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (Exception e) {
1312263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Can't look up the home activity; bail on configuring the icon
1313263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.w(LOG_TAG, "Problem looking up home activity!", e);
1314263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1315263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1316263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1317263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return true;
1318263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1319263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1320263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void getMetaData() {
1321263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        try {
1322263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1323263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    PackageManager.GET_META_DATA);
1324263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (ai == null || ai.metaData == null) return;
1325263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
1326263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1327263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } catch (NameNotFoundException nnfe) {
1328263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // No recovery
1329263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1331263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1332263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1333263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    // give subclasses access to the Next button
1334263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean hasNextButton() {
1335263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton != null;
1336263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1338263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public Button getNextButton() {
1339263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return mNextButton;
1340263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1341263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1342263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static class NoHomeDialogFragment extends DialogFragment {
1343263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public static void show(Activity parent) {
1344263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            final NoHomeDialogFragment dialog = new NoHomeDialogFragment();
1345263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dialog.show(parent.getFragmentManager(), null);
1346263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1348263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1349263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public Dialog onCreateDialog(Bundle savedInstanceState) {
1350263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return new AlertDialog.Builder(getActivity())
1351263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    .setMessage(R.string.only_one_home_message)
1352263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    .setPositiveButton(android.R.string.ok, null)
1353263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    .create();
1354263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1355263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1356263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1357263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static class HeaderAdapter extends ArrayAdapter<Header> {
1358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        static final int HEADER_TYPE_CATEGORY = 0;
1359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        static final int HEADER_TYPE_NORMAL = 1;
1360263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        static final int HEADER_TYPE_SWITCH = 2;
1361263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        static final int HEADER_TYPE_BUTTON = 3;
1362263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private static final int HEADER_TYPE_COUNT = HEADER_TYPE_BUTTON + 1;
1363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1364263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private final WifiEnabler mWifiEnabler;
1365263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private final BluetoothEnabler mBluetoothEnabler;
1366263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private AuthenticatorHelper mAuthHelper;
1367263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private DevicePolicyManager mDevicePolicyManager;
1368263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1369263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private static class HeaderViewHolder {
1370263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ImageView mIcon;
1371263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextView mTitle;
1372263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextView mSummary;
1373263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Switch mSwitch;
1374263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ImageButton mButton;
1375263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            View mDivider;
1376263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1377263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1378263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private LayoutInflater mInflater;
1379263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1380263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        static int getHeaderType(Header header) {
1381263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (header.fragment == null && header.intent == null) {
1382263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return HEADER_TYPE_CATEGORY;
1383263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
1384263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return HEADER_TYPE_SWITCH;
1385263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else if (header.id == R.id.security_settings) {
1386263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return HEADER_TYPE_BUTTON;
1387263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1388263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return HEADER_TYPE_NORMAL;
1389263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1390263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1391263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1392263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1393263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int getItemViewType(int position) {
1394263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = getItem(position);
1395263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return getHeaderType(header);
1396263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1397263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1398263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1399263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public boolean areAllItemsEnabled() {
1400263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return false; // because of categories
1401263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1402263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1403263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1404263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public boolean isEnabled(int position) {
1405263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return getItemViewType(position) != HEADER_TYPE_CATEGORY;
1406263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1407263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1408263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1409263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int getViewTypeCount() {
1410263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return HEADER_TYPE_COUNT;
1411263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1412263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1413263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1414263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public boolean hasStableIds() {
1415263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return true;
1416263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1417263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1418263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public HeaderAdapter(Context context, List<Header> objects,
1419263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                AuthenticatorHelper authenticatorHelper, DevicePolicyManager dpm) {
1420263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            super(context, 0, objects);
1421263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1422263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mAuthHelper = authenticatorHelper;
1423263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1424263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1425263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Temp Switches provided as placeholder until the adapter replaces these with actual
1426263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Switches inflated from their layouts. Must be done before adapter is set in super
1427263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mWifiEnabler = new WifiEnabler(context, new Switch(context));
1428263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
1429263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mDevicePolicyManager = dpm;
1430263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1431263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1432263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1433263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public View getView(int position, View convertView, ViewGroup parent) {
1434263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            HeaderViewHolder holder;
1435263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            Header header = getItem(position);
1436263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            int headerType = getHeaderType(header);
1437263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            View view = null;
1438263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1439263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (convertView == null) {
1440263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                holder = new HeaderViewHolder();
1441263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                switch (headerType) {
1442263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    case HEADER_TYPE_CATEGORY:
1443263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        view = new TextView(getContext(), null,
1444263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                android.R.attr.listSeparatorTextViewStyle);
1445263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mTitle = (TextView) view;
1446263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        break;
1447263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1448263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    case HEADER_TYPE_SWITCH:
1449263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
1450263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                false);
1451263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mIcon = (ImageView) view.findViewById(R.id.icon);
1452263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mTitle = (TextView)
1453263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.title);
1454263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mSummary = (TextView)
1455263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.summary);
1456263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mSwitch = (Switch) view.findViewById(R.id.switchWidget);
1457263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        break;
1458263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1459263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    case HEADER_TYPE_BUTTON:
1460263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        view = mInflater.inflate(R.layout.preference_header_button_item, parent,
1461263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                false);
1462263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mIcon = (ImageView) view.findViewById(R.id.icon);
1463263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mTitle = (TextView)
1464263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.title);
1465263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mSummary = (TextView)
1466263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.summary);
1467263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mButton = (ImageButton) view.findViewById(R.id.buttonWidget);
1468263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mDivider = view.findViewById(R.id.divider);
1469263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        break;
1470263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1471263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    case HEADER_TYPE_NORMAL:
1472263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        view = mInflater.inflate(
1473263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                R.layout.preference_header_item, parent,
1474263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                false);
1475263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mIcon = (ImageView) view.findViewById(R.id.icon);
1476263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mTitle = (TextView)
1477263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.title);
1478263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        holder.mSummary = (TextView)
1479263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                view.findViewById(com.android.internal.R.id.summary);
1480263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        break;
1481263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1482263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                view.setTag(holder);
1483263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1484263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                view = convertView;
1485263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                holder = (HeaderViewHolder) view.getTag();
1486263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1487263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1488263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // All view fields must be updated every time, because the view may be recycled
1489263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            switch (headerType) {
1490263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case HEADER_TYPE_CATEGORY:
1491263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    holder.mTitle.setText(header.getTitle(getContext().getResources()));
1492263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    break;
1493263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1494263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case HEADER_TYPE_SWITCH:
1495263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    // Would need a different treatment if the main menu had more switches
1496263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (header.id == R.id.wifi_settings) {
1497263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mWifiEnabler.setSwitch(holder.mSwitch);
1498263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    } else {
1499263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        mBluetoothEnabler.setSwitch(holder.mSwitch);
1500263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1501263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    updateCommonHeaderView(header, holder);
1502263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    break;
1503263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1504263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case HEADER_TYPE_BUTTON:
1505263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    if (header.id == R.id.security_settings) {
1506263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        boolean hasCert = DevicePolicyManager.hasAnyCaCertsInstalled();
1507263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        if (hasCert) {
1508263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            holder.mButton.setVisibility(View.VISIBLE);
1509263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            holder.mDivider.setVisibility(View.VISIBLE);
1510263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            boolean isManaged = mDevicePolicyManager.getDeviceOwner() != null;
1511263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            if (isManaged) {
1512263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                holder.mButton.setImageResource(R.drawable.ic_settings_about);
1513263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            } else {
1514263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                holder.mButton.setImageResource(
1515263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                        android.R.drawable.stat_notify_error);
1516263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            }
1517263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            holder.mButton.setOnClickListener(new OnClickListener() {
1518263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                @Override
1519263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                public void onClick(View v) {
1520263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                    Intent intent = new Intent(
1521263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                            android.provider.Settings.ACTION_MONITORING_CERT_INFO);
1522263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                    getContext().startActivity(intent);
1523263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                                }
1524263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            });
1525263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        } else {
1526263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            holder.mButton.setVisibility(View.GONE);
1527263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            holder.mDivider.setVisibility(View.GONE);
1528263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        }
1529263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    }
1530263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    updateCommonHeaderView(header, holder);
1531263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    break;
1532263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1533263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                case HEADER_TYPE_NORMAL:
1534263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    updateCommonHeaderView(header, holder);
1535263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    break;
1536263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1537263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1538263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return view;
1539263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1540263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1541263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private void updateCommonHeaderView(Header header, HeaderViewHolder holder) {
1542263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (header.extras != null
1543263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                        && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
1544263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    String accType = header.extras.getString(
1545263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                            ManageAccountsSettings.KEY_ACCOUNT_TYPE);
1546263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
1547263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    setHeaderIcon(holder, icon);
1548263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1549263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    holder.mIcon.setImageResource(header.iconRes);
1550263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1551263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                holder.mTitle.setText(header.getTitle(getContext().getResources()));
1552263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                CharSequence summary = header.getSummary(getContext().getResources());
1553263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                if (!TextUtils.isEmpty(summary)) {
1554263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    holder.mSummary.setVisibility(View.VISIBLE);
1555263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    holder.mSummary.setText(summary);
1556263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                } else {
1557263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    holder.mSummary.setVisibility(View.GONE);
1558263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                }
1559263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1560263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1561263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        private void setHeaderIcon(HeaderViewHolder holder, Drawable icon) {
1562263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            ViewGroup.LayoutParams lp = holder.mIcon.getLayoutParams();
1563263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            lp.width = getContext().getResources().getDimensionPixelSize(
1564263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                    R.dimen.header_icon_width);
1565263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            lp.height = lp.width;
1566263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            holder.mIcon.setLayoutParams(lp);
1567263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            holder.mIcon.setImageDrawable(icon);
1568263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1569263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1570263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void resume() {
1571263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mWifiEnabler.resume();
1572263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mBluetoothEnabler.resume();
1573263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1574263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1575263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void pause() {
1576263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mWifiEnabler.pause();
1577263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mBluetoothEnabler.pause();
1578263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1579263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1580263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1581263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void onListItemClick(ListView l, View v, int position, long id) {
1582263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (!isResumed()) {
1583263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return;
1584263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1585263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Object item = mHeaderAdapter.getItem(position);
1586263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (item instanceof Header) {
1587263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            mCurrentHeader = (Header) item;
1588263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1589263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1590263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1591263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
1592263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Called when the user selects an item in the header list.  The default
1593263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * implementation will call either
1594dc77b738a7697e57f1f630b4fe72f25835016d80Fabrice Di Meglio     * {@link #startWithFragment(String, android.os.Bundle, android.app.Fragment, int, int, CharSequence)}
159510afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio     * or {@link #switchToHeader(com.android.settings.SettingsActivity.Header, boolean)}
1596263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * as appropriate.
1597263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     *
1598263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * @param header The header that was selected.
1599263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
1600263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private void onHeaderClick(Header header) {
1601263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (header == null) return;
1602263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        if (header.fragment != null) {
160310afdb82aca7c6345d2aace30c7b66238e6bfb3cFabrice Di Meglio            switchToHeader(header, false);
1604263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        } else if (header.intent != null) {
1605263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            startActivity(header.intent);
1606263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1607263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1608263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1609263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1610263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public boolean shouldUpRecreateTask(Intent targetIntent) {
1611263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1612263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1613263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1614263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
1615263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onAccountsUpdated(Account[] accounts) {
1616263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        // TODO: watch for package upgrades to invalidate cache; see 7206643
1617263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.updateAuthDescriptions(this);
1618263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        mAuthenticatorHelper.onAccountsUpdated(this, accounts);
1619263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        invalidateHeaders();
1620263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1621263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1622263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public static void requestHomeNotice() {
1623263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        sShowNoHomeNotice = true;
1624263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1625263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1626263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
1627263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Default value for {@link Header#id Header.id} indicating that no
1628263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * identifier value is set.  All other values (including those below -1)
1629263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * are valid.
1630263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
1631263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    private static final long HEADER_ID_UNDEFINED = -1;
1632263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1633263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    /**
1634263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     * Description of a single Header item that the user can select.
1635263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio     */
1636263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    static final class Header implements Parcelable {
1637263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1638263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Identifier for this header, to correlate with a new list when
1639263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * it is updated.  The default value is
1640263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * {@link SettingsActivity#HEADER_ID_UNDEFINED}, meaning no id.
1641263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_id
1642263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1643263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public long id = HEADER_ID_UNDEFINED;
1644263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1645263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1646263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Resource ID of title of the header that is shown to the user.
1647263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_title
1648263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1649263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int titleRes;
1650263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1651263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1652263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Title of the header that is shown to the user.
1653263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_title
1654263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1655263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public CharSequence title;
1656263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1657263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1658263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Resource ID of optional summary describing what this header controls.
1659263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_summary
1660263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1661263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int summaryRes;
1662263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1663263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1664263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Optional summary describing what this header controls.
1665263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_summary
1666263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1667263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public CharSequence summary;
1668263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1669263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1670263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Optional icon resource to show for this header.
1671263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_icon
1672263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1673263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int iconRes;
1674263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1675263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1676263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Full class name of the fragment to display when this header is
1677263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * selected.
1678263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * @attr ref android.R.styleable#PreferenceHeader_fragment
1679263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1680263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public String fragment;
1681263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1682263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1683263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Optional arguments to supply to the fragment when it is
1684263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * instantiated.
1685263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1686263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public Bundle fragmentArguments;
1687263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1688263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1689263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Intent to launch when the preference is selected.
1690263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1691263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public Intent intent;
1692263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1693263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1694263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Optional additional data for use by subclasses of the activity
1695263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1696263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public Bundle extras;
1697263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1698263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public Header() {
1699263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            // Empty
1700263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1701263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1702263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1703263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Return the currently set title.  If {@link #titleRes} is set,
1704263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * this resource is loaded from <var>res</var> and returned.  Otherwise
1705263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * {@link #title} is returned.
1706263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1707263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public CharSequence getTitle(Resources res) {
1708263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (titleRes != 0) {
1709263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return res.getText(titleRes);
1710263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1711263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return title;
1712263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1713263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1714263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        /**
1715263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * Return the currently set summary.  If {@link #summaryRes} is set,
1716263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * this resource is loaded from <var>res</var> and returned.  Otherwise
1717263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         * {@link #summary} is returned.
1718263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio         */
1719263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public CharSequence getSummary(Resources res) {
1720263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (summaryRes != 0) {
1721263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return res.getText(summaryRes);
1722263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1723263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return summary;
1724263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1725263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1726263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1727263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public int describeContents() {
1728263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            return 0;
1729263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1730263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1731263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        @Override
1732263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void writeToParcel(Parcel dest, int flags) {
1733263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeLong(id);
1734263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeInt(titleRes);
1735263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextUtils.writeToParcel(title, dest, flags);
1736263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeInt(summaryRes);
1737263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            TextUtils.writeToParcel(summary, dest, flags);
1738263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeInt(iconRes);
1739263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeString(fragment);
1740263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeBundle(fragmentArguments);
1741263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (intent != null) {
1742263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                dest.writeInt(1);
1743263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                intent.writeToParcel(dest, flags);
1744263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            } else {
1745263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                dest.writeInt(0);
1746263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1747263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            dest.writeBundle(extras);
1748263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1749263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1750263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public void readFromParcel(Parcel in) {
1751263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            id = in.readLong();
1752263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            titleRes = in.readInt();
1753263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1754263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            summaryRes = in.readInt();
1755263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1756263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            iconRes = in.readInt();
1757263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            fragment = in.readString();
1758263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            fragmentArguments = in.readBundle();
1759263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (in.readInt() != 0) {
1760263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                intent = Intent.CREATOR.createFromParcel(in);
1761263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1762263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            extras = in.readBundle();
1763263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1764263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1765263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        Header(Parcel in) {
1766263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            readFromParcel(in);
1767263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        }
1768263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
1769263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        public static final Creator<Header> CREATOR = new Creator<Header>() {
1770263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public Header createFromParcel(Parcel source) {
1771263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return new Header(source);
1772263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1773263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            public Header[] newArray(int size) {
1774263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                return new Header[size];
1775263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            }
1776263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        };
1777263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
1778263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
1779