From eb62873fb63ec47e670534e215a489e820675222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 17 Nov 2018 00:52:44 +0100 Subject: [PATCH] Android O repeat sound --- android/app/src/main/AndroidManifest.xml | 2 + .../simplecloudnotifier/SCNApp.java | 2 - .../model/SoundService.java | 43 ------------ .../service/BroadcastReceiverService.java | 29 ++++++++ .../service/NotificationService.java | 29 ++++---- .../service/SoundService.java | 67 +++++++++++++++++++ 6 files changed, 114 insertions(+), 58 deletions(-) delete mode 100644 android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SoundService.java create mode 100644 android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/BroadcastReceiverService.java create mode 100644 android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/SoundService.java diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index cf2b50d..46aa758 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -36,6 +36,8 @@ + + \ No newline at end of file diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java index d982865..a487f97 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java @@ -102,8 +102,6 @@ public class SCNApp extends Application implements LifecycleObserver /* ==TODO== -[ ] - Android O repeat sound -[ ] - notifications: how does WA do it??? - there you can change shit in-app [ ] - test notification channels [ ] - Delete single message (swipe right) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SoundService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SoundService.java deleted file mode 100644 index 9fff131..0000000 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SoundService.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.blackforestbytes.simplecloudnotifier.model; - -import android.content.Context; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.Ringtone; -import android.media.RingtoneManager; -import android.net.Uri; -import android.os.Build; - -import com.blackforestbytes.simplecloudnotifier.SCNApp; -import com.blackforestbytes.simplecloudnotifier.lib.android.ThreadUtils; -import com.blackforestbytes.simplecloudnotifier.lib.string.Str; - -public class SoundService -{ - public static void playForegroundNoLooping(boolean enableSound, String soundSource, boolean forceVolume, int forceVolumeValue) - { - if (!enableSound) return; - if (Str.isNullOrWhitespace(soundSource)) return; - - if (forceVolume) - { - AudioManager aman = (AudioManager) SCNApp.getContext().getSystemService(Context.AUDIO_SERVICE); - int maxVolume = aman.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - aman.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(maxVolume * (forceVolumeValue / 100.0)), 0); - - MediaPlayer player = MediaPlayer.create(SCNApp.getMainActivity(), Uri.parse(soundSource)); - player.setLooping(false); - player.setOnCompletionListener( mp -> { mp.stop(); mp.release(); }); - player.setOnSeekCompleteListener(mp -> { mp.stop(); mp.release(); }); - player.start(); - } - else - { - Ringtone rt = RingtoneManager.getRingtone(SCNApp.getContext(), Uri.parse(soundSource)); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) rt.setLooping(false); - rt.play(); - - new Thread(() -> { ThreadUtils.safeSleep(5*1000); rt.stop(); }).start(); - } - } -} diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/BroadcastReceiverService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/BroadcastReceiverService.java new file mode 100644 index 0000000..8181504 --- /dev/null +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/BroadcastReceiverService.java @@ -0,0 +1,29 @@ +package com.blackforestbytes.simplecloudnotifier.service; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +public class BroadcastReceiverService extends BroadcastReceiver +{ + public static final int STOP_NOTIFICATION_SOUND = 10022; + public static final String ID_KEY = "com.blackforestbytes.simplecloudnotifier.BroadcastID"; + + @Override + public void onReceive(Context context, Intent intent) + { + if (intent == null) return; + Bundle extras = intent.getExtras(); + if (extras == null) return; + int notificationId = extras.getInt(ID_KEY, 0); + + if (notificationId == 10022) stopNotificationSound(); + else return; + } + + private void stopNotificationSound() + { + SoundService.stopPlaying(); + } +} diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java index c01aa13..b1b4164 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java @@ -21,7 +21,6 @@ import com.blackforestbytes.simplecloudnotifier.model.CMessage; import com.blackforestbytes.simplecloudnotifier.model.NotificationSettings; import com.blackforestbytes.simplecloudnotifier.model.PriorityEnum; import com.blackforestbytes.simplecloudnotifier.model.SCNSettings; -import com.blackforestbytes.simplecloudnotifier.model.SoundService; import com.blackforestbytes.simplecloudnotifier.view.MainActivity; import androidx.annotation.RequiresApi; @@ -70,7 +69,7 @@ public class NotificationService } } { - NotificationChannel channel1 = notifman.getNotificationChannel(CHANNEL_P0_ID); + NotificationChannel channel1 = notifman.getNotificationChannel(CHANNEL_P1_ID); if (channel1 == null) { channel1 = new NotificationChannel(CHANNEL_P1_ID, "Push notifications (normal priority)", NotificationManager.IMPORTANCE_DEFAULT); @@ -82,7 +81,7 @@ public class NotificationService } } { - NotificationChannel channel2 = notifman.getNotificationChannel(CHANNEL_P0_ID); + NotificationChannel channel2 = notifman.getNotificationChannel(CHANNEL_P2_ID); if (channel2 == null) { channel2 = new NotificationChannel(CHANNEL_P1_ID, "Push notifications (high priority)", NotificationManager.IMPORTANCE_DEFAULT); @@ -142,14 +141,12 @@ public class NotificationService if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // old - - showBackground_old(msg, ctxt, ns); + showBackground_old(msg, ctxt, ns, msg.Priority); } else { // new - - showBackground_new(msg, ctxt, ns); + showBackground_new(msg, ctxt, ns, msg.Priority); } } @@ -222,16 +219,17 @@ public class NotificationService NotificationManager mNotificationManager = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE); if (mNotificationManager == null) return; - Notification n = mBuilder.build(); - n.flags |= Notification.FLAG_AUTO_CANCEL; - - mNotificationManager.notify(0, n); - if (ns.EnableSound && !Str.isNullOrWhitespace(ns.SoundSource)) { if (ns.RepeatSound) { - //TODO + Intent intnt_stop = new Intent(SCNApp.getContext(), BroadcastReceiverService.class); + intnt_stop.putExtra(BroadcastReceiverService.ID_KEY, BroadcastReceiverService.STOP_NOTIFICATION_SOUND); + PendingIntent pi_stop = PendingIntent.getBroadcast(SCNApp.getContext().getApplicationContext(), BroadcastReceiverService.STOP_NOTIFICATION_SOUND, intnt_stop, 0); + mBuilder.addAction(new NotificationCompat.Action(-1, "Stop", pi_stop)); + mBuilder.setDeleteIntent(pi_stop); + + SoundService.playForegroundWithLooping(ns.EnableSound, ns.SoundSource, ns.ForceVolume, ns.ForceVolumeValue); } else { @@ -239,6 +237,11 @@ public class NotificationService } } + Notification n = mBuilder.build(); + n.flags |= Notification.FLAG_AUTO_CANCEL; + + mNotificationManager.notify(0, n); + if (ns.EnableVibration) { Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE); diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/SoundService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/SoundService.java new file mode 100644 index 0000000..d6a079e --- /dev/null +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/SoundService.java @@ -0,0 +1,67 @@ +package com.blackforestbytes.simplecloudnotifier.service; + +import android.content.Context; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.media.Ringtone; +import android.media.RingtoneManager; +import android.net.Uri; +import android.os.Build; + +import com.blackforestbytes.simplecloudnotifier.SCNApp; +import com.blackforestbytes.simplecloudnotifier.lib.android.ThreadUtils; +import com.blackforestbytes.simplecloudnotifier.lib.string.Str; + +public class SoundService +{ + private static MediaPlayer mpLast = null; + + public static void playForegroundNoLooping(boolean enableSound, String soundSource, boolean forceVolume, int forceVolumeValue) + { + if (!enableSound) return; + if (Str.isNullOrWhitespace(soundSource)) return; + + stopPlaying(); + + if (forceVolume) + { + AudioManager aman = (AudioManager) SCNApp.getContext().getSystemService(Context.AUDIO_SERVICE); + int maxVolume = aman.getStreamMaxVolume(AudioManager.STREAM_MUSIC); + aman.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(maxVolume * (forceVolumeValue / 100.0)), 0); + } + + MediaPlayer player = MediaPlayer.create(SCNApp.getMainActivity(), Uri.parse(soundSource)); + player.setLooping(false); + player.setOnCompletionListener( mp -> { mp.stop(); mp.release(); }); + player.setOnSeekCompleteListener(mp -> { mp.stop(); mp.release(); }); + player.start(); + mpLast = player; + } + + public static void playForegroundWithLooping(boolean enableSound, String soundSource, boolean forceVolume, int forceVolumeValue) + { + if (!enableSound) return; + if (Str.isNullOrWhitespace(soundSource)) return; + + stopPlaying(); + + if (forceVolume) + { + AudioManager aman = (AudioManager) SCNApp.getContext().getSystemService(Context.AUDIO_SERVICE); + int maxVolume = aman.getStreamMaxVolume(AudioManager.STREAM_MUSIC); + aman.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(maxVolume * (forceVolumeValue / 100.0)), 0); + } + + MediaPlayer player = MediaPlayer.create(SCNApp.getMainActivity(), Uri.parse(soundSource)); + player.setLooping(true); + player.setOnCompletionListener( mp -> { mp.stop(); mp.release(); }); + player.setOnSeekCompleteListener(mp -> { mp.stop(); mp.release(); }); + player.start(); + mpLast = player; + } + + public static void stopPlaying() + { + if (mpLast != null && mpLast.isPlaying()) { mpLast.stop(); mpLast.release(); mpLast = null; } + } +}