mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 13:17:43 +00:00
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
This commit is contained in:
parent
de7d944230
commit
c05f13afac
@ -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,6 +37,8 @@ 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 |
|
||||
@ -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;
|
||||
}
|
||||
ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user