1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.testutils.shadow; 18 19 import static android.media.AudioManager.STREAM_ACCESSIBILITY; 20 import static android.media.AudioManager.STREAM_ALARM; 21 import static android.media.AudioManager.STREAM_DTMF; 22 import static android.media.AudioManager.STREAM_MUSIC; 23 import static android.media.AudioManager.STREAM_NOTIFICATION; 24 import static android.media.AudioManager.STREAM_RING; 25 import static android.media.AudioManager.STREAM_SYSTEM; 26 import static android.media.AudioManager.STREAM_VOICE_CALL; 27 28 import static org.robolectric.RuntimeEnvironment.application; 29 30 import android.media.AudioDeviceCallback; 31 import android.media.AudioManager; 32 import android.os.Handler; 33 34 import org.robolectric.annotation.Implementation; 35 import org.robolectric.annotation.Implements; 36 import org.robolectric.shadow.api.Shadow; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 /** Robolectric shadow for the AudioManager. */ 42 @Implements(value = AudioManager.class) 43 public class ShadowAudioManager extends org.robolectric.shadows.ShadowAudioManager { 44 private int mRingerMode; 45 private int mDeviceCodes; 46 private boolean mMusicActiveRemotely; 47 private List<AudioDeviceCallback> mDeviceCallbacks = new ArrayList<>(); 48 49 @Implementation getRingerModeInternal()50 protected int getRingerModeInternal() { 51 return mRingerMode; 52 } 53 getShadow()54 public static ShadowAudioManager getShadow() { 55 return Shadow.extract(application.getSystemService(AudioManager.class)); 56 } 57 setRingerModeInternal(int mode)58 public void setRingerModeInternal(int mode) { 59 mRingerMode = mode; 60 } 61 62 /** Register audio device callback. */ 63 @Implementation registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler)64 public void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) { 65 mDeviceCallbacks.add(callback); 66 } 67 68 /** Unregister audio device callback. */ 69 @Implementation unregisterAudioDeviceCallback(AudioDeviceCallback callback)70 public void unregisterAudioDeviceCallback(AudioDeviceCallback callback) { 71 if (mDeviceCallbacks.contains(callback)) { 72 mDeviceCallbacks.remove(callback); 73 } 74 } 75 setMusicActiveRemotely(boolean flag)76 public void setMusicActiveRemotely(boolean flag) { 77 mMusicActiveRemotely = flag; 78 } 79 80 @Implementation isMusicActiveRemotely()81 public boolean isMusicActiveRemotely() { 82 return mMusicActiveRemotely; 83 } 84 85 /** Set output device. */ setOutputDevice(int deviceCodes)86 public void setOutputDevice(int deviceCodes) { 87 mDeviceCodes = deviceCodes; 88 } 89 90 /** Get devices for stream. */ 91 @Implementation getDevicesForStream(int streamType)92 public int getDevicesForStream(int streamType) { 93 switch (streamType) { 94 case STREAM_VOICE_CALL: 95 case STREAM_SYSTEM: 96 case STREAM_RING: 97 case STREAM_MUSIC: 98 case STREAM_ALARM: 99 case STREAM_NOTIFICATION: 100 case STREAM_DTMF: 101 case STREAM_ACCESSIBILITY: 102 return mDeviceCodes; 103 default: 104 return 0; 105 } 106 } 107 } 108