From 859fa2a89fbd558129ce8cae021891c6b24a3a7a Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Thu, 27 Oct 2022 17:28:27 +1300 Subject: [PATCH] Add setting to disable sound on rest timers Closes #50 --- EditSet.tsx | 1 + SettingsPage.tsx | 11 ++++++++++ .../src/main/java/com/massive/AlarmModule.kt | 3 ++- .../src/main/java/com/massive/AlarmService.kt | 7 ++++-- .../src/main/java/com/massive/TimerService.kt | 22 ++++++++++++------- db.ts | 19 +++++++++------- settings.ts | 1 + 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/EditSet.tsx b/EditSet.tsx index 7c1046f..f330962 100644 --- a/EditSet.tsx +++ b/EditSet.tsx @@ -26,6 +26,7 @@ export default function EditSet() { milliseconds, !!settings.vibrate, settings.sound, + !!settings.noSound, ); }, [settings], diff --git a/SettingsPage.tsx b/SettingsPage.tsx index 4d1cd8e..ccbc100 100644 --- a/SettingsPage.tsx +++ b/SettingsPage.tsx @@ -33,6 +33,7 @@ export default function SettingsPage() { showSets, theme, alarm, + noSound, } = settings; const {color, setColor} = useColor(); const {toast} = useSnackbar(); @@ -139,9 +140,19 @@ export default function SettingsPage() { [toast, update], ); + const changeNoSound = useCallback( + (enabled: boolean) => { + update(enabled, 'noSound'); + if (enabled) toast('Disable sound on rest timer alarms.', 4000); + else toast('Enabled sound for rest timer alarms.', 4000); + }, + [toast, update], + ); + const switches: Input[] = [ {name: 'Rest timers', value: !!alarm, onChange: changeAlarmEnabled}, {name: 'Vibrate', value: !!vibrate, onChange: changeVibrate}, + {name: 'Disable sound', value: !!noSound, onChange: changeNoSound}, {name: 'Record notifications', value: !!notify, onChange: changeNotify}, {name: 'Show images', value: !!images, onChange: changeImages}, {name: 'Show unit', value: !!showUnit, onChange: changeUnit}, diff --git a/android/app/src/main/java/com/massive/AlarmModule.kt b/android/app/src/main/java/com/massive/AlarmModule.kt index d2a30d0..7c4b0ca 100644 --- a/android/app/src/main/java/com/massive/AlarmModule.kt +++ b/android/app/src/main/java/com/massive/AlarmModule.kt @@ -48,12 +48,13 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) : @RequiresApi(api = Build.VERSION_CODES.O) @ReactMethod - fun timer(milliseconds: Int, vibrate: Boolean, sound: String?) { + fun timer(milliseconds: Int, vibrate: Boolean, sound: String?, noSound: Boolean = false) { Log.d("AlarmModule", "Queue alarm for $milliseconds delay") val intent = Intent(reactApplicationContext, TimerService::class.java) intent.putExtra("milliseconds", milliseconds) intent.putExtra("vibrate", vibrate) intent.putExtra("sound", sound) + intent.putExtra("noSound", noSound) reactApplicationContext.startService(intent) } diff --git a/android/app/src/main/java/com/massive/AlarmService.kt b/android/app/src/main/java/com/massive/AlarmService.kt index 83a795a..9b2f705 100644 --- a/android/app/src/main/java/com/massive/AlarmService.kt +++ b/android/app/src/main/java/com/massive/AlarmService.kt @@ -21,11 +21,13 @@ class AlarmService : Service(), OnPreparedListener { return START_STICKY } val sound = intent.extras?.getString("sound") - if (sound == null) { + val noSound = intent.extras?.getBoolean("noSound") == true + + if (sound == null && !noSound) { mediaPlayer = MediaPlayer.create(applicationContext, R.raw.argon) mediaPlayer?.start() mediaPlayer?.setOnCompletionListener { vibrator?.cancel() } - } else { + } else if (sound != null && !noSound) { mediaPlayer = MediaPlayer().apply { setAudioAttributes( AudioAttributes.Builder() @@ -39,6 +41,7 @@ class AlarmService : Service(), OnPreparedListener { setOnCompletionListener { vibrator?.cancel() } } } + val pattern = longArrayOf(0, 300, 1300, 300, 1300, 300) vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val vibratorManager = diff --git a/android/app/src/main/java/com/massive/TimerService.kt b/android/app/src/main/java/com/massive/TimerService.kt index 99fe279..f613b98 100644 --- a/android/app/src/main/java/com/massive/TimerService.kt +++ b/android/app/src/main/java/com/massive/TimerService.kt @@ -20,11 +20,13 @@ class TimerService : Service() { private var endMs: Int = 0 private var currentMs: Long = 0 private var vibrate: Boolean = true + private var noSound: Boolean = false private var sound: String? = null @RequiresApi(Build.VERSION_CODES.O) override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { vibrate = intent?.extras?.getBoolean("vibrate") == true + noSound = intent?.extras?.getBoolean("noSound") == true sound = intent?.extras?.getString("sound") val manager = getManager() manager.cancel(NOTIFICATION_ID_DONE) @@ -93,9 +95,11 @@ class TimerService : Service() { val manager = getManager() manager.notify(NOTIFICATION_ID_DONE, builder.build()) manager.cancel(NOTIFICATION_ID_PENDING) - val alarmIntent = Intent(applicationContext, AlarmService::class.java) - alarmIntent.putExtra("vibrate", vibrate) - alarmIntent.putExtra("sound", sound) + val alarmIntent = Intent(applicationContext, AlarmService::class.java).apply { + putExtra("vibrate", vibrate) + putExtra("sound", sound) + putExtra("noSound", noSound) + } applicationContext.startService(alarmIntent) } } @@ -124,11 +128,13 @@ class TimerService : Service() { val stopIntent = Intent(context, StopTimer::class.java) val pendingStop = PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE) - val addIntent = Intent(context, TimerService::class.java) - addIntent.action = "add" - addIntent.putExtra("vibrate", vibrate) - addIntent.putExtra("sound", sound) - addIntent.data = Uri.parse("$currentMs") + val addIntent = Intent(context, TimerService::class.java).apply { + action = "add" + putExtra("vibrate", vibrate) + putExtra("sound", sound) + putExtra("noSound", noSound) + data = Uri.parse("$currentMs") + } val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE) } else { diff --git a/db.ts b/db.ts index bed371e..7ee8fcc 100644 --- a/db.ts +++ b/db.ts @@ -41,16 +41,16 @@ const migrations = [ ) `, ` - ALTER TABLE sets ADD COLUMN hidden DEFAULT 0 + ALTER TABLE sets ADD COLUMN hidden DEFAULT false `, ` - ALTER TABLE settings ADD COLUMN notify DEFAULT 0 + ALTER TABLE settings ADD COLUMN notify DEFAULT false `, ` ALTER TABLE sets ADD COLUMN image TEXT NULL `, ` - ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT 1 + ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT true `, ` SELECT * FROM settings LIMIT 1 @@ -74,7 +74,7 @@ const migrations = [ ALTER TABLE sets ADD COLUMN seconds INTEGER NOT NULL DEFAULT 30 `, ` - ALTER TABLE settings ADD COLUMN showUnit BOOLEAN DEFAULT 1 + ALTER TABLE settings ADD COLUMN showUnit BOOLEAN DEFAULT true `, ` ALTER TABLE sets ADD COLUMN steps TEXT NULL @@ -94,10 +94,10 @@ const migrations = [ UPDATE settings SET showUnit = 1 `, ` - ALTER TABLE settings ADD COLUMN workouts BOOLEAN DEFAULT 1 + ALTER TABLE settings ADD COLUMN workouts BOOLEAN DEFAULT true `, ` - ALTER TABLE settings ADD COLUMN steps BOOLEAN DEFAULT 1 + ALTER TABLE settings ADD COLUMN steps BOOLEAN DEFAULT true `, ` ALTER TABLE settings ADD COLUMN nextAlarm TEXT NULL @@ -109,17 +109,20 @@ const migrations = [ ALTER TABLE settings ADD COLUMN date TEXT NULL `, ` - ALTER TABLE settings ADD COLUMN showDate BOOLEAN DEFAULT 0 + ALTER TABLE settings ADD COLUMN showDate BOOLEAN DEFAULT false `, ` ALTER TABLE settings ADD COLUMN theme TEXT `, ` - ALTER TABLE settings ADD COLUMN showSets BOOLEAN DEFAULT 1 + ALTER TABLE settings ADD COLUMN showSets BOOLEAN DEFAULT true `, ` CREATE INDEX sets_created ON sets(created) `, + ` + ALTER TABLE settings ADD COLUMN noSound BOOLEAN DEFAULT false + `, ]; export let db: SQLiteDatabase; diff --git a/settings.ts b/settings.ts index 6a422e6..4c4715f 100644 --- a/settings.ts +++ b/settings.ts @@ -11,4 +11,5 @@ export default interface Settings { showDate: number; theme: 'system' | 'dark' | 'light'; showSets: number; + noSound: number; }