From 6ed1bf6284d7286a042ee5410eb2d7404e40ca55 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sun, 25 Dec 2016 15:16:47 +0100 Subject: [PATCH] Fix crash when ringtone is null. --- .../FindMyPhoneActivity.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java index 2c565bff..2ebba699 100644 --- a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java +++ b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java @@ -8,6 +8,7 @@ import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -33,8 +34,23 @@ public class FindMyPhoneActivity extends Activity { WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); + findViewById(R.id.bFindMyPhone).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + 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); + } if (android.os.Build.VERSION.SDK_INT >= 21) { AudioAttributes.Builder b = new AudioAttributes.Builder(); @@ -46,12 +62,6 @@ public class FindMyPhoneActivity extends Activity { ringtone.play(); - findViewById(R.id.bFindMyPhone).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - finish(); - } - }); } @Override