From 6fc19c625f67045bf38b8bbcbc02e5fb4634f8b6 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Mon, 22 Mar 2021 07:12:46 +0100 Subject: [PATCH] Use our own NumberPickerPreference class --- README.md | 3 +- app/build.gradle | 1 - .../Preferences/NumberPickerPreference.java | 102 ++++++++++++++++++ .../res/values/aboutlibraries_definitions.xml | 17 +-- app/src/main/res/values/attrs.xml | 8 ++ app/src/main/res/xml/preferences.xml | 22 ++-- 6 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/org/shadowice/flocke/andotp/Preferences/NumberPickerPreference.java create mode 100644 app/src/main/res/values/attrs.xml diff --git a/README.md b/README.md index eb45e792..7ae13727 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,6 @@ If you want to show your appreciation for our work with a small donation you can * [material-intro](https://github.com/heinrichreimer/material-intro) * [MaterialProgressBar](https://github.com/DreaminginCodeZH/MaterialProgressBar) * [OpenPGP API library](https://github.com/open-keychain/openpgp-api) - * [VNTNumberPickerPreference](https://github.com/vanniktech/VNTNumberPickerPreference) * [ZXing Android Embedded](https://github.com/journeyapps/zxing-android-embedded) * [Droid Sans Mono Zeromod](https://github.com/AlbertoDorado/droid-sans-mono-zeromod) @@ -141,11 +140,13 @@ If you want to show your appreciation for our work with a small donation you can * [Code Parts from Google's Android Samples](https://android.googlesource.com/platform/development/+/master/samples/Vault/src/com/example/android/vault) * [LetterBitmap](https://stackoverflow.com/questions/23122088/colored-boxed-with-letters-a-la-gmail) * [DimensionConverter](https://stackoverflow.com/questions/8343971/how-to-parse-a-dimension-string-and-convert-it-to-a-dimension-value) + * [NumberPickerPreference](https://github.com/Alobar/AndroidPreferenceTest/tree/master/alobar-preference) #### Previously used open-source components: * [FABsMenu](https://github.com/jahirfiquitiva/FABsMenu) * [LicensesDialog](https://github.com/PSDev/LicensesDialog) + * [VNTNumberPickerPreference](https://github.com/vanniktech/VNTNumberPickerPreference) #### Previously used code examples: diff --git a/app/build.gradle b/app/build.gradle index 143ff827..71774401 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -73,7 +73,6 @@ dependencies { implementation "com.heinrichreimersoftware:material-intro:2.0.0" implementation("com.journeyapps:zxing-android-embedded:4.1.0") { transitive = false } implementation('com.google.zxing:core:3.4.1') - implementation "com.vanniktech:vntnumberpickerpreference:1.0.0" implementation "me.zhanghai.android.materialprogressbar:library:1.6.1" implementation "org.sufficientlysecure:openpgp-api:12.0" implementation "com.leinardi.android:speed-dial:3.1.1" diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Preferences/NumberPickerPreference.java b/app/src/main/java/org/shadowice/flocke/andotp/Preferences/NumberPickerPreference.java new file mode 100644 index 00000000..10f47552 --- /dev/null +++ b/app/src/main/java/org/shadowice/flocke/andotp/Preferences/NumberPickerPreference.java @@ -0,0 +1,102 @@ +package org.shadowice.flocke.andotp.Preferences; + +import android.content.Context; +import android.content.res.TypedArray; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.NumberPicker; + +import org.shadowice.flocke.andotp.R; + +/** + * A {@link android.preference.Preference} that displays a number picker as a dialog. + */ +public class NumberPickerPreference extends DialogPreference { + + public static final int DEFAULT_MAX_VALUE = 100; + public static final int DEFAULT_MIN_VALUE = 0; + public static final boolean DEFAULT_WRAP_SELECTOR_WHEEL = true; + + private final int minValue; + private final int maxValue; + private final boolean wrapSelectorWheel; + + private NumberPicker picker; + private int value; + + @SuppressWarnings("unused") + public NumberPickerPreference(Context context, AttributeSet attrs) { + this(context, attrs, android.R.attr.dialogPreferenceStyle); + } + + public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference); + minValue = a.getInteger(R.styleable.NumberPickerPreference_minValue, DEFAULT_MIN_VALUE); + maxValue = a.getInteger(R.styleable.NumberPickerPreference_maxValue, DEFAULT_MAX_VALUE); + wrapSelectorWheel = a.getBoolean(R.styleable.NumberPickerPreference_wrapSelectorWheel, DEFAULT_WRAP_SELECTOR_WHEEL); + a.recycle(); + } + + @Override + protected View onCreateDialogView() { + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + layoutParams.gravity = Gravity.CENTER; + + picker = new NumberPicker(getContext()); + picker.setLayoutParams(layoutParams); + + FrameLayout dialogView = new FrameLayout(getContext()); + dialogView.addView(picker); + + return dialogView; + } + + @Override + protected void onBindDialogView(View view) { + super.onBindDialogView(view); + picker.setMinValue(minValue); + picker.setMaxValue(maxValue); + picker.setWrapSelectorWheel(wrapSelectorWheel); + picker.setValue(getValue()); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + if (positiveResult) { + picker.clearFocus(); + int newValue = picker.getValue(); + if (callChangeListener(newValue)) { + setValue(newValue); + } + } + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + return a.getInt(index, minValue); + } + + @Override + protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { + int initialValue = restorePersistedValue ? getPersistedInt(minValue) : (Integer) defaultValue; + + setValue(initialValue); + setSummary(String.valueOf(initialValue)); + } + + public void setValue(int value) { + this.value = value; + persistInt(this.value); + setSummary(String.valueOf(this.value)); + } + + public int getValue() { + return this.value; + } +} diff --git a/app/src/main/res/values/aboutlibraries_definitions.xml b/app/src/main/res/values/aboutlibraries_definitions.xml index ea7e206a..2fdde344 100644 --- a/app/src/main/res/values/aboutlibraries_definitions.xml +++ b/app/src/main/res/values/aboutlibraries_definitions.xml @@ -1,9 +1,12 @@ - + + - + The Apache Software Foundation http://apache.org @@ -18,7 +21,7 @@ apache_2_0 - + Akira Aratani Expandable Layout @@ -32,7 +35,7 @@ apache_2_0 - + Roberto Leinardi Floating Action Button Speed Dial @@ -46,7 +49,7 @@ apache_2_0 - + Jan Heinrich Reimer https://heinrichreimer.com/ @@ -61,7 +64,7 @@ mit - + Dominik Schürmann https://www.schuermann.eu/ @@ -76,7 +79,7 @@ apache_2_0 - + Alberto Dorado Droid Sans Mono Zeromod diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml new file mode 100644 index 00000000..d6d4526c --- /dev/null +++ b/app/src/main/res/values/attrs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 8785931d..4f3f7069 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -1,4 +1,5 @@ + @@ -7,13 +8,14 @@ android:key="@string/settings_key_cat_security" android:title="@string/settings_category_title_security"> - + app:minValue="@integer/settings_min_tap_to_reveal_timeout" + app:maxValue="@integer/settings_max_tap_to_reveal_timeout" + app:wrapSelectorWheel="false" /> - + app:minValue="@integer/settings_auth_min_inactivity_delay" + app:maxValue="@integer/settings_auth_max_inactivity_delay" + app:wrapSelectorWheel="false" /> - + app:maxValue="@integer/settings_max_label_size" + app:minValue="@integer/settings_min_label_size" + app:wrapSelectorWheel="false" />