1 /*
2  * Copyright (C) 2018 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 java.util.List;
35 import org.robolectric.annotation.Implementation;
36 import org.robolectric.annotation.Implements;
37 import org.robolectric.shadow.api.Shadow;
38 
39 import java.util.ArrayList;
40 
41 @Implements(value = AudioManager.class)
42 public class ShadowAudioManager extends org.robolectric.shadows.ShadowAudioManager {
43     private int mRingerMode;
44     private int mDeviceCodes;
45     private boolean mMusicActiveRemotely;
46     private List<AudioDeviceCallback> mDeviceCallbacks = new ArrayList<>();
47 
48     @Implementation
getRingerModeInternal()49     private int getRingerModeInternal() {
50         return mRingerMode;
51     }
52 
getShadow()53     public static ShadowAudioManager getShadow() {
54         return Shadow.extract(application.getSystemService(AudioManager.class));
55     }
56 
setRingerModeInternal(int mode)57     public void setRingerModeInternal(int mode) {
58         mRingerMode = mode;
59     }
60 
61     @Implementation
registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler)62     public void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) {
63         mDeviceCallbacks.add(callback);
64     }
65 
66     @Implementation
unregisterAudioDeviceCallback(AudioDeviceCallback callback)67     public void unregisterAudioDeviceCallback(AudioDeviceCallback callback) {
68         if (mDeviceCallbacks.contains(callback)) {
69             mDeviceCallbacks.remove(callback);
70         }
71     }
72 
setMusicActiveRemotely(boolean flag)73     public void setMusicActiveRemotely(boolean flag) {
74         mMusicActiveRemotely = flag;
75     }
76 
77     @Implementation
isMusicActiveRemotely()78     public boolean isMusicActiveRemotely() {
79         return mMusicActiveRemotely;
80     }
81 
setOutputDevice(int deviceCodes)82     public void setOutputDevice(int deviceCodes) {
83         mDeviceCodes = deviceCodes;
84     }
85 
86     @Implementation
getDevicesForStream(int streamType)87     public int getDevicesForStream(int streamType) {
88         switch (streamType) {
89             case STREAM_VOICE_CALL:
90             case STREAM_SYSTEM:
91             case STREAM_RING:
92             case STREAM_MUSIC:
93             case STREAM_ALARM:
94             case STREAM_NOTIFICATION:
95             case STREAM_DTMF:
96             case STREAM_ACCESSIBILITY:
97                 return mDeviceCodes;
98             default:
99                 return 0;
100         }
101     }
102 }
103