Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90fe8141 authored by Daniel Norman's avatar Daniel Norman
Browse files

feat(expressive): Add more customization options in SliderPreference

These are all to match functionality previously available in
SeekBarPreference to unblock migration to SliderPreference:

- Allows callers to set custom touch and change listeners.
- Allows callers to set icon drawable & content description IDs at
  runtime, needed for non-XML (Catalyst) users of this preference.
- Allows callers to set a state description. This is necessary for
  sliders to share custom state with screen readers.
- Allows callers to access the underlying Slider widget.

Bug: 375168747
Test: used in com.android.settings.accessibility tests
Flag: EXEMPT refactor to unblock migration. This change is a no-op
      for callers that do not use the new methods.
Change-Id: I0ba2d5728983577d334545d9f3819ccffceb0f4c
parent ce2deed9
Loading
Loading
Loading
Loading
+98 −4
Original line number Original line Diff line number Diff line
@@ -54,10 +54,6 @@ public class SliderPreference extends Preference {


    private final int mTextStartId;
    private final int mTextStartId;
    private final int mTextEndId;
    private final int mTextEndId;
    private final int mIconStartId;
    private final int mIconEndId;
    private final int mIconStartContentDescriptionId;
    private final int mIconEndContentDescriptionId;
    private final ColorStateList mTrackActiveColor;
    private final ColorStateList mTrackActiveColor;
    private final ColorStateList mTrackInactiveColor;
    private final ColorStateList mTrackInactiveColor;
    private final ColorStateList mThumbColor;
    private final ColorStateList mThumbColor;
@@ -76,11 +72,16 @@ public class SliderPreference extends Preference {
    private int mMin;
    private int mMin;
    private int mMax;
    private int mMax;
    private int mSliderIncrement;
    private int mSliderIncrement;
    private int mIconStartId;
    private int mIconEndId;
    private int mIconStartContentDescriptionId;
    private int mIconEndContentDescriptionId;
    private int mHapticFeedbackMode = HAPTIC_FEEDBACK_MODE_NONE;
    private int mHapticFeedbackMode = HAPTIC_FEEDBACK_MODE_NONE;
    private boolean mTickVisible = false;
    private boolean mTickVisible = false;
    private boolean mAdjustable;
    private boolean mAdjustable;
    private boolean mTrackingTouch;
    private boolean mTrackingTouch;
    private CharSequence mSliderContentDescription;
    private CharSequence mSliderContentDescription;
    private CharSequence mSliderStateDescription;


    /**
    /**
     * Listener reacting to the user pressing DPAD left/right keys if {@code
     * Listener reacting to the user pressing DPAD left/right keys if {@code
@@ -123,13 +124,23 @@ public class SliderPreference extends Preference {
            if ((int) slider.getValue() != mSliderValue) {
            if ((int) slider.getValue() != mSliderValue) {
                syncValueInternal(slider);
                syncValueInternal(slider);
            }
            }
            if (mExtraTouchListener != null) {
                mExtraTouchListener.onStopTrackingTouch(slider);
            }
        }
        }


        @Override
        @Override
        public void onStartTrackingTouch(@NonNull Slider slider) {
        public void onStartTrackingTouch(@NonNull Slider slider) {
            mTrackingTouch = true;
            mTrackingTouch = true;
            if (mExtraTouchListener != null) {
                mExtraTouchListener.onStartTrackingTouch(slider);
            }
        }
        }
    };
    };

    @Nullable
    private Slider.OnSliderTouchListener mExtraTouchListener;

    private LabelFormatter mLabelFormater;
    private LabelFormatter mLabelFormater;
    // Whether the SliderPreference should continuously save the Slider value while it is being
    // Whether the SliderPreference should continuously save the Slider value while it is being
    // dragged.
    // dragged.
@@ -143,8 +154,15 @@ public class SliderPreference extends Preference {
            if (fromUser && (mUpdatesContinuously || !mTrackingTouch)) {
            if (fromUser && (mUpdatesContinuously || !mTrackingTouch)) {
                syncValueInternal(slider);
                syncValueInternal(slider);
            }
            }
            if (mExtraChangeListener != null) {
                mExtraChangeListener.onValueChange(slider, value, fromUser);
            }
        }
        }
    };
    };

    @Nullable
    private Slider.OnChangeListener mExtraChangeListener;

    // Whether to show the Slider value TextView next to the bar
    // Whether to show the Slider value TextView next to the bar
    private boolean mShowSliderValue;
    private boolean mShowSliderValue;


@@ -245,12 +263,61 @@ public class SliderPreference extends Preference {
        this(context, null);
        this(context, null);
    }
    }



    /**
     * Provide an extra {@link Slider.OnSliderTouchListener} that is called in addition to the
     * standard listener.
     */
    public void setExtraTouchListener(@Nullable Slider.OnSliderTouchListener listener) {
        mExtraTouchListener = listener;
    }

    /**
     * Provide an extra {@link Slider.OnChangeListener} that is called in addition to the
     * standard listener.
     */
    public void setExtraChangeListener(@Nullable Slider.OnChangeListener listener) {
        mExtraChangeListener = listener;
    }

    private static void setIconViewAndFrameEnabled(View iconView, ViewGroup iconFrame,
    private static void setIconViewAndFrameEnabled(View iconView, ViewGroup iconFrame,
            boolean enabled) {
            boolean enabled) {
        iconView.setEnabled(enabled);
        iconView.setEnabled(enabled);
        iconFrame.setEnabled(enabled);
        iconFrame.setEnabled(enabled);
    }
    }


    /** Set the start icon of the Slider. */
    public void setIconStart(int iconStartId) {
        if (mIconStartId != iconStartId) {
            mIconStartId = iconStartId;
            notifyChanged();
        }
    }

    /** Set the description resource id of the start icon. */
    public void setIconStartContentDescription(int iconStartContentDescriptionId) {
        if (mIconStartContentDescriptionId != iconStartContentDescriptionId) {
            mIconStartContentDescriptionId = iconStartContentDescriptionId;
            notifyChanged();
        }
    }

    /** Set the end icon of the Slider. */
    public void setIconEnd(int iconEndId) {
        if (mIconEndId != iconEndId) {
            mIconEndId = iconEndId;
            notifyChanged();
        }
    }

    /** Set the description resource id of the end icon. */
    public void setIconEndContentDescription(int iconEndContentDescriptionId) {
        if (mIconEndContentDescriptionId != iconEndContentDescriptionId) {
            mIconEndContentDescriptionId = iconEndContentDescriptionId;
            notifyChanged();
        }
    }

    @Override
    @Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        super.onBindViewHolder(holder);
@@ -284,6 +351,11 @@ public class SliderPreference extends Preference {
        } else {
        } else {
            mSlider.setContentDescription(null);
            mSlider.setContentDescription(null);
        }
        }
        if (!TextUtils.isEmpty(mSliderStateDescription)) {
            mSlider.setStateDescription(mSliderStateDescription);
        } else {
            mSlider.setStateDescription(null);
        }
        mSlider.setValueFrom(mMin);
        mSlider.setValueFrom(mMin);
        mSlider.setValueTo(mMax);
        mSlider.setValueTo(mMax);
        mSlider.setValue(mSliderValue);
        mSlider.setValue(mSliderValue);
@@ -340,6 +412,16 @@ public class SliderPreference extends Preference {
        updateIconEndIfNeeded(iconEndView);
        updateIconEndIfNeeded(iconEndView);
    }
    }


    /**
     * Gets the {@link Slider} widget owned by this preference.
     *
     * @return the slider widget
     */
    @Nullable
    public Slider getSlider() {
        return mSlider;
    }

    /**
    /**
     * Gets the lower bound set on the {@link Slider}.
     * Gets the lower bound set on the {@link Slider}.
     *
     *
@@ -527,6 +609,18 @@ public class SliderPreference extends Preference {
        }
        }
    }
    }


    /**
     * Sets the state description of the {@link Slider}.
     *
     * @param stateDescription The state description of the {@link Slider}
     */
    public void setSliderStateDescription(@Nullable CharSequence stateDescription) {
        mSliderStateDescription = stateDescription;
        if (mSlider != null) {
            mSlider.setStateDescription(stateDescription);
        }
    }

    @Override
    @Override
    protected void onSetInitialValue(@Nullable Object defaultValue) {
    protected void onSetInitialValue(@Nullable Object defaultValue) {
        if (defaultValue == null) {
        if (defaultValue == null) {