adding the alarm sound picker dialog from Commons

This commit is contained in:
tibbi 2018-03-30 17:27:49 +02:00
parent b43f63a142
commit c9146b10af
5 changed files with 47 additions and 71 deletions

View file

@ -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"

View file

@ -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<Parcelable>(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)
}
}
}

View file

@ -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))
}

View file

@ -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()

View file

@ -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"