blob: 8e5c73bfc022710d29fe2592776135b7024eb954 [file] [log] [blame]
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001/*
2 * Copyright (C) 2008 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
17package com.android.server;
18
Eric Laurenta1f05cc2019-06-21 11:39:28 -070019import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT;
20import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT_BIT;
21import static com.android.server.input.InputManagerService.SW_LINEOUT_INSERT;
22import static com.android.server.input.InputManagerService.SW_LINEOUT_INSERT_BIT;
23import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT;
24import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT_BIT;
25
Craig Mautner2f39e9f2012-09-21 11:39:54 -070026import android.content.Context;
Eric Laurenta1f05cc2019-06-21 11:39:28 -070027import android.media.AudioManager;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070028import android.os.Handler;
29import android.os.Looper;
30import android.os.Message;
31import android.os.PowerManager;
32import android.os.PowerManager.WakeLock;
33import android.os.UEventObserver;
Eric Laurenta1f05cc2019-06-21 11:39:28 -070034import android.util.Log;
Nick Chalko5f1402c2018-11-09 11:52:16 -080035import android.util.Pair;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070036import android.util.Slog;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070037import android.view.InputDevice;
38
39import com.android.internal.R;
40import com.android.server.input.InputManagerService;
41import com.android.server.input.InputManagerService.WiredAccessoryCallbacks;
Nick Chalko5f1402c2018-11-09 11:52:16 -080042
Craig Mautner2f39e9f2012-09-21 11:39:54 -070043import java.io.File;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070044import java.io.FileNotFoundException;
Eric Laurenta1f05cc2019-06-21 11:39:28 -070045import java.io.FileReader;
Nick Chalko5f1402c2018-11-09 11:52:16 -080046import java.io.IOException;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070047import java.util.ArrayList;
48import java.util.List;
Jeff Sharkeyfea17de2013-06-11 14:13:09 -070049import java.util.Locale;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070050
51/**
52 * <p>WiredAccessoryManager monitors for a wired headset on the main board or dock using
53 * both the InputManagerService notifyWiredAccessoryChanged interface and the UEventObserver
54 * subsystem.
55 */
56final class WiredAccessoryManager implements WiredAccessoryCallbacks {
57 private static final String TAG = WiredAccessoryManager.class.getSimpleName();
Nick Chalko5f1402c2018-11-09 11:52:16 -080058 private static final boolean LOG = false;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070059
60 private static final int BIT_HEADSET = (1 << 0);
61 private static final int BIT_HEADSET_NO_MIC = (1 << 1);
62 private static final int BIT_USB_HEADSET_ANLG = (1 << 2);
63 private static final int BIT_USB_HEADSET_DGTL = (1 << 3);
64 private static final int BIT_HDMI_AUDIO = (1 << 4);
Jon Eklund43cc8bb2014-07-28 16:07:24 -050065 private static final int BIT_LINEOUT = (1 << 5);
Nick Chalko5f1402c2018-11-09 11:52:16 -080066 private static final int SUPPORTED_HEADSETS = (BIT_HEADSET | BIT_HEADSET_NO_MIC |
67 BIT_USB_HEADSET_ANLG | BIT_USB_HEADSET_DGTL |
68 BIT_HDMI_AUDIO | BIT_LINEOUT);
Craig Mautner2f39e9f2012-09-21 11:39:54 -070069
70 private static final String NAME_H2W = "h2w";
71 private static final String NAME_USB_AUDIO = "usb_audio";
72 private static final String NAME_HDMI_AUDIO = "hdmi_audio";
73 private static final String NAME_HDMI = "hdmi";
74
75 private static final int MSG_NEW_DEVICE_STATE = 1;
Eric Laurent4a5eeb92014-05-06 10:49:04 -070076 private static final int MSG_SYSTEM_READY = 2;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070077
78 private final Object mLock = new Object();
79
80 private final WakeLock mWakeLock; // held while there is a pending route change
81 private final AudioManager mAudioManager;
82
83 private int mHeadsetState;
84
85 private int mSwitchValues;
86
87 private final WiredAccessoryObserver mObserver;
Nick Chalko5f1402c2018-11-09 11:52:16 -080088 private final WiredAccessoryExtconObserver mExtconObserver;
Craig Mautner2f39e9f2012-09-21 11:39:54 -070089 private final InputManagerService mInputManager;
90
91 private final boolean mUseDevInputEventForAudioJack;
92
93 public WiredAccessoryManager(Context context, InputManagerService inputManager) {
Nick Chalko5f1402c2018-11-09 11:52:16 -080094 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Craig Mautner2f39e9f2012-09-21 11:39:54 -070095 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryManager");
96 mWakeLock.setReferenceCounted(false);
Nick Chalko5f1402c2018-11-09 11:52:16 -080097 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Craig Mautner2f39e9f2012-09-21 11:39:54 -070098 mInputManager = inputManager;
99
100 mUseDevInputEventForAudioJack =
101 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
102
Nick Chalko5f1402c2018-11-09 11:52:16 -0800103 mExtconObserver = new WiredAccessoryExtconObserver();
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700104 mObserver = new WiredAccessoryObserver();
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700105 }
106
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700107 private void onSystemReady() {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700108 if (mUseDevInputEventForAudioJack) {
109 int switchValues = 0;
Nick Chalko5f1402c2018-11-09 11:52:16 -0800110 if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_HEADPHONE_INSERT)
111 == 1) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700112 switchValues |= SW_HEADPHONE_INSERT_BIT;
113 }
Nick Chalko5f1402c2018-11-09 11:52:16 -0800114 if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_MICROPHONE_INSERT)
115 == 1) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700116 switchValues |= SW_MICROPHONE_INSERT_BIT;
117 }
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500118 if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_LINEOUT_INSERT) == 1) {
119 switchValues |= SW_LINEOUT_INSERT_BIT;
120 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700121 notifyWiredAccessoryChanged(0, switchValues,
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500122 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700123 }
124
Nick Chalko5f1402c2018-11-09 11:52:16 -0800125
126 if (ExtconUEventObserver.extconExists()) {
127 if (mUseDevInputEventForAudioJack) {
128 Log.w(TAG, "Both input event and extcon are used for audio jack,"
129 + " please just choose one.");
130 }
131 mExtconObserver.init();
132 } else {
133 mObserver.init();
134 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700135 }
136
137 @Override
138 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask) {
Nick Chalko5f1402c2018-11-09 11:52:16 -0800139 if (LOG) {
140 Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos
141 + " bits=" + switchCodeToString(switchValues, switchMask)
142 + " mask=" + Integer.toHexString(switchMask));
143 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700144
145 synchronized (mLock) {
146 int headset;
147 mSwitchValues = (mSwitchValues & ~switchMask) | switchValues;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500148 switch (mSwitchValues &
Nick Chalko5f1402c2018-11-09 11:52:16 -0800149 (SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT)) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700150 case 0:
151 headset = 0;
152 break;
153
154 case SW_HEADPHONE_INSERT_BIT:
155 headset = BIT_HEADSET_NO_MIC;
156 break;
157
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500158 case SW_LINEOUT_INSERT_BIT:
159 headset = BIT_LINEOUT;
160 break;
161
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700162 case SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT:
163 headset = BIT_HEADSET;
164 break;
165
166 case SW_MICROPHONE_INSERT_BIT:
167 headset = BIT_HEADSET;
168 break;
169
170 default:
171 headset = 0;
172 break;
173 }
174
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500175 updateLocked(NAME_H2W,
Nick Chalko5f1402c2018-11-09 11:52:16 -0800176 (mHeadsetState & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) | headset);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700177 }
178 }
179
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700180 @Override
181 public void systemReady() {
182 synchronized (mLock) {
183 mWakeLock.acquire();
184
185 Message msg = mHandler.obtainMessage(MSG_SYSTEM_READY, 0, 0, null);
186 mHandler.sendMessage(msg);
187 }
188 }
189
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700190 /**
191 * Compare the existing headset state with the new state and pass along accordingly. Note
192 * that this only supports a single headset at a time. Inserting both a usb and jacked headset
193 * results in support for the last one plugged in. Similarly, unplugging either is seen as
194 * unplugging all.
195 *
Nick Chalko5f1402c2018-11-09 11:52:16 -0800196 * @param newName One of the NAME_xxx variables defined above.
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700197 * @param newState 0 or one of the BIT_xxx variables defined above.
198 */
199 private void updateLocked(String newName, int newState) {
200 // Retain only relevant bits
201 int headsetState = newState & SUPPORTED_HEADSETS;
202 int usb_headset_anlg = headsetState & BIT_USB_HEADSET_ANLG;
203 int usb_headset_dgtl = headsetState & BIT_USB_HEADSET_DGTL;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500204 int h2w_headset = headsetState & (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700205 boolean h2wStateChange = true;
206 boolean usbStateChange = true;
Nick Chalko5f1402c2018-11-09 11:52:16 -0800207 if (LOG) {
208 Slog.v(TAG, "newName=" + newName
209 + " newState=" + newState
210 + " headsetState=" + headsetState
211 + " prev headsetState=" + mHeadsetState);
212 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700213
214 if (mHeadsetState == headsetState) {
215 Log.e(TAG, "No state change.");
216 return;
217 }
218
219 // reject all suspect transitions: only accept state changes from:
220 // - a: 0 headset to 1 headset
221 // - b: 1 headset to 0 headset
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500222 if (h2w_headset == (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700223 Log.e(TAG, "Invalid combination, unsetting h2w flag");
224 h2wStateChange = false;
225 }
226 // - c: 0 usb headset to 1 usb headset
227 // - d: 1 usb headset to 0 usb headset
228 if (usb_headset_anlg == BIT_USB_HEADSET_ANLG && usb_headset_dgtl == BIT_USB_HEADSET_DGTL) {
229 Log.e(TAG, "Invalid combination, unsetting usb flag");
230 usbStateChange = false;
231 }
232 if (!h2wStateChange && !usbStateChange) {
233 Log.e(TAG, "invalid transition, returning ...");
234 return;
235 }
236
237 mWakeLock.acquire();
238
Paul McLean338f27a2015-05-13 15:41:00 -0700239 Log.i(TAG, "MSG_NEW_DEVICE_STATE");
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700240 Message msg = mHandler.obtainMessage(MSG_NEW_DEVICE_STATE, headsetState,
Paul McLean338f27a2015-05-13 15:41:00 -0700241 mHeadsetState, "");
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700242 mHandler.sendMessage(msg);
243
244 mHeadsetState = headsetState;
245 }
246
247 private final Handler mHandler = new Handler(Looper.myLooper(), null, true) {
248 @Override
249 public void handleMessage(Message msg) {
250 switch (msg.what) {
251 case MSG_NEW_DEVICE_STATE:
Nick Chalko5f1402c2018-11-09 11:52:16 -0800252 setDevicesState(msg.arg1, msg.arg2, (String) msg.obj);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700253 mWakeLock.release();
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700254 break;
255 case MSG_SYSTEM_READY:
256 onSystemReady();
257 mWakeLock.release();
258 break;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700259 }
260 }
261 };
262
263 private void setDevicesState(
264 int headsetState, int prevHeadsetState, String headsetName) {
265 synchronized (mLock) {
266 int allHeadsets = SUPPORTED_HEADSETS;
267 for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
268 if ((curHeadset & allHeadsets) != 0) {
269 setDeviceStateLocked(curHeadset, headsetState, prevHeadsetState, headsetName);
270 allHeadsets &= ~curHeadset;
271 }
272 }
273 }
274 }
275
276 private void setDeviceStateLocked(int headset,
277 int headsetState, int prevHeadsetState, String headsetName) {
278 if ((headsetState & headset) != (prevHeadsetState & headset)) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700279 int outDevice = 0;
280 int inDevice = 0;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700281 int state;
282
283 if ((headsetState & headset) != 0) {
284 state = 1;
285 } else {
286 state = 0;
287 }
288
289 if (headset == BIT_HEADSET) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700290 outDevice = AudioManager.DEVICE_OUT_WIRED_HEADSET;
291 inDevice = AudioManager.DEVICE_IN_WIRED_HEADSET;
Nick Chalko5f1402c2018-11-09 11:52:16 -0800292 } else if (headset == BIT_HEADSET_NO_MIC) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700293 outDevice = AudioManager.DEVICE_OUT_WIRED_HEADPHONE;
Nick Chalko5f1402c2018-11-09 11:52:16 -0800294 } else if (headset == BIT_LINEOUT) {
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500295 outDevice = AudioManager.DEVICE_OUT_LINE;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700296 } else if (headset == BIT_USB_HEADSET_ANLG) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700297 outDevice = AudioManager.DEVICE_OUT_ANLG_DOCK_HEADSET;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700298 } else if (headset == BIT_USB_HEADSET_DGTL) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700299 outDevice = AudioManager.DEVICE_OUT_DGTL_DOCK_HEADSET;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700300 } else if (headset == BIT_HDMI_AUDIO) {
Eric Laurentae4506e2014-05-29 16:04:32 -0700301 outDevice = AudioManager.DEVICE_OUT_HDMI;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700302 } else {
Nick Chalko5f1402c2018-11-09 11:52:16 -0800303 Slog.e(TAG, "setDeviceState() invalid headset type: " + headset);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700304 return;
305 }
306
Paul McLean10804eb2015-01-28 11:16:35 -0800307 if (LOG) {
308 Slog.v(TAG, "headsetName: " + headsetName +
309 (state == 1 ? " connected" : " disconnected"));
310 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700311
Eric Laurentae4506e2014-05-29 16:04:32 -0700312 if (outDevice != 0) {
Nick Chalko5f1402c2018-11-09 11:52:16 -0800313 mAudioManager.setWiredDeviceConnectionState(outDevice, state, "", headsetName);
Eric Laurentae4506e2014-05-29 16:04:32 -0700314 }
315 if (inDevice != 0) {
Nick Chalko5f1402c2018-11-09 11:52:16 -0800316 mAudioManager.setWiredDeviceConnectionState(inDevice, state, "", headsetName);
Eric Laurentae4506e2014-05-29 16:04:32 -0700317 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700318 }
319 }
320
321 private String switchCodeToString(int switchValues, int switchMask) {
322 StringBuffer sb = new StringBuffer();
323 if ((switchMask & SW_HEADPHONE_INSERT_BIT) != 0 &&
324 (switchValues & SW_HEADPHONE_INSERT_BIT) != 0) {
325 sb.append("SW_HEADPHONE_INSERT ");
326 }
327 if ((switchMask & SW_MICROPHONE_INSERT_BIT) != 0 &&
328 (switchValues & SW_MICROPHONE_INSERT_BIT) != 0) {
329 sb.append("SW_MICROPHONE_INSERT");
330 }
331 return sb.toString();
332 }
333
334 class WiredAccessoryObserver extends UEventObserver {
335 private final List<UEventInfo> mUEventInfo;
336
337 public WiredAccessoryObserver() {
338 mUEventInfo = makeObservedUEventList();
339 }
340
341 void init() {
342 synchronized (mLock) {
343 if (LOG) Slog.v(TAG, "init()");
344 char[] buffer = new char[1024];
345
346 for (int i = 0; i < mUEventInfo.size(); ++i) {
347 UEventInfo uei = mUEventInfo.get(i);
348 try {
349 int curState;
350 FileReader file = new FileReader(uei.getSwitchStatePath());
351 int len = file.read(buffer, 0, 1024);
352 file.close();
Narayan Kamatha09b4d22016-04-15 18:32:45 +0100353 curState = Integer.parseInt((new String(buffer, 0, len)).trim());
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700354
355 if (curState > 0) {
356 updateStateLocked(uei.getDevPath(), uei.getDevName(), curState);
357 }
358 } catch (FileNotFoundException e) {
359 Slog.w(TAG, uei.getSwitchStatePath() +
360 " not found while attempting to determine initial switch state");
361 } catch (Exception e) {
Nick Chalkoc2740c22018-09-18 12:04:53 -0700362 Slog.e(TAG, "Error while attempting to determine initial switch state for "
Nick Chalko5f1402c2018-11-09 11:52:16 -0800363 + uei.getDevName(), e);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700364 }
365 }
366 }
367
368 // At any given time accessories could be inserted
369 // one on the board, one on the dock and one on HDMI:
370 // observe three UEVENTs
371 for (int i = 0; i < mUEventInfo.size(); ++i) {
372 UEventInfo uei = mUEventInfo.get(i);
Nick Chalko5f1402c2018-11-09 11:52:16 -0800373 startObserving("DEVPATH=" + uei.getDevPath());
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700374 }
375 }
376
377 private List<UEventInfo> makeObservedUEventList() {
378 List<UEventInfo> retVal = new ArrayList<UEventInfo>();
379 UEventInfo uei;
380
381 // Monitor h2w
382 if (!mUseDevInputEventForAudioJack) {
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500383 uei = new UEventInfo(NAME_H2W, BIT_HEADSET, BIT_HEADSET_NO_MIC, BIT_LINEOUT);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700384 if (uei.checkSwitchExists()) {
385 retVal.add(uei);
386 } else {
387 Slog.w(TAG, "This kernel does not have wired headset support");
388 }
389 }
390
391 // Monitor USB
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500392 uei = new UEventInfo(NAME_USB_AUDIO, BIT_USB_HEADSET_ANLG, BIT_USB_HEADSET_DGTL, 0);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700393 if (uei.checkSwitchExists()) {
394 retVal.add(uei);
395 } else {
396 Slog.w(TAG, "This kernel does not have usb audio support");
397 }
398
399 // Monitor HDMI
400 //
401 // If the kernel has support for the "hdmi_audio" switch, use that. It will be
402 // signalled only when the HDMI driver has a video mode configured, and the downstream
403 // sink indicates support for audio in its EDID.
404 //
405 // If the kernel does not have an "hdmi_audio" switch, just fall back on the older
406 // "hdmi" switch instead.
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500407 uei = new UEventInfo(NAME_HDMI_AUDIO, BIT_HDMI_AUDIO, 0, 0);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700408 if (uei.checkSwitchExists()) {
409 retVal.add(uei);
410 } else {
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500411 uei = new UEventInfo(NAME_HDMI, BIT_HDMI_AUDIO, 0, 0);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700412 if (uei.checkSwitchExists()) {
413 retVal.add(uei);
414 } else {
415 Slog.w(TAG, "This kernel does not have HDMI audio support");
416 }
417 }
418
419 return retVal;
420 }
421
422 @Override
423 public void onUEvent(UEventObserver.UEvent event) {
424 if (LOG) Slog.v(TAG, "Headset UEVENT: " + event.toString());
425
426 try {
427 String devPath = event.get("DEVPATH");
428 String name = event.get("SWITCH_NAME");
429 int state = Integer.parseInt(event.get("SWITCH_STATE"));
430 synchronized (mLock) {
431 updateStateLocked(devPath, name, state);
432 }
433 } catch (NumberFormatException e) {
434 Slog.e(TAG, "Could not parse switch state from event " + event);
435 }
436 }
437
438 private void updateStateLocked(String devPath, String name, int state) {
439 for (int i = 0; i < mUEventInfo.size(); ++i) {
440 UEventInfo uei = mUEventInfo.get(i);
441 if (devPath.equals(uei.getDevPath())) {
442 updateLocked(name, uei.computeNewHeadsetState(mHeadsetState, state));
443 return;
444 }
445 }
446 }
447
448 private final class UEventInfo {
449 private final String mDevName;
450 private final int mState1Bits;
451 private final int mState2Bits;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500452 private final int mStateNbits;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700453
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500454 public UEventInfo(String devName, int state1Bits, int state2Bits, int stateNbits) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700455 mDevName = devName;
456 mState1Bits = state1Bits;
457 mState2Bits = state2Bits;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500458 mStateNbits = stateNbits;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700459 }
460
Nick Chalko5f1402c2018-11-09 11:52:16 -0800461 public String getDevName() {
462 return mDevName;
463 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700464
465 public String getDevPath() {
Jeff Sharkeyfea17de2013-06-11 14:13:09 -0700466 return String.format(Locale.US, "/devices/virtual/switch/%s", mDevName);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700467 }
468
469 public String getSwitchStatePath() {
Jeff Sharkeyfea17de2013-06-11 14:13:09 -0700470 return String.format(Locale.US, "/sys/class/switch/%s/state", mDevName);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700471 }
472
473 public boolean checkSwitchExists() {
474 File f = new File(getSwitchStatePath());
475 return f.exists();
476 }
477
478 public int computeNewHeadsetState(int headsetState, int switchState) {
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500479 int preserveMask = ~(mState1Bits | mState2Bits | mStateNbits);
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700480 int setBits = ((switchState == 1) ? mState1Bits :
Nick Chalko5f1402c2018-11-09 11:52:16 -0800481 ((switchState == 2) ? mState2Bits :
482 ((switchState == mStateNbits) ? mStateNbits : 0)));
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700483
484 return ((headsetState & preserveMask) | setBits);
485 }
486 }
487 }
Nick Chalko5f1402c2018-11-09 11:52:16 -0800488
489 private class WiredAccessoryExtconObserver extends ExtconStateObserver<Pair<Integer, Integer>> {
490 private final List<ExtconInfo> mExtconInfos;
491
492 WiredAccessoryExtconObserver() {
493 mExtconInfos = ExtconInfo.getExtconInfos(".*audio.*");
494
495 }
496
497 private void init() {
498 for (ExtconInfo extconInfo : mExtconInfos) {
499 Pair<Integer, Integer> state = null;
500 try {
501 state = parseStateFromFile(extconInfo);
502 } catch (FileNotFoundException e) {
503 Slog.w(TAG, extconInfo.getStatePath()
504 + " not found while attempting to determine initial state", e);
505 } catch (IOException e) {
506 Slog.e(
507 TAG,
508 "Error reading " + extconInfo.getStatePath()
509 + " while attempting to determine initial state",
510 e);
511 }
512 if (state != null) {
513 updateState(extconInfo, extconInfo.getName(), state);
514 }
515 if (LOG) Slog.d(TAG, "observing " + extconInfo.getName());
516 startObserving(extconInfo);
517 }
518
519 }
520
521 @Override
522 public Pair<Integer, Integer> parseState(ExtconInfo extconInfo, String status) {
523 if (LOG) Slog.v(TAG, "status " + status);
524 int []maskAndState = {0,0};
525 // extcon event state changes from kernel4.9
526 // new state will be like STATE=MICROPHONE=1\nHEADPHONE=0
527 updateBit(maskAndState, BIT_HEADSET_NO_MIC, status, "HEADPHONE") ;
528 updateBit(maskAndState, BIT_HEADSET, status,"MICROPHONE") ;
529 updateBit(maskAndState, BIT_HDMI_AUDIO, status,"HDMI") ;
530 updateBit(maskAndState, BIT_LINEOUT, status,"LINE-OUT") ;
531 if (LOG) Slog.v(TAG, "mask " + maskAndState[0] + " state " + maskAndState[1]);
532 return Pair.create(maskAndState[0],maskAndState[1]);
533 }
534
535 @Override
536 public void updateState(ExtconInfo extconInfo, String name,
537 Pair<Integer, Integer> maskAndState) {
538 synchronized (mLock) {
539 int mask = maskAndState.first;
540 int state = maskAndState.second;
Eric Laurenta1f05cc2019-06-21 11:39:28 -0700541 updateLocked(name, mHeadsetState & ~(mask & ~state) | (mask & state));
Nick Chalko5f1402c2018-11-09 11:52:16 -0800542 return;
543 }
544 }
545 }
546
547 /**
548 * Updates the mask bit at {@code position} to 1 and the state bit at {@code position} to true
549 * if {@code name=1} or false if {}@code name=0} is contained in {@code state}.
550 */
551 private static void updateBit(int[] maskAndState, int position, String state, String name) {
552 maskAndState[0] |= position;
553 if (state.contains(name + "=1")) {
554 maskAndState[0] |= position;
555 maskAndState[1] |= position;
556 } else if (state.contains(name + "=0")) {
557 maskAndState[0] |= position;
558 maskAndState[1] &= ~position;
559 }
560 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700561}