Use Ringtone instead of MediaPlayer directly

(Copied from rhodey's PR)

Fixes #3340
Closes #3342

// FREEBIe
This commit is contained in:
Moxie Marlinspike 2015-12-08 04:40:26 -08:00
parent c7dd956f45
commit 81fa209910

View File

@ -23,10 +23,14 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Spannable;
@ -265,7 +269,6 @@ public class MessageNotifier {
}
private static void sendInThreadNotification(Context context, Recipients recipients) {
try {
if (!TextSecurePreferences.isInThreadNotifications(context)) {
return;
}
@ -288,29 +291,17 @@ public class MessageNotifier {
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();
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
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);
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);
}
});
player.start();
} catch (IOException ioe) {
Log.w("MessageNotifier", ioe);
}
ringtone.play();
}
private static void appendPushNotificationState(@NonNull Context context,