From ba70e6b523e9a340a338fc2fc0d94e986d93edd5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 31 Jan 2018 21:17:45 +0100 Subject: [PATCH] check if we can generate a public uri from the selected reminder sound uri --- .../calendar/activities/SettingsActivity.kt | 16 +++++++++++++--- .../calendar/extensions/Context.kt | 5 ++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index 15f0b32cd..77575a858 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.os.Bundle import android.os.Parcelable import android.text.TextUtils +import com.simplemobiletools.calendar.BuildConfig import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog import com.simplemobiletools.calendar.dialogs.SelectCalendarsDialog @@ -23,6 +24,7 @@ import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALENDAR import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.activity_settings.* +import java.io.File import java.util.* class SettingsActivity : SimpleActivity() { @@ -367,12 +369,20 @@ class SettingsActivity : SimpleActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (resultCode == RESULT_OK) { if (requestCode == GET_RINGTONE_URI) { - val uri = data?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) + var uri = data?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) + if (uri == null) { config.reminderSound = "" } else { - settings_reminder_sound.text = RingtoneManager.getRingtone(this, uri as Uri)?.getTitle(this) - config.reminderSound = uri.toString() + try { + if ((uri as Uri).scheme == "file") { + uri = getFilePublicUri(File(uri.path), BuildConfig.APPLICATION_ID) + } + settings_reminder_sound.text = RingtoneManager.getRingtone(this, uri)?.getTitle(this) + config.reminderSound = uri.toString() + } catch (e: Exception) { + showErrorToast(e) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index 2f219c92e..b3559b606 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -199,7 +199,10 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, even var soundUri = Uri.parse(context.config.reminderSound) if (soundUri.scheme == "file") { - soundUri = context.getFilePublicUri(File(soundUri.path), BuildConfig.APPLICATION_ID) + try { + soundUri = context.getFilePublicUri(File(soundUri.path), BuildConfig.APPLICATION_ID) + } catch (ignored: Exception) { + } } val builder = NotificationCompat.Builder(context)