Browse Source

added button to test and adjust alarm volume.

yattoz 4 years ago
parent
commit
99ab795320

+ 0 - 3
.idea/codeStyles/Project.xml View File

1
 <component name="ProjectCodeStyleConfiguration">
1
 <component name="ProjectCodeStyleConfiguration">
2
   <code_scheme name="Project" version="173">
2
   <code_scheme name="Project" version="173">
3
-    <AndroidXmlCodeStyleSettings>
4
-      <option name="ARRANGEMENT_SETTINGS_MIGRATED_TO_191" value="true" />
5
-    </AndroidXmlCodeStyleSettings>
6
     <JetCodeStyleSettings>
3
     <JetCodeStyleSettings>
7
       <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
4
       <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
8
     </JetCodeStyleSettings>
5
     </JetCodeStyleSettings>

+ 2 - 1
.idea/gradle.xml View File

1
 <?xml version="1.0" encoding="UTF-8"?>
1
 <?xml version="1.0" encoding="UTF-8"?>
2
 <project version="4">
2
 <project version="4">
3
+  <component name="GradleMigrationSettings" migrationVersion="1" />
3
   <component name="GradleSettings">
4
   <component name="GradleSettings">
4
     <option name="linkedExternalProjectsSettings">
5
     <option name="linkedExternalProjectsSettings">
5
       <GradleProjectSettings>
6
       <GradleProjectSettings>
7
+        <option name="testRunner" value="PLATFORM" />
6
         <option name="distributionType" value="DEFAULT_WRAPPED" />
8
         <option name="distributionType" value="DEFAULT_WRAPPED" />
7
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
9
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
8
         <option name="modules">
10
         <option name="modules">
12
           </set>
14
           </set>
13
         </option>
15
         </option>
14
         <option name="resolveModulePerSourceSet" value="false" />
16
         <option name="resolveModulePerSourceSet" value="false" />
15
-        <option name="testRunner" value="PLATFORM" />
16
       </GradleProjectSettings>
17
       </GradleProjectSettings>
17
     </option>
18
     </option>
18
   </component>
19
   </component>

+ 30 - 0
.idea/jarRepositories.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="RemoteRepositoriesConfiguration">
4
+    <remote-repository>
5
+      <option name="id" value="central" />
6
+      <option name="name" value="Maven Central repository" />
7
+      <option name="url" value="https://repo1.maven.org/maven2" />
8
+    </remote-repository>
9
+    <remote-repository>
10
+      <option name="id" value="jboss.community" />
11
+      <option name="name" value="JBoss Community repository" />
12
+      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
13
+    </remote-repository>
14
+    <remote-repository>
15
+      <option name="id" value="BintrayJCenter" />
16
+      <option name="name" value="BintrayJCenter" />
17
+      <option name="url" value="https://jcenter.bintray.com/" />
18
+    </remote-repository>
19
+    <remote-repository>
20
+      <option name="id" value="maven" />
21
+      <option name="name" value="maven" />
22
+      <option name="url" value="https://jitpack.io" />
23
+    </remote-repository>
24
+    <remote-repository>
25
+      <option name="id" value="Google" />
26
+      <option name="name" value="Google" />
27
+      <option name="url" value="https://dl.google.com/dl/android/maven2/" />
28
+    </remote-repository>
29
+  </component>
30
+</project>

+ 3 - 3
app/build.gradle View File

16
 }
16
 }
17
 
17
 
18
 android {
18
 android {
19
-    compileSdkVersion 29
20
-    buildToolsVersion "29.0.2"
19
+    compileSdkVersion 30
20
+    buildToolsVersion "30.0.0"
21
     compileOptions {
21
     compileOptions {
22
         sourceCompatibility JavaVersion.VERSION_1_8
22
         sourceCompatibility JavaVersion.VERSION_1_8
23
         targetCompatibility JavaVersion.VERSION_1_8
23
         targetCompatibility JavaVersion.VERSION_1_8
28
     defaultConfig {
28
     defaultConfig {
29
         applicationId "fr.forum_thalie.tsumugi"
29
         applicationId "fr.forum_thalie.tsumugi"
30
         minSdkVersion 16
30
         minSdkVersion 16
31
-        targetSdkVersion 29
31
+        targetSdkVersion 30
32
         versionCode 115
32
         versionCode 115
33
         versionName "1.1.5"
33
         versionName "1.1.5"
34
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
34
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

+ 1 - 1
app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt View File

117
         }
117
         }
118
     }
118
     }
119
 
119
 
120
-    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
120
+    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
121
         super.onRestoreInstanceState(savedInstanceState ?: Bundle())
121
         super.onRestoreInstanceState(savedInstanceState ?: Bundle())
122
         // Now that BottomNavigationBar has restored its instance state
122
         // Now that BottomNavigationBar has restored its instance state
123
         // and its selectedItemId, we can proceed with setting up the
123
         // and its selectedItemId, we can proceed with setting up the

+ 33 - 0
app/src/main/java/fr/forum_thalie/tsumugi/alarm/RadioAlarmExtensions.kt View File

1
+package fr.forum_thalie.tsumugi.alarm
2
+
3
+import android.content.Context
4
+import android.content.Intent
5
+import fr.forum_thalie.tsumugi.Actions
6
+import fr.forum_thalie.tsumugi.RadioService
7
+
8
+
9
+fun playOnFallback(c: Context) {
10
+    val a = Actions.PLAY_OR_FALLBACK
11
+    val i = Intent(c, RadioService::class.java)
12
+    i.putExtra("action", a.name)
13
+    c.startService(i)
14
+}
15
+
16
+fun resume(c: Context)
17
+{
18
+
19
+}
20
+
21
+fun resumeStop(c: Context) {
22
+    val a = Actions.PLAY
23
+    val i = Intent(c, RadioService::class.java)
24
+    i.putExtra("action", a.name)
25
+    c.startService(i)
26
+}
27
+
28
+fun resumePlay(c: Context) {
29
+    val a = Actions.PLAY
30
+    val i = Intent(c, RadioService::class.java)
31
+    i.putExtra("action", a.name)
32
+    c.startService(i)
33
+}

+ 90 - 0
app/src/main/java/fr/forum_thalie/tsumugi/preferences/AlarmFragment.kt View File

2
 
2
 
3
 import android.app.TimePickerDialog
3
 import android.app.TimePickerDialog
4
 import android.content.Context
4
 import android.content.Context
5
+import android.content.Intent
6
+import android.media.AudioManager
5
 import android.os.Bundle
7
 import android.os.Bundle
6
 import android.util.Log
8
 import android.util.Log
9
+import android.view.KeyEvent
10
+import androidx.appcompat.app.AlertDialog
7
 import androidx.appcompat.app.AppCompatActivity
11
 import androidx.appcompat.app.AppCompatActivity
8
 import androidx.core.content.edit
12
 import androidx.core.content.edit
9
 import androidx.preference.*
13
 import androidx.preference.*
10
 import fr.forum_thalie.tsumugi.*
14
 import fr.forum_thalie.tsumugi.*
11
 import fr.forum_thalie.tsumugi.R
15
 import fr.forum_thalie.tsumugi.R
12
 import fr.forum_thalie.tsumugi.alarm.RadioAlarm
16
 import fr.forum_thalie.tsumugi.alarm.RadioAlarm
17
+import fr.forum_thalie.tsumugi.playerstore.PlayerStore
18
+import kotlinx.coroutines.CoroutineScope
19
+import kotlinx.coroutines.GlobalScope.coroutineContext
20
+import kotlinx.coroutines.Job
21
+import kotlinx.coroutines.delay
22
+import kotlinx.coroutines.launch
13
 import java.util.*
23
 import java.util.*
24
+import kotlin.coroutines.CoroutineContext
25
+import kotlin.math.max
26
+import kotlin.math.min
27
+
14
 
28
 
15
 class AlarmFragment : PreferenceFragmentCompat() {
29
 class AlarmFragment : PreferenceFragmentCompat() {
16
 
30
 
130
         }
144
         }
131
 
145
 
132
 
146
 
147
+        val testAlarmVolume: Preference? = findPreference("testAlarmVolume")
148
+        testAlarmVolume!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
149
+
150
+            // get previous state: if it's playing, we'll resume playing as multimedia; if it was stopped, we'll stop
151
+            val isPlayingMultimedia: Boolean = PlayerStore.instance.isPlaying.value ?: false
152
+
153
+            val builder1 = AlertDialog.Builder(requireContext())
154
+            builder1.setTitle(R.string.test_alarm_volume)
155
+            builder1.setMessage(R.string.popupTestAlarmVolume)
156
+            builder1.setCancelable(false)
157
+            builder1.setPositiveButton(
158
+                getString(R.string.finished)
159
+            ) { dialog, _ ->
160
+                // put the radio back to media sound, or off if it was off.
161
+                if (isPlayingMultimedia)
162
+                {
163
+                    actionOnService(Actions.PLAY)
164
+                } else {
165
+                    actionOnService(Actions.STOP)
166
+                }
167
+                dialog.cancel()
168
+            }
169
+
170
+            val adjustAlarmVolume: (Int) -> Unit = debounce<Int>(50, coroutineContext) {
171
+                val keyCode = it
172
+
173
+                val audioManager = requireContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
174
+                val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM)
175
+                val minVolume = 0 // audioManager.getStreamMinVolume(AudioManager.STREAM_ALARM) <- require API28+
176
+                val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM)
177
+                Log.d(tag, "current, max = $currentVolume, $maxVolume")
178
+                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
179
+                    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, max(currentVolume - 1, minVolume), AudioManager.FLAG_SHOW_UI)
180
+
181
+                } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
182
+                    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, min(currentVolume + 1, maxVolume), AudioManager.FLAG_SHOW_UI)
183
+                }
184
+
185
+            }
186
+
187
+            builder1.setOnKeyListener { dialogInterface, i, event ->
188
+                adjustAlarmVolume(i)
189
+                true
190
+            }
191
+
192
+            val alert11 = builder1.create()
193
+
194
+            // start as alarm
195
+            actionOnService(Actions.PLAY_OR_FALLBACK)
196
+
197
+            alert11.show()
198
+
199
+            true
200
+        }
201
+
133
         alarmDays?.isEnabled = isWakingUp?.isChecked ?: false
202
         alarmDays?.isEnabled = isWakingUp?.isChecked ?: false
134
         timeSet?.isEnabled = isWakingUp?.isChecked ?: false
203
         timeSet?.isEnabled = isWakingUp?.isChecked ?: false
135
         snoozeDuration?.isEnabled = isWakingUp?.isChecked ?: false
204
         snoozeDuration?.isEnabled = isWakingUp?.isChecked ?: false
136
 
205
 
137
     }
206
     }
138
 
207
 
208
+    private fun actionOnService (a: Actions) {
209
+        // start as alarm
210
+        val i = Intent(requireContext(), RadioService::class.java)
211
+        i.putExtra("action", a.name)
212
+        requireContext().startService(i)
213
+    }
214
+
215
+    private fun <T> debounce(delayMs: Long = 500L,
216
+                             coroutineContext: CoroutineContext,
217
+                             f: (T) -> Unit): (T) -> Unit {
218
+        var debounceJob: Job? = null
219
+        return { param: T ->
220
+            if (debounceJob?.isCompleted != false) {
221
+                debounceJob = CoroutineScope(coroutineContext).launch {
222
+                    delay(delayMs)
223
+                    f(param)
224
+                }
225
+            }
226
+        }
227
+    }
228
+
139
 
229
 
140
 }
230
 }

+ 4 - 0
app/src/main/res/values-fr/strings.xml View File

56
     <string name="action_refresh">Raffraîchir les données</string>
56
     <string name="action_refresh">Raffraîchir les données</string>
57
     <string name="autoPlay">\"Play\" au branchement de la prise casque</string>
57
     <string name="autoPlay">\"Play\" au branchement de la prise casque</string>
58
     <string name="settings">Paramètres</string>
58
     <string name="settings">Paramètres</string>
59
+    <string name="test_alarm_volume">Tester et ajuster le volume de l\'alarme</string>
60
+    <string name="test_alarm_volume_summary">Touchez pour écouter et régler le volume</string>
61
+    <string name="popupTestAlarmVolume">La radio est à présent lancée en mode alarme. Ajustez le volume avec les boutons de volume de votre appareil.</string>
62
+    <string name="finished">Terminé</string>
59
 
63
 
60
 </resources>
64
 </resources>

+ 4 - 0
app/src/main/res/values/strings.xml View File

76
     <string name="action_refresh">Refresh data</string>
76
     <string name="action_refresh">Refresh data</string>
77
     <string name="autoPlay">Auto play when plugging headphones</string>
77
     <string name="autoPlay">Auto play when plugging headphones</string>
78
     <string name="settings">Settings</string>
78
     <string name="settings">Settings</string>
79
+    <string name="test_alarm_volume">Test and change alarm volume</string>
80
+    <string name="test_alarm_volume_summary">Touch this to listen to the alarm volume and set it up</string>
81
+    <string name="popupTestAlarmVolume">The radio stream is now started in alarm mode. Adjust the volume with the volume buttons on your device.</string>
82
+    <string name="finished">Done</string>
79
 
83
 
80
 </resources>
84
 </resources>

+ 6 - 0
app/src/main/res/xml/alarm_preferences.xml View File

37
         android:summary="%s"
37
         android:summary="%s"
38
         />
38
         />
39
 
39
 
40
+    <Preference
41
+        app:iconSpaceReserved="false"
42
+        android:key="testAlarmVolume"
43
+        android:title="@string/test_alarm_volume"
44
+        android:summary="@string/test_alarm_volume_summary" />
45
+
40
 </PreferenceScreen>
46
 </PreferenceScreen>