|
@@ -0,0 +1,129 @@
|
|
1
|
+package fr.forum_thalie.tsumugi.preferences
|
|
2
|
+
|
|
3
|
+import android.content.Context
|
|
4
|
+import android.content.Intent
|
|
5
|
+import android.media.AudioManager
|
|
6
|
+import android.os.Bundle
|
|
7
|
+import android.view.KeyEvent
|
|
8
|
+import android.view.LayoutInflater
|
|
9
|
+import android.view.View
|
|
10
|
+import android.view.ViewGroup
|
|
11
|
+import androidx.appcompat.app.AlertDialog
|
|
12
|
+import androidx.appcompat.app.AppCompatActivity
|
|
13
|
+import androidx.preference.PreferenceFragmentCompat
|
|
14
|
+import androidx.preference.PreferenceManager
|
|
15
|
+import androidx.preference.SeekBarPreference
|
|
16
|
+import androidx.preference.SwitchPreferenceCompat
|
|
17
|
+import fr.forum_thalie.tsumugi.Actions
|
|
18
|
+import fr.forum_thalie.tsumugi.R
|
|
19
|
+import fr.forum_thalie.tsumugi.RadioService
|
|
20
|
+import fr.forum_thalie.tsumugi.playerstore.PlayerStore
|
|
21
|
+import kotlinx.coroutines.*
|
|
22
|
+import kotlin.coroutines.CoroutineContext
|
|
23
|
+import kotlin.math.max
|
|
24
|
+import kotlin.math.min
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+class AlarmAdjustVolumeFragment : PreferenceFragmentCompat() {
|
|
28
|
+ override fun onAttach(context: Context) {
|
|
29
|
+ (activity as AppCompatActivity).supportActionBar?.title = context.getString(R.string.test_alarm_volume)
|
|
30
|
+ super.onAttach(context)
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ // get previous state: if it's playing, we'll resume playing as multimedia; if it was stopped, we'll stop
|
|
34
|
+ private var isPlayingMultimedia: Boolean = PlayerStore.instance.isPlaying.value ?: false
|
|
35
|
+
|
|
36
|
+ override fun onStop() {
|
|
37
|
+ if (isPlayingMultimedia)
|
|
38
|
+ {
|
|
39
|
+ actionOnService(Actions.PLAY)
|
|
40
|
+ } else {
|
|
41
|
+ actionOnService(Actions.PAUSE)
|
|
42
|
+ }
|
|
43
|
+ PlayerStore.instance.volume.value = PreferenceManager.getDefaultSharedPreferences(requireContext()).getInt("volume", 100)
|
|
44
|
+ super.onStop()
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ override fun onResume() {
|
|
48
|
+
|
|
49
|
+ isPlayingMultimedia = PlayerStore.instance.isPlaying.value ?: false
|
|
50
|
+ // start as alarm
|
|
51
|
+ actionOnService(Actions.PLAY_OR_FALLBACK)
|
|
52
|
+ super.onResume()
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
56
|
+ super.onViewCreated(view, savedInstanceState)
|
|
57
|
+
|
|
58
|
+ fun <T> debounce(delayMs: Long = 500L,
|
|
59
|
+ coroutineContext: CoroutineContext,
|
|
60
|
+ f: (T) -> Unit): (T) -> Unit {
|
|
61
|
+ var debounceJob: Job? = null
|
|
62
|
+ return { param: T ->
|
|
63
|
+ if (debounceJob?.isCompleted != false) {
|
|
64
|
+ debounceJob = CoroutineScope(coroutineContext).launch {
|
|
65
|
+ delay(delayMs)
|
|
66
|
+ f(param)
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ val adjustAlarmVolume: (Int) -> Unit = debounce<Int>(50, GlobalScope.coroutineContext) {
|
|
73
|
+ android.util.Log.d(tag, "button $it pushed")
|
|
74
|
+ val keyCode = it
|
|
75
|
+ val audioManager = requireContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
|
76
|
+ audioManager.apply {
|
|
77
|
+ val currentVolume = this.getStreamVolume(AudioManager.STREAM_ALARM)
|
|
78
|
+ val minVolume = 0 // audioManager.getStreamMinVolume(AudioManager.STREAM_ALARM) <- require API28+
|
|
79
|
+ val maxVolume = this.getStreamMaxVolume(AudioManager.STREAM_ALARM)
|
|
80
|
+ if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
|
81
|
+ this.setStreamVolume(AudioManager.STREAM_ALARM, max(currentVolume - 1, minVolume), AudioManager.FLAG_SHOW_UI)
|
|
82
|
+
|
|
83
|
+ } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
|
|
84
|
+ this.setStreamVolume(AudioManager.STREAM_ALARM, min(currentVolume + 1, maxVolume), AudioManager.FLAG_SHOW_UI)
|
|
85
|
+ } else {
|
|
86
|
+
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ }
|
|
91
|
+ view.isFocusableInTouchMode = true;
|
|
92
|
+ view.requestFocus();
|
|
93
|
+ view.setOnKeyListener { _, i, event ->
|
|
94
|
+ if (i == KeyEvent.KEYCODE_VOLUME_DOWN || i == KeyEvent.KEYCODE_VOLUME_UP) {
|
|
95
|
+ adjustAlarmVolume(i)
|
|
96
|
+ true
|
|
97
|
+ } else {
|
|
98
|
+ false
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
104
|
+ setPreferencesFromResource(R.xml.alarm_adjust_volume_preferences, rootKey)
|
|
105
|
+
|
|
106
|
+ val alarmVolumeSeekBar = findPreference<SeekBarPreference>("alarmVolume")
|
|
107
|
+ alarmVolumeSeekBar?.apply {
|
|
108
|
+ min = 0
|
|
109
|
+ max = 100
|
|
110
|
+ updatesContinuously = true
|
|
111
|
+ setOnPreferenceChangeListener { _, newValue ->
|
|
112
|
+ actionOnService(Actions.VOLUME, newValue as Int)
|
|
113
|
+ true
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+ private fun actionOnService(a: Actions, v: Int = 0)
|
|
121
|
+ {
|
|
122
|
+ val i = Intent(requireContext(), RadioService::class.java)
|
|
123
|
+ i.putExtra("action", a.name)
|
|
124
|
+ i.putExtra("value", v)
|
|
125
|
+ //[REMOVE LOG CALLS]Log.d(tag, "Sending intent ${a.name}")
|
|
126
|
+ requireContext().startService(i)
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+}
|