Merge "Accessibility shortcut secondary action - Settings view status link with checkbox"

This commit is contained in:
Jason Hsu
2020-01-14 04:30:10 +00:00
committed by Android (Google) Code Review
2 changed files with 46 additions and 10 deletions

View File

@@ -53,7 +53,10 @@ public class ShortcutPreference extends Preference {
} }
private OnClickListener mListener = null; private OnClickListener mListener = null;
private static final float DISABLED_ALPHA = 0.5f;
private static final float ENABLED_ALPHA = 1.0f;
private int mSettingsVisibility = View.VISIBLE; private int mSettingsVisibility = View.VISIBLE;
private boolean mAutoEnabledSettings;
private boolean mChecked = false; private boolean mChecked = false;
ShortcutPreference(Context context, AttributeSet attrs) { ShortcutPreference(Context context, AttributeSet attrs) {
@@ -65,24 +68,41 @@ public class ShortcutPreference extends Preference {
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
final CheckBox checkBox = holder.itemView.findViewById(R.id.checkbox);
final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame); final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame);
if (mainFrame != null && checkBox != null) { if (mainFrame != null) {
mainFrame.setOnClickListener(view -> callOnCheckboxClicked()); mainFrame.setOnClickListener(view -> callOnCheckboxClicked());
}
final CheckBox checkBox = holder.itemView.findViewById(R.id.checkbox);
if (checkBox != null) {
checkBox.setChecked(mChecked); checkBox.setChecked(mChecked);
} }
final View settings = holder.itemView.findViewById(android.R.id.widget_frame); final View settings = holder.itemView.findViewById(android.R.id.widget_frame);
final View divider = holder.itemView.findViewById(R.id.divider); if (settings != null) {
if (settings != null && divider != null) {
settings.setOnClickListener(view -> callOnSettingsClicked()); settings.setOnClickListener(view -> callOnSettingsClicked());
settings.setEnabled(mAutoEnabledSettings ? mChecked : /* enabled */ true);
float alpha;
if (mAutoEnabledSettings) {
alpha = mChecked ? ENABLED_ALPHA : DISABLED_ALPHA;
} else {
alpha = ENABLED_ALPHA;
}
settings.setAlpha(alpha);
settings.setVisibility(mSettingsVisibility); settings.setVisibility(mSettingsVisibility);
}
final View divider = holder.itemView.findViewById(R.id.divider);
if (divider != null) {
divider.setVisibility(mSettingsVisibility); divider.setVisibility(mSettingsVisibility);
} }
} }
/** /**
* Set the shortcut checkbox according to checked value. * Sets the shortcut checkbox according to checked value.
* *
* @param checked the state value of shortcut checkbox * @param checked the state value of shortcut checkbox
*/ */
@@ -94,7 +114,7 @@ public class ShortcutPreference extends Preference {
} }
/** /**
* Get the checked value of shortcut checkbox. * Gets the checked value of shortcut checkbox.
* *
* @return the checked value of shortcut checkbox * @return the checked value of shortcut checkbox
*/ */
@@ -102,7 +122,22 @@ public class ShortcutPreference extends Preference {
return mChecked; return mChecked;
} }
/**
* Automatically/Manually enable settings according to checkbox click status.
*
* Automatically enable settings means settings view enabled when checkbox is clicked, and
* disabled when checkbox is not clicked.
* Manually enable settings means settings view always enabled.
*
* @param autoEnabled True will automatically enable settings, false will let settings view
* always enabled.
*/
public void setAutoEnabledSettings(boolean autoEnabled) {
if (mAutoEnabledSettings != autoEnabled) {
mAutoEnabledSettings = autoEnabled;
notifyChanged();
}
}
/** /**
* Sets the visibility state of Settings view. * Sets the visibility state of Settings view.
@@ -129,6 +164,7 @@ public class ShortcutPreference extends Preference {
setLayoutResource(R.layout.accessibility_shortcut_secondary_action); setLayoutResource(R.layout.accessibility_shortcut_secondary_action);
setWidgetLayoutResource(R.layout.preference_widget_settings); setWidgetLayoutResource(R.layout.preference_widget_settings);
setIconSpaceReserved(false); setIconSpaceReserved(false);
mAutoEnabledSettings = true;
} }
private void callOnSettingsClicked() { private void callOnSettingsClicked() {

View File

@@ -69,7 +69,7 @@ public class ShortcutPreferenceTest {
} }
@Test @Test
public void testClickLinearLayout_checkboxClicked() { public void clickLinearLayout_checkboxClicked() {
mShortcutPreference.onBindViewHolder(mPreferenceViewHolder); mShortcutPreference.onBindViewHolder(mPreferenceViewHolder);
mShortcutPreference.setOnClickListener(mListener); mShortcutPreference.setOnClickListener(mListener);
@@ -81,7 +81,7 @@ public class ShortcutPreferenceTest {
} }
@Test @Test
public void testClickSettings_settingsClicked() { public void clickSettings_settingsClicked() {
mShortcutPreference.onBindViewHolder(mPreferenceViewHolder); mShortcutPreference.onBindViewHolder(mPreferenceViewHolder);
mShortcutPreference.setOnClickListener(mListener); mShortcutPreference.setOnClickListener(mListener);
@@ -92,7 +92,7 @@ public class ShortcutPreferenceTest {
} }
@Test @Test
public void testSetCheckedTrue_getCheckedIsTrue() { public void setCheckedTrue_getCheckedIsTrue() {
mShortcutPreference.setChecked(true); mShortcutPreference.setChecked(true);
assertThat(mShortcutPreference.getChecked()).isEqualTo(true); assertThat(mShortcutPreference.getChecked()).isEqualTo(true);