• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MUSIC;
22 import static android.media.AudioManager.STREAM_NOTIFICATION;
23 import static android.media.AudioManager.STREAM_RING;
24 import static android.media.AudioManager.STREAM_SYSTEM;
25 import static android.media.AudioManager.STREAM_VOICE_CALL;
26 import static android.media.AudioManager.STREAM_DTMF;
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.annotation.Resetter;
37 import org.robolectric.shadow.api.Shadow;
38 
39 import java.util.ArrayList;
40 
41 @Implements(value = AudioManager.class, inheritImplementationMethods = true)
42 public class ShadowAudioManager extends org.robolectric.shadows.ShadowAudioManager {
43     private int mRingerMode;
44     private int mDeviceCodes;
45     private boolean mMusicActiveRemotely;
46     private boolean mBluetoothScoOn;
47     private ArrayList<AudioDeviceCallback> mDeviceCallbacks = new ArrayList();
48 
49     @Implementation
getRingerModeInternal()50     private 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     @Implementation
registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler)63     public void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) {
64         mDeviceCallbacks.add(callback);
65     }
66 
67     @Implementation
unregisterAudioDeviceCallback(AudioDeviceCallback callback)68     public void unregisterAudioDeviceCallback(AudioDeviceCallback callback) {
69         if (mDeviceCallbacks.contains(callback)) {
70             mDeviceCallbacks.remove(callback);
71         }
72     }
73 
setMusicActiveRemotely(boolean flag)74     public void setMusicActiveRemotely(boolean flag) {
75         mMusicActiveRemotely = flag;
76     }
77 
78     @Implementation
isMusicActiveRemotely()79     public boolean isMusicActiveRemotely() {
80         return mMusicActiveRemotely;
81     }
82 
setOutputDevice(int deviceCodes)83     public void setOutputDevice(int deviceCodes) {
84         mDeviceCodes = deviceCodes;
85     }
86 
87     @Implementation
getDevicesForStream(int streamType)88     public int getDevicesForStream(int streamType) {
89         switch (streamType) {
90             case STREAM_VOICE_CALL:
91             case STREAM_SYSTEM:
92             case STREAM_RING:
93             case STREAM_MUSIC:
94             case STREAM_ALARM:
95             case STREAM_NOTIFICATION:
96             case STREAM_DTMF:
97             case STREAM_ACCESSIBILITY:
98                 return mDeviceCodes;
99             default:
100                 return 0;
101         }
102     }
103 
104     @Resetter
reset()105     public void reset() {
106         mDeviceCallbacks.clear();
107     }
108 
setBluetoothScoOn(boolean bluetoothScoOn)109     public void setBluetoothScoOn(boolean bluetoothScoOn) {
110         mBluetoothScoOn = bluetoothScoOn;
111     }
112 
113     @Implementation
isBluetoothScoOn()114     public boolean isBluetoothScoOn() { return mBluetoothScoOn; }
115 }
116