From 2f577097d16cf4ec535e34424dae580e227448d8 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 13 May 2017 08:10:47 +0200 Subject: [PATCH] work around for brokens ROMs that expose file uris as notification sound --- .../conversations/persistance/FileBackend.java | 2 +- .../services/NotificationService.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 51dc53b2c..081d924f0 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -65,7 +65,7 @@ public class FileBackend { private static final SimpleDateFormat IMAGE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); - private static final String FILE_PROVIDER = ".files"; + public static final String FILE_PROVIDER = ".files"; private XmppConnectionService mXmppConnectionService; diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index ae31f99f1..d1e41a746 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -22,6 +22,7 @@ import android.util.DisplayMetrics; import android.util.Log; import android.util.Pair; +import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Calendar; @@ -40,6 +41,7 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.SettingsActivity; @@ -288,7 +290,12 @@ public class NotificationService { mBuilder.setVibrate(new long[]{0}); } if (ringtone != null) { - mBuilder.setSound(Uri.parse(ringtone)); + Uri uri = Uri.parse(ringtone); + try { + mBuilder.setSound(fixRingtoneUri(uri)); + } catch (SecurityException e) { + Log.d(Config.LOGTAG,"unable to use custom notification sound "+uri.toString()); + } } } if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -302,6 +309,14 @@ public class NotificationService { } } + private Uri fixRingtoneUri(Uri uri) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && "file".equals(uri.getScheme())) { + return FileBackend.getUriForFile(mXmppConnectionService,new File(uri.getPath())); + } else { + return uri; + } + } + private Builder buildMultipleConversation() { final Builder mBuilder = new NotificationCompat.Builder( mXmppConnectionService);