Skip to content

Commit d4066e7

Browse files
committed
material time picker
1 parent 6a567c5 commit d4066e7

File tree

20 files changed

+123
-4
lines changed

20 files changed

+123
-4
lines changed

app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ class MainActivity : AppCompatActivity(), SlidingLayout.Listener {
684684
it.onMinimize()
685685
it.onClose()
686686
closePlayer()
687-
if (prefs.getBoolean(C.SLEEP_TIMER_LOCK, true)) {
687+
if (prefs.getBoolean(C.SLEEP_TIMER_LOCK, false)) {
688688
if ((getSystemService(POWER_SERVICE) as PowerManager).let {
689689
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
690690
it.isInteractive

app/src/main/java/com/github/andreyasadchy/xtra/ui/player/BasePlayerFragment.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.pm.PackageManager
88
import android.content.res.Configuration
99
import android.os.Build
1010
import android.os.Bundle
11+
import android.text.format.DateFormat
1112
import android.util.Log
1213
import android.view.View
1314
import android.view.ViewGroup
@@ -57,6 +58,7 @@ import com.github.andreyasadchy.xtra.util.FragmentUtils
5758
import com.github.andreyasadchy.xtra.util.LifecycleListener
5859
import com.github.andreyasadchy.xtra.util.TwitchApiHelper
5960
import com.github.andreyasadchy.xtra.util.disable
61+
import com.github.andreyasadchy.xtra.util.getAlertDialogBuilder
6062
import com.github.andreyasadchy.xtra.util.gone
6163
import com.github.andreyasadchy.xtra.util.hideKeyboard
6264
import com.github.andreyasadchy.xtra.util.isInPortraitOrientation
@@ -66,6 +68,8 @@ import com.github.andreyasadchy.xtra.util.shortToast
6668
import com.github.andreyasadchy.xtra.util.toast
6769
import com.github.andreyasadchy.xtra.util.visible
6870
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
71+
import com.google.android.material.timepicker.MaterialTimePicker
72+
import com.google.android.material.timepicker.TimeFormat
6973
import com.google.common.util.concurrent.ListenableFuture
7074
import com.google.common.util.concurrent.MoreExecutors
7175
import kotlinx.coroutines.delay
@@ -517,7 +521,7 @@ abstract class BasePlayerFragment : BaseNetworkFragment(), LifecycleListener, Sl
517521
} else if (((activity as? MainActivity)?.getSleepTimerTimeLeft() ?: 0) > 0L) {
518522
context.toast(R.string.timer_canceled)
519523
}
520-
if (lockScreen != prefs.getBoolean(C.SLEEP_TIMER_LOCK, true)) {
524+
if (lockScreen != prefs.getBoolean(C.SLEEP_TIMER_LOCK, false)) {
521525
prefs.edit { putBoolean(C.SLEEP_TIMER_LOCK, lockScreen) }
522526
}
523527
(activity as? MainActivity)?.setSleepTimer(durationMs)
@@ -567,7 +571,35 @@ abstract class BasePlayerFragment : BaseNetworkFragment(), LifecycleListener, Sl
567571
}
568572

569573
fun showSleepTimerDialog() {
570-
SleepTimerDialog.show(childFragmentManager, (activity as? MainActivity)?.getSleepTimerTimeLeft() ?: 0)
574+
if (requireContext().prefs().getBoolean(C.SLEEP_TIMER_USE_TIME_PICKER, false)) {
575+
if (((activity as? MainActivity)?.getSleepTimerTimeLeft() ?: 0) > 0L) {
576+
requireContext().getAlertDialogBuilder()
577+
.setMessage(getString(R.string.stop_sleep_timer_message))
578+
.setPositiveButton(getString(R.string.yes)) { _, _ ->
579+
onSleepTimerChanged(-1L, 0, 0, requireContext().prefs().getBoolean(C.SLEEP_TIMER_LOCK, false))
580+
}
581+
.setNegativeButton(getString(R.string.no), null)
582+
.show()
583+
} else {
584+
val savedValue = requireContext().prefs().getInt(C.SLEEP_TIMER_TIME, 15)
585+
val picker = MaterialTimePicker.Builder()
586+
.setTimeFormat(if (DateFormat.is24HourFormat(requireContext())) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H)
587+
.setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK)
588+
.setHour(savedValue / 60)
589+
.setMinute(savedValue % 60)
590+
.build()
591+
picker.addOnPositiveButtonClickListener {
592+
val minutes = TwitchApiHelper.getMinutesLeft(picker.hour, picker.minute)
593+
onSleepTimerChanged(minutes * 60_000L, minutes / 60, minutes % 60, requireContext().prefs().getBoolean(C.SLEEP_TIMER_LOCK, false))
594+
requireContext().prefs().edit {
595+
putInt(C.SLEEP_TIMER_TIME, picker.hour * 60 + picker.minute)
596+
}
597+
}
598+
picker.show(childFragmentManager, null)
599+
}
600+
} else {
601+
SleepTimerDialog.show(childFragmentManager, (activity as? MainActivity)?.getSleepTimerTimeLeft() ?: 0)
602+
}
571603
}
572604

573605
fun showQualityDialog() {

app/src/main/java/com/github/andreyasadchy/xtra/ui/player/SleepTimerDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SleepTimerDialog : DialogFragment() {
7676
val admin = ComponentName(requireContext(), AdminReceiver::class.java)
7777
if ((requireContext().getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager).isAdminActive(admin)) {
7878
lockCheckbox.apply {
79-
isChecked = requireContext().prefs().getBoolean(C.SLEEP_TIMER_LOCK, true)
79+
isChecked = requireContext().prefs().getBoolean(C.SLEEP_TIMER_LOCK, false)
8080
text = context.getString(R.string.sleep_timer_lock)
8181
}
8282
} else {

app/src/main/java/com/github/andreyasadchy/xtra/ui/settings/SettingsActivity.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.andreyasadchy.xtra.ui.settings
22

33
import android.Manifest
44
import android.app.Activity
5+
import android.app.admin.DevicePolicyManager
56
import android.content.ComponentName
67
import android.content.ContentResolver
78
import android.content.Intent
@@ -51,6 +52,7 @@ import com.github.andreyasadchy.xtra.R
5152
import com.github.andreyasadchy.xtra.databinding.ActivitySettingsBinding
5253
import com.github.andreyasadchy.xtra.model.Account
5354
import com.github.andreyasadchy.xtra.ui.main.IntegrityDialog
55+
import com.github.andreyasadchy.xtra.util.AdminReceiver
5456
import com.github.andreyasadchy.xtra.util.C
5557
import com.github.andreyasadchy.xtra.util.DisplayUtils
5658
import com.github.andreyasadchy.xtra.util.DownloadUtils
@@ -403,6 +405,17 @@ class SettingsActivity : AppCompatActivity() {
403405
true
404406
}
405407

408+
findPreference<SwitchPreferenceCompat>("sleep_timer_lock")?.setOnPreferenceChangeListener { _, newValue ->
409+
if (newValue == true && !(requireContext().getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager).isAdminActive(ComponentName(requireContext(), AdminReceiver::class.java))) {
410+
requireContext().startActivity(
411+
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).apply {
412+
putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, ComponentName(requireContext(), AdminReceiver::class.java))
413+
}
414+
)
415+
}
416+
true
417+
}
418+
406419
findPreference<Preference>("admin_settings")?.setOnPreferenceClickListener {
407420
startActivity(Intent().setComponent(ComponentName("com.android.settings", "com.android.settings.DeviceAdminSettings")))
408421
true

app/src/main/java/com/github/andreyasadchy/xtra/util/C.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ object C {
4848
const val KEY_CHAT_OPENED = "key_chat_opened"
4949
const val KEY_CHAT_BAR_VISIBLE = "key_chat_bar_visible"
5050
const val SLEEP_TIMER_MINUTES = "sleep_timer_minutes"
51+
const val SLEEP_TIMER_TIME = "sleep_timer_time"
5152
const val SLEEP_TIMER_LOCK = "sleep_timer_lock"
53+
const val SLEEP_TIMER_USE_TIME_PICKER = "sleep_timer_use_time_picker"
5254
const val SORT_DEFAULT_GAME_VIDEOS = "sort_default_game_videos"
5355
const val SORT_DEFAULT_GAME_CLIPS = "sort_default_game_clips"
5456
const val SORT_DEFAULT_CHANNEL_VIDEOS = "sort_default_channel_videos"

app/src/main/java/com/github/andreyasadchy/xtra/util/TwitchApiHelper.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ object TwitchApiHelper {
167167
} else null
168168
}
169169

170+
fun getMinutesLeft(hour: Int, minute: Int): Int {
171+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
172+
val currentDate = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.systemDefault())
173+
val date = currentDate.withHour(hour).withMinute(minute).let {
174+
if (it < currentDate) it.plusDays(1) else it
175+
}
176+
return ChronoUnit.MINUTES.between(currentDate, date).toInt()
177+
} else {
178+
val currentDate = Calendar.getInstance()
179+
val date = Calendar.getInstance()
180+
date.set(Calendar.HOUR_OF_DAY, hour)
181+
date.set(Calendar.MINUTE, minute)
182+
if (date < currentDate) {
183+
date.add(Calendar.DAY_OF_YEAR, 1)
184+
}
185+
return ((date.timeInMillis - currentDate.timeInMillis) / 60000).toInt()
186+
}
187+
}
188+
170189
fun getTimestamp(input: Long, timestampFormat: String?): String? {
171190
val pattern = when (timestampFormat) {
172191
"0" -> "H:mm"

app/src/main/res/values-ar/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,7 @@
646646
<string name="notification_live_channel_title">Live notifications</string>
647647
<string name="live_notification">%s is live</string>
648648
<string name="live_notifications">Live notifications</string>
649+
<string name="sleep_timer_lock_screen">Lock screen after sleep timer is done</string>
650+
<string name="sleep_timer_time_picker">Use Material time picker for sleep timer</string>
651+
<string name="stop_sleep_timer_message">Stop sleep timer?</string>
649652
</resources>

app/src/main/res/values-de/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,7 @@
646646
<string name="notification_live_channel_title">Live-Benachrichtigungen</string>
647647
<string name="live_notification">%s ist Live</string>
648648
<string name="live_notifications">Live-Benachrichtigungen</string>
649+
<string name="sleep_timer_lock_screen">Lock screen after sleep timer is done</string>
650+
<string name="sleep_timer_time_picker">Use Material time picker for sleep timer</string>
651+
<string name="stop_sleep_timer_message">Stop sleep timer?</string>
649652
</resources>

app/src/main/res/values-es/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,7 @@
646646
<string name="notification_live_channel_title">Live notifications</string>
647647
<string name="live_notification">%s is live</string>
648648
<string name="live_notifications">Live notifications</string>
649+
<string name="sleep_timer_lock_screen">Lock screen after sleep timer is done</string>
650+
<string name="sleep_timer_time_picker">Use Material time picker for sleep timer</string>
651+
<string name="stop_sleep_timer_message">Stop sleep timer?</string>
649652
</resources>

app/src/main/res/values-fr/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,7 @@
646646
<string name="notification_live_channel_title">Live notifications</string>
647647
<string name="live_notification">%s is live</string>
648648
<string name="live_notifications">Live notifications</string>
649+
<string name="sleep_timer_lock_screen">Lock screen after sleep timer is done</string>
650+
<string name="sleep_timer_time_picker">Use Material time picker for sleep timer</string>
651+
<string name="stop_sleep_timer_message">Stop sleep timer?</string>
649652
</resources>

0 commit comments

Comments
 (0)