mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-24 08:47:46 +00:00
Use Ringtone instead of MediaPlayer directly
(Copied from rhodey's PR) Fixes #3340 Closes #3342 // FREEBIe
This commit is contained in:
parent
c7dd956f45
commit
81fa209910
@ -23,10 +23,14 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.media.AudioAttributes;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
import android.media.Ringtone;
|
||||||
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
@ -265,52 +269,39 @@ public class MessageNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void sendInThreadNotification(Context context, Recipients recipients) {
|
private static void sendInThreadNotification(Context context, Recipients recipients) {
|
||||||
try {
|
if (!TextSecurePreferences.isInThreadNotifications(context)) {
|
||||||
if (!TextSecurePreferences.isInThreadNotifications(context)) {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uri uri = recipients != null ? recipients.getRingtone() : null;
|
|
||||||
|
|
||||||
if (uri == null) {
|
|
||||||
String ringtone = TextSecurePreferences.getNotificationRingtone(context);
|
|
||||||
|
|
||||||
if (ringtone == null) {
|
|
||||||
Log.w(TAG, "ringtone preference was null.");
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
uri = Uri.parse(ringtone);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uri == null) {
|
|
||||||
Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MediaPlayer player = new MediaPlayer();
|
|
||||||
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
|
|
||||||
player.setDataSource(context, uri);
|
|
||||||
player.setLooping(false);
|
|
||||||
player.setVolume(0.25f, 0.25f);
|
|
||||||
player.prepare();
|
|
||||||
|
|
||||||
final AudioManager audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
|
|
||||||
|
|
||||||
audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
|
|
||||||
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
|
|
||||||
|
|
||||||
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
||||||
@Override
|
|
||||||
public void onCompletion(MediaPlayer mp) {
|
|
||||||
audioManager.abandonAudioFocus(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
player.start();
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.w("MessageNotifier", ioe);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uri uri = recipients != null ? recipients.getRingtone() : null;
|
||||||
|
|
||||||
|
if (uri == null) {
|
||||||
|
String ringtone = TextSecurePreferences.getNotificationRingtone(context);
|
||||||
|
|
||||||
|
if (ringtone == null) {
|
||||||
|
Log.w(TAG, "ringtone preference was null.");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
uri = Uri.parse(ringtone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uri == null) {
|
||||||
|
Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= 21) {
|
||||||
|
ringtone.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
|
||||||
|
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
|
||||||
|
.build());
|
||||||
|
} else {
|
||||||
|
ringtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
ringtone.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void appendPushNotificationState(@NonNull Context context,
|
private static void appendPushNotificationState(@NonNull Context context,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user