Update master switch style

- When off, background grey
- When on, background accent
- Switch toggle always white, only change the slider color during on/off

Change-Id: I0ca49439f4e20e75517d503f28cc3068930591e8
Bug: 69462439
Test: robotests
This commit is contained in:
Fan Zhang
2018-01-10 12:22:36 -08:00
parent 256ebf4245
commit 46b5474be4
16 changed files with 210 additions and 182 deletions

View File

@@ -22,6 +22,8 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.StringRes;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
@@ -42,6 +44,7 @@ import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.RestrictedLockUtils;
import java.util.ArrayList;
import java.util.List;
public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedChangeListener,
View.OnClickListener {
@@ -56,7 +59,14 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
void onSwitchChanged(Switch switchView, boolean isChecked);
}
private MetricsFeatureProvider mMetricsFeatureProvider;
private static final int[] XML_ATTRIBUTES = {
R.attr.switchBarMarginStart,
R.attr.switchBarMarginEnd,
R.attr.switchBarBackgroundColor,
R.attr.switchBarBackgroundActivatedColor};
private final List<OnSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final TextAppearanceSpan mSummarySpan;
private ToggleSwitch mSwitch;
@@ -64,20 +74,20 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
private TextView mTextView;
private String mLabel;
private String mSummary;
@ColorInt
private int mBackgroundColor;
@ColorInt
private int mBackgroundActivatedColor;
@StringRes
private int mOnTextId;
@StringRes
private int mOffTextId;
private boolean mLoggingIntialized;
private boolean mDisabledByAdmin;
private EnforcedAdmin mEnforcedAdmin = null;
private String mMetricsTag;
private final ArrayList<OnSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
private static int[] XML_ATTRIBUTES = {
R.attr.switchBarMarginStart, R.attr.switchBarMarginEnd,
R.attr.switchBarBackgroundColor};
public SwitchBar(Context context) {
this(context, null);
@@ -99,32 +109,30 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
final TypedArray a = context.obtainStyledAttributes(attrs, XML_ATTRIBUTES);
int switchBarMarginStart = (int) a.getDimension(0, 0);
int switchBarMarginEnd = (int) a.getDimension(1, 0);
int switchBarBackgroundColor = (int) a.getColor(2, 0);
mBackgroundColor = a.getColor(2, 0);
mBackgroundActivatedColor = a.getColor(3, 0);
a.recycle();
mTextView = (TextView) findViewById(R.id.switch_text);
mTextView = findViewById(R.id.switch_text);
mTextView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
mSummarySpan = new TextAppearanceSpan(mContext, R.style.TextAppearance_Small_SwitchBar);
ViewGroup.MarginLayoutParams lp = (MarginLayoutParams) mTextView.getLayoutParams();
lp.setMarginStart(switchBarMarginStart);
mSwitch = (ToggleSwitch) findViewById(R.id.switch_widget);
mSwitch = findViewById(R.id.switch_widget);
// Prevent onSaveInstanceState() to be called as we are managing the state of the Switch
// on our own
mSwitch.setSaveEnabled(false);
mSwitch.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
lp = (MarginLayoutParams) mSwitch.getLayoutParams();
lp.setMarginEnd(switchBarMarginEnd);
setBackgroundColor(switchBarBackgroundColor);
setBackgroundColor(mBackgroundColor);
setSwitchBarText(R.string.switch_on_text, R.string.switch_off_text);
addOnSwitchChangeListener(new OnSwitchChangeListener() {
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
setTextViewLabel(isChecked);
}
});
addOnSwitchChangeListener(
(switchView, isChecked) -> setTextViewLabelAndBackground(isChecked));
mRestrictedIcon = findViewById(R.id.restricted_icon);
@@ -140,15 +148,16 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mMetricsTag = tag;
}
public void setTextViewLabel(boolean isChecked) {
public void setTextViewLabelAndBackground(boolean isChecked) {
mLabel = getResources().getString(isChecked ? mOnTextId : mOffTextId);
setBackgroundColor(isChecked ? mBackgroundActivatedColor : mBackgroundColor);
updateText();
}
public void setSwitchBarText(int onText, int offText) {
mOnTextId = onText;
mOffTextId = offText;
setTextViewLabel(isChecked());
setTextViewLabelAndBackground(isChecked());
}
public void setSummary(String summary) {
@@ -169,12 +178,12 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
}
public void setChecked(boolean checked) {
setTextViewLabel(checked);
setTextViewLabelAndBackground(checked);
mSwitch.setChecked(checked);
}
public void setCheckedInternal(boolean checked) {
setTextViewLabel(checked);
setTextViewLabelAndBackground(checked);
mSwitch.setCheckedInternal(checked);
}
@@ -290,8 +299,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
*/
private SavedState(Parcel in) {
super(in);
checked = (Boolean)in.readValue(null);
visible = (Boolean)in.readValue(null);
checked = (Boolean) in.readValue(null);
visible = (Boolean) in.readValue(null);
}
@Override
@@ -338,7 +347,7 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
super.onRestoreInstanceState(ss.getSuperState());
mSwitch.setCheckedInternal(ss.checked);
setTextViewLabel(ss.checked);
setTextViewLabelAndBackground(ss.checked);
setVisibility(ss.visible ? View.VISIBLE : View.GONE);
mSwitch.setOnCheckedChangeListener(ss.visible ? this : null);