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

Commit b4b3263d authored by Philipp Heckel's avatar Philipp Heckel
Browse files

Use custom sound from channel for insistent sound

parent 5101d1a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ class ApiService {
        user: User? = null,
        message: String,
        title: String = "",
        priority: Int = 3,
        priority: Int = PRIORITY_DEFAULT,
        tags: List<String> = emptyList(),
        delay: String = "",
        body: RequestBody? = null,
@@ -45,7 +45,7 @@ class ApiService {
    ) {
        val url = topicUrl(baseUrl, topic)
        val query = mutableListOf<String>()
        if (priority in 1..5) {
        if (priority in ALL_PRIORITIES) {
            query.add("priority=$priority")
        }
        if (tags.isNotEmpty()) {
+20 −12
Original line number Diff line number Diff line
@@ -59,13 +59,13 @@ class NotificationService(val context: Context) {
    }

    fun createNotificationChannels() {
        (1..5).forEach { priority -> maybeCreateNotificationChannel(priority) }
        ALL_PRIORITIES.forEach { priority -> maybeCreateNotificationChannel(priority) }
    }

    private fun displayInternal(subscription: Subscription, notification: Notification, update: Boolean = false) {
        val title = formatTitle(subscription, notification)
        val channelId = toChannelId(notification.priority)
        val insistent = notification.priority == 5 &&
        val insistent = notification.priority == PRIORITY_MAX &&
                (repository.getInsistentMaxPriorityEnabled() || subscription.insistent == Repository.INSISTENT_MAX_PRIORITY_ENABLED)
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_notification)
@@ -351,9 +351,9 @@ class NotificationService(val context: Context) {

            val pause = 300L
            val channel = when (priority) {
                1 -> NotificationChannel(CHANNEL_ID_MIN, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN)
                2 -> NotificationChannel(CHANNEL_ID_LOW, context.getString(R.string.channel_notifications_low_name), NotificationManager.IMPORTANCE_LOW)
                4 -> {
                PRIORITY_MIN -> NotificationChannel(CHANNEL_ID_MIN, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN)
                PRIORITY_LOW -> NotificationChannel(CHANNEL_ID_LOW, context.getString(R.string.channel_notifications_low_name), NotificationManager.IMPORTANCE_LOW)
                PRIORITY_HIGH -> {
                    val channel = NotificationChannel(CHANNEL_ID_HIGH, context.getString(R.string.channel_notifications_high_name), NotificationManager.IMPORTANCE_HIGH)
                    channel.enableVibration(true)
                    channel.vibrationPattern = longArrayOf(
@@ -362,7 +362,7 @@ class NotificationService(val context: Context) {
                    )
                    channel
                }
                5 -> {
                PRIORITY_MAX -> {
                    val channel = NotificationChannel(CHANNEL_ID_MAX, context.getString(R.string.channel_notifications_max_name), NotificationManager.IMPORTANCE_HIGH) // IMPORTANCE_MAX does not exist
                    channel.enableLights(true)
                    channel.enableVibration(true)
@@ -385,10 +385,10 @@ class NotificationService(val context: Context) {

    private fun toChannelId(priority: Int): String {
        return when (priority) {
            1 -> CHANNEL_ID_MIN
            2 -> CHANNEL_ID_LOW
            4 -> CHANNEL_ID_HIGH
            5 -> CHANNEL_ID_MAX
            PRIORITY_MIN -> CHANNEL_ID_MIN
            PRIORITY_LOW -> CHANNEL_ID_LOW
            PRIORITY_HIGH -> CHANNEL_ID_HIGH
            PRIORITY_MAX -> CHANNEL_ID_MAX
            else -> CHANNEL_ID_DEFAULT
        }
    }
@@ -400,11 +400,10 @@ class NotificationService(val context: Context) {
        try {
            val mediaPlayer = repository.mediaPlayer
            val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
            val alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                Log.d(TAG, "Media player: Playing insistent alarm on alarm channel")
                mediaPlayer.reset()
                mediaPlayer.setDataSource(context, alert)
                mediaPlayer.setDataSource(context, getInsistentSound())
                mediaPlayer.setAudioAttributes(AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build())
                mediaPlayer.isLooping = true;
                mediaPlayer.prepare()
@@ -417,6 +416,15 @@ class NotificationService(val context: Context) {
        }
    }

    private fun getInsistentSound(): Uri {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = notificationManager.getNotificationChannel(toChannelId(PRIORITY_MAX))
            channel.sound
        } else {
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        }
    }

    /**
     * Activity used to launch a URL.
     * .
+5 −5
Original line number Diff line number Diff line
@@ -143,22 +143,22 @@ class DetailAdapter(private val activity: Activity, private val lifecycleScope:

        private fun renderPriority(context: Context, notification: Notification) {
            when (notification.priority) {
                1 -> {
                PRIORITY_MIN -> {
                    priorityImageView.visibility = View.VISIBLE
                    priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_1_24dp))
                }
                2 -> {
                PRIORITY_LOW -> {
                    priorityImageView.visibility = View.VISIBLE
                    priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_2_24dp))
                }
                3 -> {
                PRIORITY_DEFAULT -> {
                    priorityImageView.visibility = View.GONE
                }
                4 -> {
                PRIORITY_HIGH -> {
                    priorityImageView.visibility = View.VISIBLE
                    priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_4_24dp))
                }
                5 -> {
                PRIORITY_MAX -> {
                    priorityImageView.visibility = View.VISIBLE
                    priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_5_24dp))
                }
+2 −2
Original line number Diff line number Diff line
@@ -210,8 +210,8 @@ class DetailSettingsActivity : AppCompatActivity() {
                    value = repository.getMinPriority()
                }
                val summary = when (value) {
                    1 -> getString(R.string.settings_notifications_min_priority_summary_any)
                    5 -> getString(R.string.settings_notifications_min_priority_summary_max)
                    PRIORITY_MIN -> getString(R.string.settings_notifications_min_priority_summary_any)
                    PRIORITY_MAX -> getString(R.string.settings_notifications_min_priority_summary_max)
                    else -> {
                        val minPriorityString = toPriorityString(requireContext(), value)
                        getString(R.string.settings_notifications_min_priority_summary_x_or_higher, value, minPriorityString)
+2 −2
Original line number Diff line number Diff line
@@ -191,8 +191,8 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
            }
            minPriority?.summaryProvider = Preference.SummaryProvider<ListPreference> { pref ->
                when (val minPriorityValue = pref.value.toIntOrNull() ?: 1) { // 1/low means all priorities
                    1 -> getString(R.string.settings_notifications_min_priority_summary_any)
                    5 -> getString(R.string.settings_notifications_min_priority_summary_max)
                    PRIORITY_MIN -> getString(R.string.settings_notifications_min_priority_summary_any)
                    PRIORITY_MAX -> getString(R.string.settings_notifications_min_priority_summary_max)
                    else -> {
                        val minPriorityString = toPriorityString(requireContext(), minPriorityValue)
                        getString(R.string.settings_notifications_min_priority_summary_x_or_higher, minPriorityValue, minPriorityString)
Loading