Merge "Can disable checkbox without tint on text" into pi-dev am: 48831c837f

am: ee3164bd42

Change-Id: I63a71e144fd3b93c2c9b285cd637ba656092fa79
This commit is contained in:
Beverly Tai
2018-04-19 10:30:43 -07:00
committed by android-build-merger
6 changed files with 84 additions and 27 deletions

View File

@@ -24,6 +24,7 @@ import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settings.widget.DisabledCheckBoxPreference;
public class ZenModeVisEffectPreferenceController
extends AbstractZenModePreferenceController
@@ -78,9 +79,9 @@ public class ZenModeVisEffectPreferenceController
if (parentSuppressed) {
((CheckBoxPreference) preference).setChecked(parentSuppressed);
onPreferenceChange(preference, parentSuppressed);
preference.setEnabled(false);
((DisabledCheckBoxPreference) preference).enableCheckbox(false);
} else {
preference.setEnabled(true);
((DisabledCheckBoxPreference) preference).enableCheckbox(true);
((CheckBoxPreference) preference).setChecked(suppressed);
}
}

View File

@@ -17,46 +17,85 @@
package com.android.settings.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.view.View;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.PreferenceViewHolder;
/**
* A CheckboxPreference with a disabled checkbox. Differs from CheckboxPreference.setDisabled()
* in that the text is not dimmed.
* A CheckboxPreference that can disable its checkbox separate from its text.
* Differs from CheckboxPreference.setDisabled() in that the text is not dimmed.
*/
public class DisabledCheckBoxPreference extends CheckBoxPreference {
private PreferenceViewHolder mViewHolder;
private View mCheckBox;
private boolean mEnabledCheckBox;
public DisabledCheckBoxPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, defStyleRes);
}
public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, 0);
}
public DisabledCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setupDisabledCheckBoxPreference(context, attrs, 0, 0);
}
public DisabledCheckBoxPreference(Context context) {
super(context);
setupDisabledCheckBoxPreference(context, null, 0, 0);
}
private void setupDisabledCheckBoxPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
for (int i = a.getIndexCount() - 1; i >= 0; i--) {
int attr = a.getIndex(i);
switch (attr) {
case com.android.internal.R.styleable.Preference_enabled:
mEnabledCheckBox = a.getBoolean(attr, true);
break;
}
}
a.recycle();
// Always tell super class this preference is enabled.
// We manually enable/disable checkbox using enableCheckBox.
super.setEnabled(true);
enableCheckbox(mEnabledCheckBox);
}
public void enableCheckbox(boolean enabled) {
mEnabledCheckBox = enabled;
if (mViewHolder != null && mCheckBox != null) {
mCheckBox.setEnabled(mEnabledCheckBox);
mViewHolder.itemView.setEnabled(mEnabledCheckBox);
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
mViewHolder = holder;
mCheckBox = holder.findViewById(android.R.id.checkbox);
View view = holder.findViewById(android.R.id.checkbox);
view.setEnabled(false);
holder.itemView.setEnabled(false);
enableCheckbox(mEnabledCheckBox);
}
@Override
protected void performClick(View view) {
// Do nothing
// only perform clicks if the checkbox is enabled
if (mEnabledCheckBox) {
super.performClick(view);
}
}
}