2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.os.Message;
26 import android.os.PowerManager;
27 import android.os.PowerManager.WakeLock;
28 import android.os.UEventObserver;
29 import android.util.Slog;
30 import android.media.AudioManager;
31 import android.util.Log;
32 import android.view.InputDevice;
34 import com.android.internal.R;
35 import com.android.server.input.InputManagerService;
36 import com.android.server.input.InputManagerService.WiredAccessoryCallbacks;
37 import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT;
38 import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT;
39 import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT_BIT;
40 import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT_BIT;
43 import java.io.FileReader;
44 import java.io.FileNotFoundException;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Locale;
50 * <p>WiredAccessoryManager monitors for a wired headset on the main board or dock using
51 * both the InputManagerService notifyWiredAccessoryChanged interface and the UEventObserver
54 final class WiredAccessoryManager implements WiredAccessoryCallbacks {
55 private static final String TAG = WiredAccessoryManager.class.getSimpleName();
56 private static final boolean LOG = true;
58 private static final int BIT_HEADSET = (1 << 0);
59 private static final int BIT_HEADSET_NO_MIC = (1 << 1);
60 private static final int BIT_USB_HEADSET_ANLG = (1 << 2);
61 private static final int BIT_USB_HEADSET_DGTL = (1 << 3);
62 private static final int BIT_HDMI_AUDIO = (1 << 4);
63 private static final int SUPPORTED_HEADSETS = (BIT_HEADSET|BIT_HEADSET_NO_MIC|
64 BIT_USB_HEADSET_ANLG|BIT_USB_HEADSET_DGTL|
67 private static final String NAME_H2W = "h2w";
68 private static final String NAME_USB_AUDIO = "usb_audio";
69 private static final String NAME_HDMI_AUDIO = "hdmi_audio";
70 private static final String NAME_HDMI = "hdmi";
72 private static final int MSG_NEW_DEVICE_STATE = 1;
74 private final Object mLock = new Object();
76 private final WakeLock mWakeLock; // held while there is a pending route change
77 private final AudioManager mAudioManager;
79 private int mHeadsetState;
81 private int mSwitchValues;
83 private final WiredAccessoryObserver mObserver;
84 private final InputManagerService mInputManager;
86 private final boolean mUseDevInputEventForAudioJack;
88 public WiredAccessoryManager(Context context, InputManagerService inputManager) {
89 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
90 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryManager");
91 mWakeLock.setReferenceCounted(false);
92 mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
93 mInputManager = inputManager;
95 mUseDevInputEventForAudioJack =
96 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
98 mObserver = new WiredAccessoryObserver();
100 context.registerReceiver(new BroadcastReceiver() {
102 public void onReceive(Context ctx, Intent intent) {
106 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
109 private void bootCompleted() {
110 if (mUseDevInputEventForAudioJack) {
111 int switchValues = 0;
112 if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_HEADPHONE_INSERT) == 1) {
113 switchValues |= SW_HEADPHONE_INSERT_BIT;
115 if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_MICROPHONE_INSERT) == 1) {
116 switchValues |= SW_MICROPHONE_INSERT_BIT;
118 notifyWiredAccessoryChanged(0, switchValues,
119 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT);
126 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask) {
127 if (LOG) Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos
128 + " bits=" + switchCodeToString(switchValues, switchMask)
129 + " mask=" + Integer.toHexString(switchMask));
131 synchronized (mLock) {
133 mSwitchValues = (mSwitchValues & ~switchMask) | switchValues;
134 switch (mSwitchValues & (SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT)) {
139 case SW_HEADPHONE_INSERT_BIT:
140 headset = BIT_HEADSET_NO_MIC;
143 case SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT:
144 headset = BIT_HEADSET;
147 case SW_MICROPHONE_INSERT_BIT:
148 headset = BIT_HEADSET;
156 updateLocked(NAME_H2W, (mHeadsetState & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC)) | headset);
161 * Compare the existing headset state with the new state and pass along accordingly. Note
162 * that this only supports a single headset at a time. Inserting both a usb and jacked headset
163 * results in support for the last one plugged in. Similarly, unplugging either is seen as
166 * @param newName One of the NAME_xxx variables defined above.
167 * @param newState 0 or one of the BIT_xxx variables defined above.
169 private void updateLocked(String newName, int newState) {
170 // Retain only relevant bits
171 int headsetState = newState & SUPPORTED_HEADSETS;
172 int usb_headset_anlg = headsetState & BIT_USB_HEADSET_ANLG;
173 int usb_headset_dgtl = headsetState & BIT_USB_HEADSET_DGTL;
174 int h2w_headset = headsetState & (BIT_HEADSET | BIT_HEADSET_NO_MIC);
175 boolean h2wStateChange = true;
176 boolean usbStateChange = true;
177 if (LOG) Slog.v(TAG, "newName=" + newName
178 + " newState=" + newState
179 + " headsetState=" + headsetState
180 + " prev headsetState=" + mHeadsetState);
182 if (mHeadsetState == headsetState) {
183 Log.e(TAG, "No state change.");
187 // reject all suspect transitions: only accept state changes from:
188 // - a: 0 headset to 1 headset
189 // - b: 1 headset to 0 headset
190 if (h2w_headset == (BIT_HEADSET | BIT_HEADSET_NO_MIC)) {
191 Log.e(TAG, "Invalid combination, unsetting h2w flag");
192 h2wStateChange = false;
194 // - c: 0 usb headset to 1 usb headset
195 // - d: 1 usb headset to 0 usb headset
196 if (usb_headset_anlg == BIT_USB_HEADSET_ANLG && usb_headset_dgtl == BIT_USB_HEADSET_DGTL) {
197 Log.e(TAG, "Invalid combination, unsetting usb flag");
198 usbStateChange = false;
200 if (!h2wStateChange && !usbStateChange) {
201 Log.e(TAG, "invalid transition, returning ...");
207 Message msg = mHandler.obtainMessage(MSG_NEW_DEVICE_STATE, headsetState,
208 mHeadsetState, newName);
209 mHandler.sendMessage(msg);
211 mHeadsetState = headsetState;
214 private final Handler mHandler = new Handler(Looper.myLooper(), null, true) {
216 public void handleMessage(Message msg) {
218 case MSG_NEW_DEVICE_STATE:
219 setDevicesState(msg.arg1, msg.arg2, (String)msg.obj);
225 private void setDevicesState(
226 int headsetState, int prevHeadsetState, String headsetName) {
227 synchronized (mLock) {
228 int allHeadsets = SUPPORTED_HEADSETS;
229 for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
230 if ((curHeadset & allHeadsets) != 0) {
231 setDeviceStateLocked(curHeadset, headsetState, prevHeadsetState, headsetName);
232 allHeadsets &= ~curHeadset;
238 private void setDeviceStateLocked(int headset,
239 int headsetState, int prevHeadsetState, String headsetName) {
240 if ((headsetState & headset) != (prevHeadsetState & headset)) {
244 if ((headsetState & headset) != 0) {
250 if (headset == BIT_HEADSET) {
251 device = AudioManager.DEVICE_OUT_WIRED_HEADSET;
252 } else if (headset == BIT_HEADSET_NO_MIC){
253 device = AudioManager.DEVICE_OUT_WIRED_HEADPHONE;
254 } else if (headset == BIT_USB_HEADSET_ANLG) {
255 device = AudioManager.DEVICE_OUT_ANLG_DOCK_HEADSET;
256 } else if (headset == BIT_USB_HEADSET_DGTL) {
257 device = AudioManager.DEVICE_OUT_DGTL_DOCK_HEADSET;
258 } else if (headset == BIT_HDMI_AUDIO) {
259 device = AudioManager.DEVICE_OUT_AUX_DIGITAL;
261 Slog.e(TAG, "setDeviceState() invalid headset type: "+headset);
266 Slog.v(TAG, "device "+headsetName+((state == 1) ? " connected" : " disconnected"));
268 mAudioManager.setWiredDeviceConnectionState(device, state, headsetName);
272 private String switchCodeToString(int switchValues, int switchMask) {
273 StringBuffer sb = new StringBuffer();
274 if ((switchMask & SW_HEADPHONE_INSERT_BIT) != 0 &&
275 (switchValues & SW_HEADPHONE_INSERT_BIT) != 0) {
276 sb.append("SW_HEADPHONE_INSERT ");
278 if ((switchMask & SW_MICROPHONE_INSERT_BIT) != 0 &&
279 (switchValues & SW_MICROPHONE_INSERT_BIT) != 0) {
280 sb.append("SW_MICROPHONE_INSERT");
282 return sb.toString();
285 class WiredAccessoryObserver extends UEventObserver {
286 private final List<UEventInfo> mUEventInfo;
288 public WiredAccessoryObserver() {
289 mUEventInfo = makeObservedUEventList();
293 synchronized (mLock) {
294 if (LOG) Slog.v(TAG, "init()");
295 char[] buffer = new char[1024];
297 for (int i = 0; i < mUEventInfo.size(); ++i) {
298 UEventInfo uei = mUEventInfo.get(i);
301 FileReader file = new FileReader(uei.getSwitchStatePath());
302 int len = file.read(buffer, 0, 1024);
304 curState = Integer.valueOf((new String(buffer, 0, len)).trim());
307 updateStateLocked(uei.getDevPath(), uei.getDevName(), curState);
309 } catch (FileNotFoundException e) {
310 Slog.w(TAG, uei.getSwitchStatePath() +
311 " not found while attempting to determine initial switch state");
312 } catch (Exception e) {
318 // At any given time accessories could be inserted
319 // one on the board, one on the dock and one on HDMI:
320 // observe three UEVENTs
321 for (int i = 0; i < mUEventInfo.size(); ++i) {
322 UEventInfo uei = mUEventInfo.get(i);
323 startObserving("DEVPATH="+uei.getDevPath());
327 private List<UEventInfo> makeObservedUEventList() {
328 List<UEventInfo> retVal = new ArrayList<UEventInfo>();
332 if (!mUseDevInputEventForAudioJack) {
333 uei = new UEventInfo(NAME_H2W, BIT_HEADSET, BIT_HEADSET_NO_MIC);
334 if (uei.checkSwitchExists()) {
337 Slog.w(TAG, "This kernel does not have wired headset support");
342 uei = new UEventInfo(NAME_USB_AUDIO, BIT_USB_HEADSET_ANLG, BIT_USB_HEADSET_DGTL);
343 if (uei.checkSwitchExists()) {
346 Slog.w(TAG, "This kernel does not have usb audio support");
351 // If the kernel has support for the "hdmi_audio" switch, use that. It will be
352 // signalled only when the HDMI driver has a video mode configured, and the downstream
353 // sink indicates support for audio in its EDID.
355 // If the kernel does not have an "hdmi_audio" switch, just fall back on the older
356 // "hdmi" switch instead.
357 uei = new UEventInfo(NAME_HDMI_AUDIO, BIT_HDMI_AUDIO, 0);
358 if (uei.checkSwitchExists()) {
361 uei = new UEventInfo(NAME_HDMI, BIT_HDMI_AUDIO, 0);
362 if (uei.checkSwitchExists()) {
365 Slog.w(TAG, "This kernel does not have HDMI audio support");
373 public void onUEvent(UEventObserver.UEvent event) {
374 if (LOG) Slog.v(TAG, "Headset UEVENT: " + event.toString());
377 String devPath = event.get("DEVPATH");
378 String name = event.get("SWITCH_NAME");
379 int state = Integer.parseInt(event.get("SWITCH_STATE"));
380 synchronized (mLock) {
381 updateStateLocked(devPath, name, state);
383 } catch (NumberFormatException e) {
384 Slog.e(TAG, "Could not parse switch state from event " + event);
388 private void updateStateLocked(String devPath, String name, int state) {
389 for (int i = 0; i < mUEventInfo.size(); ++i) {
390 UEventInfo uei = mUEventInfo.get(i);
391 if (devPath.equals(uei.getDevPath())) {
392 updateLocked(name, uei.computeNewHeadsetState(mHeadsetState, state));
398 private final class UEventInfo {
399 private final String mDevName;
400 private final int mState1Bits;
401 private final int mState2Bits;
403 public UEventInfo(String devName, int state1Bits, int state2Bits) {
405 mState1Bits = state1Bits;
406 mState2Bits = state2Bits;
409 public String getDevName() { return mDevName; }
411 public String getDevPath() {
412 return String.format(Locale.US, "/devices/virtual/switch/%s", mDevName);
415 public String getSwitchStatePath() {
416 return String.format(Locale.US, "/sys/class/switch/%s/state", mDevName);
419 public boolean checkSwitchExists() {
420 File f = new File(getSwitchStatePath());
424 public int computeNewHeadsetState(int headsetState, int switchState) {
425 int preserveMask = ~(mState1Bits | mState2Bits);
426 int setBits = ((switchState == 1) ? mState1Bits :
427 ((switchState == 2) ? mState2Bits : 0));
429 return ((headsetState & preserveMask) | setBits);