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.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,7 +269,6 @@ 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;
} }
@ -288,29 +291,17 @@ public class MessageNotifier {
return; return;
} }
MediaPlayer player = new MediaPlayer(); Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
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)); if (Build.VERSION.SDK_INT >= 21) {
ringtone.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); .build());
} else {
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { ringtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
@Override
public void onCompletion(MediaPlayer mp) {
audioManager.abandonAudioFocus(null);
} }
});
player.start(); ringtone.play();
} catch (IOException ioe) {
Log.w("MessageNotifier", ioe);
}
} }
private static void appendPushNotificationState(@NonNull Context context, private static void appendPushNotificationState(@NonNull Context context,