Skip to content

Commit

Permalink
ProperSeekBarPreference: Fix auto reset in few cases
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Vashi <[email protected]>
  • Loading branch information
neobuddy89 authored and ralf979 committed Feb 8, 2023
1 parent 464245f commit 4752859
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 34 deletions.
11 changes: 8 additions & 3 deletions res/layout/preference_proper_seekbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,22 @@
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />

<SeekBar
<LinearLayout
android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toEndOf="@id/minus"
android:layout_toStartOf="@id/plus"
android:layout_centerVertical="true" />

</RelativeLayout>

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:paddingStart="16dp"
android:orientation="vertical" />
</LinearLayout>
1 change: 0 additions & 1 deletion res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@
<attr name="showSign" format="boolean" />
<attr name="units" format="string|reference" />
<attr name="continuousUpdates" format="boolean" />
<attr name="defaultText" format="string|reference" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
Expand Down Expand Up @@ -63,16 +65,20 @@ public ProperSeekBarPreference(Context context, AttributeSet attrs, int defStyle

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProperSeekBarPreference);
try {
mInterval = a.getInt(R.styleable.ProperSeekBarPreference_interval, mInterval);
mShowSign = a.getBoolean(R.styleable.ProperSeekBarPreference_showSign, mShowSign);
String units = a.getString(R.styleable.ProperSeekBarPreference_units);
if (units != null)
mUnits = units;
mContinuousUpdates = a.getBoolean(R.styleable.ProperSeekBarPreference_continuousUpdates, mContinuousUpdates);
} finally {
a.recycle();
}

try {
String newInterval = attrs.getAttributeValue(SETTINGS_NS, "interval");
if (newInterval != null)
mInterval = Integer.parseInt(newInterval);
} catch (Exception e) {
Log.e(TAG, "Invalid interval value", e);
}
mUnits = getAttributeStringValue(attrs, SETTINGS_NS, "units", "");
mMinValue = attrs.getAttributeIntValue(SETTINGS_NS, "min", mMinValue);
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", mMaxValue);
if (mMaxValue < mMinValue)
Expand All @@ -86,6 +92,7 @@ public ProperSeekBarPreference(Context context, AttributeSet attrs, int defStyle
mValue = mMinValue;
}

mSeekBar = new SeekBar(context, attrs);
setLayoutResource(R.layout.preference_proper_seekbar);
}

Expand All @@ -106,21 +113,37 @@ public ProperSeekBarPreference(Context context) {
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
try
{
// move our seekbar to the new view we've been given
ViewParent oldContainer = mSeekBar.getParent();
ViewGroup newContainer = (ViewGroup) holder.findViewById(R.id.seekbar);
if (oldContainer != newContainer) {
// remove the seekbar from the old view
if (oldContainer != null) {
((ViewGroup) oldContainer).removeView(mSeekBar);
}
// remove the existing seekbar (there may not be one) and add ours
newContainer.removeAllViews();
newContainer.addView(mSeekBar, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
} catch (Exception ex) {
Log.e(TAG, "Error binding view: " + ex.toString());
}

mSeekBar.setMax(getSeekValue(mMaxValue));
mSeekBar.setProgress(getSeekValue(mValue));
mSeekBar.setEnabled(isEnabled());

mValueTextView = (TextView) holder.findViewById(R.id.value);
mResetImageView = (ImageView) holder.findViewById(R.id.reset);
mMinusImageView = (ImageView) holder.findViewById(R.id.minus);
mPlusImageView = (ImageView) holder.findViewById(R.id.plus);

mSeekBar = (SeekBar) holder.findViewById(R.id.seekbar);

mSeekBar.setMax(getSeekValue(mMaxValue));
mSeekBar.setProgress(getSeekValue(mValue));

updateValueViews();

mSeekBar.setOnSeekBarChangeListener(this);

mResetImageView.setOnClickListener(this);
mMinusImageView.setOnClickListener(this);
mPlusImageView.setOnClickListener(this);
Expand All @@ -129,6 +152,16 @@ public void onBindViewHolder(PreferenceViewHolder holder) {
mPlusImageView.setOnLongClickListener(this);
}

private String getAttributeStringValue(AttributeSet attrs, String namespace, String name,
String defaultValue) {
String value = attrs.getAttributeValue(namespace, name);

if (value == null)
value = defaultValue;

return value;
}

protected int getLimitedValue(int v) {
return v < mMinValue ? mMinValue : (v > mMaxValue ? mMaxValue : v);
}
Expand All @@ -142,26 +175,37 @@ protected String getTextValue(int v) {
}

protected void updateValueViews() {
mValueTextView.setText(getContext().getString(R.string.proper_seekbar_value,
(!mTrackingTouch || mContinuousUpdates ? getTextValue(mValue) + (mDefaultValueExists && mValue == mDefaultValue ? " (" + getContext().getString(R.string.proper_seekbar_default_value) + ")" : "")
if (mValueTextView != null) {
mValueTextView.setText(getContext().getString(R.string.proper_seekbar_value,
(!mTrackingTouch || mContinuousUpdates ? getTextValue(mValue) +
(mDefaultValueExists && mValue == mDefaultValue ? " (" +
getContext().getString(R.string.proper_seekbar_default_value) + ")" : "")
: "[" + getTextValue(mTrackingValue) + "]")));
if (!mDefaultValueExists || mValue == mDefaultValue || mTrackingTouch)
mResetImageView.setVisibility(View.INVISIBLE);
else
mResetImageView.setVisibility(View.VISIBLE);
if (mValue == mMinValue || mTrackingTouch) {
mMinusImageView.setClickable(false);
mMinusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
} else {
mMinusImageView.setClickable(true);
mMinusImageView.clearColorFilter();
}
if (mValue == mMaxValue || mTrackingTouch) {
mPlusImageView.setClickable(false);
mPlusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
} else {
mPlusImageView.setClickable(true);
mPlusImageView.clearColorFilter();
if (mResetImageView != null) {
if (!mDefaultValueExists || mValue == mDefaultValue || mTrackingTouch)
mResetImageView.setVisibility(View.INVISIBLE);
else
mResetImageView.setVisibility(View.VISIBLE);
}
if (mMinusImageView != null) {
if (mValue == mMinValue || mTrackingTouch) {
mMinusImageView.setClickable(false);
mMinusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color),
PorterDuff.Mode.MULTIPLY);
} else {
mMinusImageView.setClickable(true);
mMinusImageView.clearColorFilter();
}
}
if (mPlusImageView != null) {
if (mValue == mMaxValue || mTrackingTouch) {
mPlusImageView.setClickable(false);
mPlusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
} else {
mPlusImageView.setClickable(true);
mPlusImageView.clearColorFilter();
}
}
}

Expand All @@ -187,8 +231,6 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

mValue = newValue;
updateValueViews();

notifyChanged();
}
}

Expand All @@ -203,6 +245,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
mTrackingTouch = false;
if (!mContinuousUpdates)
onProgressChanged(mSeekBar, getSeekValue(mTrackingValue), false);
notifyChanged();
}

@Override
Expand Down

0 comments on commit 4752859

Please sign in to comment.