diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt index 70d23f51..84543abf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt @@ -8,7 +8,7 @@ import android.widget.RadioGroup import com.simplemobiletools.clock.R import com.simplemobiletools.clock.activities.SimpleActivity import com.simplemobiletools.clock.extensions.config -import com.simplemobiletools.clock.extensions.getAlarms +import com.simplemobiletools.clock.extensions.getAlarmSounds import com.simplemobiletools.clock.models.AlarmSound import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.setupDialogStuff @@ -17,16 +17,20 @@ import com.simplemobiletools.commons.views.MyCompatRadioButton import kotlinx.android.synthetic.main.dialog_select_alarm_sound.view.* 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 view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null) - private var alarms = ArrayList() + private var alarmSounds = ArrayList() private var mediaPlayer = MediaPlayer() + private val config = activity.config init { - activity.getAlarms { - alarms = it - gotAlarms() + activity.getAlarmSounds { + alarmSounds = it + gotSystemAlarms() } + view.dialog_select_alarm_your_label.setTextColor(activity.getAdjustedPrimaryColor()) view.dialog_select_alarm_system_label.setTextColor(activity.getAdjustedPrimaryColor()) AlertDialog.Builder(activity) @@ -39,42 +43,43 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin } } - private fun gotAlarms() { - val config = activity.config - view.dialog_select_alarm_radio.apply { - alarms.forEachIndexed { index, alarmSound -> - val radioButton = (activity.layoutInflater.inflate(R.layout.item_select_alarm, null) as MyCompatRadioButton).apply { - text = alarmSound.title - isChecked = alarmSound.uri == currentUri - id = index - setColors(config.textColor, activity.getAdjustedPrimaryColor(), config.backgroundColor) - setOnClickListener { - try { - mediaPlayer.stop() - mediaPlayer = MediaPlayer().apply { - setAudioStreamType(audioStream) - setDataSource(context, Uri.parse(alarmSound.uri)) - isLooping = true - prepare() - start() - } - } catch (e: Exception) { - activity.showErrorToast(e) - } - } - } + private fun gotSystemAlarms() { + alarmSounds.forEach { + addAlarmSound(it, view.dialog_select_alarm_system_radio) + } + } - addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) + private fun addAlarmSound(alarmSound: AlarmSound, holder: ViewGroup) { + val radioButton = (activity.layoutInflater.inflate(R.layout.item_select_alarm, null) as MyCompatRadioButton).apply { + text = alarmSound.title + isChecked = alarmSound.uri == currentUri + id = alarmSound.id + setColors(config.textColor, activity.getAdjustedPrimaryColor(), config.backgroundColor) + setOnClickListener { + try { + mediaPlayer.stop() + mediaPlayer = MediaPlayer().apply { + setAudioStreamType(audioStream) + setDataSource(context, Uri.parse(alarmSound.uri)) + isLooping = true + prepare() + start() + } + } catch (e: Exception) { + activity.showErrorToast(e) + } } } + + holder.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) } private fun dialogConfirmed() { - val checkedId = view.dialog_select_alarm_radio.checkedRadioButtonId + val checkedId = view.dialog_select_alarm_system_radio.checkedRadioButtonId if (checkedId == -1) { callback(null) } else { - callback(alarms[checkedId]) + callback(alarmSounds.firstOrNull { it.id == checkedId }) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Activity.kt index 517f1f2b..d6711e6d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Activity.kt @@ -16,16 +16,17 @@ fun Activity.showOverLockscreen() { WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) } -fun BaseSimpleActivity.getAlarms(callback: (ArrayList) -> Unit) { +fun BaseSimpleActivity.getAlarmSounds(callback: (ArrayList) -> Unit) { val alarms = ArrayList() val manager = RingtoneManager(this) manager.setType(RingtoneManager.TYPE_ALARM) try { val cursor = manager.cursor - val defaultAlarm = AlarmSound(getDefaultAlarmTitle(), getDefaultAlarmUri().toString()) + val defaultAlarm = AlarmSound(0, getDefaultAlarmTitle(), getDefaultAlarmUri().toString()) alarms.add(defaultAlarm) + var curId = 1 while (cursor.moveToNext()) { val title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX) var uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX) @@ -33,7 +34,7 @@ fun BaseSimpleActivity.getAlarms(callback: (ArrayList) -> Unit) { if (!uri.endsWith(id)) { uri += "/$id" } - val alarmSound = AlarmSound(title, uri) + val alarmSound = AlarmSound(curId++, title, uri) alarms.add(alarmSound) } callback(alarms) @@ -41,7 +42,7 @@ fun BaseSimpleActivity.getAlarms(callback: (ArrayList) -> Unit) { if (e is SecurityException) { handlePermission(PERMISSION_READ_STORAGE) { if (it) { - getAlarms(callback) + getAlarmSounds(callback) } else { showErrorToast(e) callback(ArrayList()) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/models/AlarmSound.kt b/app/src/main/kotlin/com/simplemobiletools/clock/models/AlarmSound.kt index 26874646..8961a229 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/models/AlarmSound.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/models/AlarmSound.kt @@ -1,3 +1,3 @@ package com.simplemobiletools.clock.models -data class AlarmSound(var title: String, var uri: String) +data class AlarmSound(val id: Int, var title: String, var uri: String) diff --git a/app/src/main/res/layout/dialog_select_alarm_sound.xml b/app/src/main/res/layout/dialog_select_alarm_sound.xml index 93ac5cc8..b0e70a1b 100644 --- a/app/src/main/res/layout/dialog_select_alarm_sound.xml +++ b/app/src/main/res/layout/dialog_select_alarm_sound.xml @@ -11,6 +11,22 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + +