[Catalyst] Media volume migration
Bug: 364898461 Test: atest MediaVolumePreferenceTest Flag: com.android.settings.flags.catalyst_sound_screen Change-Id: I172f336a7d1c843d071dd920d29aa35b4a1a8aaf
This commit is contained in:
118
src/com/android/settings/notification/MediaVolumePreference.kt
Normal file
118
src/com/android/settings/notification/MediaVolumePreference.kt
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* 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.content.Context
|
||||||
|
import android.media.AudioManager.STREAM_MUSIC
|
||||||
|
import android.os.UserHandle
|
||||||
|
import android.os.UserManager
|
||||||
|
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.*
|
||||||
|
import com.android.settingslib.preference.PreferenceBinding
|
||||||
|
|
||||||
|
// LINT.IfChange
|
||||||
|
open class MediaVolumePreference :
|
||||||
|
PreferenceMetadata,
|
||||||
|
PreferenceBinding,
|
||||||
|
PersistentPreference<Int>,
|
||||||
|
RangeValue,
|
||||||
|
PreferenceAvailabilityProvider,
|
||||||
|
PreferenceIconProvider,
|
||||||
|
PreferenceRestrictionProvider {
|
||||||
|
override val key: String
|
||||||
|
get() = KEY
|
||||||
|
|
||||||
|
override val title: Int
|
||||||
|
get() = R.string.media_volume_option_title
|
||||||
|
|
||||||
|
override fun getIcon(context: Context) =
|
||||||
|
when {
|
||||||
|
VolumeHelper.isMuted(context, STREAM_MUSIC) -> R.drawable.ic_media_stream_off
|
||||||
|
else -> R.drawable.ic_media_stream
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isAvailable(context: Context) =
|
||||||
|
context.resources.getBoolean(R.bool.config_show_media_volume)
|
||||||
|
|
||||||
|
override fun isRestricted(context: Context) =
|
||||||
|
RestrictedLockUtilsInternal.hasBaseUserRestriction(
|
||||||
|
context,
|
||||||
|
UserManager.DISALLOW_ADJUST_VOLUME,
|
||||||
|
UserHandle.myUserId(),
|
||||||
|
) ||
|
||||||
|
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
|
||||||
|
context,
|
||||||
|
UserManager.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_MUSIC) as T
|
||||||
|
|
||||||
|
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
|
||||||
|
helper.setStreamVolume(STREAM_MUSIC, value as Int)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getMinValue(context: Context) =
|
||||||
|
createAudioHelper(context).getMinVolume(STREAM_MUSIC)
|
||||||
|
|
||||||
|
override fun getMaxValue(context: Context) =
|
||||||
|
createAudioHelper(context).getMaxVolume(STREAM_MUSIC)
|
||||||
|
|
||||||
|
override fun createWidget(context: Context) = VolumeSeekBarPreference(context)
|
||||||
|
|
||||||
|
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
|
||||||
|
super.bind(preference, metadata)
|
||||||
|
(preference as VolumeSeekBarPreference).apply {
|
||||||
|
setStream(STREAM_MUSIC)
|
||||||
|
setMuteIcon(R.drawable.ic_media_stream_off)
|
||||||
|
setListener { updateContentDescription(this) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun createAudioHelper(context: Context) = AudioHelper(context)
|
||||||
|
|
||||||
|
fun updateContentDescription(preference: VolumeSeekBarPreference) {
|
||||||
|
when {
|
||||||
|
preference.isMuted() ->
|
||||||
|
preference.updateContentDescription(
|
||||||
|
preference.context.getString(
|
||||||
|
R.string.volume_content_description_silent_mode,
|
||||||
|
preference.title,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else -> preference.updateContentDescription(preference.title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val KEY = "media_volume"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// LINT.ThenChange(MediaVolumePreferenceController.java)
|
@@ -42,6 +42,7 @@ import com.android.settingslib.media.BluetoothMediaDevice;
|
|||||||
import com.android.settingslib.media.MediaDevice;
|
import com.android.settingslib.media.MediaDevice;
|
||||||
import com.android.settingslib.media.MediaOutputConstants;
|
import com.android.settingslib.media.MediaOutputConstants;
|
||||||
|
|
||||||
|
// LINT.IfChange
|
||||||
public class MediaVolumePreferenceController extends VolumeSeekBarPreferenceController {
|
public class MediaVolumePreferenceController extends VolumeSeekBarPreferenceController {
|
||||||
private static final String TAG = "MediaVolumePreCtrl";
|
private static final String TAG = "MediaVolumePreCtrl";
|
||||||
private static final String KEY_MEDIA_VOLUME = "media_volume";
|
private static final String KEY_MEDIA_VOLUME = "media_volume";
|
||||||
@@ -204,3 +205,4 @@ public class MediaVolumePreferenceController extends VolumeSeekBarPreferenceCont
|
|||||||
return MediaOutputIndicatorWorker.class;
|
return MediaOutputIndicatorWorker.class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// LINT.ThenChange(MediaVolumePreference.kt)
|
||||||
|
@@ -49,6 +49,7 @@ class SoundScreen : PreferenceScreenCreator, PreferenceIconProvider {
|
|||||||
|
|
||||||
override fun getPreferenceHierarchy(context: Context) =
|
override fun getPreferenceHierarchy(context: Context) =
|
||||||
preferenceHierarchy(this) {
|
preferenceHierarchy(this) {
|
||||||
|
+MediaVolumePreference() order -180
|
||||||
+CallVolumePreference() order -170
|
+CallVolumePreference() order -170
|
||||||
+DialPadTonePreference() order -50
|
+DialPadTonePreference() order -50
|
||||||
}
|
}
|
||||||
|
62
src/com/android/settings/notification/VolumeHelper.kt
Normal file
62
src/com/android/settings/notification/VolumeHelper.kt
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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.NotificationManager
|
||||||
|
import android.app.NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS
|
||||||
|
import android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA
|
||||||
|
import android.content.Context
|
||||||
|
import android.media.AudioManager
|
||||||
|
import android.media.AudioManager.*
|
||||||
|
import android.provider.Settings.Global.ZEN_MODE_ALARMS
|
||||||
|
import android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
|
||||||
|
import android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS
|
||||||
|
import android.service.notification.ZenModeConfig
|
||||||
|
|
||||||
|
class VolumeHelper {
|
||||||
|
companion object {
|
||||||
|
fun isMuted(context: Context, streamType: Int): Boolean {
|
||||||
|
val audioManager = context.getSystemService(AudioManager::class.java)
|
||||||
|
return audioManager.isStreamMute(streamType) && !isZenMuted(context, streamType)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isZenMuted(context: Context, streamType: Int): Boolean {
|
||||||
|
val notificationManager = context.getSystemService(NotificationManager::class.java)
|
||||||
|
val zenMode = notificationManager.getZenMode()
|
||||||
|
val notificationPolicy = notificationManager.getConsolidatedNotificationPolicy()
|
||||||
|
val isAllowAlarms =
|
||||||
|
(notificationPolicy.priorityCategories and PRIORITY_CATEGORY_ALARMS) != 0
|
||||||
|
val isAllowMedia =
|
||||||
|
(notificationPolicy.priorityCategories and PRIORITY_CATEGORY_MEDIA) != 0
|
||||||
|
val isAllowRinger =
|
||||||
|
!ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(notificationPolicy)
|
||||||
|
return isNotificationOrRingStream(streamType)
|
||||||
|
&& zenMode == ZEN_MODE_ALARMS || zenMode == ZEN_MODE_NO_INTERRUPTIONS
|
||||||
|
|| (zenMode == ZEN_MODE_IMPORTANT_INTERRUPTIONS
|
||||||
|
&& (!isAllowRinger && isNotificationOrRingStream(streamType)
|
||||||
|
|| !isAllowMedia && isMediaStream(streamType)
|
||||||
|
|| !isAllowAlarms && isAlarmStream(streamType)))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isNotificationOrRingStream(streamType: Int) =
|
||||||
|
streamType == STREAM_RING || streamType == STREAM_NOTIFICATION
|
||||||
|
|
||||||
|
private fun isAlarmStream(streamType: Int) = streamType == STREAM_ALARM
|
||||||
|
|
||||||
|
private fun isMediaStream(streamType: Int) = streamType == STREAM_MUSIC
|
||||||
|
}
|
||||||
|
}
|
@@ -58,6 +58,7 @@ import org.robolectric.annotation.Config;
|
|||||||
import org.robolectric.annotation.Implementation;
|
import org.robolectric.annotation.Implementation;
|
||||||
import org.robolectric.annotation.Implements;
|
import org.robolectric.annotation.Implements;
|
||||||
|
|
||||||
|
// LINT.IfChange
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = MediaVolumePreferenceControllerTest.ShadowSliceBackgroundWorker.class)
|
@Config(shadows = MediaVolumePreferenceControllerTest.ShadowSliceBackgroundWorker.class)
|
||||||
public class MediaVolumePreferenceControllerTest {
|
public class MediaVolumePreferenceControllerTest {
|
||||||
@@ -269,3 +270,4 @@ public class MediaVolumePreferenceControllerTest {
|
|||||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// LINT.ThenChange(MediaVolumePreference.kt)
|
||||||
|
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.content.ContextWrapper
|
||||||
|
import android.content.res.Resources
|
||||||
|
import androidx.test.core.app.ApplicationProvider
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.ArgumentMatchers.anyInt
|
||||||
|
import org.mockito.kotlin.doReturn
|
||||||
|
import org.mockito.kotlin.mock
|
||||||
|
import org.mockito.kotlin.stub
|
||||||
|
|
||||||
|
// LINT.IfChange
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class MediaVolumePreferenceTest {
|
||||||
|
private val mockResources = mock<Resources>()
|
||||||
|
|
||||||
|
private val mediaVolumePreference = MediaVolumePreference()
|
||||||
|
private val context =
|
||||||
|
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
|
||||||
|
override fun getResources(): Resources = mockResources
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isAvailable_configTrue_shouldReturnTrue() {
|
||||||
|
mockResources.stub { on { getBoolean(anyInt()) } doReturn true }
|
||||||
|
|
||||||
|
assertThat(mediaVolumePreference.isAvailable(context)).isTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isAvailable_configFalse_shouldReturnFalse() {
|
||||||
|
mockResources.stub { on { getBoolean(anyInt()) } doReturn false }
|
||||||
|
|
||||||
|
assertThat(mediaVolumePreference.isAvailable(context)).isFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// LINT.ThenChange(MediaVolumePreferenceControllerTest.java)
|
Reference in New Issue
Block a user