diff --git a/res/drawable/ic_cherish_about_settings.xml b/res/drawable/ic_cherish_about_settings.xml index 04e1f12..78c6a25 100644 --- a/res/drawable/ic_cherish_about_settings.xml +++ b/res/drawable/ic_cherish_about_settings.xml @@ -1,7 +1,7 @@ - - \ No newline at end of file + + diff --git a/res/drawable/ic_cherish_animation_settings.xml b/res/drawable/ic_cherish_animation_settings.xml index 5e5376d..f414f2f 100644 --- a/res/drawable/ic_cherish_animation_settings.xml +++ b/res/drawable/ic_cherish_animation_settings.xml @@ -1,9 +1,10 @@ + android:viewportHeight="24"> - - \ No newline at end of file + + diff --git a/res/drawable/ic_cherish_button_settings.xml b/res/drawable/ic_cherish_button_settings.xml index e7dc290..8790b68 100644 --- a/res/drawable/ic_cherish_button_settings.xml +++ b/res/drawable/ic_cherish_button_settings.xml @@ -1,7 +1,13 @@ + - - \ No newline at end of file + + + + diff --git a/res/drawable/ic_cherish_lockscreen_settings.xml b/res/drawable/ic_cherish_lockscreen_settings.xml index 268c2f6..65e9516 100644 --- a/res/drawable/ic_cherish_lockscreen_settings.xml +++ b/res/drawable/ic_cherish_lockscreen_settings.xml @@ -1,7 +1,10 @@ - - \ No newline at end of file + + + diff --git a/res/drawable/ic_cherish_misc_settings.xml b/res/drawable/ic_cherish_misc_settings.xml index eac1443..4fdfd8e 100644 --- a/res/drawable/ic_cherish_misc_settings.xml +++ b/res/drawable/ic_cherish_misc_settings.xml @@ -1,19 +1,18 @@ + android:viewportHeight="24"> - \ No newline at end of file + diff --git a/res/drawable/ic_cherish_notification_settings.xml b/res/drawable/ic_cherish_notification_settings.xml index e887042..00075d5 100644 --- a/res/drawable/ic_cherish_notification_settings.xml +++ b/res/drawable/ic_cherish_notification_settings.xml @@ -1,10 +1,11 @@ + android:viewportHeight="24"> - - \ No newline at end of file + + diff --git a/res/drawable/ic_cherish_power_settings.xml b/res/drawable/ic_cherish_power_settings.xml index 5d1d251..30bbe6c 100644 --- a/res/drawable/ic_cherish_power_settings.xml +++ b/res/drawable/ic_cherish_power_settings.xml @@ -1,7 +1,9 @@ - - \ No newline at end of file + + diff --git a/res/drawable/ic_cherish_quick_settings.xml b/res/drawable/ic_cherish_quick_settings.xml index cc60bc7..2d3ff6f 100644 --- a/res/drawable/ic_cherish_quick_settings.xml +++ b/res/drawable/ic_cherish_quick_settings.xml @@ -1,7 +1,18 @@ - - \ No newline at end of file + + + + + + diff --git a/res/drawable/ic_cherish_status_settings.xml b/res/drawable/ic_cherish_status_settings.xml index 34777df..9f60be7 100644 --- a/res/drawable/ic_cherish_status_settings.xml +++ b/res/drawable/ic_cherish_status_settings.xml @@ -1,7 +1,10 @@ - - \ No newline at end of file + + + diff --git a/res/drawable/ic_cherish_theme_settings.xml b/res/drawable/ic_cherish_theme_settings.xml index 0b36c8b..7598e59 100644 --- a/res/drawable/ic_cherish_theme_settings.xml +++ b/res/drawable/ic_cherish_theme_settings.xml @@ -1,21 +1,10 @@ - + + - - - - - \ No newline at end of file + android:fillColor="?android:attr/colorAccent" + android:pathData="M7.5,2C5.71,3.15 4.5,5.18 4.5,7.5C4.5,9.82 5.71,11.85 7.53,13C4.46,13 2,10.54 2,7.5A5.5,5.5 0 0,1 7.5,2M19.07,3.5L20.5,4.93L4.93,20.5L3.5,19.07L19.07,3.5M12.89,5.93L11.41,5L9.97,6L10.39,4.3L9,3.24L10.75,3.12L11.33,1.47L12,3.1L13.73,3.13L12.38,4.26L12.89,5.93M9.59,9.54L8.43,8.81L7.31,9.59L7.65,8.27L6.56,7.44L7.92,7.35L8.37,6.06L8.88,7.33L10.24,7.36L9.19,8.23L9.59,9.54M19,13.5A5.5,5.5 0 0,1 13.5,19C12.28,19 11.15,18.6 10.24,17.93L17.93,10.24C18.6,11.15 19,12.28 19,13.5M14.6,20.08L17.37,18.93L17.13,22.28L14.6,20.08M18.93,17.38L20.08,14.61L22.28,17.15L18.93,17.38M20.08,12.42L18.94,9.64L22.28,9.88L20.08,12.42M9.63,18.93L12.4,20.08L9.87,22.27L9.63,18.93Z" /> + diff --git a/res/drawable/ic_custom_seekbar_minus.xml b/res/drawable/ic_custom_seekbar_minus.xml new file mode 100644 index 0000000..28777b8 --- /dev/null +++ b/res/drawable/ic_custom_seekbar_minus.xml @@ -0,0 +1,22 @@ + + + + + diff --git a/res/drawable/ic_custom_seekbar_plus.xml b/res/drawable/ic_custom_seekbar_plus.xml new file mode 100644 index 0000000..a1873f4 --- /dev/null +++ b/res/drawable/ic_custom_seekbar_plus.xml @@ -0,0 +1,22 @@ + + + + + diff --git a/res/drawable/ic_custom_seekbar_reset.xml b/res/drawable/ic_custom_seekbar_reset.xml new file mode 100644 index 0000000..8bea21e --- /dev/null +++ b/res/drawable/ic_custom_seekbar_reset.xml @@ -0,0 +1,24 @@ + + + + + + diff --git a/res/layout/preference_cherish_seekbar.xml b/res/layout/preference_cherish_seekbar.xml index 576fa3f..53048ed 100644 --- a/res/layout/preference_cherish_seekbar.xml +++ b/res/layout/preference_cherish_seekbar.xml @@ -14,79 +14,66 @@ limitations under the License. --> - + android:layout_height="75dp" + android:clickable="true" + android:focusable="true" + card_view:cardCornerRadius="12dp" + card_view:cardElevation="4dp" + android:layout_marginTop="16dp" + android:layout_marginHorizontal="32dp" + android:foreground="?android:attr/selectableItemBackground"> - - - - - + android:layout_height="match_parent"> + + + + + + + android:layout_height="match_parent" + android:gravity="center" + android:layout_centerInParent="true" + android:orientation="vertical"> + - - + android:gravity="center" + android:marqueeRepeatLimit="marquee_forever" + android:singleLine="true" + android:textStyle="bold" + android:textSize="16sp"/> + + - - - - - - - - + + diff --git a/res/layout/preference_custom_seekbar.xml b/res/layout/preference_custom_seekbar.xml new file mode 100644 index 0000000..b33b5a8 --- /dev/null +++ b/res/layout/preference_custom_seekbar.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/values/cherish_attrs.xml b/res/values/cherish_attrs.xml index c38ef7c..3574fe5 100644 --- a/res/values/cherish_attrs.xml +++ b/res/values/cherish_attrs.xml @@ -20,6 +20,8 @@ + + diff --git a/res/values/cherish_colors.xml b/res/values/cherish_colors.xml index dec5359..0fc9b35 100644 --- a/res/values/cherish_colors.xml +++ b/res/values/cherish_colors.xml @@ -27,7 +27,8 @@ #ffc9e6 #dfedc6 #edc6d1 + #66000000 - - #d6d6d6 + + #d6d6d6 diff --git a/res/values/cherish_strings.xml b/res/values/cherish_strings.xml index dd37180..86df51e 100644 --- a/res/values/cherish_strings.xml +++ b/res/values/cherish_strings.xml @@ -1161,4 +1161,10 @@ Media Notification options Artwork media background Set media artwork as notification media background + + + Value: %s + Default + Default value: %s\nLong press to set + Default value is set diff --git a/res/xml/cherish_settings.xml b/res/xml/cherish_settings.xml index d789b0e..e70db68 100644 --- a/res/xml/cherish_settings.xml +++ b/res/xml/cherish_settings.xml @@ -13,68 +13,69 @@ See the License for the specific language governing permissions and limitations under the License. --> - - + android:icon="@drawable/ic_cherish_status_settings"/> - + android:icon="@drawable/ic_cherish_quick_settings"/> - + android:icon="@drawable/ic_cherish_theme_settings"/> - - - + android:icon="@drawable/ic_cherish_lockscreen_settings"/> - + android:icon="@drawable/ic_cherish_power_settings"/> - - + android:icon="@drawable/ic_cherish_notification_settings"/> - + android:icon="@drawable/ic_cherish_misc_settings"/> - + android:icon="@drawable/ic_cherish_about_settings" /> diff --git a/src/com/cherish/settings/preferences/CherishPreference.java b/src/com/cherish/settings/preferences/CherishPreference.java new file mode 100644 index 0000000..361902d --- /dev/null +++ b/src/com/cherish/settings/preferences/CherishPreference.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.cherish.settings.preferences; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import androidx.annotation.VisibleForTesting; +import androidx.core.content.res.TypedArrayUtils; +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.Utils; + +public class CherishPreference extends Preference { + + private final View.OnClickListener mClickListener = v -> performClick(v); + + private boolean mAllowDividerAbove; + private boolean mAllowDividerBelow; + + public CherishPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Preference); + + mAllowDividerAbove = TypedArrayUtils.getBoolean(a, R.styleable.Preference_allowDividerAbove, + R.styleable.Preference_allowDividerAbove, false); + mAllowDividerBelow = TypedArrayUtils.getBoolean(a, R.styleable.Preference_allowDividerBelow, + R.styleable.Preference_allowDividerBelow, false); + a.recycle(); + + setLayoutResource(R.layout.preference_cherish_seekbar); + } + + public CherishPreference(Context context, View view) { + super(context); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + holder.itemView.setOnClickListener(mClickListener); + + final boolean selectable = isSelectable(); + holder.itemView.setFocusable(selectable); + holder.itemView.setClickable(selectable); + holder.setDividerAllowedAbove(mAllowDividerAbove); + holder.setDividerAllowedBelow(mAllowDividerBelow); + } + +} diff --git a/src/com/cherish/settings/preferences/CustomSeekBarPreference.java b/src/com/cherish/settings/preferences/CustomSeekBarPreference.java index 908bce4..45dabb1 100644 --- a/src/com/cherish/settings/preferences/CustomSeekBarPreference.java +++ b/src/com/cherish/settings/preferences/CustomSeekBarPreference.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 The Dirty Unicorns Project + * Copyright (C) 2016-2019 crDroid Android Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,55 +18,60 @@ package com.cherish.settings.preferences; import android.content.Context; import android.content.res.TypedArray; +import android.graphics.PorterDuff; +import androidx.preference.*; +import androidx.core.content.res.TypedArrayUtils; import android.util.AttributeSet; import android.util.Log; -import android.view.ViewParent; +import android.view.View; import android.view.ViewGroup; +import android.view.ViewParent; +import android.widget.ImageView; import android.widget.SeekBar; import android.widget.TextView; -import androidx.preference.*; +import android.widget.Toast; import com.android.settings.R; -public class CustomSeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener { - private final String TAG = getClass().getName(); +public class CustomSeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener, + View.OnClickListener, View.OnLongClickListener { + protected final String TAG = getClass().getName(); private static final String SETTINGS_NS = "http://schemas.android.com/apk/res/com.android.settings"; - private static final String ANDROIDNS = "http://schemas.android.com/apk/res/android"; - private static final int DEFAULT_VALUE = 50; + protected static final String ANDROIDNS = "http://schemas.android.com/apk/res/android"; - private int mMin = 0; - private int mInterval = 1; - private int mCurrentValue; - private int mDefaultValue = -1; - private int mMax = 255; - private String mUnits = ""; - private String mDefaultText = ""; - private SeekBar mSeekBar; - private TextView mTitle; - private TextView mStatusText; + protected int mInterval = 1; + protected boolean mShowSign = false; + protected String mUnits = ""; + protected boolean mContinuousUpdates = false; - public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { + protected int mMinValue = 0; + protected int mMaxValue = 100; + protected boolean mDefaultValueExists = false; + protected int mDefaultValue; + + protected int mValue; + + protected TextView mValueTextView; + protected ImageView mResetImageView; + protected ImageView mMinusImageView; + protected ImageView mPlusImageView; + protected SeekBar mSeekBar; + + protected boolean mTrackingTouch = false; + protected int mTrackingValue; + + public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - final TypedArray a = context.obtainStyledAttributes( - attrs, R.styleable.CustomSeekBarPreference); - mMax = attrs.getAttributeIntValue(ANDROIDNS, "max", 255); - mMin = attrs.getAttributeIntValue(SETTINGS_NS, "min", 0); - mDefaultValue = attrs.getAttributeIntValue(ANDROIDNS, "defaultValue", -1); - if (mDefaultValue > mMax) { - mDefaultValue = mMax; - } - mUnits = getAttributeStringValue(attrs, SETTINGS_NS, "units", ""); - mDefaultText = getAttributeStringValue(attrs, SETTINGS_NS, "defaultText", "Def"); - - Integer id = a.getResourceId(R.styleable.CustomSeekBarPreference_units, 0); - if (id > 0) { - mUnits = context.getResources().getString(id); - } - id = a.getResourceId(R.styleable.CustomSeekBarPreference_defaultText, 0); - if (id > 0) { - mDefaultText = context.getResources().getString(id); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomSeekBarPreference); + try { + mShowSign = a.getBoolean(R.styleable.CustomSeekBarPreference_showSign, mShowSign); + String units = a.getString(R.styleable.CustomSeekBarPreference_units); + if (units != null) + mUnits = " " + units; + mContinuousUpdates = a.getBoolean(R.styleable.CustomSeekBarPreference_continuousUpdates, mContinuousUpdates); + } finally { + a.recycle(); } try { @@ -76,12 +81,21 @@ public class CustomSeekBarPreference extends Preference implements SeekBar.OnSee } catch (Exception e) { Log.e(TAG, "Invalid interval value", e); } + mMinValue = attrs.getAttributeIntValue(SETTINGS_NS, "min", mMinValue); + mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", mMaxValue); + if (mMaxValue < mMinValue) + mMaxValue = mMinValue; + String defaultValue = attrs.getAttributeValue(ANDROIDNS, "defaultValue"); + mDefaultValueExists = defaultValue != null && !defaultValue.isEmpty(); + if (mDefaultValueExists) { + mDefaultValue = getLimitedValue(Integer.parseInt(defaultValue)); + mValue = mDefaultValue; + } else { + mValue = mMinValue; + } - a.recycle(); mSeekBar = new SeekBar(context, attrs); - mSeekBar.setMax(mMax - mMin); - mSeekBar.setOnSeekBarChangeListener(this); - setLayoutResource(R.layout.preference_cherish_seekbar); + setLayoutResource(R.layout.preference_custom_seekbar); } public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { @@ -89,43 +103,23 @@ public class CustomSeekBarPreference extends Preference implements SeekBar.OnSee } public CustomSeekBarPreference(Context context, AttributeSet attrs) { - this(context, attrs, 0); + this(context, attrs, TypedArrayUtils.getAttr(context, + androidx.preference.R.attr.preferenceStyle, + android.R.attr.preferenceStyle)); } public CustomSeekBarPreference(Context context) { this(context, null); } - private String getAttributeStringValue(AttributeSet attrs, String namespace, String name, - String defaultValue) { - String value = attrs.getAttributeValue(namespace, name); - if (value == null) - value = defaultValue; - - return value; - } - @Override - public void onDependencyChanged(Preference dependency, boolean disableDependent) { - super.onDependencyChanged(dependency, disableDependent); - this.setShouldDisableView(true); - if (mTitle != null) - mTitle.setEnabled(!disableDependent); - if (mSeekBar != null) - mSeekBar.setEnabled(!disableDependent); - if (mStatusText != null) - mStatusText.setEnabled(!disableDependent); - } - - @Override - public void onBindViewHolder(PreferenceViewHolder view) { - super.onBindViewHolder(view); + 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) view.findViewById(R.id.seekBarPrefBarContainer); - + ViewGroup newContainer = (ViewGroup) holder.findViewById(R.id.seekbar); if (oldContainer != newContainer) { // remove the seekbar from the old view if (oldContainer != null) { @@ -139,115 +133,212 @@ public class CustomSeekBarPreference extends Preference implements SeekBar.OnSee } catch (Exception ex) { Log.e(TAG, "Error binding view: " + ex.toString()); } - mStatusText = (TextView) view.findViewById(R.id.seekBarPrefValue); - if (mCurrentValue == mDefaultValue) { - mStatusText.setText(mDefaultText); - } else { - mStatusText.setText(String.valueOf(mCurrentValue) + mUnits); - } - mSeekBar.setProgress(mCurrentValue - mMin); - mTitle = (TextView) view.findViewById(android.R.id.title); - - view.setDividerAllowedAbove(false); - //view.setDividerAllowedBelow(false); + 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); + + updateValueViews(); + + mSeekBar.setOnSeekBarChangeListener(this); + mResetImageView.setOnClickListener(this); + mMinusImageView.setOnClickListener(this); + mPlusImageView.setOnClickListener(this); + mResetImageView.setOnLongClickListener(this); + mMinusImageView.setOnLongClickListener(this); + mPlusImageView.setOnLongClickListener(this); } - public void setMax(int max) { - mMax = max; - mSeekBar.setMax(mMax - mMin); + protected int getLimitedValue(int v) { + return v < mMinValue ? mMinValue : (v > mMaxValue ? mMaxValue : v); } - public void setMin(int min) { - mMin = min; - mSeekBar.setMax(mMax - mMin); + protected int getSeekValue(int v) { + return 0 - Math.floorDiv(mMinValue - v, mInterval); } - public void setIntervalValue(int value) { - mInterval = value; + protected String getTextValue(int v) { + return (mShowSign && v > 0 ? "+" : "") + String.valueOf(v) + mUnits; } - public void setValue(int value) { - mCurrentValue = value; + protected void updateValueViews() { + if (mValueTextView != null) { + mValueTextView.setText(getContext().getString(R.string.custom_seekbar_value, + (!mTrackingTouch || mContinuousUpdates ? getTextValue(mValue) + + (mDefaultValueExists && mValue == mDefaultValue ? " (" + + getContext().getString(R.string.custom_seekbar_default_value) + ")" : "") + : getTextValue(mTrackingValue)))); + } + 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(); + } + } + } + + protected void changeValue(int newValue) { + // for subclasses } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - int newValue = progress + mMin; - if (newValue > mMax) - newValue = mMax; - else if (newValue < mMin) - newValue = mMin; - else if (mInterval != 1 && newValue % mInterval != 0) - newValue = Math.round(((float) newValue) / mInterval) * mInterval; - - // change rejected, revert to the previous value - if (!callChangeListener(newValue)) { - seekBar.setProgress(mCurrentValue - mMin); - return; - } - // change accepted, store it - mCurrentValue = newValue; - if (mStatusText != null) { - if (newValue == mDefaultValue) { - mStatusText.setText(mDefaultText); - } else { - mStatusText.setText(String.valueOf(newValue) + mUnits); + int newValue = getLimitedValue(mMinValue + (progress * mInterval)); + if (mTrackingTouch && !mContinuousUpdates) { + mTrackingValue = newValue; + updateValueViews(); + } else if (mValue != newValue) { + // change rejected, revert to the previous value + if (!callChangeListener(newValue)) { + mSeekBar.setProgress(getSeekValue(mValue)); + return; } + // change accepted, store it + changeValue(newValue); + persistInt(newValue); + + mValue = newValue; + updateValueViews(); } - persistInt(newValue); } @Override public void onStartTrackingTouch(SeekBar seekBar) { + mTrackingValue = mValue; + mTrackingTouch = true; } @Override public void onStopTrackingTouch(SeekBar seekBar) { + mTrackingTouch = false; + if (!mContinuousUpdates) + onProgressChanged(mSeekBar, getSeekValue(mTrackingValue), false); notifyChanged(); } @Override - protected Object onGetDefaultValue(TypedArray ta, int index) { - int defaultValue = ta.getInt(index, DEFAULT_VALUE); - return defaultValue; + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.reset) { + Toast.makeText(getContext(), getContext().getString(R.string.custom_seekbar_default_value_to_set, getTextValue(mDefaultValue)), + Toast.LENGTH_LONG).show(); + } else if (id == R.id.minus) { + setValue(mValue - mInterval, true); + } else if (id == R.id.plus) { + setValue(mValue + mInterval, true); + } } + @Override + public boolean onLongClick(View v) { + int id = v.getId(); + if (id == R.id.reset) { + setValue(mDefaultValue, true); + //Toast.makeText(getContext(), getContext().getString(R.string.custom_seekbar_default_value_is_set), + // Toast.LENGTH_LONG).show(); + } else if (id == R.id.minus) { + setValue(mMaxValue - mMinValue > mInterval * 2 && mMaxValue + mMinValue < mValue * 2 ? Math.floorDiv(mMaxValue + mMinValue, 2) : mMinValue, true); + } else if (id == R.id.plus) { + setValue(mMaxValue - mMinValue > mInterval * 2 && mMaxValue + mMinValue > mValue * 2 ? -1 * Math.floorDiv(-1 * (mMaxValue + mMinValue), 2) : mMaxValue, true); + } + return true; + } + + // dont need too much shit about initial and default values + // its all done in constructor already + @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { - if (restoreValue) { - mCurrentValue = getPersistedInt(mCurrentValue); - } - else { - int temp = 0; - try { - temp = (Integer) defaultValue; - } catch (Exception ex) { - Log.e(TAG, "Invalid default value: " + defaultValue.toString()); - } - persistInt(temp); - mCurrentValue = temp; - } - } - - public void setDefaultValue(int value) { - mDefaultValue = value; - if (mDefaultValue > mMax) { - mDefaultValue = mMax; - } - if (mCurrentValue == mDefaultValue && mStatusText != null) { - mStatusText.setText(mDefaultText); - } + if (restoreValue) + mValue = getPersistedInt(mValue); } @Override - public void setEnabled(boolean enabled) { - if (mSeekBar != null && mStatusText != null && mTitle != null) { - mSeekBar.setEnabled(enabled); - mStatusText.setEnabled(enabled); - mTitle.setEnabled(enabled); + public void setDefaultValue(Object defaultValue) { + if (defaultValue instanceof Integer) + setDefaultValue((Integer) defaultValue, mSeekBar != null); + else + setDefaultValue(defaultValue == null ? (String) null : defaultValue.toString(), mSeekBar != null); + } + + public void setDefaultValue(int newValue, boolean update) { + newValue = getLimitedValue(newValue); + if (!mDefaultValueExists || mDefaultValue != newValue) { + mDefaultValueExists = true; + mDefaultValue = newValue; + if (update) + updateValueViews(); } - super.setEnabled(enabled); + } + + public void setDefaultValue(String newValue, boolean update) { + if (mDefaultValueExists && (newValue == null || newValue.isEmpty())) { + mDefaultValueExists = false; + if (update) + updateValueViews(); + } else if (newValue != null && !newValue.isEmpty()) { + setDefaultValue(Integer.parseInt(newValue), update); + } + } + + public void setMax(int max) { + mMaxValue = max; + mSeekBar.setMax(mMaxValue - mMinValue); + } + + public void setMin(int min) { + mMinValue = min; + mSeekBar.setMax(mMaxValue - mMinValue); + } + + public void setValue(int newValue) { + mValue = getLimitedValue(newValue); + if (mSeekBar != null) mSeekBar.setProgress(getSeekValue(mValue)); + } + + public void setValue(int newValue, boolean update) { + newValue = getLimitedValue(newValue); + if (mValue != newValue) { + if (update) + mSeekBar.setProgress(getSeekValue(newValue)); + else + mValue = newValue; + } + } + + public int getValue() { + return mValue; + } + + // need some methods here to set/get other attrs at runtime, + // but who really need this ... + + public void refresh(int newValue) { + // this will ... + setValue(newValue, mSeekBar != null); } }