fix #14, request storage permission at alarm picker if necessary

This commit is contained in:
tibbi 2018-03-29 10:57:39 +02:00
parent 87c1b0ea08
commit 9f2eb79c9f
3 changed files with 41 additions and 16 deletions

View File

@ -7,6 +7,7 @@
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.USE_FINGERPRINT"

View File

@ -4,28 +4,48 @@ import android.media.MediaPlayer
import android.net.Uri
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import android.widget.RadioButton
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.models.AlarmSound
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showErrorToast
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 view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null)
private val alarms = activity.getAlarms()
private var alarms = ArrayList<AlarmSound>()
private var mediaPlayer = MediaPlayer()
init {
activity.getAlarms {
alarms = it
gotAlarms()
}
AlertDialog.Builder(activity)
.setOnDismissListener { mediaPlayer.stop() }
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
window.volumeControlStream = audioStream
}
}
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 RadioButton).apply {
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()
@ -45,15 +65,6 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
}
}
AlertDialog.Builder(activity)
.setOnDismissListener { mediaPlayer.stop() }
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
window.volumeControlStream = audioStream
}
}
private fun dialogConfirmed() {

View File

@ -4,7 +4,9 @@ import android.app.Activity
import android.media.RingtoneManager
import android.view.WindowManager
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.PERMISSION_READ_STORAGE
import java.util.*
fun Activity.showOverLockscreen() {
@ -14,7 +16,7 @@ fun Activity.showOverLockscreen() {
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}
fun Activity.getAlarms(): ArrayList<AlarmSound> {
fun BaseSimpleActivity.getAlarms(callback: (ArrayList<AlarmSound>) -> Unit) {
val alarms = ArrayList<AlarmSound>()
val manager = RingtoneManager(this)
manager.setType(RingtoneManager.TYPE_ALARM)
@ -34,9 +36,20 @@ fun Activity.getAlarms(): ArrayList<AlarmSound> {
val alarmSound = AlarmSound(title, uri)
alarms.add(alarmSound)
}
callback(alarms)
} catch (e: Exception) {
showErrorToast(e)
if (e is SecurityException) {
handlePermission(PERMISSION_READ_STORAGE) {
if (it) {
getAlarms(callback)
} else {
showErrorToast(e)
callback(ArrayList())
}
}
} else {
showErrorToast(e)
callback(ArrayList())
}
}
return alarms
}