From 212588cd901e6793bc8c13abfea54a988ba8b16e Mon Sep 17 00:00:00 2001 From: Rien Maertens Date: Sun, 5 Jul 2020 12:39:06 +0200 Subject: [PATCH 1/2] Fix possibly error in alarm notification --- .../com/simplemobiletools/clock/extensions/Context.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 48e21408..9d9b6fab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -314,9 +314,7 @@ fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent { @SuppressLint("NewApi") fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification { var soundUri = alarm.soundUri - if (soundUri == SILENT) { - soundUri = "" - } else { + if (soundUri != SILENT) { grantReadUriPermission(soundUri) } @@ -350,12 +348,15 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No .setPriority(Notification.PRIORITY_HIGH) .setDefaults(Notification.DEFAULT_LIGHTS) .setAutoCancel(true) - .setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM) .setChannelId(channelId) .addAction(R.drawable.ic_snooze_vector, getString(R.string.snooze), getSnoozePendingIntent(alarm)) .addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), getHideAlarmPendingIntent(alarm)) - builder.setVisibility(Notification.VISIBILITY_PUBLIC) + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + if (soundUri != SILENT) { + builder.setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM); + } if (alarm.vibrate) { val vibrateArray = LongArray(2) { 500 } From f7cc9568dd899518560b1c3513125107a0e4db9b Mon Sep 17 00:00:00 2001 From: Rien Maertens Date: Sun, 5 Jul 2020 12:39:47 +0200 Subject: [PATCH 2/2] Fix ReminderActivity: vibration and audio error - There was no code to vibrate the device, this was added - If 'no sound' was selected as the alarm audio source, it would show an error toast saying "setDataSource failed" because the URL was "silent" Fixes #139, #189 --- .../clock/activities/ReminderActivity.kt | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt index 4dc06c47..0abae786 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -1,12 +1,12 @@ package com.simplemobiletools.clock.activities import android.annotation.SuppressLint +import android.content.Context import android.content.Intent import android.media.AudioManager import android.media.MediaPlayer import android.net.Uri -import android.os.Bundle -import android.os.Handler +import android.os.* import android.view.MotionEvent import android.view.ViewGroup import android.view.animation.AnimationUtils @@ -17,6 +17,7 @@ import com.simplemobiletools.clock.helpers.getPassedSeconds import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.MINUTE_SECONDS +import com.simplemobiletools.commons.helpers.SILENT import com.simplemobiletools.commons.helpers.isOreoPlus import kotlinx.android.synthetic.main.activity_reminder.* @@ -30,6 +31,7 @@ class ReminderActivity : SimpleActivity() { private var didVibrate = false private var alarm: Alarm? = null private var mediaPlayer: MediaPlayer? = null + private var vibrator: Vibrator? = null; private var lastVolumeValue = 0.1f private var dragDownX = 0f @@ -64,7 +66,7 @@ class ReminderActivity : SimpleActivity() { }, maxDuration * 1000L) setupButtons() - setupAudio() + setupEffects() } private fun setupButtons() { @@ -155,27 +157,40 @@ class ReminderActivity : SimpleActivity() { } } - private fun setupAudio() { + private fun setupEffects() { if (!isAlarmReminder || !config.increaseVolumeGradually) { lastVolumeValue = 1f } - val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri) - try { - mediaPlayer = MediaPlayer().apply { - setAudioStreamType(AudioManager.STREAM_ALARM) - setDataSource(this@ReminderActivity, soundUri) - setVolume(lastVolumeValue, lastVolumeValue) - isLooping = true - prepare() - start() + val doVibrate = if (alarm != null) alarm!!.vibrate else config.timerVibrate; + if (doVibrate) { + val pattern = LongArray(2){ 500 }; + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0)) + } else { + vibrator?.vibrate(pattern, 0); } - } catch (e: Exception) { - showErrorToast(e) } - if (config.increaseVolumeGradually) { - scheduleVolumeIncrease() + val soundUri = if (alarm != null) alarm!!.soundUri else config.timerSoundUri; + if (soundUri != SILENT) { + try { + mediaPlayer = MediaPlayer().apply { + setAudioStreamType(AudioManager.STREAM_ALARM) + setDataSource(this@ReminderActivity, Uri.parse(soundUri)) + setVolume(lastVolumeValue, lastVolumeValue) + isLooping = true + prepare() + start() + } + } catch (e: Exception) { + showErrorToast(e) + } + + if (config.increaseVolumeGradually) { + scheduleVolumeIncrease() + } } } @@ -197,17 +212,19 @@ class ReminderActivity : SimpleActivity() { increaseVolumeHandler.removeCallbacksAndMessages(null) maxReminderDurationHandler.removeCallbacksAndMessages(null) swipeGuideFadeHandler.removeCallbacksAndMessages(null) - destroyPlayer() + destroyEffects() } - private fun destroyPlayer() { + private fun destroyEffects() { mediaPlayer?.stop() mediaPlayer?.release() mediaPlayer = null + vibrator?.cancel(); + vibrator = null; } private fun snoozeAlarm() { - destroyPlayer() + destroyEffects() if (config.useSameSnooze) { setupAlarmClock(alarm!!, config.snoozeTime * MINUTE_SECONDS) finishActivity() @@ -225,7 +242,7 @@ class ReminderActivity : SimpleActivity() { scheduleNextAlarm(alarm!!, false) } - destroyPlayer() + destroyEffects() finish() overridePendingTransition(0, 0) }