allow selecting custom sounds as notification sound

This commit is contained in:
tibbi 2018-03-29 20:37:10 +02:00
parent 6daa6a4dbf
commit aecfb9f5f1
2 changed files with 24 additions and 12 deletions

View File

@ -22,21 +22,20 @@ import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.isKitkatPlus import com.simplemobiletools.commons.helpers.isKitkatPlus
import com.simplemobiletools.commons.views.MyCompatRadioButton import com.simplemobiletools.commons.views.MyCompatRadioButton
import kotlinx.android.synthetic.main.dialog_select_alarm_sound.view.* import kotlinx.android.synthetic.main.dialog_select_alarm_sound.view.*
import java.util.LinkedHashSet
import kotlin.collections.ArrayList
class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val audioStream: Int, val callback: (alarmSound: AlarmSound?) -> Unit) { class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val audioStream: Int, val callback: (alarmSound: AlarmSound?) -> Unit) {
private val ADD_NEW_SOUND_ID = -1 private val ADD_NEW_SOUND_ID = -2
private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null) private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null)
private var alarmSounds = ArrayList<AlarmSound>() private var systemAlarmSounds = ArrayList<AlarmSound>()
private var yourAlarmSounds = ArrayList<AlarmSound>()
private var mediaPlayer = MediaPlayer() private var mediaPlayer = MediaPlayer()
private val config = activity.config private val config = activity.config
private val dialog: AlertDialog private val dialog: AlertDialog
init { init {
activity.getAlarmSounds { activity.getAlarmSounds {
alarmSounds = it systemAlarmSounds = it
gotSystemAlarms() gotSystemAlarms()
} }
@ -56,8 +55,8 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
} }
private fun addYourAlarms() { private fun addYourAlarms() {
val token = object : TypeToken<LinkedHashSet<AlarmSound>>() {}.type val token = object : TypeToken<ArrayList<AlarmSound>>() {}.type
val yourAlarmSounds = Gson().fromJson<LinkedHashSet<AlarmSound>>(config.yourAlarmSounds, token) ?: LinkedHashSet() yourAlarmSounds = Gson().fromJson<ArrayList<AlarmSound>>(config.yourAlarmSounds, token) ?: ArrayList()
yourAlarmSounds.add(AlarmSound(ADD_NEW_SOUND_ID, activity.getString(R.string.add_new_sound), "")) yourAlarmSounds.add(AlarmSound(ADD_NEW_SOUND_ID, activity.getString(R.string.add_new_sound), ""))
yourAlarmSounds.forEach { yourAlarmSounds.forEach {
addAlarmSound(it, view.dialog_select_alarm_your_radio) addAlarmSound(it, view.dialog_select_alarm_your_radio)
@ -65,7 +64,7 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
} }
private fun gotSystemAlarms() { private fun gotSystemAlarms() {
alarmSounds.forEach { systemAlarmSounds.forEach {
addAlarmSound(it, view.dialog_select_alarm_system_radio) addAlarmSound(it, view.dialog_select_alarm_system_radio)
} }
} }
@ -78,6 +77,12 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
setColors(config.textColor, activity.getAdjustedPrimaryColor(), config.backgroundColor) setColors(config.textColor, activity.getAdjustedPrimaryColor(), config.backgroundColor)
setOnClickListener { setOnClickListener {
alarmClicked(alarmSound) alarmClicked(alarmSound)
if (holder == view.dialog_select_alarm_system_radio) {
view.dialog_select_alarm_your_radio.clearCheck()
} else {
view.dialog_select_alarm_system_radio.clearCheck()
}
} }
} }
@ -114,11 +119,12 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
} }
private fun dialogConfirmed() { private fun dialogConfirmed() {
val checkedId = view.dialog_select_alarm_system_radio.checkedRadioButtonId if (view.dialog_select_alarm_your_radio.checkedRadioButtonId != -1) {
if (checkedId == -1) { val checkedId = view.dialog_select_alarm_your_radio.checkedRadioButtonId
callback(null) callback(yourAlarmSounds.firstOrNull { it.id == checkedId })
} else { } else {
callback(alarmSounds.firstOrNull { it.id == checkedId }) val checkedId = view.dialog_select_alarm_system_radio.checkedRadioButtonId
callback(systemAlarmSounds.firstOrNull { it.id == checkedId })
} }
} }
} }

View File

@ -296,6 +296,12 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm, add
} }
} }
try {
// ensure custom reminder sounds play well
grantUriPermission("com.android.systemui", Uri.parse(alarm.soundUri), Intent.FLAG_GRANT_READ_URI_PERMISSION)
} catch (ignored: Exception) {
}
val reminderActivityIntent = getReminderActivityIntent() val reminderActivityIntent = getReminderActivityIntent()
val builder = NotificationCompat.Builder(this) val builder = NotificationCompat.Builder(this)
.setContentTitle(label) .setContentTitle(label)