getHeaders() {
return mHeaders;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mHeaders.size() > 0) {
outState.putParcelableArrayList(SAVE_KEY_HEADERS_TAG, mHeaders);
if (mCurrentHeader != null) {
int index = mHeaders.indexOf(mCurrentHeader);
if (index >= 0) {
outState.putInt(SAVE_KEY_CURRENT_HEADER_TAG, index);
}
}
}
if (mTitleStack.size() > 0) {
outState.putParcelableList(SAVE_KEY_TITLES_TAG, mTitleStack);
}
}
@Override
public void onResume() {
super.onResume();
mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
invalidateHeaders();
}
};
mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
mDevelopmentPreferencesListener);
mHeaderAdapter.resume();
invalidateHeaders();
registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
mDrawerLayout.setDrawerListener(new DrawerListener());
}
@Override
public void onPause() {
super.onPause();
mDrawerLayout.setDrawerListener(null);
unregisterReceiver(mBatteryInfoReceiver);
mHeaderAdapter.pause();
mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
mDevelopmentPreferencesListener);
mDevelopmentPreferencesListener = null;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mListeningToAccountUpdates) {
AccountManager.get(this).removeOnAccountsUpdatedListener(this);
}
}
/**
* @hide
*/
protected boolean isValidFragment(String fragmentName) {
// Almost all fragments are wrapped in this,
// except for a few that have their own activities.
for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
}
return false;
}
/**
* When in two-pane mode, switch to the fragment pane to show the given
* preference fragment.
*
* @param header The new header to display.
* @param validate true means that the fragment's Header needs to be validated
*/
private void switchToHeader(Header header, boolean validate) {
if (mCurrentHeader == header) {
// This is the header we are currently displaying. Just make sure
// to pop the stack up to its root state.
getFragmentManager().popBackStack(BACK_STACK_PREFS,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
mTitleStack.clear();
if (header.fragment == null) {
throw new IllegalStateException("can't switch to header that has no fragment");
}
switchToHeaderInner(header.fragment, header.fragmentArguments, validate);
setSelectedHeader(header);
final CharSequence title;
if (header.fragment.equals("com.android.settings.dashboard.DashboardSummary")) {
title = getResources().getString(R.string.settings_label);
} else {
title = header.getTitle(getResources());
}
final TitlePair pair = new TitlePair(0, title);
mTitleStack.add(pair);
setTitle(title);
}
}
private void setSelectedHeader(Header header) {
mCurrentHeader = header;
int index = mHeaders.indexOf(header);
if (mDrawer != null) {
if (index >= 0) {
mDrawer.setItemChecked(index, true);
} else {
mDrawer.clearChoices();
}
}
}
public Header onGetInitialHeader() {
String fragmentClass = getStartingFragmentClass(super.getIntent());
if (fragmentClass != null) {
Header header = new Header();
header.fragment = fragmentClass;
header.title = getTitle();
header.fragmentArguments = getIntent().getExtras();
return header;
}
return mFirstHeader;
}
/**
* When in two-pane mode, switch the fragment pane to show the given
* preference fragment.
*
* @param fragmentName The name of the fragment to display.
* @param args Optional arguments to supply to the fragment.
* @param validate true means that the fragment's Header needs to be validated
*/
private void switchToHeader(String fragmentName, Bundle args, boolean validate) {
setSelectedHeader(null);
switchToHeaderInner(fragmentName, args, validate);
}
private void switchToHeaderInner(String fragmentName, Bundle args, boolean validate) {
getFragmentManager().popBackStack(BACK_STACK_PREFS,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
if (validate && !isValidFragment(fragmentName)) {
throw new IllegalArgumentException("Invalid fragment for this activity: "
+ fragmentName);
}
Fragment f = Fragment.instantiate(this, fragmentName, args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.replace(R.id.prefs, f);
transaction.commitAllowingStateLoss();
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// If it is not launched from history, then reset to top-level
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
if (mDrawer != null) {
mDrawer.setSelectionFromTop(0, 0);
}
}
}
/**
* Called to determine whether the header list should be hidden.
* The default implementation returns the
* value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied.
* This is set to false, for example, when the activity is being re-launched
* to show a particular preference activity.
*/
public boolean onIsHidingHeaders() {
return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
}
private void highlightHeader(int id) {
if (id != 0) {
Integer index = mHeaderIndexMap.get(id);
if (index != null && mDrawer != null) {
mDrawer.setItemChecked(index, true);
if (mDrawer.getVisibility() == View.VISIBLE) {
mDrawer.smoothScrollToPosition(index);
}
}
}
}
@Override
public Intent getIntent() {
Intent superIntent = super.getIntent();
String startingFragment = getStartingFragmentClass(superIntent);
// This is called from super.onCreate, isMultiPane() is not yet reliable
// Do not use onIsHidingHeaders either, which relies itself on this method
if (startingFragment != null) {
Intent modIntent = new Intent(superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Bundle args = superIntent.getExtras();
if (args != null) {
args = new Bundle(args);
} else {
args = new Bundle();
}
args.putParcelable("intent", superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
return modIntent;
}
return superIntent;
}
/**
* Checks if the component name in the intent is different from the Settings class and
* returns the class name to load as a fragment.
*/
private String getStartingFragmentClass(Intent intent) {
if (mFragmentClass != null) return mFragmentClass;
String intentClass = intent.getComponent().getClassName();
if (intentClass.equals(getClass().getName())) return null;
if ("com.android.settings.ManageApplications".equals(intentClass)
|| "com.android.settings.RunningServices".equals(intentClass)
|| "com.android.settings.applications.StorageUse".equals(intentClass)) {
// Old names of manage apps.
intentClass = com.android.settings.applications.ManageApplications.class.getName();
}
return intentClass;
}
/**
* Start a new fragment containing a preference panel. If the preferences
* are being displayed in multi-pane mode, the given fragment class will
* be instantiated and placed in the appropriate pane. If running in
* single-pane mode, a new activity will be launched in which to show the
* fragment.
*
* @param fragmentClass Full name of the class implementing the fragment.
* @param args Any desired arguments to supply to the fragment.
* @param titleRes Optional resource identifier of the title of this
* fragment.
* @param titleText Optional text of the title of this fragment.
* @param resultTo Optional fragment that result data should be sent to.
* If non-null, resultTo.onActivityResult() will be called when this
* preference panel is done. The launched panel must use
* {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
* @param resultRequestCode If resultTo is non-null, this is the caller's
* request code to be received with the resut.
*/
public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
CharSequence titleText, Fragment resultTo,
int resultRequestCode) {
startWithFragment(fragmentClass, args, resultTo, resultRequestCode, titleRes, titleText);
}
/**
* Called by a preference panel fragment to finish itself.
*
* @param caller The fragment that is asking to be finished.
* @param resultCode Optional result code to send back to the original
* launching fragment.
* @param resultData Optional result data to send back to the original
* launching fragment.
*/
public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
setResult(resultCode, resultData);
}
/**
* Start a new fragment.
*
* @param fragment The fragment to start
* @param push If true, the current fragment will be pushed onto the back stack. If false,
* the current fragment will be replaced.
*/
public void startPreferenceFragment(Fragment fragment, boolean push) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.prefs, fragment);
if (push) {
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(BACK_STACK_PREFS);
} else {
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
}
transaction.commitAllowingStateLoss();
}
/**
* Start a new fragment.
*
* @param fragmentName The name of the fragment to display.
* @param args Optional arguments to supply to the fragment.
* @param resultTo Option fragment that should receive the result of
* the activity launch.
* @param resultRequestCode If resultTo is non-null, this is the request code in which to
* report the result.
* @param titleRes Resource ID of string to display for the title of. If the Resource ID is a
* valid one then it will be used to get the title. Otherwise the titleText
* argument will be used as the title.
* @param titleText string to display for the title of.
*/
private void startWithFragment(String fragmentName, Bundle args, Fragment resultTo,
int resultRequestCode, int titleRes, CharSequence titleText) {
Fragment f = Fragment.instantiate(this, fragmentName, args);
if (resultTo != null) {
f.setTargetFragment(resultTo, resultRequestCode);
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.prefs, f);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(BACK_STACK_PREFS);
transaction.commitAllowingStateLoss();
final TitlePair pair;
final CharSequence cs;
if (titleRes != 0) {
pair = new TitlePair(titleRes, null);
cs = getText(titleRes);
} else {
pair = new TitlePair(0, titleText);
cs = titleText;
}
setTitle(cs);
mTitleStack.add(pair);
}
/**
* Called when the activity needs its list of headers build. By
* implementing this and adding at least one item to the list, you
* will cause the activity to run in its modern fragment mode. Note
* that this function may not always be called; for example, if the
* activity has been asked to display a particular fragment without
* the header list, there is no need to build the headers.
*
* Typical implementations will use {@link #loadHeadersFromResource}
* to fill in the list from a resource.
*
* @param headers The list in which to place the headers.
*/
private void onBuildHeaders(List headers) {
loadHeadersFromResource(R.xml.settings_headers, headers);
updateHeaderList(headers);
}
/**
* Parse the given XML file as a header description, adding each
* parsed Header into the target list.
*
* @param resid The XML resource to load and parse.
* @param target The list in which the parsed headers should be placed.
*/
private void loadHeadersFromResource(int resid, List target) {
XmlResourceParser parser = null;
try {
parser = getResources().getXml(resid);
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& type != XmlPullParser.START_TAG) {
// Parse next until start tag is found
}
String nodeName = parser.getName();
if (!"preference-headers".equals(nodeName)) {
throw new RuntimeException(
"XML document must start with tag; found"
+ nodeName + " at " + parser.getPositionDescription());
}
Bundle curBundle = null;
final int outerDepth = parser.getDepth();
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
nodeName = parser.getName();
if ("header".equals(nodeName)) {
Header header = new Header();
TypedArray sa = obtainStyledAttributes(
attrs, com.android.internal.R.styleable.PreferenceHeader);
header.id = sa.getResourceId(
com.android.internal.R.styleable.PreferenceHeader_id,
(int)HEADER_ID_UNDEFINED);
TypedValue tv = sa.peekValue(
com.android.internal.R.styleable.PreferenceHeader_title);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.titleRes = tv.resourceId;
} else {
header.title = tv.string;
}
}
tv = sa.peekValue(
com.android.internal.R.styleable.PreferenceHeader_summary);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.summaryRes = tv.resourceId;
} else {
header.summary = tv.string;
}
}
header.iconRes = sa.getResourceId(
com.android.internal.R.styleable.PreferenceHeader_icon, 0);
header.fragment = sa.getString(
com.android.internal.R.styleable.PreferenceHeader_fragment);
sa.recycle();
if (curBundle == null) {
curBundle = new Bundle();
}
final int innerDepth = parser.getDepth();
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String innerNodeName = parser.getName();
if (innerNodeName.equals("extra")) {
getResources().parseBundleExtra("extra", attrs, curBundle);
XmlUtils.skipCurrentTag(parser);
} else if (innerNodeName.equals("intent")) {
header.intent = Intent.parseIntent(getResources(), parser, attrs);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
if (curBundle.size() > 0) {
header.fragmentArguments = curBundle;
curBundle = null;
}
target.add(header);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
} catch (XmlPullParserException e) {
throw new RuntimeException("Error parsing headers", e);
} catch (IOException e) {
throw new RuntimeException("Error parsing headers", e);
} finally {
if (parser != null) parser.close();
}
}
private void updateHeaderList(List target) {
final boolean showDev = mDevelopmentPreferences.getBoolean(
DevelopmentSettings.PREF_SHOW,
android.os.Build.TYPE.equals("eng"));
int i = 0;
final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
mHeaderIndexMap.clear();
while (i < target.size()) {
Header header = target.get(i);
// Ids are integers, so downcasting
int id = (int) header.id;
if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
} else if (id == R.id.wifi_settings) {
// Remove WiFi Settings if WiFi service is not available.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
target.remove(i);
}
} else if (id == R.id.bluetooth_settings) {
// Remove Bluetooth Settings if Bluetooth service is not available.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
target.remove(i);
}
} else if (id == R.id.data_usage_settings) {
// Remove data usage when kernel module not enabled
final INetworkManagementService netManager = INetworkManagementService.Stub
.asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
try {
if (!netManager.isBandwidthControlEnabled()) {
target.remove(i);
}
} catch (RemoteException e) {
// ignored
}
} else if (id == R.id.battery_settings) {
// Remove battery settings when battery is not available. (e.g. TV)
if (!mBatteryPresent) {
target.remove(i);
}
} else if (id == R.id.account_settings) {
int headerIndex = i + 1;
i = insertAccountsHeaders(target, headerIndex);
} else if (id == R.id.home_settings) {
if (!updateHomeSettingHeaders(header)) {
target.remove(i);
}
} else if (id == R.id.user_settings) {
if (!UserHandle.MU_ENABLED
|| !UserManager.supportsMultipleUsers()
|| Utils.isMonkeyRunning()) {
target.remove(i);
}
} else if (id == R.id.nfc_payment_settings) {
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
target.remove(i);
} else {
// Only show if NFC is on and we have the HCE feature
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
target.remove(i);
}
}
} else if (id == R.id.development_settings) {
if (!showDev) {
target.remove(i);
}
} else if (id == R.id.account_add) {
if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
target.remove(i);
}
}
if (i < target.size() && target.get(i) == header
&& UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
&& !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
target.remove(i);
}
// Increment if the current one wasn't removed by the Utils code.
if (i < target.size() && target.get(i) == header) {
// Hold on to the first header, when we need to reset to the top-level
if (mFirstHeader == null &&
HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
mFirstHeader = header;
}
mHeaderIndexMap.put(id, i);
i++;
}
}
}
private int insertAccountsHeaders(List target, int headerIndex) {
String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
List accountHeaders = new ArrayList(accountTypes.length);
for (String accountType : accountTypes) {
CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
if (label == null) {
continue;
}
Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
boolean skipToAccount = accounts.length == 1
&& !mAuthenticatorHelper.hasAccountPreferences(accountType);
Header accHeader = new Header();
accHeader.title = label;
if (accHeader.extras == null) {
accHeader.extras = new Bundle();
}
if (skipToAccount) {
accHeader.fragment = AccountSyncSettings.class.getName();
accHeader.fragmentArguments = new Bundle();
// Need this for the icon
accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
accounts[0]);
} else {
accHeader.fragment = ManageAccountsSettings.class.getName();
accHeader.fragmentArguments = new Bundle();
accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
accountType);
accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
label.toString());
}
accountHeaders.add(accHeader);
mAuthenticatorHelper.preloadDrawableForType(this, accountType);
}
// Sort by label
Collections.sort(accountHeaders, new Comparator() {
@Override
public int compare(Header h1, Header h2) {
return h1.title.toString().compareTo(h2.title.toString());
}
});
for (Header header : accountHeaders) {
target.add(headerIndex++, header);
}
if (!mListeningToAccountUpdates) {
AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
mListeningToAccountUpdates = true;
}
return headerIndex;
}
private boolean updateHomeSettingHeaders(Header header) {
// Once we decide to show Home settings, keep showing it forever
SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
return true;
}
try {
final ArrayList homeApps = new ArrayList();
getPackageManager().getHomeActivities(homeApps);
if (homeApps.size() < 2) {
// When there's only one available home app, omit this settings
// category entirely at the top level UI. If the user just
// uninstalled the penultimate home app candidiate, we also
// now tell them about why they aren't seeing 'Home' in the list.
if (sShowNoHomeNotice) {
sShowNoHomeNotice = false;
NoHomeDialogFragment.show(this);
}
return false;
} else {
// Okay, we're allowing the Home settings category. Tell it, when
// invoked via this front door, that we'll need to be told about the
// case when the user uninstalls all but one home app.
if (header.fragmentArguments == null) {
header.fragmentArguments = new Bundle();
}
header.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
}
} catch (Exception e) {
// Can't look up the home activity; bail on configuring the icon
Log.w(LOG_TAG, "Problem looking up home activity!", e);
}
sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
return true;
}
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null) return;
mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
} catch (NameNotFoundException nnfe) {
// No recovery
Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
}
}
// give subclasses access to the Next button
public boolean hasNextButton() {
return mNextButton != null;
}
public Button getNextButton() {
return mNextButton;
}
public static class NoHomeDialogFragment extends DialogFragment {
public static void show(Activity parent) {
final NoHomeDialogFragment dialog = new NoHomeDialogFragment();
dialog.show(parent.getFragmentManager(), null);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.only_one_home_message)
.setPositiveButton(android.R.string.ok, null)
.create();
}
}
private static class HeaderAdapter extends ArrayAdapter {
static final int HEADER_TYPE_CATEGORY = 0;
static final int HEADER_TYPE_NORMAL = 1;
static final int HEADER_TYPE_SWITCH = 2;
static final int HEADER_TYPE_BUTTON = 3;
private static final int HEADER_TYPE_COUNT = HEADER_TYPE_BUTTON + 1;
private final WifiEnabler mWifiEnabler;
private final BluetoothEnabler mBluetoothEnabler;
private AuthenticatorHelper mAuthHelper;
private DevicePolicyManager mDevicePolicyManager;
private static class HeaderViewHolder {
ImageView mIcon;
TextView mTitle;
TextView mSummary;
Switch mSwitch;
ImageButton mButton;
View mDivider;
}
private LayoutInflater mInflater;
static int getHeaderType(Header header) {
if (header.fragment == null && header.intent == null) {
return HEADER_TYPE_CATEGORY;
} else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
return HEADER_TYPE_SWITCH;
} else if (header.id == R.id.security_settings) {
return HEADER_TYPE_BUTTON;
} else {
return HEADER_TYPE_NORMAL;
}
}
@Override
public int getItemViewType(int position) {
Header header = getItem(position);
return getHeaderType(header);
}
@Override
public boolean areAllItemsEnabled() {
return false; // because of categories
}
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) != HEADER_TYPE_CATEGORY;
}
@Override
public int getViewTypeCount() {
return HEADER_TYPE_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
public HeaderAdapter(Context context, List objects,
AuthenticatorHelper authenticatorHelper, DevicePolicyManager dpm) {
super(context, 0, objects);
mAuthHelper = authenticatorHelper;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Temp Switches provided as placeholder until the adapter replaces these with actual
// Switches inflated from their layouts. Must be done before adapter is set in super
mWifiEnabler = new WifiEnabler(context, new Switch(context));
mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
mDevicePolicyManager = dpm;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
Header header = getItem(position);
int headerType = getHeaderType(header);
View view = null;
if (convertView == null) {
holder = new HeaderViewHolder();
switch (headerType) {
case HEADER_TYPE_CATEGORY:
view = new TextView(getContext(), null,
android.R.attr.listSeparatorTextViewStyle);
holder.mTitle = (TextView) view;
break;
case HEADER_TYPE_SWITCH:
view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
false);
holder.mIcon = (ImageView) view.findViewById(R.id.icon);
holder.mTitle = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.mSummary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
holder.mSwitch = (Switch) view.findViewById(R.id.switchWidget);
break;
case HEADER_TYPE_BUTTON:
view = mInflater.inflate(R.layout.preference_header_button_item, parent,
false);
holder.mIcon = (ImageView) view.findViewById(R.id.icon);
holder.mTitle = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.mSummary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
holder.mButton = (ImageButton) view.findViewById(R.id.buttonWidget);
holder.mDivider = view.findViewById(R.id.divider);
break;
case HEADER_TYPE_NORMAL:
view = mInflater.inflate(
R.layout.preference_header_item, parent,
false);
holder.mIcon = (ImageView) view.findViewById(R.id.icon);
holder.mTitle = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.mSummary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
break;
}
view.setTag(holder);
} else {
view = convertView;
holder = (HeaderViewHolder) view.getTag();
}
// All view fields must be updated every time, because the view may be recycled
switch (headerType) {
case HEADER_TYPE_CATEGORY:
holder.mTitle.setText(header.getTitle(getContext().getResources()));
break;
case HEADER_TYPE_SWITCH:
// Would need a different treatment if the main menu had more switches
if (header.id == R.id.wifi_settings) {
mWifiEnabler.setSwitch(holder.mSwitch);
} else {
mBluetoothEnabler.setSwitch(holder.mSwitch);
}
updateCommonHeaderView(header, holder);
break;
case HEADER_TYPE_BUTTON:
if (header.id == R.id.security_settings) {
boolean hasCert = DevicePolicyManager.hasAnyCaCertsInstalled();
if (hasCert) {
holder.mButton.setVisibility(View.VISIBLE);
holder.mDivider.setVisibility(View.VISIBLE);
boolean isManaged = mDevicePolicyManager.getDeviceOwner() != null;
if (isManaged) {
holder.mButton.setImageResource(R.drawable.ic_settings_about);
} else {
holder.mButton.setImageResource(
android.R.drawable.stat_notify_error);
}
holder.mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
android.provider.Settings.ACTION_MONITORING_CERT_INFO);
getContext().startActivity(intent);
}
});
} else {
holder.mButton.setVisibility(View.GONE);
holder.mDivider.setVisibility(View.GONE);
}
}
updateCommonHeaderView(header, holder);
break;
case HEADER_TYPE_NORMAL:
updateCommonHeaderView(header, holder);
break;
}
return view;
}
private void updateCommonHeaderView(Header header, HeaderViewHolder holder) {
if (header.extras != null
&& header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
String accType = header.extras.getString(
ManageAccountsSettings.KEY_ACCOUNT_TYPE);
Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
setHeaderIcon(holder, icon);
} else {
holder.mIcon.setImageResource(header.iconRes);
}
holder.mTitle.setText(header.getTitle(getContext().getResources()));
CharSequence summary = header.getSummary(getContext().getResources());
if (!TextUtils.isEmpty(summary)) {
holder.mSummary.setVisibility(View.VISIBLE);
holder.mSummary.setText(summary);
} else {
holder.mSummary.setVisibility(View.GONE);
}
}
private void setHeaderIcon(HeaderViewHolder holder, Drawable icon) {
ViewGroup.LayoutParams lp = holder.mIcon.getLayoutParams();
lp.width = getContext().getResources().getDimensionPixelSize(
R.dimen.header_icon_width);
lp.height = lp.width;
holder.mIcon.setLayoutParams(lp);
holder.mIcon.setImageDrawable(icon);
}
public void resume() {
mWifiEnabler.resume();
mBluetoothEnabler.resume();
}
public void pause() {
mWifiEnabler.pause();
mBluetoothEnabler.pause();
}
}
private void onListItemClick(ListView l, View v, int position, long id) {
if (!isResumed()) {
return;
}
Object item = mHeaderAdapter.getItem(position);
if (item instanceof Header) {
mSelectedHeader = (Header) item;
}
}
/**
* Called when the user selects an item in the header list. The default
* implementation will call either
* {@link #startWithFragment(String, android.os.Bundle, android.app.Fragment, int, int, CharSequence)}
* or {@link #switchToHeader(com.android.settings.SettingsActivity.Header, boolean)}
* as appropriate.
*
* @param header The header that was selected.
*/
private void onHeaderClick(Header header) {
if (header == null) return;
if (header.fragment != null) {
switchToHeader(header, false);
} else if (header.intent != null) {
startActivity(header.intent);
}
}
@Override
public boolean shouldUpRecreateTask(Intent targetIntent) {
return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
}
@Override
public void onAccountsUpdated(Account[] accounts) {
// TODO: watch for package upgrades to invalidate cache; see 7206643
mAuthenticatorHelper.updateAuthDescriptions(this);
mAuthenticatorHelper.onAccountsUpdated(this, accounts);
invalidateHeaders();
}
public static void requestHomeNotice() {
sShowNoHomeNotice = true;
}
/**
* Default value for {@link Header#id Header.id} indicating that no
* identifier value is set. All other values (including those below -1)
* are valid.
*/
private static final long HEADER_ID_UNDEFINED = -1;
/**
* Description of a single Header item that the user can select.
*/
static final class Header implements Parcelable {
/**
* Identifier for this header, to correlate with a new list when
* it is updated. The default value is
* {@link SettingsActivity#HEADER_ID_UNDEFINED}, meaning no id.
* @attr ref android.R.styleable#PreferenceHeader_id
*/
public long id = HEADER_ID_UNDEFINED;
/**
* Resource ID of title of the header that is shown to the user.
* @attr ref android.R.styleable#PreferenceHeader_title
*/
public int titleRes;
/**
* Title of the header that is shown to the user.
* @attr ref android.R.styleable#PreferenceHeader_title
*/
public CharSequence title;
/**
* Resource ID of optional summary describing what this header controls.
* @attr ref android.R.styleable#PreferenceHeader_summary
*/
public int summaryRes;
/**
* Optional summary describing what this header controls.
* @attr ref android.R.styleable#PreferenceHeader_summary
*/
public CharSequence summary;
/**
* Optional icon resource to show for this header.
* @attr ref android.R.styleable#PreferenceHeader_icon
*/
public int iconRes;
/**
* Full class name of the fragment to display when this header is
* selected.
* @attr ref android.R.styleable#PreferenceHeader_fragment
*/
public String fragment;
/**
* Optional arguments to supply to the fragment when it is
* instantiated.
*/
public Bundle fragmentArguments;
/**
* Intent to launch when the preference is selected.
*/
public Intent intent;
/**
* Optional additional data for use by subclasses of the activity
*/
public Bundle extras;
public Header() {
// Empty
}
/**
* Return the currently set title. If {@link #titleRes} is set,
* this resource is loaded from res and returned. Otherwise
* {@link #title} is returned.
*/
public CharSequence getTitle(Resources res) {
if (titleRes != 0) {
return res.getText(titleRes);
}
return title;
}
/**
* Return the currently set summary. If {@link #summaryRes} is set,
* this resource is loaded from res and returned. Otherwise
* {@link #summary} is returned.
*/
public CharSequence getSummary(Resources res) {
if (summaryRes != 0) {
return res.getText(summaryRes);
}
return summary;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeInt(titleRes);
TextUtils.writeToParcel(title, dest, flags);
dest.writeInt(summaryRes);
TextUtils.writeToParcel(summary, dest, flags);
dest.writeInt(iconRes);
dest.writeString(fragment);
dest.writeBundle(fragmentArguments);
if (intent != null) {
dest.writeInt(1);
intent.writeToParcel(dest, flags);
} else {
dest.writeInt(0);
}
dest.writeBundle(extras);
}
public void readFromParcel(Parcel in) {
id = in.readLong();
titleRes = in.readInt();
title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
summaryRes = in.readInt();
summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
iconRes = in.readInt();
fragment = in.readString();
fragmentArguments = in.readBundle();
if (in.readInt() != 0) {
intent = Intent.CREATOR.createFromParcel(in);
}
extras = in.readBundle();
}
Header(Parcel in) {
readFromParcel(in);
}
public static final Creator CREATOR = new Creator() {
public Header createFromParcel(Parcel source) {
return new Header(source);
}
public Header[] newArray(int size) {
return new Header[size];
}
};
}
}