-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace time duration picker dialog for screen lock timeout.
- Loading branch information
1 parent
db65edb
commit 2305015
Showing
10 changed files
with
220 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/main/java/org/thoughtcrime/securesms/components/TimeDurationPickerDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.thoughtcrime.securesms.components | ||
|
||
import android.app.Dialog | ||
import android.os.Bundle | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.setFragmentResult | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import org.thoughtcrime.securesms.R | ||
import org.thoughtcrime.securesms.databinding.TimeDurationPickerDialogBinding | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
/** | ||
* Time duration dialog for selection a duration of hours and minutes. Currently | ||
* designed specifically for screen lock but could easily be generalized in the future | ||
* if needed. | ||
* | ||
* Uses [setFragmentResult] to pass the provided duration back in milliseconds. | ||
*/ | ||
class TimeDurationPickerDialog : DialogFragment(), NumericKeyboardView.Listener { | ||
|
||
private var _binding: TimeDurationPickerDialogBinding? = null | ||
private val binding: TimeDurationPickerDialogBinding | ||
get() = _binding!! | ||
|
||
private var duration: String = "0000" | ||
private var full: Boolean = false | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
_binding = TimeDurationPickerDialogBinding.inflate(layoutInflater) | ||
|
||
binding.durationKeyboard.listener = this | ||
|
||
setDuration(requireArguments().getLong(ARGUMENT_DURATION).milliseconds) | ||
|
||
return MaterialAlertDialogBuilder(requireContext()) | ||
.setView(binding.root) | ||
.setPositiveButton(R.string.TimeDurationPickerDialog_positive_button) { _, _ -> | ||
setFragmentResult( | ||
RESULT_DURATION, | ||
bundleOf( | ||
RESULT_KEY_DURATION_MILLISECONDS to getDuration().inWholeMilliseconds | ||
) | ||
) | ||
} | ||
.setNegativeButton(android.R.string.cancel, null) | ||
.show() | ||
} | ||
|
||
override fun onKeyPress(keyCode: Int) { | ||
if (full && keyCode != -1) { | ||
return | ||
} | ||
|
||
duration = if (keyCode == -1) { | ||
"0" + duration.substring(0, 3) | ||
} else { | ||
duration.substring(1) + keyCode | ||
} | ||
|
||
updateDuration() | ||
} | ||
|
||
private fun updateDuration() { | ||
binding.durationHour.text = duration.substring(0, 2) | ||
binding.durationMinute.text = duration.substring(2) | ||
full = duration.toInt() > 1000 | ||
} | ||
|
||
private fun setDuration(duration: Duration) { | ||
val hour = duration.inWholeMinutes / 60 | ||
val minute = duration.inWholeMinutes.mod(60) | ||
this.duration = String.format("%02d%02d", hour, minute) | ||
updateDuration() | ||
} | ||
|
||
private fun getDuration(): Duration { | ||
val hours = duration.substring(0, 2).toInt() | ||
val minutes = duration.substring(2).toInt() | ||
|
||
return hours.hours.plus(minutes.minutes) | ||
} | ||
|
||
companion object { | ||
const val RESULT_DURATION = "RESULT_DURATION" | ||
const val RESULT_KEY_DURATION_MILLISECONDS = "RESULT_KEY_DURATION_MILLISECONDS" | ||
|
||
private const val ARGUMENT_DURATION = "ARGUMENT_DURATION" | ||
|
||
fun create(duration: Duration): TimeDurationPickerDialog { | ||
return TimeDurationPickerDialog().apply { | ||
arguments = bundleOf( | ||
ARGUMENT_DURATION to duration.inWholeMilliseconds | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layoutDirection="ltr"> | ||
|
||
|
||
<LinearLayout | ||
android:id="@+id/duration_layout" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:background="@color/signal_colorSurfaceVariant" | ||
android:gravity="center_horizontal" | ||
android:orientation="horizontal" | ||
android:paddingVertical="8dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<TextView | ||
android:id="@+id/duration_hour" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_horizontal" | ||
android:textAppearance="@style/Signal.Text.HeadlineLarge" | ||
android:textColor="@color/signal_colorOnSurfaceVariant" | ||
tools:text="01" /> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_horizontal" | ||
android:text="@string/TimeDurationPickerDialog_single_letter_hour_abbreviation" | ||
android:textAppearance="@style/Signal.Text.BodySmall" | ||
android:textColor="@color/signal_colorOnSurfaceVariant" /> | ||
|
||
<TextView | ||
android:id="@+id/duration_minute" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="16dp" | ||
android:gravity="center_horizontal" | ||
android:textAppearance="@style/Signal.Text.HeadlineLarge" | ||
android:textColor="@color/signal_colorOnSurfaceVariant" | ||
tools:text="01" /> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_horizontal" | ||
android:text="@string/TimeDurationPickerDialog_single_letter_minute_abbreviation" | ||
android:textAppearance="@style/Signal.Text.BodySmall" | ||
android:textColor="@color/signal_colorOnSurfaceVariant" /> | ||
|
||
</LinearLayout> | ||
|
||
<TextView | ||
android:id="@+id/duration_header" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="12dp" | ||
android:paddingHorizontal="16dp" | ||
android:text="@string/TimeDurationPickerDialog_minimum_duration_warning" | ||
android:textColor="@color/signal_colorOnSurface" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/duration_layout" /> | ||
|
||
<org.thoughtcrime.securesms.components.NumericKeyboardView | ||
android:id="@+id/duration_keyboard" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/duration_header" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.