add a helper function for adding alarm sounds to radiogroup

This commit is contained in:
tibbi 2018-03-29 12:10:47 +02:00
parent ce319c617c
commit 1d46eacdec
4 changed files with 60 additions and 38 deletions

View File

@ -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<AlarmSound>()
private var alarmSounds = ArrayList<AlarmSound>()
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 })
}
}
}

View File

@ -16,16 +16,17 @@ fun Activity.showOverLockscreen() {
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}
fun BaseSimpleActivity.getAlarms(callback: (ArrayList<AlarmSound>) -> Unit) {
fun BaseSimpleActivity.getAlarmSounds(callback: (ArrayList<AlarmSound>) -> Unit) {
val alarms = ArrayList<AlarmSound>()
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<AlarmSound>) -> 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<AlarmSound>) -> Unit) {
if (e is SecurityException) {
handlePermission(PERMISSION_READ_STORAGE) {
if (it) {
getAlarms(callback)
getAlarmSounds(callback)
} else {
showErrorToast(e)
callback(ArrayList())

View File

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

View File

@ -11,6 +11,22 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_select_alarm_your_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/bigger_margin"
android:paddingTop="@dimen/bigger_margin"
android:text="@string/your_sounds"/>
<RadioGroup
android:id="@+id/dialog_select_alarm_your_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/medium_margin"/>
<TextView
android:id="@+id/dialog_select_alarm_system_label"
android:layout_width="wrap_content"
@ -20,7 +36,7 @@
android:text="@string/system_sounds"/>
<RadioGroup
android:id="@+id/dialog_select_alarm_radio"
android:id="@+id/dialog_select_alarm_system_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_margin"