New feature “Text and reading options” for SetupWizard, Wallpaper, and Settings (8/n).
- Extending the LabeledSeekBarPreference 1) Add new attributes, "iconStart", "iconEnd", "iconStartContentDescription", "iconEndContentDescription" 2) Add new interface setOnSeekBarChangeListener - It will be integrated with display/font size items in next patches. Bug: 211503117 Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER=LabeledSeekBarPreferenceTest Change-Id: Id8fe4fb68062c0e92ca4c291d2f7c47303e8691e
This commit is contained in:
@@ -21,6 +21,8 @@ import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -28,17 +30,40 @@ import androidx.annotation.Nullable;
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.internal.util.Preconditions;
|
||||
import com.android.settings.R;
|
||||
|
||||
/** A slider preference with left and right labels **/
|
||||
/**
|
||||
* A labeled {@link SeekBarPreference} with left and right text label, icon label, or both.
|
||||
*
|
||||
* <p>
|
||||
* The component provides the attribute usage below.
|
||||
* <attr name="textStart" format="reference" />
|
||||
* <attr name="textEnd" format="reference" />
|
||||
* <attr name="tickMark" format="reference" />
|
||||
* <attr name="iconStart" format="reference" />
|
||||
* <attr name="iconEnd" format="reference" />
|
||||
* <attr name="iconStartContentDescription" format="reference" />
|
||||
* <attr name="iconEndContentDescription" format="reference" />
|
||||
* </p>
|
||||
*
|
||||
* <p> If you set the attribute values {@code iconStartContentDescription} or {@code
|
||||
* iconEndContentDescription} from XML, you must also set the corresponding attributes {@code
|
||||
* iconStart} or {@code iconEnd}, otherwise throws an {@link IllegalArgumentException}.</p>
|
||||
*/
|
||||
public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
|
||||
private final int mTextStartId;
|
||||
private final int mTextEndId;
|
||||
private final int mTickMarkId;
|
||||
private final int mIconStartId;
|
||||
private final int mIconEndId;
|
||||
private final int mIconStartContentDescriptionId;
|
||||
private final int mIconEndContentDescriptionId;
|
||||
private OnPreferenceChangeListener mStopListener;
|
||||
@Nullable
|
||||
private CharSequence mSummary;
|
||||
private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener;
|
||||
|
||||
public LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
@@ -56,6 +81,25 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
R.string.summary_placeholder);
|
||||
mTickMarkId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_tickMark, /* defValue= */ 0);
|
||||
mIconStartId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_iconStart, /* defValue= */ 0);
|
||||
mIconEndId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_iconEnd, /* defValue= */ 0);
|
||||
|
||||
mIconStartContentDescriptionId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_iconStartContentDescription,
|
||||
/* defValue= */ 0);
|
||||
Preconditions.checkArgument(!(mIconStartContentDescriptionId != 0 && mIconStartId == 0),
|
||||
"The resource of the iconStart attribute may be invalid or not set, "
|
||||
+ "you should set the iconStart attribute and have the valid resource.");
|
||||
|
||||
mIconEndContentDescriptionId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_iconEndContentDescription,
|
||||
/* defValue= */ 0);
|
||||
Preconditions.checkArgument(!(mIconEndContentDescriptionId != 0 && mIconEndId == 0),
|
||||
"The resource of the iconEnd attribute may be invalid or not set, "
|
||||
+ "you should set the iconEnd attribute and have the valid resource.");
|
||||
|
||||
mSummary = styledAttrs.getText(R.styleable.Preference_android_summary);
|
||||
styledAttrs.recycle();
|
||||
}
|
||||
@@ -75,10 +119,9 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
startText.setText(mTextStartId);
|
||||
endText.setText(mTextEndId);
|
||||
|
||||
final SeekBar seekBar = (SeekBar) holder.findViewById(com.android.internal.R.id.seekbar);
|
||||
if (mTickMarkId != 0) {
|
||||
final Drawable tickMark = getContext().getDrawable(mTickMarkId);
|
||||
final SeekBar seekBar = (SeekBar) holder.findViewById(
|
||||
com.android.internal.R.id.seekbar);
|
||||
seekBar.setTickMark(tickMark);
|
||||
}
|
||||
|
||||
@@ -90,19 +133,52 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
summary.setText(null);
|
||||
summary.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
final ViewGroup iconStartFrame = (ViewGroup) holder.findViewById(R.id.icon_start_frame);
|
||||
final ImageView iconStartView = (ImageView) holder.findViewById(R.id.icon_start);
|
||||
updateIconStartIfNeeded(iconStartFrame, iconStartView, seekBar);
|
||||
|
||||
final ViewGroup iconEndFrame = (ViewGroup) holder.findViewById(R.id.icon_end_frame);
|
||||
final ImageView iconEndView = (ImageView) holder.findViewById(R.id.icon_end);
|
||||
updateIconEndIfNeeded(iconEndFrame, iconEndView, seekBar);
|
||||
}
|
||||
|
||||
public void setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener) {
|
||||
mStopListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
super.onStartTrackingTouch(seekBar);
|
||||
|
||||
if (mSeekBarChangeListener != null) {
|
||||
mSeekBarChangeListener.onStartTrackingTouch(seekBar);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
super.onProgressChanged(seekBar, progress, fromUser);
|
||||
|
||||
if (mSeekBarChangeListener != null) {
|
||||
mSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
super.onStopTrackingTouch(seekBar);
|
||||
|
||||
if (mSeekBarChangeListener != null) {
|
||||
mSeekBarChangeListener.onStopTrackingTouch(seekBar);
|
||||
}
|
||||
|
||||
if (mStopListener != null) {
|
||||
mStopListener.onPreferenceChange(this, seekBar.getProgress());
|
||||
}
|
||||
|
||||
// Need to update the icon enabled status
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,5 +199,68 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
public CharSequence getSummary() {
|
||||
return mSummary;
|
||||
}
|
||||
|
||||
public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener seekBarChangeListener) {
|
||||
mSeekBarChangeListener = seekBarChangeListener;
|
||||
}
|
||||
|
||||
private void updateIconStartIfNeeded(ViewGroup iconFrame, ImageView iconStart,
|
||||
SeekBar seekBar) {
|
||||
if (mIconStartId == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (iconStart.getDrawable() == null) {
|
||||
iconStart.setImageResource(mIconStartId);
|
||||
}
|
||||
|
||||
if (mIconStartContentDescriptionId != 0) {
|
||||
final String contentDescription =
|
||||
iconFrame.getContext().getString(mIconStartContentDescriptionId);
|
||||
iconFrame.setContentDescription(contentDescription);
|
||||
}
|
||||
|
||||
iconFrame.setOnClickListener((view) -> {
|
||||
final int progress = getProgress();
|
||||
if (progress > 0) {
|
||||
setProgress(progress - 1);
|
||||
}
|
||||
});
|
||||
|
||||
iconFrame.setVisibility(View.VISIBLE);
|
||||
setIconViewAndFrameEnabled(iconStart, seekBar.getProgress() > 0);
|
||||
}
|
||||
|
||||
private void updateIconEndIfNeeded(ViewGroup iconFrame, ImageView iconEnd, SeekBar seekBar) {
|
||||
if (mIconEndId == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (iconEnd.getDrawable() == null) {
|
||||
iconEnd.setImageResource(mIconEndId);
|
||||
}
|
||||
|
||||
if (mIconEndContentDescriptionId != 0) {
|
||||
final String contentDescription =
|
||||
iconFrame.getContext().getString(mIconEndContentDescriptionId);
|
||||
iconFrame.setContentDescription(contentDescription);
|
||||
}
|
||||
|
||||
iconFrame.setOnClickListener((view) -> {
|
||||
final int progress = getProgress();
|
||||
if (progress < getMax()) {
|
||||
setProgress(progress + 1);
|
||||
}
|
||||
});
|
||||
|
||||
iconFrame.setVisibility(View.VISIBLE);
|
||||
setIconViewAndFrameEnabled(iconEnd, seekBar.getProgress() < seekBar.getMax());
|
||||
}
|
||||
|
||||
private static void setIconViewAndFrameEnabled(View iconView, boolean enabled) {
|
||||
iconView.setEnabled(enabled);
|
||||
final ViewGroup iconFrame = (ViewGroup) iconView.getParent();
|
||||
iconFrame.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user