Merge "Add SwitchPreference"

This commit is contained in:
Gilles Debunne
2011-06-09 10:25:25 -07:00
committed by Android (Google) Code Review
12 changed files with 636 additions and 324 deletions

View File

@@ -14255,20 +14255,10 @@ package android.os.storage {
package android.preference {
public class CheckBoxPreference extends android.preference.Preference {
public class CheckBoxPreference extends android.preference.TwoStatePreference {
ctor public CheckBoxPreference(android.content.Context, android.util.AttributeSet, int);
ctor public CheckBoxPreference(android.content.Context, android.util.AttributeSet);
ctor public CheckBoxPreference(android.content.Context);
method public boolean getDisableDependentsState();
method public java.lang.CharSequence getSummaryOff();
method public java.lang.CharSequence getSummaryOn();
method public boolean isChecked();
method public void setChecked(boolean);
method public void setDisableDependentsState(boolean);
method public void setSummaryOff(java.lang.CharSequence);
method public void setSummaryOff(int);
method public void setSummaryOn(java.lang.CharSequence);
method public void setSummaryOn(int);
}
public abstract class DialogPreference extends android.preference.Preference implements android.content.DialogInterface.OnClickListener android.content.DialogInterface.OnDismissListener android.preference.PreferenceManager.OnActivityDestroyListener {

View File

@@ -181,9 +181,9 @@ package android {
public static final class R.attr {
ctor public R.attr();
field public static final int absListViewStyle = 16842858; // 0x101006a
field public static final int accessibilityEventTypes = 16843647; // 0x101037f
field public static final int accessibilityFeedbackType = 16843649; // 0x1010381
field public static final int accessibilityFlags = 16843651; // 0x1010383
field public static final int accessibilityEventTypes = 16843650; // 0x1010382
field public static final int accessibilityFeedbackType = 16843652; // 0x1010384
field public static final int accessibilityFlags = 16843654; // 0x1010386
field public static final int accountPreferences = 16843423; // 0x101029f
field public static final int accountType = 16843407; // 0x101028f
field public static final int action = 16842797; // 0x101002d
@@ -203,7 +203,7 @@ package android {
field public static final int actionModeCopyDrawable = 16843538; // 0x1010312
field public static final int actionModeCutDrawable = 16843537; // 0x1010311
field public static final int actionModePasteDrawable = 16843539; // 0x1010313
field public static final int actionModeSelectAllDrawable = 16843645; // 0x101037d
field public static final int actionModeSelectAllDrawable = 16843648; // 0x1010380
field public static final int actionOverflowButtonStyle = 16843510; // 0x10102f6
field public static final int actionViewClass = 16843516; // 0x10102fc
field public static final int activatedBackgroundIndicator = 16843517; // 0x10102fd
@@ -275,7 +275,7 @@ package android {
field public static final int cacheColorHint = 16843009; // 0x1010101
field public static final int calendarViewShown = 16843596; // 0x101034c
field public static final int calendarViewStyle = 16843613; // 0x101035d
field public static final int canRetrieveWindowContent = 16843652; // 0x1010384
field public static final int canRetrieveWindowContent = 16843655; // 0x1010387
field public static final int candidatesTextStyleSpans = 16843312; // 0x1010230
field public static final deprecated int capitalize = 16843113; // 0x1010169
field public static final int centerBright = 16842956; // 0x10100cc
@@ -308,9 +308,9 @@ package android {
field public static final int colorBackgroundCacheHint = 16843435; // 0x10102ab
field public static final int colorForeground = 16842800; // 0x1010030
field public static final int colorForegroundInverse = 16843270; // 0x1010206
field public static final int columnCount = 16843636; // 0x1010374
field public static final int columnCount = 16843639; // 0x1010377
field public static final int columnDelay = 16843215; // 0x10101cf
field public static final int columnOrderPreserved = 16843637; // 0x1010375
field public static final int columnOrderPreserved = 16843640; // 0x1010378
field public static final int columnWidth = 16843031; // 0x1010117
field public static final int compatibleWidthLimitDp = 16843621; // 0x1010365
field public static final int completionHint = 16843122; // 0x1010172
@@ -455,7 +455,7 @@ package android {
field public static final int fromXScale = 16843202; // 0x10101c2
field public static final int fromYDelta = 16843208; // 0x10101c8
field public static final int fromYScale = 16843204; // 0x10101c4
field public static final int fullBackupAgent = 16843632; // 0x1010370
field public static final int fullBackupAgent = 16843635; // 0x1010373
field public static final int fullBright = 16842954; // 0x10100ca
field public static final int fullDark = 16842950; // 0x10100c6
field public static final int functionalTest = 16842787; // 0x1010023
@@ -533,7 +533,7 @@ package android {
field public static final int installLocation = 16843447; // 0x10102b7
field public static final int interpolator = 16843073; // 0x1010141
field public static final int isAlwaysSyncable = 16843571; // 0x1010333
field public static final int isAuxiliary = 16843646; // 0x101037e
field public static final int isAuxiliary = 16843649; // 0x1010381
field public static final int isDefault = 16843297; // 0x1010221
field public static final int isIndicator = 16843079; // 0x1010147
field public static final int isModifier = 16843334; // 0x1010246
@@ -570,7 +570,7 @@ package android {
field public static final int layerType = 16843604; // 0x1010354
field public static final int layout = 16842994; // 0x10100f2
field public static final int layoutAnimation = 16842988; // 0x10100ec
field public static final int layoutDirection = 16843631; // 0x101036f
field public static final int layoutDirection = 16843634; // 0x1010372
field public static final int layout_above = 16843140; // 0x1010184
field public static final int layout_alignBaseline = 16843142; // 0x1010186
field public static final int layout_alignBottom = 16843146; // 0x101018a
@@ -587,8 +587,8 @@ package android {
field public static final int layout_centerInParent = 16843151; // 0x101018f
field public static final int layout_centerVertical = 16843153; // 0x1010191
field public static final int layout_column = 16843084; // 0x101014c
field public static final int layout_columnSpan = 16843643; // 0x101037b
field public static final int layout_columnWeight = 16843644; // 0x101037c
field public static final int layout_columnSpan = 16843646; // 0x101037e
field public static final int layout_columnWeight = 16843647; // 0x101037f
field public static final int layout_gravity = 16842931; // 0x10100b3
field public static final int layout_height = 16842997; // 0x10100f5
field public static final int layout_margin = 16842998; // 0x10100f6
@@ -596,9 +596,9 @@ package android {
field public static final int layout_marginLeft = 16842999; // 0x10100f7
field public static final int layout_marginRight = 16843001; // 0x10100f9
field public static final int layout_marginTop = 16843000; // 0x10100f8
field public static final int layout_row = 16843640; // 0x1010378
field public static final int layout_rowSpan = 16843641; // 0x1010379
field public static final int layout_rowWeight = 16843642; // 0x101037a
field public static final int layout_row = 16843643; // 0x101037b
field public static final int layout_rowSpan = 16843644; // 0x101037c
field public static final int layout_rowWeight = 16843645; // 0x101037d
field public static final int layout_scale = 16843155; // 0x1010193
field public static final int layout_span = 16843085; // 0x101014d
field public static final int layout_toLeftOf = 16843138; // 0x1010182
@@ -628,7 +628,7 @@ package android {
field public static final int loopViews = 16843527; // 0x1010307
field public static final int manageSpaceActivity = 16842756; // 0x1010004
field public static final int mapViewStyle = 16842890; // 0x101008a
field public static final int marginsIncludedInAlignment = 16843639; // 0x1010377
field public static final int marginsIncludedInAlignment = 16843642; // 0x101037a
field public static final int marqueeRepeatLimit = 16843293; // 0x101021d
field public static final int max = 16843062; // 0x1010136
field public static final int maxDate = 16843584; // 0x1010340
@@ -665,7 +665,7 @@ package android {
field public static final int nextFocusUp = 16842979; // 0x10100e3
field public static final int noHistory = 16843309; // 0x101022d
field public static final int normalScreens = 16843397; // 0x1010285
field public static final int notificationTimeout = 16843650; // 0x1010382
field public static final int notificationTimeout = 16843653; // 0x1010385
field public static final int numColumns = 16843032; // 0x1010118
field public static final int numStars = 16843076; // 0x1010144
field public static final deprecated int numeric = 16843109; // 0x1010165
@@ -682,7 +682,7 @@ package android {
field public static final int overScrollFooter = 16843459; // 0x10102c3
field public static final int overScrollHeader = 16843458; // 0x10102c2
field public static final int overScrollMode = 16843457; // 0x10102c1
field public static final int packageNames = 16843648; // 0x1010380
field public static final int packageNames = 16843651; // 0x1010383
field public static final int padding = 16842965; // 0x10100d5
field public static final int paddingBottom = 16842969; // 0x10100d9
field public static final int paddingLeft = 16842966; // 0x10100d6
@@ -772,11 +772,11 @@ package android {
field public static final int rotation = 16843558; // 0x1010326
field public static final int rotationX = 16843559; // 0x1010327
field public static final int rotationY = 16843560; // 0x1010328
field public static final int rowCount = 16843634; // 0x1010372
field public static final int rowCount = 16843637; // 0x1010375
field public static final int rowDelay = 16843216; // 0x10101d0
field public static final int rowEdgeFlags = 16843329; // 0x1010241
field public static final int rowHeight = 16843058; // 0x1010132
field public static final int rowOrderPreserved = 16843635; // 0x1010373
field public static final int rowOrderPreserved = 16843638; // 0x1010376
field public static final int saveEnabled = 16842983; // 0x10100e7
field public static final int scaleGravity = 16843262; // 0x10101fe
field public static final int scaleHeight = 16843261; // 0x10101fd
@@ -889,12 +889,15 @@ package android {
field public static final int subtitleTextStyle = 16843513; // 0x10102f9
field public static final int suggestActionMsg = 16843228; // 0x10101dc
field public static final int suggestActionMsgColumn = 16843229; // 0x10101dd
field public static final int suggestionsEnabled = 16843633; // 0x1010371
field public static final int suggestionsEnabled = 16843636; // 0x1010374
field public static final int summary = 16843241; // 0x10101e9
field public static final int summaryColumn = 16843426; // 0x10102a2
field public static final int summaryOff = 16843248; // 0x10101f0
field public static final int summaryOn = 16843247; // 0x10101ef
field public static final int supportsUploading = 16843419; // 0x101029b
field public static final int switchPreferenceStyle = 16843629; // 0x101036d
field public static final int switchTextOff = 16843628; // 0x101036c
field public static final int switchTextOn = 16843627; // 0x101036b
field public static final int syncable = 16842777; // 0x1010019
field public static final int tabStripEnabled = 16843453; // 0x10102bd
field public static final int tabStripLeft = 16843451; // 0x10102bb
@@ -957,9 +960,9 @@ package android {
field public static final int textEditPasteWindowLayout = 16843540; // 0x1010314
field public static final int textEditSideNoPasteWindowLayout = 16843615; // 0x101035f
field public static final int textEditSidePasteWindowLayout = 16843614; // 0x101035e
field public static final int textEditSuggestionItemLayout = 16843630; // 0x101036e
field public static final int textEditSuggestionsBottomWindowLayout = 16843628; // 0x101036c
field public static final int textEditSuggestionsTopWindowLayout = 16843629; // 0x101036d
field public static final int textEditSuggestionItemLayout = 16843633; // 0x1010371
field public static final int textEditSuggestionsBottomWindowLayout = 16843631; // 0x101036f
field public static final int textEditSuggestionsTopWindowLayout = 16843632; // 0x1010370
field public static final int textFilterEnabled = 16843007; // 0x10100ff
field public static final int textIsSelectable = 16843542; // 0x1010316
field public static final int textOff = 16843045; // 0x1010125
@@ -971,7 +974,7 @@ package android {
field public static final int textSelectHandleWindowStyle = 16843464; // 0x10102c8
field public static final int textSize = 16842901; // 0x1010095
field public static final int textStyle = 16842903; // 0x1010097
field public static final int textSuggestionsWindowStyle = 16843627; // 0x101036b
field public static final int textSuggestionsWindowStyle = 16843630; // 0x101036e
field public static final int textViewStyle = 16842884; // 0x1010084
field public static final int theme = 16842752; // 0x1010000
field public static final int thickness = 16843360; // 0x1010260
@@ -1007,7 +1010,7 @@ package android {
field public static final int unfocusedMonthDateColor = 16843588; // 0x1010344
field public static final int unselectedAlpha = 16843278; // 0x101020e
field public static final int updatePeriodMillis = 16843344; // 0x1010250
field public static final int useDefaultMargins = 16843638; // 0x1010376
field public static final int useDefaultMargins = 16843641; // 0x1010379
field public static final int useIntrinsicSizeAsMinimum = 16843536; // 0x1010310
field public static final int useLevel = 16843167; // 0x101019f
field public static final int userVisible = 16843409; // 0x1010291
@@ -14433,20 +14436,10 @@ package android.os.storage {
package android.preference {
public class CheckBoxPreference extends android.preference.Preference {
public class CheckBoxPreference extends android.preference.TwoStatePreference {
ctor public CheckBoxPreference(android.content.Context, android.util.AttributeSet, int);
ctor public CheckBoxPreference(android.content.Context, android.util.AttributeSet);
ctor public CheckBoxPreference(android.content.Context);
method public boolean getDisableDependentsState();
method public java.lang.CharSequence getSummaryOff();
method public java.lang.CharSequence getSummaryOn();
method public boolean isChecked();
method public void setChecked(boolean);
method public void setDisableDependentsState(boolean);
method public void setSummaryOff(java.lang.CharSequence);
method public void setSummaryOff(int);
method public void setSummaryOn(java.lang.CharSequence);
method public void setSummaryOn(int);
}
public abstract class DialogPreference extends android.preference.Preference implements android.content.DialogInterface.OnClickListener android.content.DialogInterface.OnDismissListener android.preference.PreferenceManager.OnActivityDestroyListener {
@@ -14774,6 +14767,34 @@ package android.preference {
method public void setShowSilent(boolean);
}
public class SwitchPreference extends android.preference.TwoStatePreference {
ctor public SwitchPreference(android.content.Context, android.util.AttributeSet, int);
ctor public SwitchPreference(android.content.Context, android.util.AttributeSet);
ctor public SwitchPreference(android.content.Context);
method public java.lang.CharSequence getSwitchTextOff();
method public java.lang.CharSequence getSwitchTextOn();
method public void setSwitchTextOff(java.lang.CharSequence);
method public void setSwitchTextOff(int);
method public void setSwitchTextOn(java.lang.CharSequence);
method public void setSwitchTextOn(int);
}
public abstract class TwoStatePreference extends android.preference.Preference {
ctor public TwoStatePreference(android.content.Context, android.util.AttributeSet, int);
ctor public TwoStatePreference(android.content.Context, android.util.AttributeSet);
ctor public TwoStatePreference(android.content.Context);
method public boolean getDisableDependentsState();
method public java.lang.CharSequence getSummaryOff();
method public java.lang.CharSequence getSummaryOn();
method public boolean isChecked();
method public void setChecked(boolean);
method public void setDisableDependentsState(boolean);
method public void setSummaryOff(java.lang.CharSequence);
method public void setSummaryOff(int);
method public void setSummaryOn(java.lang.CharSequence);
method public void setSummaryOn(int);
}
}
package android.provider {

View File

@@ -16,20 +16,11 @@
package android.preference;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.TextView;
/**
* A {@link Preference} that provides checkbox widget
@@ -41,31 +32,18 @@ import android.widget.TextView;
* @attr ref android.R.styleable#CheckBoxPreference_summaryOn
* @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
*/
public class CheckBoxPreference extends Preference {
public class CheckBoxPreference extends TwoStatePreference {
private CharSequence mSummaryOn;
private CharSequence mSummaryOff;
private boolean mChecked;
private boolean mSendAccessibilityEventViewClickedType;
private AccessibilityManager mAccessibilityManager;
private boolean mDisableDependentsState;
public CheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.CheckBoxPreference, defStyle, 0);
mSummaryOn = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn);
mSummaryOff = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff);
mDisableDependentsState = a.getBoolean(
com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false);
setSummaryOn(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn));
setSummaryOff(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff));
setDisableDependentsState(a.getBoolean(
com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false));
a.recycle();
mAccessibilityManager =
(AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
}
public CheckBoxPreference(Context context, AttributeSet attrs) {
@@ -84,246 +62,9 @@ public class CheckBoxPreference extends Preference {
if (checkboxView != null && checkboxView instanceof Checkable) {
((Checkable) checkboxView).setChecked(mChecked);
// send an event to announce the value change of the CheckBox and is done here
// because clicking a preference does not immediately change the checked state
// for example when enabling the WiFi
if (mSendAccessibilityEventViewClickedType &&
mAccessibilityManager.isEnabled() &&
checkboxView.isEnabled()) {
mSendAccessibilityEventViewClickedType = false;
// we send an event on behalf of the check box because in onBind the latter
// is detached from its parent and such views do not send accessibility events
AccessibilityEvent event = AccessibilityEvent.obtain(
AccessibilityEvent.TYPE_VIEW_CLICKED);
event.setClassName(checkboxView.getClass().getName());
event.setPackageName(getContext().getPackageName());
event.setEnabled(checkboxView.isEnabled());
event.setContentDescription(checkboxView.getContentDescription());
event.setChecked(((Checkable) checkboxView).isChecked());
mAccessibilityManager.sendAccessibilityEvent(event);
}
sendAccessibilityEventForView(checkboxView);
}
// Sync the summary view
TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
if (summaryView != null) {
boolean useDefaultSummary = true;
if (mChecked && mSummaryOn != null) {
summaryView.setText(mSummaryOn);
useDefaultSummary = false;
} else if (!mChecked && mSummaryOff != null) {
summaryView.setText(mSummaryOff);
useDefaultSummary = false;
}
if (useDefaultSummary) {
final CharSequence summary = getSummary();
if (summary != null) {
summaryView.setText(summary);
useDefaultSummary = false;
}
}
int newVisibility = View.GONE;
if (!useDefaultSummary) {
// Someone has written to it
newVisibility = View.VISIBLE;
}
if (newVisibility != summaryView.getVisibility()) {
summaryView.setVisibility(newVisibility);
}
}
syncSummaryView(view);
}
@Override
protected void onClick() {
super.onClick();
boolean newValue = !isChecked();
// in onBindView() an AccessibilityEventViewClickedType is sent to announce the change
// not sending
mSendAccessibilityEventViewClickedType = true;
if (!callChangeListener(newValue)) {
return;
}
setChecked(newValue);
}
/**
* Sets the checked state and saves it to the {@link SharedPreferences}.
*
* @param checked The checked state.
*/
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
persistBoolean(checked);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
/**
* Returns the checked state.
*
* @return The checked state.
*/
public boolean isChecked() {
return mChecked;
}
@Override
public boolean shouldDisableDependents() {
boolean shouldDisable = mDisableDependentsState ? mChecked : !mChecked;
return shouldDisable || super.shouldDisableDependents();
}
/**
* Sets the summary to be shown when checked.
*
* @param summary The summary to be shown when checked.
*/
public void setSummaryOn(CharSequence summary) {
mSummaryOn = summary;
if (isChecked()) {
notifyChanged();
}
}
/**
* @see #setSummaryOn(CharSequence)
* @param summaryResId The summary as a resource.
*/
public void setSummaryOn(int summaryResId) {
setSummaryOn(getContext().getString(summaryResId));
}
/**
* Returns the summary to be shown when checked.
* @return The summary.
*/
public CharSequence getSummaryOn() {
return mSummaryOn;
}
/**
* Sets the summary to be shown when unchecked.
*
* @param summary The summary to be shown when unchecked.
*/
public void setSummaryOff(CharSequence summary) {
mSummaryOff = summary;
if (!isChecked()) {
notifyChanged();
}
}
/**
* @see #setSummaryOff(CharSequence)
* @param summaryResId The summary as a resource.
*/
public void setSummaryOff(int summaryResId) {
setSummaryOff(getContext().getString(summaryResId));
}
/**
* Returns the summary to be shown when unchecked.
* @return The summary.
*/
public CharSequence getSummaryOff() {
return mSummaryOff;
}
/**
* Returns whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*
* @return Whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*/
public boolean getDisableDependentsState() {
return mDisableDependentsState;
}
/**
* Sets whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*
* @param disableDependentsState The preference state that should disable dependents.
*/
public void setDisableDependentsState(boolean disableDependentsState) {
mDisableDependentsState = disableDependentsState;
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getBoolean(index, false);
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setChecked(restoreValue ? getPersistedBoolean(mChecked)
: (Boolean) defaultValue);
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
return superState;
}
final SavedState myState = new SavedState(superState);
myState.checked = isChecked();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setChecked(myState.checked);
}
private static class SavedState extends BaseSavedState {
boolean checked;
public SavedState(Parcel source) {
super(source);
checked = source.readInt() == 1;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(checked ? 1 : 0);
}
public SavedState(Parcelable superState) {
super(superState);
}
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.preference;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.Switch;
/**
* A {@link Preference} that provides a two-state toggleable option.
* <p>
* This preference will store a boolean into the SharedPreferences.
*
* @attr ref android.R.styleable#SwitchPreference_summaryOff
* @attr ref android.R.styleable#SwitchPreference_summaryOn
* @attr ref android.R.styleable#SwitchPreference_switchTextOff
* @attr ref android.R.styleable#SwitchPreference_switchTextOn
* @attr ref android.R.styleable#SwitchPreference_disableDependentsState
*/
public class SwitchPreference extends TwoStatePreference {
// Switch text for on and off states
private CharSequence mSwitchOn;
private CharSequence mSwitchOff;
private final Listener mListener = new Listener();
private class Listener implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
@Override
public void onClick(View v) {
SwitchPreference.this.onClick();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SwitchPreference.this.setChecked(isChecked);
}
}
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
* @param defStyle Theme attribute defining the default style options
*/
public SwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.SwitchPreference, defStyle, 0);
setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn));
setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff));
setSwitchTextOn(a.getString(
com.android.internal.R.styleable.SwitchPreference_switchTextOn));
setSwitchTextOff(a.getString(
com.android.internal.R.styleable.SwitchPreference_switchTextOff));
setDisableDependentsState(a.getBoolean(
com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false));
a.recycle();
}
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
*/
public SwitchPreference(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
}
/**
* Construct a new SwitchPreference with default style options.
*
* @param context The Context that will style this preference
*/
public SwitchPreference(Context context) {
this(context, null);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
View checkableView = view.findViewById(com.android.internal.R.id.switchWidget);
if (checkableView != null && checkableView instanceof Checkable) {
((Checkable) checkableView).setChecked(mChecked);
sendAccessibilityEventForView(checkableView);
if (checkableView instanceof Switch) {
final Switch switchView = (Switch) checkableView;
switchView.setTextOn(mSwitchOn);
switchView.setTextOff(mSwitchOff);
switchView.setOnCheckedChangeListener(mListener);
}
if (checkableView.hasFocusable()) {
// This is a focusable list item. Attach a click handler to toggle the button
// for the rest of the item.
view.setOnClickListener(mListener);
}
}
syncSummaryView(view);
}
/**
* Set the text displayed on the switch widget in the on state.
* This should be a very short string; one word if possible.
*
* @param onText Text to display in the on state
*/
public void setSwitchTextOn(CharSequence onText) {
mSwitchOn = onText;
notifyChanged();
}
/**
* Set the text displayed on the switch widget in the off state.
* This should be a very short string; one word if possible.
*
* @param offText Text to display in the off state
*/
public void setSwitchTextOff(CharSequence offText) {
mSwitchOff = offText;
notifyChanged();
}
/**
* Set the text displayed on the switch widget in the on state.
* This should be a very short string; one word if possible.
*
* @param resId The text as a string resource ID
*/
public void setSwitchTextOn(int resId) {
setSwitchTextOn(getContext().getString(resId));
}
/**
* Set the text displayed on the switch widget in the off state.
* This should be a very short string; one word if possible.
*
* @param resId The text as a string resource ID
*/
public void setSwitchTextOff(int resId) {
setSwitchTextOff(getContext().getString(resId));
}
/**
* @return The text that will be displayed on the switch widget in the on state
*/
public CharSequence getSwitchTextOn() {
return mSwitchOn;
}
/**
* @return The text that will be displayed on the switch widget in the off state
*/
public CharSequence getSwitchTextOff() {
return mSwitchOff;
}
}

View File

@@ -0,0 +1,309 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.preference;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.TextView;
/**
* Common base class for preferences that have two selectable states, persist a
* boolean value in SharedPreferences, and may have dependent preferences that are
* enabled/disabled based on the current state.
*/
public abstract class TwoStatePreference extends Preference {
private CharSequence mSummaryOn;
private CharSequence mSummaryOff;
boolean mChecked;
private boolean mSendAccessibilityEventViewClickedType;
private AccessibilityManager mAccessibilityManager;
private boolean mDisableDependentsState;
public TwoStatePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mAccessibilityManager =
(AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
}
public TwoStatePreference(Context context, AttributeSet attrs) {
super(context, attrs);
mAccessibilityManager =
(AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
}
public TwoStatePreference(Context context) {
super(context);
mAccessibilityManager =
(AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
}
@Override
protected void onClick() {
super.onClick();
boolean newValue = !isChecked();
// in onBindView() an AccessibilityEventViewClickedType is sent to announce the change
// not sending
mSendAccessibilityEventViewClickedType = true;
if (!callChangeListener(newValue)) {
return;
}
setChecked(newValue);
}
/**
* Sets the checked state and saves it to the {@link SharedPreferences}.
*
* @param checked The checked state.
*/
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
persistBoolean(checked);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
/**
* Returns the checked state.
*
* @return The checked state.
*/
public boolean isChecked() {
return mChecked;
}
@Override
public boolean shouldDisableDependents() {
boolean shouldDisable = mDisableDependentsState ? mChecked : !mChecked;
return shouldDisable || super.shouldDisableDependents();
}
/**
* Sets the summary to be shown when checked.
*
* @param summary The summary to be shown when checked.
*/
public void setSummaryOn(CharSequence summary) {
mSummaryOn = summary;
if (isChecked()) {
notifyChanged();
}
}
/**
* @see #setSummaryOn(CharSequence)
* @param summaryResId The summary as a resource.
*/
public void setSummaryOn(int summaryResId) {
setSummaryOn(getContext().getString(summaryResId));
}
/**
* Returns the summary to be shown when checked.
* @return The summary.
*/
public CharSequence getSummaryOn() {
return mSummaryOn;
}
/**
* Sets the summary to be shown when unchecked.
*
* @param summary The summary to be shown when unchecked.
*/
public void setSummaryOff(CharSequence summary) {
mSummaryOff = summary;
if (!isChecked()) {
notifyChanged();
}
}
/**
* @see #setSummaryOff(CharSequence)
* @param summaryResId The summary as a resource.
*/
public void setSummaryOff(int summaryResId) {
setSummaryOff(getContext().getString(summaryResId));
}
/**
* Returns the summary to be shown when unchecked.
* @return The summary.
*/
public CharSequence getSummaryOff() {
return mSummaryOff;
}
/**
* Returns whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*
* @return Whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*/
public boolean getDisableDependentsState() {
return mDisableDependentsState;
}
/**
* Sets whether dependents are disabled when this preference is on ({@code true})
* or when this preference is off ({@code false}).
*
* @param disableDependentsState The preference state that should disable dependents.
*/
public void setDisableDependentsState(boolean disableDependentsState) {
mDisableDependentsState = disableDependentsState;
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getBoolean(index, false);
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setChecked(restoreValue ? getPersistedBoolean(mChecked)
: (Boolean) defaultValue);
}
/**
* Send an accessibility event for the given view if appropriate
* @param view View that should send the event
*/
void sendAccessibilityEventForView(View view) {
// send an event to announce the value change of the state. It is done here
// because clicking a preference does not immediately change the checked state
// for example when enabling the WiFi
if (mSendAccessibilityEventViewClickedType &&
mAccessibilityManager.isEnabled() &&
view.isEnabled()) {
mSendAccessibilityEventViewClickedType = false;
int eventType = AccessibilityEvent.TYPE_VIEW_CLICKED;
view.sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
}
}
/**
* Sync a summary view contained within view's subhierarchy with the correct summary text.
* @param view View where a summary should be located
*/
void syncSummaryView(View view) {
// Sync the summary view
TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
if (summaryView != null) {
boolean useDefaultSummary = true;
if (mChecked && mSummaryOn != null) {
summaryView.setText(mSummaryOn);
useDefaultSummary = false;
} else if (!mChecked && mSummaryOff != null) {
summaryView.setText(mSummaryOff);
useDefaultSummary = false;
}
if (useDefaultSummary) {
final CharSequence summary = getSummary();
if (summary != null) {
summaryView.setText(summary);
useDefaultSummary = false;
}
}
int newVisibility = View.GONE;
if (!useDefaultSummary) {
// Someone has written to it
newVisibility = View.VISIBLE;
}
if (newVisibility != summaryView.getVisibility()) {
summaryView.setVisibility(newVisibility);
}
}
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
return superState;
}
final SavedState myState = new SavedState(superState);
myState.checked = isChecked();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setChecked(myState.checked);
}
static class SavedState extends BaseSavedState {
boolean checked;
public SavedState(Parcel source) {
super(source);
checked = source.readInt() == 1;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(checked ? 1 : 0);
}
public SavedState(Parcelable superState) {
super(superState);
}
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}

View File

@@ -16,8 +16,6 @@
package android.widget;
import com.android.internal.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
@@ -37,6 +35,8 @@ import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import com.android.internal.R;
/**
* A Switch is a two-state toggle switch widget that can select between two
* options. The user may drag the "thumb" back and forth to choose the selected option,
@@ -84,6 +84,7 @@ public class Switch extends CompoundButton {
private Layout mOnLayout;
private Layout mOffLayout;
@SuppressWarnings("hiding")
private final Rect mTempRect = new Rect();
private static final int[] CHECKED_STATE_SET = {

View File

@@ -22,7 +22,8 @@
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
android:paddingRight="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground" >
<ImageView
android:id="@+android:id/icon"

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Layout used by SwitchPreference for the switch widget style. This is inflated
inside android.R.layout.preference. -->
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dip"
android:focusable="false"
android:clickable="false" />

View File

@@ -374,11 +374,11 @@
pointer will go until that pointers go up thereby enabling touches
with multiple pointers to be split across multiple windows. -->
<attr name="windowEnableSplitTouch" format="boolean" />
<!-- Control whether a container should automatically close itself if
the user touches outside of it. This only applies to activities
and dialogs.
<p>Note: this attribute will only be respected for applications
that are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
or later. -->
@@ -548,7 +548,7 @@
<!-- NumberPicker input text style. -->
<attr name="numberPickerInputTextStyle" format="reference" />
<!-- The CalndarView style. -->
<!-- The CalendarView style. -->
<attr name="calendarViewStyle" format="reference" />
<!-- The TimePicker style. -->
@@ -658,6 +658,8 @@
<attr name="preferenceLayoutChild" format="reference" />
<!-- Preference panel style -->
<attr name="preferencePanelStyle" format="reference" />
<!-- Default style for switch preferences. -->
<attr name="switchPreferenceStyle" format="reference" />
<!-- ============================ -->
<!-- Text selection handle styles -->
@@ -1914,7 +1916,7 @@
<!-- Place the scroll bar on the right. -->
<enum name="right" value="2" />
</attr>
<!-- Specifies the type of layer backing this view. The default value is none.
Refer to {@link android.view.View#setLayerType(int, android.graphics.Paint)}
for more information.-->
@@ -4814,7 +4816,7 @@
<!-- The view id of the AppWidget subview which should be auto-advanced.
by the widget's host. -->
<attr name="autoAdvanceViewId" format="reference" />
<!-- Optional parameter which indicates if and how this widget can be
<!-- Optional parameter which indicates if and how this widget can be
resized. -->
<attr name="resizeMode" format="integer">
<flag name="none" value="0x0" />
@@ -5160,4 +5162,24 @@
<attr name="allowMassStorage" format="boolean" />
</declare-styleable>
<declare-styleable name="SwitchPreference">
<!-- The summary for the Preference in a PreferenceActivity screen when the
SwitchPreference is checked. If separate on/off summaries are not
needed, the summary attribute can be used instead. -->
<attr name="summaryOn" />
<!-- The summary for the Preference in a PreferenceActivity screen when the
SwitchPreference is unchecked. If separate on/off summaries are not
needed, the summary attribute can be used instead. -->
<attr name="summaryOff" />
<!-- The text used on the switch itself when in the "on" state.
This should be a very SHORT string, as it appears in a small space. -->
<attr name="switchTextOn" format="string" />
<!-- The text used on the switch itself when in the "off" state.
This should be a very SHORT string, as it appears in a small space. -->
<attr name="switchTextOff" format="string" />
<!-- The state (true for on, or false for off) that causes dependents to be disabled. By default,
dependents will be disabled when this is unchecked, so the value of this preference is false. -->
<attr name="disableDependentsState" />
</declare-styleable>
</resources>

View File

@@ -1282,6 +1282,7 @@
Resources added in version 11 of the platform (Honeycomb / 3.0).
=============================================================== -->
<eat-comment />
<public type="attr" name="allContactsName" id="0x010102cc" />
<public type="attr" name="windowActionBar" id="0x010102cd" />
<public type="attr" name="actionBarStyle" id="0x010102ce" />
@@ -1442,6 +1443,7 @@
<public type="interpolator" name="accelerate_quad" id="0x010c0000" />
<!-- Acceleration curve matching a quadtratic ease in function. -->
<public type="interpolator" name="decelerate_quad" id="0x010c0001" />
<!-- Acceleration curve matching a cubic ease out function. -->
<public type="interpolator" name="accelerate_cubic" id="0x010c0002" />
<!-- Acceleration curve matching a cubic ease in function. -->
@@ -1661,7 +1663,7 @@
<public type="style" name="Theme.Holo.Light.NoActionBar.Fullscreen" />
<!-- ===============================================================
Resources added in version 13 of the platform (Ice Cream Sandwich)
Resources added in version 14 of the platform (Ice Cream Sandwich)
=============================================================== -->
<eat-comment />
<public type="attr" name="state_hovered" />
@@ -1669,6 +1671,10 @@
<public type="attr" name="state_drag_hovered" />
<public type="attr" name="stopWithTask" />
<public type="attr" name="switchTextOn" />
<public type="attr" name="switchTextOff" />
<public type="attr" name="switchPreferenceStyle" />
<public type="style" name="TextAppearance.SuggestionHighlight" />
<public type="style" name="Theme.Holo.SplitActionBarWhenNarrow" />
<public type="style" name="Theme.Holo.Light.SplitActionBarWhenNarrow" />

View File

@@ -903,6 +903,12 @@
<item name="android:widgetLayout">@android:layout/preference_widget_checkbox</item>
</style>
<style name="Preference.SwitchPreference">
<item name="android:widgetLayout">@android:layout/preference_widget_switch</item>
<item name="android:switchTextOn">@android:string/capital_on</item>
<item name="android:switchTextOff">@android:string/capital_off</item>
</style>
<style name="Preference.PreferenceScreen">
</style>
@@ -947,6 +953,12 @@
<item name="android:widgetLayout">@android:layout/preference_widget_checkbox</item>
</style>
<style name="Preference.Holo.SwitchPreference">
<item name="android:widgetLayout">@android:layout/preference_widget_switch</item>
<item name="android:switchTextOn">@android:string/capital_on</item>
<item name="android:switchTextOff">@android:string/capital_off</item>
</style>
<style name="Preference.Holo.PreferenceScreen">
</style>

View File

@@ -247,6 +247,7 @@
<item name="preferenceStyle">@android:style/Preference</item>
<item name="preferenceInformationStyle">@android:style/Preference.Information</item>
<item name="checkBoxPreferenceStyle">@android:style/Preference.CheckBoxPreference</item>
<item name="switchPreferenceStyle">@android:style/Preference.SwitchPreference</item>
<item name="yesNoPreferenceStyle">@android:style/Preference.DialogPreference.YesNoPreference</item>
<item name="dialogPreferenceStyle">@android:style/Preference.DialogPreference</item>
<item name="editTextPreferenceStyle">@android:style/Preference.DialogPreference.EditTextPreference</item>
@@ -687,7 +688,7 @@
<item name="android:imeExtractEnterAnimation">@android:anim/input_method_extract_enter</item>
<item name="android:imeExtractExitAnimation">@android:anim/input_method_extract_exit</item>
</style>
<!-- Theme for the search input bar. -->
<style name="Theme.SearchBar" parent="Theme.Holo.Light.Panel">
<item name="windowContentOverlay">@null</item>
@@ -1003,6 +1004,7 @@
<item name="preferenceStyle">@android:style/Preference.Holo</item>
<item name="preferenceInformationStyle">@android:style/Preference.Holo.Information</item>
<item name="checkBoxPreferenceStyle">@android:style/Preference.Holo.CheckBoxPreference</item>
<item name="switchPreferenceStyle">@android:style/Preference.Holo.SwitchPreference</item>
<item name="yesNoPreferenceStyle">@android:style/Preference.Holo.DialogPreference.YesNoPreference</item>
<item name="dialogPreferenceStyle">@android:style/Preference.Holo.DialogPreference</item>
<item name="editTextPreferenceStyle">@android:style/Preference.Holo.DialogPreference.EditTextPreference</item>
@@ -1291,6 +1293,7 @@
<item name="preferenceStyle">@android:style/Preference.Holo</item>
<item name="preferenceInformationStyle">@android:style/Preference.Holo.Information</item>
<item name="checkBoxPreferenceStyle">@android:style/Preference.Holo.CheckBoxPreference</item>
<item name="switchPreferenceStyle">@android:style/Preference.Holo.SwitchPreference</item>
<item name="yesNoPreferenceStyle">@android:style/Preference.Holo.DialogPreference.YesNoPreference</item>
<item name="dialogPreferenceStyle">@android:style/Preference.Holo.DialogPreference</item>
<item name="editTextPreferenceStyle">@android:style/Preference.Holo.DialogPreference.EditTextPreference</item>