Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Eric Laurent | a1f05cc | 2019-06-21 11:39:28 -0700 | [diff] [blame] | 19 | import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT; |
| 20 | import static com.android.server.input.InputManagerService.SW_HEADPHONE_INSERT_BIT; |
| 21 | import static com.android.server.input.InputManagerService.SW_LINEOUT_INSERT; |
| 22 | import static com.android.server.input.InputManagerService.SW_LINEOUT_INSERT_BIT; |
| 23 | import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT; |
| 24 | import static com.android.server.input.InputManagerService.SW_MICROPHONE_INSERT_BIT; |
| 25 | |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Eric Laurent | a1f05cc | 2019-06-21 11:39:28 -0700 | [diff] [blame] | 27 | import android.media.AudioManager; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
| 31 | import android.os.PowerManager; |
| 32 | import android.os.PowerManager.WakeLock; |
| 33 | import android.os.UEventObserver; |
Eric Laurent | a1f05cc | 2019-06-21 11:39:28 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 35 | import android.util.Pair; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 37 | import android.view.InputDevice; |
| 38 | |
| 39 | import com.android.internal.R; |
| 40 | import com.android.server.input.InputManagerService; |
| 41 | import com.android.server.input.InputManagerService.WiredAccessoryCallbacks; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 42 | |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 43 | import java.io.File; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 44 | import java.io.FileNotFoundException; |
Eric Laurent | a1f05cc | 2019-06-21 11:39:28 -0700 | [diff] [blame] | 45 | import java.io.FileReader; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 46 | import java.io.IOException; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.List; |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 49 | import java.util.Locale; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 50 | |
| 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 | */ |
| 56 | final class WiredAccessoryManager implements WiredAccessoryCallbacks { |
| 57 | private static final String TAG = WiredAccessoryManager.class.getSimpleName(); |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 58 | private static final boolean LOG = false; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 59 | |
| 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 65 | private static final int BIT_LINEOUT = (1 << 5); |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 66 | 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 Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 69 | |
| 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 Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 76 | private static final int MSG_SYSTEM_READY = 2; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 77 | |
| 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 Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 88 | private final WiredAccessoryExtconObserver mExtconObserver; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 89 | private final InputManagerService mInputManager; |
| 90 | |
| 91 | private final boolean mUseDevInputEventForAudioJack; |
| 92 | |
| 93 | public WiredAccessoryManager(Context context, InputManagerService inputManager) { |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 94 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 95 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryManager"); |
| 96 | mWakeLock.setReferenceCounted(false); |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 97 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 98 | mInputManager = inputManager; |
| 99 | |
| 100 | mUseDevInputEventForAudioJack = |
| 101 | context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack); |
| 102 | |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 103 | mExtconObserver = new WiredAccessoryExtconObserver(); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 104 | mObserver = new WiredAccessoryObserver(); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 107 | private void onSystemReady() { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 108 | if (mUseDevInputEventForAudioJack) { |
| 109 | int switchValues = 0; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 110 | if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_HEADPHONE_INSERT) |
| 111 | == 1) { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 112 | switchValues |= SW_HEADPHONE_INSERT_BIT; |
| 113 | } |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 114 | if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_MICROPHONE_INSERT) |
| 115 | == 1) { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 116 | switchValues |= SW_MICROPHONE_INSERT_BIT; |
| 117 | } |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 118 | if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_LINEOUT_INSERT) == 1) { |
| 119 | switchValues |= SW_LINEOUT_INSERT_BIT; |
| 120 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 121 | notifyWiredAccessoryChanged(0, switchValues, |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 122 | SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 125 | |
| 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 Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask) { |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 139 | if (LOG) { |
| 140 | Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos |
| 141 | + " bits=" + switchCodeToString(switchValues, switchMask) |
| 142 | + " mask=" + Integer.toHexString(switchMask)); |
| 143 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 144 | |
| 145 | synchronized (mLock) { |
| 146 | int headset; |
| 147 | mSwitchValues = (mSwitchValues & ~switchMask) | switchValues; |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 148 | switch (mSwitchValues & |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 149 | (SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT)) { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 150 | case 0: |
| 151 | headset = 0; |
| 152 | break; |
| 153 | |
| 154 | case SW_HEADPHONE_INSERT_BIT: |
| 155 | headset = BIT_HEADSET_NO_MIC; |
| 156 | break; |
| 157 | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 158 | case SW_LINEOUT_INSERT_BIT: |
| 159 | headset = BIT_LINEOUT; |
| 160 | break; |
| 161 | |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 162 | 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 175 | updateLocked(NAME_H2W, |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 176 | (mHeadsetState & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) | headset); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 180 | @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 Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 190 | /** |
| 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 Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 196 | * @param newName One of the NAME_xxx variables defined above. |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 197 | * @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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 204 | int h2w_headset = headsetState & (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 205 | boolean h2wStateChange = true; |
| 206 | boolean usbStateChange = true; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 207 | if (LOG) { |
| 208 | Slog.v(TAG, "newName=" + newName |
| 209 | + " newState=" + newState |
| 210 | + " headsetState=" + headsetState |
| 211 | + " prev headsetState=" + mHeadsetState); |
| 212 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 213 | |
| 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 222 | if (h2w_headset == (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 223 | 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 McLean | 338f27a | 2015-05-13 15:41:00 -0700 | [diff] [blame] | 239 | Log.i(TAG, "MSG_NEW_DEVICE_STATE"); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 240 | Message msg = mHandler.obtainMessage(MSG_NEW_DEVICE_STATE, headsetState, |
Paul McLean | 338f27a | 2015-05-13 15:41:00 -0700 | [diff] [blame] | 241 | mHeadsetState, ""); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 242 | 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 Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 252 | setDevicesState(msg.arg1, msg.arg2, (String) msg.obj); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 253 | mWakeLock.release(); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 254 | break; |
| 255 | case MSG_SYSTEM_READY: |
| 256 | onSystemReady(); |
| 257 | mWakeLock.release(); |
| 258 | break; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 259 | } |
| 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 Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 279 | int outDevice = 0; |
| 280 | int inDevice = 0; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 281 | int state; |
| 282 | |
| 283 | if ((headsetState & headset) != 0) { |
| 284 | state = 1; |
| 285 | } else { |
| 286 | state = 0; |
| 287 | } |
| 288 | |
| 289 | if (headset == BIT_HEADSET) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 290 | outDevice = AudioManager.DEVICE_OUT_WIRED_HEADSET; |
| 291 | inDevice = AudioManager.DEVICE_IN_WIRED_HEADSET; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 292 | } else if (headset == BIT_HEADSET_NO_MIC) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 293 | outDevice = AudioManager.DEVICE_OUT_WIRED_HEADPHONE; |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 294 | } else if (headset == BIT_LINEOUT) { |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 295 | outDevice = AudioManager.DEVICE_OUT_LINE; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 296 | } else if (headset == BIT_USB_HEADSET_ANLG) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 297 | outDevice = AudioManager.DEVICE_OUT_ANLG_DOCK_HEADSET; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 298 | } else if (headset == BIT_USB_HEADSET_DGTL) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 299 | outDevice = AudioManager.DEVICE_OUT_DGTL_DOCK_HEADSET; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 300 | } else if (headset == BIT_HDMI_AUDIO) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 301 | outDevice = AudioManager.DEVICE_OUT_HDMI; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 302 | } else { |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 303 | Slog.e(TAG, "setDeviceState() invalid headset type: " + headset); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 307 | if (LOG) { |
| 308 | Slog.v(TAG, "headsetName: " + headsetName + |
| 309 | (state == 1 ? " connected" : " disconnected")); |
| 310 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 311 | |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 312 | if (outDevice != 0) { |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 313 | mAudioManager.setWiredDeviceConnectionState(outDevice, state, "", headsetName); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 314 | } |
| 315 | if (inDevice != 0) { |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 316 | mAudioManager.setWiredDeviceConnectionState(inDevice, state, "", headsetName); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 317 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 318 | } |
| 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 Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 353 | curState = Integer.parseInt((new String(buffer, 0, len)).trim()); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 354 | |
| 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 Chalko | c2740c2 | 2018-09-18 12:04:53 -0700 | [diff] [blame] | 362 | Slog.e(TAG, "Error while attempting to determine initial switch state for " |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 363 | + uei.getDevName(), e); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 364 | } |
| 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 Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 373 | startObserving("DEVPATH=" + uei.getDevPath()); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 374 | } |
| 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 383 | uei = new UEventInfo(NAME_H2W, BIT_HEADSET, BIT_HEADSET_NO_MIC, BIT_LINEOUT); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 384 | 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 392 | uei = new UEventInfo(NAME_USB_AUDIO, BIT_USB_HEADSET_ANLG, BIT_USB_HEADSET_DGTL, 0); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 393 | 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 407 | uei = new UEventInfo(NAME_HDMI_AUDIO, BIT_HDMI_AUDIO, 0, 0); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 408 | if (uei.checkSwitchExists()) { |
| 409 | retVal.add(uei); |
| 410 | } else { |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 411 | uei = new UEventInfo(NAME_HDMI, BIT_HDMI_AUDIO, 0, 0); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 412 | 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 452 | private final int mStateNbits; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 453 | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 454 | public UEventInfo(String devName, int state1Bits, int state2Bits, int stateNbits) { |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 455 | mDevName = devName; |
| 456 | mState1Bits = state1Bits; |
| 457 | mState2Bits = state2Bits; |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 458 | mStateNbits = stateNbits; |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 461 | public String getDevName() { |
| 462 | return mDevName; |
| 463 | } |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 464 | |
| 465 | public String getDevPath() { |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 466 | return String.format(Locale.US, "/devices/virtual/switch/%s", mDevName); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | public String getSwitchStatePath() { |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 470 | return String.format(Locale.US, "/sys/class/switch/%s/state", mDevName); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 471 | } |
| 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 Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 479 | int preserveMask = ~(mState1Bits | mState2Bits | mStateNbits); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 480 | int setBits = ((switchState == 1) ? mState1Bits : |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 481 | ((switchState == 2) ? mState2Bits : |
| 482 | ((switchState == mStateNbits) ? mStateNbits : 0))); |
Craig Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 483 | |
| 484 | return ((headsetState & preserveMask) | setBits); |
| 485 | } |
| 486 | } |
| 487 | } |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 488 | |
| 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 Laurent | a1f05cc | 2019-06-21 11:39:28 -0700 | [diff] [blame] | 541 | updateLocked(name, mHeadsetState & ~(mask & ~state) | (mask & state)); |
Nick Chalko | 5f1402c | 2018-11-09 11:52:16 -0800 | [diff] [blame] | 542 | 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 Mautner | 2f39e9f | 2012-09-21 11:39:54 -0700 | [diff] [blame] | 561 | } |