From c05f13afac3eaefa1759fb3c074dd9d8d7f38a95 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Thu, 1 Feb 2018 11:47:34 +0100 Subject: [PATCH] Loop ringtone in FindMyPhonePlugin Summary: BUG: 368089 FindMyPhone does not loop ringtones, which makes problems with short ringtones. Additionally, it depends on the volume of the alarm channel, so if the alarms are muted, no sound can be heard. This patch raises the alarm volume to max when the activity gets started and restores the previous value when it stops. Test Plan: Trigger findmyphone, hear sound looping. Set alarm volume to zero and check if it gets restored. Reviewers: #kde_connect, mtijink Reviewed By: #kde_connect, mtijink Subscribers: mtijink, #kde_connect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D9947 --- .../FindMyPhoneActivity.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java index fa4f6a77..3d1c7fcc 100644 --- a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java +++ b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java @@ -1,10 +1,10 @@ package org.kde.kdeconnect.Plugins.FindMyPhonePlugin; import android.app.Activity; +import android.content.Context; import android.content.Intent; -import android.media.AudioAttributes; import android.media.AudioManager; -import android.media.Ringtone; +import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; @@ -16,13 +16,16 @@ import android.view.WindowManager; import org.kde.kdeconnect_tp.R; public class FindMyPhoneActivity extends Activity { - Ringtone ringtone; + + private MediaPlayer mediaPlayer; + private int previousVolume; + private AudioManager audioManager; @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); - if(ringtone != null) { + if (mediaPlayer != null) { // If this activity was already open and we received the ring packet again, just finish it finish(); } @@ -34,10 +37,12 @@ public class FindMyPhoneActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_my_phone); + audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | - WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | - WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); findViewById(R.id.bFindMyPhone).setOnClickListener(new View.OnClickListener() { @Override @@ -51,36 +56,37 @@ public class FindMyPhoneActivity extends Activity { protected void onStart() { super.onStart(); - Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); - ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri); - if (ringtone == null) { - ringtoneUri = RingtoneManager.getValidRingtoneUri(getApplicationContext()); - if (ringtoneUri == null) { - Log.e("FindMyPhone", "Could not find a ringtone to play!"); - return; + try { + // Make sure we are heard even when the phone is silent, restore original volume later + previousVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); + audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0); + + Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); + if (alert == null) { + alert = RingtoneManager.getValidRingtoneUri(getApplicationContext()); } - ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri); + + mediaPlayer = new MediaPlayer(); + mediaPlayer.setDataSource(this, alert); + mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); + mediaPlayer.setLooping(true); + mediaPlayer.prepare(); + mediaPlayer.start(); + + } catch (Exception e) { + Log.e("FindMyPhoneActivity", "Exception", e); } - if (android.os.Build.VERSION.SDK_INT >= 21) { - AudioAttributes.Builder b = new AudioAttributes.Builder(); - b.setUsage(AudioAttributes.USAGE_ALARM); - ringtone.setAudioAttributes(b.build()); - } else { - ringtone.setStreamType(AudioManager.STREAM_ALARM); - } - - ringtone.play(); } @Override protected void onStop() { super.onStop(); - if(ringtone != null) { - ringtone.stop(); - ringtone = null; + if (mediaPlayer != null) { + mediaPlayer.stop(); } + audioManager.setStreamVolume(AudioManager.STREAM_ALARM, previousVolume, 0); } }