diff options
| author | TheScarastic <warabhishek@gmail.com> | 2021-07-19 01:59:46 +0700 |
|---|---|---|
| committer | nebrassy <nebras30@gmail.com> | 2022-06-12 16:30:54 +0200 |
| commit | af1b8095db374368353995f0abf245cde4ce27ee (patch) | |
| tree | 9fe82ea9634787592e6d83fd91a850a900bc869d | |
| parent | 0bd52f1d7562f8c6b09189fcf1d130f93392a536 (diff) | |
vayu: parts: Implement SeekBarPreference
[nullxception: imported from ffd149893f9 ("vayu: Add touch profiles for
gaming and benchmarking")]
Signed-off-by: Nauval Rizky <enuma.alrizky@gmail.com>
Signed-off-by: David Setiawan <fryevia@foxmail.com>
Change-Id: I52a2c3bceef76d323f182dbbcd2a96f77487f3de
| -rw-r--r-- | parts/res/layout/preference_slider.xml | 83 | ||||
| -rw-r--r-- | parts/src/org/lineageos/settings/widget/SeekBarPreference.java | 280 |
2 files changed, 363 insertions, 0 deletions
diff --git a/parts/res/layout/preference_slider.xml b/parts/res/layout/preference_slider.xml new file mode 100644 index 0000000..ba2f8e9 --- /dev/null +++ b/parts/res/layout/preference_slider.xml @@ -0,0 +1,83 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2015 The Android Open Source Project + (C) 2018-2020 The LineageOS 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. +--> + +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:minHeight="?android:attr/listPreferredItemHeight" + android:gravity="center_vertical" + android:paddingStart="?android:attr/listPreferredItemPaddingStart" + android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:background="?android:attr/activatedBackgroundIndicator" + android:clipToPadding="false"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical" + android:layout_marginTop="8dip" + android:layout_marginBottom="8dip"> + + <TextView android:id="@android:id/title" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:paddingTop="8dp" + android:paddingStart="56dp" + android:singleLine="true" + android:textAppearance="?android:attr/textAppearanceListItem" + android:ellipsize="marquee" + android:fadingEdge="horizontal" /> + + <FrameLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginTop="8dp"> + + <ImageView + android:id="@+id/icon" + android:layout_gravity="center_vertical|start" + android:layout_width="48dp" + android:layout_height="48dp" /> + + <SeekBar + android:id="@+id/seekbar" + android:layout_marginStart="56dp" + android:layout_marginEnd="16dp" + android:layout_gravity="center_vertical" + android:layout_width="match_parent" + android:layout_height="48dp" + style="@android:style/Widget.Material.SeekBar.Discrete" + android:tickMarkTint="@android:color/black" /> + + </FrameLayout> + + <TextView + android:id="@android:id/summary" + android:paddingStart="56dp" + android:paddingTop="4dp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceListItemSecondary" + android:textColor="?android:attr/textColorSecondary" + android:gravity="center_vertical" + android:minLines="3" + android:maxLines="4" /> + + </LinearLayout> + +</FrameLayout> diff --git a/parts/src/org/lineageos/settings/widget/SeekBarPreference.java b/parts/src/org/lineageos/settings/widget/SeekBarPreference.java new file mode 100644 index 0000000..68d1e13 --- /dev/null +++ b/parts/src/org/lineageos/settings/widget/SeekBarPreference.java @@ -0,0 +1,280 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * 2017-2020 The LineageOS 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 org.lineageos.settings.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.os.Parcel; +import android.os.Parcelable; +import android.util.AttributeSet; +import android.view.KeyEvent; +import android.view.View; +import android.widget.ImageView; +import android.widget.SeekBar; +import android.widget.SeekBar.OnSeekBarChangeListener; + +import androidx.preference.PreferenceViewHolder; + +import com.android.settingslib.RestrictedPreference; + +import org.lineageos.settings.R; + +/** + * Based on android.preference.SeekBarPreference, but uses support preference as base. + */ +public class SeekBarPreference extends RestrictedPreference + implements OnSeekBarChangeListener, View.OnKeyListener { + + private int mProgress; + private int mMax; + private boolean mTrackingTouch; + + private ImageView mIconView; + private Drawable mIcon; + + public SeekBarPreference( + Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + + TypedArray a = context.obtainStyledAttributes( + attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes); + setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax)); + a.recycle(); + + a = context.obtainStyledAttributes(attrs, + com.android.internal.R.styleable.SeekBarPreference, defStyleAttr, defStyleRes); + final int layoutResId = a.getResourceId( + com.android.internal.R.styleable.SeekBarPreference_layout, + com.android.internal.R.layout.preference_widget_seekbar); + a.recycle(); + + setLayoutResource(layoutResId); + } + + public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public SeekBarPreference(Context context, AttributeSet attrs) { + this(context, attrs, com.android.internal.R.attr.seekBarPreferenceStyle); + } + + public SeekBarPreference(Context context) { + this(context, null); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder view) { + super.onBindViewHolder(view); + + mIconView = (ImageView) view.findViewById(R.id.icon); + if (mIcon != null) { + mIconView.setImageDrawable(mIcon); + } + + view.itemView.setOnKeyListener(this); + SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar); + seekBar.setOnSeekBarChangeListener(this); + seekBar.setMax(mMax); + seekBar.setProgress(mProgress); + seekBar.setEnabled(isEnabled()); + } + + public ImageView getIconView() { + return mIconView; + } + + public void setIconDrawable(Drawable drawable) { + if (mIconView != null) { + mIconView.setImageDrawable(drawable); + } + mIcon = drawable; + } + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + setProgress(restoreValue ? getPersistedInt(mProgress) + : (Integer) defaultValue); + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + return a.getInt(index, 0); + } + + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + if (event.getAction() != KeyEvent.ACTION_DOWN) { + return false; + } + + SeekBar seekBar = (SeekBar) v.findViewById(R.id.seekbar); + if (seekBar == null) { + return false; + } + return seekBar.onKeyDown(keyCode, event); + } + + public void setMax(int max) { + if (max != mMax) { + mMax = max; + notifyChanged(); + } + } + + public void setProgress(int progress) { + setProgress(progress, true); + } + + private void setProgress(int progress, boolean notifyChanged) { + if (progress > mMax) { + progress = mMax; + } + if (progress < 0) { + progress = 0; + } + if (progress != mProgress) { + mProgress = progress; + persistInt(progress); + if (notifyChanged) { + notifyChanged(); + } + } + } + + public int getProgress() { + return mProgress; + } + + /** + * Persist the seekBar's progress value if callChangeListener + * returns true, otherwise set the seekBar's progress to the stored value + */ + void syncProgress(SeekBar seekBar) { + int progress = seekBar.getProgress(); + if (progress != mProgress) { + if (callChangeListener(progress)) { + setProgress(progress, false); + } else { + seekBar.setProgress(mProgress); + } + } + } + + @Override + public void onProgressChanged( + SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser && !mTrackingTouch) { + syncProgress(seekBar); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + mTrackingTouch = true; + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + mTrackingTouch = false; + if (seekBar.getProgress() != mProgress) { + syncProgress(seekBar); + } + } + + @Override + protected Parcelable onSaveInstanceState() { + /* + * Suppose a client uses this preference type without persisting. We + * must save the instance state so it is able to, for example, survive + * orientation changes. + */ + + final Parcelable superState = super.onSaveInstanceState(); + if (isPersistent()) { + // No need to save instance state since it's persistent + return superState; + } + + // Save the instance state + final SavedState myState = new SavedState(superState); + myState.progress = mProgress; + myState.max = mMax; + return myState; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (!state.getClass().equals(SavedState.class)) { + // Didn't save state for us in onSaveInstanceState + super.onRestoreInstanceState(state); + return; + } + + // Restore the instance state + SavedState myState = (SavedState) state; + super.onRestoreInstanceState(myState.getSuperState()); + mProgress = myState.progress; + mMax = myState.max; + notifyChanged(); + } + + /** + * SavedState, a subclass of {@link BaseSavedState}, will store the state + * of MyPreference, a subclass of Preference. + * <p> + * It is important to always call through to super methods. + */ + private static class SavedState extends BaseSavedState { + int progress; + int max; + + public SavedState(Parcel source) { + super(source); + + // Restore the click counter + progress = source.readInt(); + max = source.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + + // Save the click counter + dest.writeInt(progress); + dest.writeInt(max); + } + + public SavedState(Parcelable superState) { + super(superState); + } + + @SuppressWarnings("unused") + public static final Parcelable.Creator<SavedState> CREATOR = + new Parcelable.Creator<SavedState>() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } +} |
