Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a48d160e authored by tibbi's avatar tibbi
Browse files

add a helper function for storing a frehly picked alarm sound

parent 09f798dd
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.github.ajalt.reprint.core.Reprint
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_BACKGROUND_COLOR
@@ -461,3 +463,28 @@ fun Context.grantReadUriPermission(uriString: String) {
    } catch (ignored: Exception) {
    }
}

fun Context.storeNewYourAlarmSound(resultData: Intent): AlarmSound {
    val uri = resultData.data
    var filename = getFilenameFromUri(uri)
    if (filename.isEmpty()) {
        filename = getString(R.string.alarm)
    }

    val token = object : TypeToken<ArrayList<AlarmSound>>() {}.type
    val yourAlarmSounds = Gson().fromJson<ArrayList<AlarmSound>>(baseConfig.yourAlarmSounds, token) ?: ArrayList()
    val newAlarmSoundId = (yourAlarmSounds.maxBy { it.id }?.id ?: YOUR_ALARM_SOUNDS_MIN_ID) + 1
    val newAlarmSound = AlarmSound(newAlarmSoundId, filename, uri.toString())
    if (yourAlarmSounds.firstOrNull { it.uri == uri.toString() } == null) {
        yourAlarmSounds.add(newAlarmSound)
    }

    baseConfig.yourAlarmSounds = Gson().toJson(yourAlarmSounds)

    if (isKitkatPlus()) {
        val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        contentResolver.takePersistableUriPermission(uri, takeFlags)
    }

    return newAlarmSound
}
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ const val BROADCAST_REFRESH_MEDIA = "com.simplemobiletools.REFRESH_MEDIA"
const val OTG_PATH = "otg:/"
const val ALARM_SOUND_TYPE_ALARM = 1
const val ALARM_SOUND_TYPE_NOTIFICATION = 2
const val YOUR_ALARM_SOUNDS_MIN_ID = 1000

const val HOUR_MINUTES = 60
const val DAY_MINUTES = 24 * HOUR_MINUTES