[Catalyst] Ring volume migration (1/n)

Bug: 373978964
Test: atest SeparateRingVolumePreferenceTest
Flag: com.android.settings.flags.catalyst_sound_screen
Change-Id: Ibf8f546c84ffb5467f62a250de37d96b5c974e15
This commit is contained in:
Mill Chen
2024-10-23 09:42:58 +00:00
parent 75b443375f
commit 7c9379e5d2
5 changed files with 327 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C) 2024 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.app.INotificationManager
import android.app.NotificationManager
import android.content.Context
import android.media.AudioManager.RINGER_MODE_NORMAL
import android.media.AudioManager.RINGER_MODE_SILENT
import android.media.AudioManager.RINGER_MODE_VIBRATE
import android.media.AudioManager.STREAM_RING
import android.os.ServiceManager
import android.os.UserHandle
import android.os.UserManager.DISALLOW_ADJUST_VOLUME
import android.os.Vibrator
import android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS
import android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS
import androidx.preference.Preference
import com.android.settings.R
import com.android.settingslib.RestrictedLockUtilsInternal
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.NoOpKeyedObservable
import com.android.settingslib.metadata.PersistentPreference
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceIconProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceRestrictionProvider
import com.android.settingslib.metadata.RangeValue
import com.android.settingslib.preference.PreferenceBinding
// LINT.IfChange
open class SeparateRingVolumePreference :
PreferenceMetadata,
PreferenceBinding,
PersistentPreference<Int>,
RangeValue,
PreferenceAvailabilityProvider,
PreferenceIconProvider,
PreferenceRestrictionProvider {
override val key: String
get() = KEY
override val title: Int
get() = R.string.separate_ring_volume_option_title
override fun getIcon(context: Context) =
when {
VolumeHelper.isMuted(context, STREAM_RING) -> getMuteIcon(context)
else -> R.drawable.ic_ring_volume
}
override fun isAvailable(context: Context) = !createAudioHelper(context).isSingleVolume()
override fun isEnabled(context: Context) =
!RestrictedLockUtilsInternal.hasBaseUserRestriction(
context,
DISALLOW_ADJUST_VOLUME,
UserHandle.myUserId(),
)
override fun isRestricted(context: Context) =
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
context,
DISALLOW_ADJUST_VOLUME,
UserHandle.myUserId(),
) != null
override fun storage(context: Context): KeyValueStore {
val helper = createAudioHelper(context)
return object : NoOpKeyedObservable<String>(), KeyValueStore {
override fun contains(key: String) = key == KEY
@Suppress("UNCHECKED_CAST")
override fun <T : Any> getValue(key: String, valueType: Class<T>) =
helper.getStreamVolume(STREAM_RING) as T
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
helper.setStreamVolume(STREAM_RING, value as Int)
}
}
}
override fun getMinValue(context: Context) =
createAudioHelper(context).getMinVolume(STREAM_RING)
override fun getMaxValue(context: Context) =
createAudioHelper(context).getMaxVolume(STREAM_RING)
override fun createWidget(context: Context) = VolumeSeekBarPreference(context)
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
super.bind(preference, metadata)
(preference as VolumeSeekBarPreference).apply {
setStream(STREAM_RING)
setMuteIcon(getMuteIcon(preference.context))
setListener { updateContentDescription(this) }
setSuppressionText(getSuppressionText(preference.context))
}
}
open fun createAudioHelper(context: Context) = AudioHelper(context)
fun updateContentDescription(preference: VolumeSeekBarPreference) {
val context = preference.context
val ringerMode = getEffectiveRingerMode(context)
when (ringerMode) {
RINGER_MODE_VIBRATE ->
preference.updateContentDescription(
context.getString(R.string.ringer_content_description_vibrate_mode)
)
RINGER_MODE_SILENT ->
preference.updateContentDescription(
context.getString(R.string.ringer_content_description_silent_mode)
)
else -> preference.updateContentDescription(preference.title)
}
}
fun getMuteIcon(context: Context): Int {
val ringerMode = getEffectiveRingerMode(context)
return when (ringerMode) {
RINGER_MODE_NORMAL -> R.drawable.ic_ring_volume
RINGER_MODE_VIBRATE -> R.drawable.ic_volume_ringer_vibrate
else -> R.drawable.ic_ring_volume_off
}
}
fun getEffectiveRingerMode(context: Context): Int {
val hasVibrator = context.getSystemService(Vibrator::class.java)?.hasVibrator() ?: false
val ringerMode = createAudioHelper(context).ringerModeInternal
return when {
!hasVibrator && ringerMode == RINGER_MODE_VIBRATE -> RINGER_MODE_SILENT
else -> ringerMode
}
}
fun getSuppressionText(context: Context): String? {
val suppressor = NotificationManager.from(context).getEffectsSuppressor()
val notificationManager =
INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE)
)
val hints = notificationManager.getHintsFromListenerNoToken()
return when {
hintsMatch(hints) -> SuppressorHelper.getSuppressionText(context, suppressor)
else -> null
}
}
private fun hintsMatch(hints: Int) =
(hints and HINT_HOST_DISABLE_CALL_EFFECTS) != 0 ||
(hints and HINT_HOST_DISABLE_EFFECTS) != 0
companion object {
const val KEY = "separate_ring_volume"
}
}
// LINT.ThenChange(SeparateRingVolumePreferenceController.java)

View File

@@ -35,6 +35,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
/**
* This slider is used to represent ring volume when ring is separated from notification
*/
// LINT.IfChange
public class SeparateRingVolumePreferenceController extends
RingerModeAffectedVolumePreferenceController {
@@ -149,3 +150,4 @@ public class SeparateRingVolumePreferenceController extends
}
}
// LINT.ThenChange(SeparateRingVolumePreference.kt)

View File

@@ -51,6 +51,7 @@ class SoundScreen : PreferenceScreenCreator, PreferenceIconProvider {
preferenceHierarchy(this) {
+MediaVolumePreference() order -180
+CallVolumePreference() order -170
+SeparateRingVolumePreference() order -155
+DialPadTonePreference() order -50
}