Add duration preference with number picker

This commit is contained in:
J-Jamet
2021-04-10 19:29:19 +02:00
parent c40b255022
commit 014b0cce14
13 changed files with 483 additions and 39 deletions

View File

@@ -20,9 +20,12 @@
package com.kunzisoft.keepass.settings
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.settings.preferencedialogfragment.DurationDialogFragmentCompat
class MagikeyboardSettingsFragment : PreferenceFragmentCompat() {
@@ -30,4 +33,31 @@ class MagikeyboardSettingsFragment : PreferenceFragmentCompat() {
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences_keyboard, rootKey)
}
override fun onDisplayPreferenceDialog(preference: Preference?) {
var otherDialogFragment = false
var dialogFragment: DialogFragment? = null
// Main Preferences
when (preference?.key) {
getString(R.string.keyboard_entry_timeout_key) -> {
dialogFragment = DurationDialogFragmentCompat.newInstance(preference.key)
}
else -> otherDialogFragment = true
}
if (dialogFragment != null) {
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_PREF_FRAGMENT)
}
// Could not be handled here. Try with the super method.
else if (otherDialogFragment) {
super.onDisplayPreferenceDialog(preference)
}
}
companion object {
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
}
}

View File

@@ -30,6 +30,7 @@ import android.view.autofill.AutofillManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.preference.ListPreference
import androidx.preference.Preference
@@ -46,6 +47,7 @@ import com.kunzisoft.keepass.education.Education
import com.kunzisoft.keepass.icons.IconPackChooser
import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService
import com.kunzisoft.keepass.settings.preference.IconPackListPreference
import com.kunzisoft.keepass.settings.preferencedialogfragment.DurationDialogFragmentCompat
import com.kunzisoft.keepass.utils.UriUtil
@@ -448,6 +450,31 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
}
override fun onDisplayPreferenceDialog(preference: Preference?) {
var otherDialogFragment = false
var dialogFragment: DialogFragment? = null
// Main Preferences
when (preference?.key) {
getString(R.string.app_timeout_key),
getString(R.string.clipboard_timeout_key),
getString(R.string.temp_advanced_unlock_timeout_key) -> {
dialogFragment = DurationDialogFragmentCompat.newInstance(preference.key)
}
else -> otherDialogFragment = true
}
if (dialogFragment != null) {
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_PREF_FRAGMENT)
}
// Could not be handled here. Try with the super method.
else if (otherDialogFragment) {
super.onDisplayPreferenceDialog(preference)
}
}
override fun onResume() {
super.onResume()
activity?.let { activity ->
@@ -478,7 +505,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
companion object {
private const val REQUEST_CODE_AUTOFILL = 5201
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
}
}

View File

@@ -576,7 +576,6 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
}
companion object {
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preference
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.preference.DialogPreference
import com.kunzisoft.keepass.R
class DurationDialogPreference @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.dialogPreferenceStyle,
defStyleRes: Int = defStyleAttr)
: DialogPreference(context, attrs, defStyleAttr, defStyleRes) {
private var mDuration: Long = 0L
override fun getDialogLayoutResource(): Int {
return R.layout.pref_dialog_duration
}
/**
* Get current duration of preference
*/
fun getDuration(): Long {
return if (mDuration >= 0) mDuration else -1
}
/**
* Assign [duration] of preference
*/
fun setDuration(duration: Long) {
persistString(duration.toString())
notifyChanged()
}
override fun onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any?) {
if (restorePersistedValue) {
mDuration = getPersistedString(mDuration.toString()).toLongOrNull() ?: mDuration
} else {
mDuration = defaultValue?.toString()?.toLongOrNull() ?: mDuration
persistString(mDuration.toString())
}
}
override fun onGetDefaultValue(a: TypedArray?, index: Int): Any {
return try {
a?.getString(index)?.toLongOrNull() ?: mDuration
} catch (e: Exception) {
mDuration
}
}
// Was previously a string
override fun persistString(value: String?): Boolean {
mDuration = value?.toLongOrNull() ?: mDuration
return super.persistString(value)
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright 2021 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.os.Bundle
import android.view.View
import android.widget.NumberPicker
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.settings.preference.DurationDialogPreference
class DurationDialogFragmentCompat : InputPreferenceDialogFragmentCompat() {
private var mEnabled = true
private var mDays = 0
private var mHours = 0
private var mMinutes = 0
private var mSeconds = 0
private var daysNumberPicker: NumberPicker? = null
private var hoursNumberPicker: NumberPicker? = null
private var minutesNumberPicker: NumberPicker? = null
private var secondsNumberPicker: NumberPicker? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// To get items from saved instance state
if (savedInstanceState != null
&& savedInstanceState.containsKey(ENABLE_KEY)
&& savedInstanceState.containsKey(DAYS_KEY)
&& savedInstanceState.containsKey(HOURS_KEY)
&& savedInstanceState.containsKey(MINUTES_KEY)
&& savedInstanceState.containsKey(SECONDS_KEY)) {
mEnabled = savedInstanceState.getBoolean(ENABLE_KEY)
mDays = savedInstanceState.getInt(DAYS_KEY)
mHours = savedInstanceState.getInt(HOURS_KEY)
mMinutes = savedInstanceState.getInt(MINUTES_KEY)
mSeconds = savedInstanceState.getInt(SECONDS_KEY)
} else {
val currentPreference = preference
if (currentPreference is DurationDialogPreference) {
durationToDaysHoursMinutesSeconds(currentPreference.getDuration())
}
}
}
private fun durationToDaysHoursMinutesSeconds(duration: Long) {
if (duration < 0) {
mDays = 0
mHours = 0
mMinutes = 0
mSeconds = 0
} else {
mDays = (duration / (24L * 60L * 60L * 1000L)).toInt()
val daysMilliseconds = mDays * 24L * 60L * 60L * 1000L
mHours = ((duration - daysMilliseconds) / (60L * 60L * 1000L)).toInt()
val hoursMilliseconds = mHours * 60L * 60L * 1000L
mMinutes = ((duration - daysMilliseconds - hoursMilliseconds) / (60L * 1000L)).toInt()
val minutesMilliseconds = mMinutes * 60L * 1000L
mSeconds = ((duration - daysMilliseconds - hoursMilliseconds - minutesMilliseconds) / (1000L)).toInt()
}
}
private fun assignValuesInViews() {
daysNumberPicker?.value = mDays
hoursNumberPicker?.value = mHours
minutesNumberPicker?.value = mMinutes
secondsNumberPicker?.value = mSeconds
}
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
daysNumberPicker = view.findViewById<NumberPicker>(R.id.days_picker).apply {
minValue = 0
maxValue = 364
setOnValueChangedListener { _, _, newVal ->
mDays = newVal
activateSwitch()
}
}
hoursNumberPicker = view.findViewById<NumberPicker>(R.id.hours_picker).apply {
minValue = 0
maxValue = 23
setOnValueChangedListener { _, _, newVal ->
mHours = newVal
activateSwitch()
}
}
minutesNumberPicker = view.findViewById<NumberPicker>(R.id.minutes_picker).apply {
minValue = 0
maxValue = 59
setOnValueChangedListener { _, _, newVal ->
mMinutes = newVal
activateSwitch()
}
}
secondsNumberPicker = view.findViewById<NumberPicker>(R.id.seconds_picker).apply {
minValue = 0
maxValue = 59
setOnValueChangedListener { _, _, newVal ->
mSeconds = newVal
activateSwitch()
}
}
setSwitchAction({ isChecked ->
mEnabled = isChecked
}, mDays + mHours + mMinutes + mSeconds > 0)
assignValuesInViews()
}
private fun buildDuration(): Long {
return if (mEnabled) {
mDays * 24L * 60L * 60L * 1000L +
mHours * 60L * 60L * 1000L +
mMinutes * 60L * 1000L +
mSeconds * 1000L
} else {
-1
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(ENABLE_KEY, mEnabled)
outState.putInt(DAYS_KEY, mDays)
outState.putInt(HOURS_KEY, mHours)
outState.putInt(MINUTES_KEY, mMinutes)
outState.putInt(SECONDS_KEY, mSeconds)
}
override fun onDialogClosed(positiveResult: Boolean) {
if (positiveResult) {
val currentPreference = preference
if (currentPreference is DurationDialogPreference) {
currentPreference.setDuration(buildDuration())
}
}
}
companion object {
private const val ENABLE_KEY = "ENABLE_KEY"
private const val DAYS_KEY = "DAYS_KEY"
private const val HOURS_KEY = "HOURS_KEY"
private const val MINUTES_KEY = "MINUTES_KEY"
private const val SECONDS_KEY = "SECONDS_KEY"
fun newInstance(key: String): DurationDialogFragmentCompat {
val fragment = DurationDialogFragmentCompat()
val bundle = Bundle(1)
bundle.putString(ARG_KEY, key)
fragment.arguments = bundle
return fragment
}
}
}

View File

@@ -154,4 +154,14 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
onCheckedChange?.invoke(isChecked)
}
}
fun activateSwitch() {
if (switchElementView?.isChecked != true)
switchElementView?.isChecked = true
}
fun deactivateSwitch() {
if (switchElementView?.isChecked == true)
switchElementView?.isChecked = false
}
}

View File

@@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M19,3h-1L18,1h-2v2L8,3L8,1L6,1v2L5,3c-1.11,0 -1.99,0.9 -1.99,2L3,19c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM19,19L5,19L5,8h14v11zM7,10h5v5L7,15z"/>
</vector>

View File

@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 Jeremy Jamet / Kunzisoft.
This file is part of KeePassDX.
KeePassDX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KeePassDX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
-->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/edit"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAutofill="noExcludeDescendants"
tools:targetApi="o">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/explanation_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:gravity="center"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
style="@style/KeepassDXStyle.TextAppearance.SmallTitle"/>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/explanation_text" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/switch_element">
<LinearLayout
android:id="@+id/duration_days_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintEnd_toStartOf="@+id/duration_hours_picker"
app:layout_constraintRight_toLeftOf="@+id/duration_hours_picker">
<NumberPicker
android:id="@+id/days_picker"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:src="@drawable/ic_day_white_24dp"
app:tint="?android:attr/textColor"
android:contentDescription="@string/digits" />
</LinearLayout>
<LinearLayout
android:id="@+id/duration_hours_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="HardcodedText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/duration_days_picker"
app:layout_constraintLeft_toRightOf="@+id/duration_days_picker"
app:layout_constraintEnd_toStartOf="@+id/duration_time_picker"
app:layout_constraintRight_toLeftOf="@+id/duration_time_picker">
<NumberPicker
android:id="@+id/hours_picker"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="bold"
android:text=":" />
</LinearLayout>
<LinearLayout
android:id="@+id/duration_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="HardcodedText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/duration_hours_picker"
app:layout_constraintLeft_toRightOf="@+id/duration_hours_picker"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent">
<NumberPicker
android:id="@+id/minutes_picker"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="28sp"
android:text="'"/>
<NumberPicker
android:id="@+id/seconds_picker"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="28sp"
android:text="''"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -288,31 +288,6 @@
<string name="timeout_backup_key" translatable="false">timeout_backup_key</string>
<string name="timeout_default" translatable="false">300000</string>
<string name="timeout_screen_off" translatable="false">1500</string>
<string-array name="timeout_values">
<item translatable="false">5000</item>
<item translatable="false">10000</item>
<item translatable="false">20000</item>
<item translatable="false">30000</item>
<item translatable="false">60000</item>
<item translatable="false">300000</item>
<item translatable="false">900000</item>
<item translatable="false">1800000</item>
<item translatable="false">-1</item>
</string-array>
<string-array name="large_timeout_values">
<item translatable="false">300000</item>
<item translatable="false">900000</item>
<item translatable="false">1800000</item>
<item translatable="false">3600000</item>
<item translatable="false">7200000</item>
<item translatable="false">18000000</item>
<item translatable="false">36000000</item>
<item translatable="false">86400000</item>
<item translatable="false">172800000</item>
<item translatable="false">604800000</item>
<item translatable="false">2592000000</item>
<item translatable="false">-1</item>
</string-array>
<!-- Text Size -->
<dimen name="list_icon_size_default" translatable="false">32dp</dimen>

View File

@@ -47,13 +47,11 @@
android:title="@string/temp_advanced_unlock_enable_title"
android:summary="@string/temp_advanced_unlock_enable_summary"
android:defaultValue="@bool/temp_advanced_unlock_enable_default"/>
<ListPreference
<com.kunzisoft.keepass.settings.preference.DurationDialogPreference
android:key="@string/temp_advanced_unlock_timeout_key"
android:title="@string/temp_advanced_unlock_timeout_title"
android:summary="@string/temp_advanced_unlock_timeout_summary"
android:dependency="@string/temp_advanced_unlock_enable_key"
android:entries="@array/large_timeout_options"
android:entryValues="@array/large_timeout_values"
android:dialogTitle="@string/advanced_unlock_timeout"
android:defaultValue="@string/temp_advanced_unlock_timeout_default"/>
<Preference

View File

@@ -69,12 +69,10 @@
<PreferenceCategory
android:title="@string/lock">
<ListPreference
<com.kunzisoft.keepass.settings.preference.DurationDialogPreference
android:key="@string/app_timeout_key"
android:title="@string/app_timeout"
android:summary="@string/app_timeout_summary"
android:entries="@array/timeout_options"
android:entryValues="@array/timeout_values"
android:dialogTitle="@string/app_timeout"
android:defaultValue="@string/timeout_default"/>
<SwitchPreference

View File

@@ -66,12 +66,10 @@
android:summary="@string/clear_clipboard_notification_summary"
android:dependency="@string/clipboard_notifications_key"
android:defaultValue="@bool/clear_clipboard_notification_default"/>
<ListPreference
<com.kunzisoft.keepass.settings.preference.DurationDialogPreference
android:key="@string/clipboard_timeout_key"
android:title="@string/clipboard_timeout"
android:summary="@string/clipboard_timeout_summary"
android:entries="@array/timeout_options"
android:entryValues="@array/timeout_values"
android:dialogTitle="@string/clipboard_timeout"
android:defaultValue="@string/clipboard_timeout_default"/>
<SwitchPreference

View File

@@ -31,12 +31,10 @@
android:summary="@string/keyboard_notification_entry_clear_close_summary"
android:dependency="@string/keyboard_notification_entry_key"
android:defaultValue="@bool/keyboard_notification_entry_clear_close_default"/>
<ListPreference
<com.kunzisoft.keepass.settings.preference.DurationDialogPreference
android:key="@string/keyboard_entry_timeout_key"
android:title="@string/keyboard_entry_timeout_title"
android:summary="@string/keyboard_entry_timeout_summary"
android:entries="@array/timeout_options"
android:entryValues="@array/timeout_values"
android:dialogTitle="@string/keyboard_entry_timeout_title"
android:defaultValue="@string/keyboard_entry_timeout_default"
android:dependency="@string/keyboard_notification_entry_clear_close_key"/>