1 /*
2  * Copyright (C) 2017 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.server.audio;
18 
19 import android.media.AudioAttributes;
20 import android.media.AudioManager;
21 import android.media.AudioSystem;
22 
23 import com.android.server.audio.AudioDeviceInventory.WiredDeviceConnectionState;
24 
25 
26 public class AudioServiceEvents {
27 
28     final static class PhoneStateEvent extends AudioEventLogger.Event {
29         final String mPackage;
30         final int mOwnerPid;
31         final int mRequesterPid;
32         final int mRequestedMode;
33         final int mActualMode;
34 
PhoneStateEvent(String callingPackage, int requesterPid, int requestedMode, int ownerPid, int actualMode)35         PhoneStateEvent(String callingPackage, int requesterPid, int requestedMode,
36                         int ownerPid, int actualMode) {
37             mPackage = callingPackage;
38             mRequesterPid = requesterPid;
39             mRequestedMode = requestedMode;
40             mOwnerPid = ownerPid;
41             mActualMode = actualMode;
42         }
43 
44         @Override
eventToString()45         public String eventToString() {
46             return new StringBuilder("setMode(").append(AudioSystem.modeToString(mRequestedMode))
47                     .append(") from package=").append(mPackage)
48                     .append(" pid=").append(mRequesterPid)
49                     .append(" selected mode=").append(AudioSystem.modeToString(mActualMode))
50                     .append(" by pid=").append(mOwnerPid).toString();
51         }
52     }
53 
54     final static class WiredDevConnectEvent extends AudioEventLogger.Event {
55         final WiredDeviceConnectionState mState;
56 
WiredDevConnectEvent(WiredDeviceConnectionState state)57         WiredDevConnectEvent(WiredDeviceConnectionState state) {
58             mState = state;
59         }
60 
61         @Override
eventToString()62         public String eventToString() {
63             return new StringBuilder("setWiredDeviceConnectionState(")
64                     .append(" type:").append(Integer.toHexString(mState.mType))
65                     .append(" state:").append(AudioSystem.deviceStateToString(mState.mState))
66                     .append(" addr:").append(mState.mAddress)
67                     .append(" name:").append(mState.mName)
68                     .append(") from ").append(mState.mCaller).toString();
69         }
70     }
71 
72     final static class ForceUseEvent extends AudioEventLogger.Event {
73         final int mUsage;
74         final int mConfig;
75         final String mReason;
76 
ForceUseEvent(int usage, int config, String reason)77         ForceUseEvent(int usage, int config, String reason) {
78             mUsage = usage;
79             mConfig = config;
80             mReason = reason;
81         }
82 
83         @Override
eventToString()84         public String eventToString() {
85             return new StringBuilder("setForceUse(")
86                     .append(AudioSystem.forceUseUsageToString(mUsage))
87                     .append(", ").append(AudioSystem.forceUseConfigToString(mConfig))
88                     .append(") due to ").append(mReason).toString();
89         }
90     }
91 
92     final static class VolumeEvent extends AudioEventLogger.Event {
93         static final int VOL_ADJUST_SUGG_VOL = 0;
94         static final int VOL_ADJUST_STREAM_VOL = 1;
95         static final int VOL_SET_STREAM_VOL = 2;
96         static final int VOL_SET_HEARING_AID_VOL = 3;
97         static final int VOL_SET_AVRCP_VOL = 4;
98         static final int VOL_ADJUST_VOL_UID = 5;
99         static final int VOL_VOICE_ACTIVITY_HEARING_AID = 6;
100         static final int VOL_MODE_CHANGE_HEARING_AID = 7;
101         static final int VOL_SET_GROUP_VOL = 8;
102 
103         final int mOp;
104         final int mStream;
105         final int mVal1;
106         final int mVal2;
107         final String mCaller;
108         final String mGroupName;
109         final AudioAttributes mAudioAttributes;
110 
111         /** used for VOL_ADJUST_VOL_UID,
112          *           VOL_ADJUST_SUGG_VOL,
113          *           VOL_ADJUST_STREAM_VOL,
114          *           VOL_SET_STREAM_VOL */
VolumeEvent(int op, int stream, int val1, int val2, String caller)115         VolumeEvent(int op, int stream, int val1, int val2, String caller) {
116             mOp = op;
117             mStream = stream;
118             mVal1 = val1;
119             mVal2 = val2;
120             mCaller = caller;
121             mGroupName = null;
122             mAudioAttributes = null;
123         }
124 
125         /** used for VOL_SET_HEARING_AID_VOL*/
VolumeEvent(int op, int index, int gainDb)126         VolumeEvent(int op, int index, int gainDb) {
127             mOp = op;
128             mVal1 = index;
129             mVal2 = gainDb;
130             // unused
131             mStream = -1;
132             mCaller = null;
133             mGroupName = null;
134             mAudioAttributes = null;
135         }
136 
137         /** used for VOL_SET_AVRCP_VOL */
VolumeEvent(int op, int index)138         VolumeEvent(int op, int index) {
139             mOp = op;
140             mVal1 = index;
141             // unused
142             mVal2 = 0;
143             mStream = -1;
144             mCaller = null;
145             mGroupName = null;
146             mAudioAttributes = null;
147         }
148 
149         /** used for VOL_VOICE_ACTIVITY_HEARING_AID */
VolumeEvent(int op, boolean voiceActive, int stream, int index)150         VolumeEvent(int op, boolean voiceActive, int stream, int index) {
151             mOp = op;
152             mStream = stream;
153             mVal1 = index;
154             mVal2 = voiceActive ? 1 : 0;
155             // unused
156             mCaller = null;
157             mGroupName = null;
158             mAudioAttributes = null;
159         }
160 
161         /** used for VOL_MODE_CHANGE_HEARING_AID */
VolumeEvent(int op, int mode, int stream, int index)162         VolumeEvent(int op, int mode, int stream, int index) {
163             mOp = op;
164             mStream = stream;
165             mVal1 = index;
166             mVal2 = mode;
167             // unused
168             mCaller = null;
169             mGroupName = null;
170             mAudioAttributes = null;
171         }
172 
173         /** used for VOL_SET_GROUP_VOL */
VolumeEvent(int op, AudioAttributes aa, String group, int index, int flags, String caller)174         VolumeEvent(int op, AudioAttributes aa, String group, int index, int flags, String caller) {
175             mOp = op;
176             mStream = -1;
177             mVal1 = index;
178             mVal2 = flags;
179             mCaller = caller;
180             mGroupName = group;
181             mAudioAttributes = aa;
182         }
183 
184         @Override
eventToString()185         public String eventToString() {
186             switch (mOp) {
187                 case VOL_ADJUST_SUGG_VOL:
188                     return new StringBuilder("adjustSuggestedStreamVolume(sugg:")
189                             .append(AudioSystem.streamToString(mStream))
190                             .append(" dir:").append(AudioManager.adjustToString(mVal1))
191                             .append(" flags:0x").append(Integer.toHexString(mVal2))
192                             .append(") from ").append(mCaller)
193                             .toString();
194                 case VOL_ADJUST_STREAM_VOL:
195                     return new StringBuilder("adjustStreamVolume(stream:")
196                             .append(AudioSystem.streamToString(mStream))
197                             .append(" dir:").append(AudioManager.adjustToString(mVal1))
198                             .append(" flags:0x").append(Integer.toHexString(mVal2))
199                             .append(") from ").append(mCaller)
200                             .toString();
201                 case VOL_SET_STREAM_VOL:
202                     return new StringBuilder("setStreamVolume(stream:")
203                             .append(AudioSystem.streamToString(mStream))
204                             .append(" index:").append(mVal1)
205                             .append(" flags:0x").append(Integer.toHexString(mVal2))
206                             .append(") from ").append(mCaller)
207                             .toString();
208                 case VOL_SET_HEARING_AID_VOL:
209                     return new StringBuilder("setHearingAidVolume:")
210                             .append(" index:").append(mVal1)
211                             .append(" gain dB:").append(mVal2)
212                             .toString();
213                 case VOL_SET_AVRCP_VOL:
214                     return new StringBuilder("setAvrcpVolume:")
215                             .append(" index:").append(mVal1)
216                             .toString();
217                 case VOL_ADJUST_VOL_UID:
218                     return new StringBuilder("adjustStreamVolumeForUid(stream:")
219                             .append(AudioSystem.streamToString(mStream))
220                             .append(" dir:").append(AudioManager.adjustToString(mVal1))
221                             .append(" flags:0x").append(Integer.toHexString(mVal2))
222                             .append(") from ").append(mCaller)
223                             .toString();
224                 case VOL_VOICE_ACTIVITY_HEARING_AID:
225                     return new StringBuilder("Voice activity change (")
226                             .append(mVal2 == 1 ? "active" : "inactive")
227                             .append(") causes setting HEARING_AID volume to idx:").append(mVal1)
228                             .append(" stream:").append(AudioSystem.streamToString(mStream))
229                             .toString();
230                 case VOL_MODE_CHANGE_HEARING_AID:
231                     return new StringBuilder("setMode(")
232                             .append(AudioSystem.modeToString(mVal2))
233                             .append(") causes setting HEARING_AID volume to idx:").append(mVal1)
234                             .append(" stream:").append(AudioSystem.streamToString(mStream))
235                             .toString();
236                 case VOL_SET_GROUP_VOL:
237                     return new StringBuilder("setVolumeIndexForAttributes(attr:")
238                             .append(mAudioAttributes.toString())
239                             .append(" group: ").append(mGroupName)
240                             .append(" index:").append(mVal1)
241                             .append(" flags:0x").append(Integer.toHexString(mVal2))
242                             .append(") from ").append(mCaller)
243                             .toString();
244                 default: return new StringBuilder("FIXME invalid op:").append(mOp).toString();
245             }
246         }
247     }
248 }
249