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

Commit 5d8376a3 authored by Jason Chiu's avatar Jason Chiu
Browse files

[Catalyst] Migrate alarm volume preference

Bug: 389131220
Test: atest AlarmVolumePreferenceTest, manual
Flag: com.android.settings.flags.catalyst_sound_screen_25q3
Change-Id: I56ccb8a8519cad741ae2f2cdc0f4a6afc3c548f7
parent f11cc713
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -108,6 +108,9 @@ const val KEY_CALL_VOLUME = "call_volume"
/** Contract key for the "Ring volume" setting. */
const val KEY_RING_VOLUME = "separate_ring_volume"

/** Contract key for the "Alarm volume" setting. */
const val KEY_ALARM_VOLUME = "alarm_volume"

/** Contract key for the "Remove animation" setting. */
const val KEY_REMOVE_ANIMATION = "remove_animation"

+127 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.notification

import android.Manifest.permission.MODIFY_AUDIO_SETTINGS
import android.Manifest.permission.MODIFY_AUDIO_SETTINGS_PRIVILEGED
import android.app.settings.SettingsEnums.ACTION_ALARM_VOLUME
import android.content.Context
import android.content.pm.PackageManager
import android.media.AudioManager.STREAM_ALARM
import android.os.UserManager.DISALLOW_ADJUST_VOLUME
import androidx.preference.Preference
import com.android.internal.R as AndroidR
import com.android.settings.R
import com.android.settings.contract.KEY_ALARM_VOLUME
import com.android.settings.metrics.PreferenceActionMetricsProvider
import com.android.settings.restriction.PreferenceRestrictionMixin
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.NoOpKeyedObservable
import com.android.settingslib.datastore.Permissions
import com.android.settingslib.datastore.and
import com.android.settingslib.metadata.IntRangeValuePreference
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceIconProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.metadata.SensitivityLevel
import com.android.settingslib.preference.PreferenceBinding

// LINT.IfChange
open class AlarmVolumePreference(private val audioHelper: AudioHelper) :
    IntRangeValuePreference,
    PreferenceBinding,
    PreferenceActionMetricsProvider,
    PreferenceAvailabilityProvider,
    PreferenceIconProvider,
    PreferenceRestrictionMixin {
    override val key: String
        get() = KEY

    override val title: Int
        get() = R.string.alarm_volume_option_title

    override val preferenceActionMetrics: Int
        get() = ACTION_ALARM_VOLUME

    override fun tags(context: Context) = arrayOf(KEY_ALARM_VOLUME)

    override fun getIcon(context: Context) =
        when {
            VolumeHelper.isMuted(context, STREAM_ALARM) -> AndroidR.drawable.ic_audio_alarm_mute
            else -> AndroidR.drawable.ic_audio_alarm
        }

    override fun isAvailable(context: Context) =
        context.resources.getBoolean(R.bool.config_show_alarm_volume) && !audioHelper.isSingleVolume

    override fun isEnabled(context: Context) = super<PreferenceRestrictionMixin>.isEnabled(context)

    override val restrictionKeys: Array<String>
        get() = arrayOf(DISALLOW_ADJUST_VOLUME)

    override fun storage(context: Context): KeyValueStore =
        object : NoOpKeyedObservable<String>(), KeyValueStore {
            override fun contains(key: String) = key == KEY

            @Suppress("UNCHECKED_CAST")
            override fun <T : Any> getValue(key: String, valueType: Class<T>) =
                audioHelper.getStreamVolume(STREAM_ALARM) as T

            override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
                audioHelper.setStreamVolume(STREAM_ALARM, value as Int)
            }
        }

    override fun getReadPermissions(context: Context) = Permissions.EMPTY

    override fun getReadPermit(context: Context, myUid: Int, callingUid: Int) =
        ReadWritePermit.ALLOW

    override fun getWritePermissions(context: Context): Permissions? {
        var permissions = Permissions.allOf(MODIFY_AUDIO_SETTINGS)
        if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
            permissions = permissions and MODIFY_AUDIO_SETTINGS_PRIVILEGED
        }
        return permissions
    }

    override fun getWritePermit(context: Context, value: Int?, myUid: Int, callingUid: Int) =
        ReadWritePermit.ALLOW

    override val sensitivityLevel
        get() = SensitivityLevel.NO_SENSITIVITY

    override fun getMinValue(context: Context) = audioHelper.getMinVolume(STREAM_ALARM)

    override fun getMaxValue(context: Context) = audioHelper.getMaxVolume(STREAM_ALARM)

    override fun createWidget(context: Context) = VolumeSeekBarPreference(context)

    override fun bind(preference: Preference, metadata: PreferenceMetadata) {
        super.bind(preference, metadata)
        (preference as VolumeSeekBarPreference).apply {
            setStream(STREAM_ALARM)
            setMuteIcon(AndroidR.drawable.ic_audio_alarm_mute)
        }
    }

    companion object {
        const val KEY = "alarm_volume"
    }
}
// LINT.ThenChange(AlarmVolumePreferenceController.java)
+2 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.text.TextUtils;

import com.android.settings.R;

// LINT.IfChange
public class AlarmVolumePreferenceController extends VolumeSeekBarPreferenceController {

    private static final String KEY_ALARM_VOLUME = "alarm_volume";
@@ -66,3 +67,4 @@ public class AlarmVolumePreferenceController extends VolumeSeekBarPreferenceCont
        return com.android.internal.R.drawable.ic_audio_alarm_mute;
    }
}
// LINT.ThenChange(AlarmVolumePreference.kt)
+3 −0
Original line number Diff line number Diff line
@@ -59,6 +59,9 @@ class SoundScreen : PreferenceScreenCreator, PreferenceIconProvider {
            +MediaVolumePreference(audioHelper) order -180
            +CallVolumePreference(audioHelper) order -170
            +SeparateRingVolumePreference(audioHelper) order -155
            if (Flags.catalystSoundScreen25q3()) {
                +AlarmVolumePreference(audioHelper) order -140
            }
            +MediaControlsScreen.KEY order -100
            +DialPadTonePreference() order -50
        }
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class AlarmVolumePreferenceControllerTest {

@@ -84,3 +85,4 @@ public class AlarmVolumePreferenceControllerTest {
        assertThat(mController.isPublicSlice()).isTrue();
    }
}
// LINT.ThenChange(AlarmVolumePreferenceTest.kt)
Loading