Android O repeat sound
This commit is contained in:
parent
f8effe7d1e
commit
eb62873fb6
@ -36,6 +36,8 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<receiver android:name=".service.BroadcastReceiverService" android:exported="false" />
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -102,8 +102,6 @@ public class SCNApp extends Application implements LifecycleObserver
|
|||||||
/*
|
/*
|
||||||
==TODO==
|
==TODO==
|
||||||
|
|
||||||
[ ] - Android O repeat sound
|
|
||||||
[ ] - notifications: how does WA do it??? - there you can change shit in-app
|
|
||||||
[ ] - test notification channels
|
[ ] - test notification channels
|
||||||
|
|
||||||
[ ] - Delete single message (swipe right)
|
[ ] - Delete single message (swipe right)
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,6 @@ import com.blackforestbytes.simplecloudnotifier.model.CMessage;
|
|||||||
import com.blackforestbytes.simplecloudnotifier.model.NotificationSettings;
|
import com.blackforestbytes.simplecloudnotifier.model.NotificationSettings;
|
||||||
import com.blackforestbytes.simplecloudnotifier.model.PriorityEnum;
|
import com.blackforestbytes.simplecloudnotifier.model.PriorityEnum;
|
||||||
import com.blackforestbytes.simplecloudnotifier.model.SCNSettings;
|
import com.blackforestbytes.simplecloudnotifier.model.SCNSettings;
|
||||||
import com.blackforestbytes.simplecloudnotifier.model.SoundService;
|
|
||||||
import com.blackforestbytes.simplecloudnotifier.view.MainActivity;
|
import com.blackforestbytes.simplecloudnotifier.view.MainActivity;
|
||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
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)
|
if (channel1 == null)
|
||||||
{
|
{
|
||||||
channel1 = new NotificationChannel(CHANNEL_P1_ID, "Push notifications (normal priority)", NotificationManager.IMPORTANCE_DEFAULT);
|
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)
|
if (channel2 == null)
|
||||||
{
|
{
|
||||||
channel2 = new NotificationChannel(CHANNEL_P1_ID, "Push notifications (high priority)", NotificationManager.IMPORTANCE_DEFAULT);
|
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)
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
||||||
{
|
{
|
||||||
// old
|
// old
|
||||||
|
showBackground_old(msg, ctxt, ns, msg.Priority);
|
||||||
showBackground_old(msg, ctxt, ns);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// new
|
// new
|
||||||
|
showBackground_new(msg, ctxt, ns, msg.Priority);
|
||||||
showBackground_new(msg, ctxt, ns);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,16 +219,17 @@ public class NotificationService
|
|||||||
NotificationManager mNotificationManager = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager mNotificationManager = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
if (mNotificationManager == null) return;
|
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.EnableSound && !Str.isNullOrWhitespace(ns.SoundSource))
|
||||||
{
|
{
|
||||||
if (ns.RepeatSound)
|
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
|
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)
|
if (ns.EnableVibration)
|
||||||
{
|
{
|
||||||
Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE);
|
Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE);
|
||||||
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user