Add remote volume slider to sound settings
1. Add RemoteVolumePreferenceController to control volume through MediaSessions. 2. Add related preference to skip calling AudioManager. Bug: 126199571 Test: RunSettingsRoboTests Change-Id: I9cb85ccf5f49be6d127cb61caf445b2ee7dfb579
This commit is contained in:
@@ -100,6 +100,7 @@
|
|||||||
<uses-permission android:name="android.permission.USE_RESERVED_DISK" />
|
<uses-permission android:name="android.permission.USE_RESERVED_DISK" />
|
||||||
<uses-permission android:name="android.permission.MANAGE_SCOPED_ACCESS_DIRECTORY_PERMISSIONS" />
|
<uses-permission android:name="android.permission.MANAGE_SCOPED_ACCESS_DIRECTORY_PERMISSIONS" />
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
|
||||||
|
|
||||||
<application android:label="@string/settings_label"
|
<application android:label="@string/settings_label"
|
||||||
android:icon="@drawable/ic_launcher_settings"
|
android:icon="@drawable/ic_launcher_settings"
|
||||||
|
@@ -7193,6 +7193,9 @@
|
|||||||
<!-- Sound: Title for the option managing media volume. [CHAR LIMIT=30] -->
|
<!-- Sound: Title for the option managing media volume. [CHAR LIMIT=30] -->
|
||||||
<string name="media_volume_option_title">Media volume</string>
|
<string name="media_volume_option_title">Media volume</string>
|
||||||
|
|
||||||
|
<!-- Sound: Title for the option managing remote media volume. [CHAR LIMIT=30] -->
|
||||||
|
<string name="remote_media_volume_option_title">Cast volume</string>
|
||||||
|
|
||||||
<!-- Sound: Title for the option managing call volume. [CHAR LIMIT=30] -->
|
<!-- Sound: Title for the option managing call volume. [CHAR LIMIT=30] -->
|
||||||
<string name="call_volume_option_title">Call volume</string>
|
<string name="call_volume_option_title">Call volume</string>
|
||||||
|
|
||||||
|
@@ -22,6 +22,15 @@
|
|||||||
settings:keywords="@string/keywords_sounds"
|
settings:keywords="@string/keywords_sounds"
|
||||||
settings:initialExpandedChildrenCount="9">
|
settings:initialExpandedChildrenCount="9">
|
||||||
|
|
||||||
|
<!-- Remote volume -->
|
||||||
|
<com.android.settings.notification.RemoteVolumeSeekBarPreference
|
||||||
|
android:key="remote_volume"
|
||||||
|
android:icon="@drawable/ic_volume_remote"
|
||||||
|
android:title="@string/remote_media_volume_option_title"
|
||||||
|
android:order="-185"
|
||||||
|
settings:allowDynamicSummaryInSlice="true"
|
||||||
|
settings:controller="com.android.settings.notification.RemoteVolumePreferenceController"/>
|
||||||
|
|
||||||
<!-- Media volume -->
|
<!-- Media volume -->
|
||||||
<com.android.settings.notification.VolumeSeekBarPreference
|
<com.android.settings.notification.VolumeSeekBarPreference
|
||||||
android:key="media_volume"
|
android:key="media_volume"
|
||||||
|
@@ -0,0 +1,159 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 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.session.MediaController;
|
||||||
|
import android.media.session.MediaSession;
|
||||||
|
import android.media.session.MediaSessionManager;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.lifecycle.OnLifecycleEvent;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
import com.android.settingslib.volume.MediaSessions;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class RemoteVolumePreferenceController extends
|
||||||
|
VolumeSeekBarPreferenceController {
|
||||||
|
|
||||||
|
private static final String KEY_REMOTE_VOLUME = "remote_volume";
|
||||||
|
@VisibleForTesting
|
||||||
|
static final int REMOTE_VOLUME = 100;
|
||||||
|
|
||||||
|
private MediaSessionManager mMediaSessionManager;
|
||||||
|
private MediaSessions mMediaSessions;
|
||||||
|
private MediaSession.Token mActiveToken;
|
||||||
|
|
||||||
|
private MediaSessions.Callbacks mCallbacks = new MediaSessions.Callbacks() {
|
||||||
|
@Override
|
||||||
|
public void onRemoteUpdate(MediaSession.Token token, String name,
|
||||||
|
MediaController.PlaybackInfo pi) {
|
||||||
|
mActiveToken = token;
|
||||||
|
mPreference.setMax(pi.getMaxVolume());
|
||||||
|
mPreference.setVisible(true);
|
||||||
|
setSliderPosition(pi.getCurrentVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemoteRemoved(MediaSession.Token t) {
|
||||||
|
if (mActiveToken == t) {
|
||||||
|
mActiveToken = null;
|
||||||
|
mPreference.setVisible(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemoteVolumeChanged(MediaSession.Token token, int flags) {
|
||||||
|
if (mActiveToken == token) {
|
||||||
|
final MediaController mediaController = new MediaController(mContext, token);
|
||||||
|
final MediaController.PlaybackInfo pi = mediaController.getPlaybackInfo();
|
||||||
|
setSliderPosition(pi.getCurrentVolume());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public RemoteVolumePreferenceController(Context context) {
|
||||||
|
super(context, KEY_REMOTE_VOLUME);
|
||||||
|
mMediaSessionManager = context.getSystemService(MediaSessionManager.class);
|
||||||
|
mMediaSessions = new MediaSessions(context, Looper.getMainLooper(), mCallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
final List<MediaController> controllers = mMediaSessionManager.getActiveSessions(null);
|
||||||
|
for (MediaController mediaController : controllers) {
|
||||||
|
final MediaController.PlaybackInfo pi = mediaController.getPlaybackInfo();
|
||||||
|
if (isRemote(pi)) {
|
||||||
|
mActiveToken = mediaController.getSessionToken();
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No active remote media at this point
|
||||||
|
return CONDITIONALLY_UNAVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
mMediaSessions.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
|
||||||
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
mMediaSessions.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSliderPosition() {
|
||||||
|
if (mPreference != null) {
|
||||||
|
return mPreference.getProgress();
|
||||||
|
}
|
||||||
|
//TODO(b/126199571): get it from media controller
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setSliderPosition(int position) {
|
||||||
|
if (mPreference != null) {
|
||||||
|
mPreference.setProgress(position);
|
||||||
|
}
|
||||||
|
//TODO(b/126199571): set it through media controller
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxSteps() {
|
||||||
|
if (mPreference != null) {
|
||||||
|
return mPreference.getMax();
|
||||||
|
}
|
||||||
|
//TODO(b/126199571): get it from media controller
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSliceable() {
|
||||||
|
return TextUtils.equals(getPreferenceKey(), KEY_REMOTE_VOLUME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPreferenceKey() {
|
||||||
|
return KEY_REMOTE_VOLUME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAudioStream() {
|
||||||
|
// This can be anything because remote volume controller doesn't rely on it.
|
||||||
|
return REMOTE_VOLUME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMuteIcon() {
|
||||||
|
return R.drawable.ic_volume_remote_mute;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isRemote(MediaController.PlaybackInfo pi) {
|
||||||
|
return pi != null
|
||||||
|
&& pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 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.util.AttributeSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A slider preference that controls remote volume, which doesn't go through
|
||||||
|
* {@link android.media.AudioManager}
|
||||||
|
**/
|
||||||
|
public class RemoteVolumeSeekBarPreference extends VolumeSeekBarPreference {
|
||||||
|
|
||||||
|
public RemoteVolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||||
|
int defStyleRes) {
|
||||||
|
super(context, attrs, defStyleAttr, defStyleRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteVolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteVolumeSeekBarPreference(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteVolumeSeekBarPreference(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStream(int stream) {
|
||||||
|
// Do nothing here, volume is not controlled by AudioManager
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void init() {
|
||||||
|
if (mSeekBar == null) return;
|
||||||
|
updateIconView();
|
||||||
|
updateSuppressionText();
|
||||||
|
}
|
||||||
|
}
|
@@ -184,6 +184,7 @@ public class SoundSettings extends DashboardFragment implements OnActivityResult
|
|||||||
volumeControllers.add(use(RingVolumePreferenceController.class));
|
volumeControllers.add(use(RingVolumePreferenceController.class));
|
||||||
volumeControllers.add(use(NotificationVolumePreferenceController.class));
|
volumeControllers.add(use(NotificationVolumePreferenceController.class));
|
||||||
volumeControllers.add(use(CallVolumePreferenceController.class));
|
volumeControllers.add(use(CallVolumePreferenceController.class));
|
||||||
|
volumeControllers.add(use(RemoteVolumePreferenceController.class));
|
||||||
|
|
||||||
use(MediaOutputPreferenceController.class).setCallback(listPreference ->
|
use(MediaOutputPreferenceController.class).setCallback(listPreference ->
|
||||||
onPreferenceDataChanged(listPreference));
|
onPreferenceDataChanged(listPreference));
|
||||||
|
@@ -40,8 +40,8 @@ import java.util.Objects;
|
|||||||
public class VolumeSeekBarPreference extends SeekBarPreference {
|
public class VolumeSeekBarPreference extends SeekBarPreference {
|
||||||
private static final String TAG = "VolumeSeekBarPreference";
|
private static final String TAG = "VolumeSeekBarPreference";
|
||||||
|
|
||||||
|
protected SeekBar mSeekBar;
|
||||||
private int mStream;
|
private int mStream;
|
||||||
private SeekBar mSeekBar;
|
|
||||||
private SeekBarVolumizer mVolumizer;
|
private SeekBarVolumizer mVolumizer;
|
||||||
private Callback mCallback;
|
private Callback mCallback;
|
||||||
private ImageView mIconView;
|
private ImageView mIconView;
|
||||||
@@ -121,7 +121,7 @@ public class VolumeSeekBarPreference extends SeekBarPreference {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
protected void init() {
|
||||||
if (mSeekBar == null) return;
|
if (mSeekBar == null) return;
|
||||||
final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
|
final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -158,7 +158,7 @@ public class VolumeSeekBarPreference extends SeekBarPreference {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateIconView() {
|
protected void updateIconView() {
|
||||||
if (mIconView == null) return;
|
if (mIconView == null) return;
|
||||||
if (mIconResId != 0) {
|
if (mIconResId != 0) {
|
||||||
mIconView.setImageResource(mIconResId);
|
mIconView.setImageResource(mIconResId);
|
||||||
@@ -195,7 +195,7 @@ public class VolumeSeekBarPreference extends SeekBarPreference {
|
|||||||
updateSuppressionText();
|
updateSuppressionText();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateSuppressionText() {
|
protected void updateSuppressionText() {
|
||||||
if (mSuppressionTextView != null && mSeekBar != null) {
|
if (mSuppressionTextView != null && mSeekBar != null) {
|
||||||
mSuppressionTextView.setText(mSuppressionText);
|
mSuppressionTextView.setText(mSuppressionText);
|
||||||
final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText);
|
final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText);
|
||||||
|
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 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 static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.media.session.MediaController;
|
||||||
|
import android.media.session.MediaSessionManager;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class RemoteVolumePreferenceControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MediaSessionManager mMediaSessionManager;
|
||||||
|
@Mock
|
||||||
|
private MediaController mMediaController;
|
||||||
|
private RemoteVolumePreferenceController mController;
|
||||||
|
private Context mContext;
|
||||||
|
private List<MediaController> mActiveSessions;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
when(mContext.getSystemService(MediaSessionManager.class)).thenReturn(mMediaSessionManager);
|
||||||
|
mActiveSessions = new ArrayList<>();
|
||||||
|
mActiveSessions.add(mMediaController);
|
||||||
|
when(mMediaSessionManager.getActiveSessions(null)).thenReturn(
|
||||||
|
mActiveSessions);
|
||||||
|
|
||||||
|
mController = new RemoteVolumePreferenceController(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAvailable_containRemoteMedia_returnTrue() {
|
||||||
|
when(mMediaController.getPlaybackInfo()).thenReturn(
|
||||||
|
new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
|
||||||
|
0, 0, 0, null));
|
||||||
|
assertThat(mController.isAvailable()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAvailable_noRemoteMedia_returnFalse() {
|
||||||
|
when(mMediaController.getPlaybackInfo()).thenReturn(
|
||||||
|
new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
|
||||||
|
0, 0, 0, null));
|
||||||
|
assertThat(mController.isAvailable()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getMuteIcon_returnMuteIcon() {
|
||||||
|
assertThat(mController.getMuteIcon()).isEqualTo(R.drawable.ic_volume_remote_mute);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAudioStream_returnRemoteVolume() {
|
||||||
|
assertThat(mController.getAudioStream()).isEqualTo(
|
||||||
|
RemoteVolumePreferenceController.REMOTE_VOLUME);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user