From c9146b10afecb4db93e43bb8c300f21009550085 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 30 Mar 2018 17:27:49 +0200 Subject: [PATCH] adding the alarm sound picker dialog from Commons --- app/build.gradle | 3 +- .../calendar/activities/SettingsActivity.kt | 63 +++++++------------ .../calendar/extensions/Context.kt | 27 +++----- .../calendar/helpers/Config.kt | 22 +++---- .../calendar/helpers/Constants.kt | 3 +- 5 files changed, 47 insertions(+), 71 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 228092fbf..707486562 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,11 +46,10 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.17.5' + implementation 'com.simplemobiletools:commons:3.17.13' implementation 'joda-time:joda-time:2.9.9' implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'com.android.support:multidex:1.0.3' - implementation 'com.google.code.gson:gson:2.8.2' debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" 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 eae50e682..41f5b287d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -2,12 +2,9 @@ package com.simplemobiletools.calendar.activities import android.content.Intent import android.content.res.Resources -import android.media.RingtoneManager -import android.net.Uri +import android.media.AudioManager 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.SelectCalendarsDialog import com.simplemobiletools.calendar.extensions.* @@ -19,12 +16,14 @@ import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.CustomIntervalPickerDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog +import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ALARM_SOUND_TYPE_NOTIFICATION import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALENDAR import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR +import com.simplemobiletools.commons.models.AlarmSound import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.activity_settings.* -import java.io.File import java.util.* class SettingsActivity : SimpleActivity() { @@ -276,28 +275,28 @@ class SettingsActivity : SimpleActivity() { } private fun setupReminderSound() { - val noRingtone = res.getString(R.string.no_ringtone_selected) - if (config.reminderSound.isEmpty()) { - settings_reminder_sound.text = noRingtone - } else { - settings_reminder_sound.text = RingtoneManager.getRingtone(this, Uri.parse(config.reminderSound))?.getTitle(this) ?: noRingtone - } + settings_reminder_sound.text = config.reminderSoundTitle settings_reminder_sound_holder.setOnClickListener { - Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply { - putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION) - putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, res.getString(R.string.reminder_sound)) - putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(config.reminderSound)) - - if (resolveActivity(packageManager) != null) - startActivityForResult(this, GET_RINGTONE_URI) - else { - toast(R.string.no_ringtone_picker) + SelectAlarmSoundDialog(this, config.reminderSoundUri, AudioManager.STREAM_NOTIFICATION, GET_RINGTONE_URI, ALARM_SOUND_TYPE_NOTIFICATION, onAlarmPicked = { + if (it != null) { + updateReminderSound(it) } - } + }, onAlarmSoundDeleted = { + if (it.uri == config.reminderSoundUri) { + val defaultAlarm = getDefaultAlarmSound(ALARM_SOUND_TYPE_NOTIFICATION) + updateReminderSound(defaultAlarm) + } + }) } } + private fun updateReminderSound(alarmSound: AlarmSound) { + config.reminderSoundTitle = alarmSound.title + config.reminderSoundUri = alarmSound.uri + settings_reminder_sound.text = alarmSound.title + } + private fun setupVibrate() { settings_vibrate.isChecked = config.vibrateOnReminder settings_vibrate_holder.setOnClickListener { @@ -381,24 +380,10 @@ class SettingsActivity : SimpleActivity() { }) override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { - if (resultCode == RESULT_OK) { - if (requestCode == GET_RINGTONE_URI) { - var uri = resultData?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) - - if (uri == null) { - config.reminderSound = "" - } else { - 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) - } - } - } + super.onActivityResult(requestCode, resultCode, resultData) + if (requestCode == GET_RINGTONE_URI && resultCode == RESULT_OK && resultData != null) { + val newAlarmSound = storeNewYourAlarmSound(resultData) + updateReminderSound(newAlarmSound) } } } 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 45ce47f6c..589b6d666 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -21,7 +21,6 @@ import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView -import com.simplemobiletools.calendar.BuildConfig import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.activities.SimpleActivity @@ -38,7 +37,6 @@ import com.simplemobiletools.commons.helpers.isOreoPlus import org.joda.time.DateTime import org.joda.time.DateTimeZone import org.joda.time.LocalDate -import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -163,17 +161,17 @@ fun Context.notifyEvent(event: Event) { val timeRange = if (event.getIsAllDay()) getString(R.string.all_day) else getFormattedEventTime(startTime, endTime) val descriptionOrLocation = if (config.replaceDescription) event.location else event.description val content = "$displayedStartDate $timeRange $descriptionOrLocation".trim() - val notification = getNotification(applicationContext, pendingIntent, event, content) + val notification = getNotification(pendingIntent, event, content) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(event.id, notification) } @SuppressLint("NewApi") -private fun getNotification(context: Context, pendingIntent: PendingIntent, event: Event, content: String): Notification { +fun Context.getNotification(pendingIntent: PendingIntent, event: Event, content: String): Notification { val channelId = "reminder_channel" if (isOreoPlus()) { - val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - val name = context.resources.getString(R.string.event_reminders) + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val name = resources.getString(R.string.event_reminders) val importance = NotificationManager.IMPORTANCE_HIGH NotificationChannel(channelId, name, importance).apply { enableLights(true) @@ -183,15 +181,10 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, even } } - var soundUri = Uri.parse(context.config.reminderSound) - if (soundUri.scheme == "file") { - try { - soundUri = context.getFilePublicUri(File(soundUri.path), BuildConfig.APPLICATION_ID) - } catch (ignored: Exception) { - } - } + val soundUri = Uri.parse(config.reminderSoundUri) + grantReadUriPermission(config.reminderSoundUri) - val builder = NotificationCompat.Builder(context) + val builder = NotificationCompat.Builder(this) .setContentTitle(event.title) .setContentText(content) .setSmallIcon(R.drawable.ic_calendar) @@ -199,15 +192,15 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, even .setPriority(Notification.PRIORITY_HIGH) .setDefaults(Notification.DEFAULT_LIGHTS) .setAutoCancel(true) - .setSound(soundUri, AudioManager.STREAM_SYSTEM) + .setSound(soundUri, AudioManager.STREAM_NOTIFICATION) .setChannelId(channelId) - .addAction(R.drawable.ic_snooze, context.getString(R.string.snooze), getSnoozePendingIntent(context, event)) + .addAction(R.drawable.ic_snooze, getString(R.string.snooze), getSnoozePendingIntent(this, event)) if (isLollipopPlus()) { builder.setVisibility(Notification.VISIBILITY_PUBLIC) } - if (context.config.vibrateOnReminder) { + if (config.vibrateOnReminder) { builder.setVibrate(longArrayOf(0, 300, 300, 300)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index ee6457b7a..0f9de4500 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -1,9 +1,11 @@ package com.simplemobiletools.calendar.helpers import android.content.Context -import android.media.RingtoneManager import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync +import com.simplemobiletools.commons.extensions.getDefaultAlarmTitle +import com.simplemobiletools.commons.extensions.getDefaultAlarmUri +import com.simplemobiletools.commons.helpers.ALARM_SOUND_TYPE_NOTIFICATION import com.simplemobiletools.commons.helpers.BaseConfig import java.util.* @@ -28,9 +30,13 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(VIBRATE, false) set(vibrate) = prefs.edit().putBoolean(VIBRATE, vibrate).apply() - var reminderSound: String - get() = prefs.getString(REMINDER_SOUND, getDefaultNotificationSound()) - set(path) = prefs.edit().putString(REMINDER_SOUND, path).apply() + var reminderSoundUri: String + get() = prefs.getString(REMINDER_SOUND_URI, context.getDefaultAlarmUri(ALARM_SOUND_TYPE_NOTIFICATION).toString()) + set(reminderSoundUri) = prefs.edit().putString(REMINDER_SOUND_URI, reminderSoundUri).apply() + + var reminderSoundTitle: String + get() = prefs.getString(REMINDER_SOUND_TITLE, context.getDefaultAlarmTitle(ALARM_SOUND_TYPE_NOTIFICATION)) + set(reminderSoundTitle) = prefs.edit().putString(REMINDER_SOUND_TITLE, reminderSoundTitle).apply() var storedView: Int get() = prefs.getInt(VIEW, MONTHLY_VIEW) @@ -97,14 +103,6 @@ class Config(context: Context) : BaseConfig(context) { displayEventTypes = currDisplayEventTypes } - private fun getDefaultNotificationSound(): String { - return try { - RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)?.toString() ?: "" - } catch (e: Exception) { - "" - } - } - fun getFontSize() = when (fontSize) { FONT_SIZE_SMALL -> getSmallFontSize() FONT_SIZE_MEDIUM -> getMediumFontSize() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index 844d6f4cc..a8d998681 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -37,7 +37,8 @@ const val WEEK_NUMBERS = "week_numbers" const val START_WEEKLY_AT = "start_weekly_at" const val END_WEEKLY_AT = "end_weekly_at" const val VIBRATE = "vibrate" -const val REMINDER_SOUND = "reminder_sound" +const val REMINDER_SOUND_URI = "reminder_sound_uri" +const val REMINDER_SOUND_TITLE = "reminder_sound_title" const val VIEW = "view" const val REMINDER_MINUTES = "reminder_minutes" const val REMINDER_MINUTES_2 = "reminder_minutes_2"