• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
26 import android.content.Context;
27 import android.media.AudioManager;
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;
34 import android.util.Log;
35 import android.util.Pair;
36 import android.util.Slog;
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;
42 
43 import java.io.File;
44 import java.io.FileNotFoundException;
45 import java.io.FileReader;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Locale;
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();
58     private static final boolean LOG = false;
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);
65     private static final int BIT_LINEOUT = (1 << 5);
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);
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;
76     private static final int MSG_SYSTEM_READY = 2;
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;
88     private final WiredAccessoryExtconObserver mExtconObserver;
89     private final InputManagerService mInputManager;
90 
91     private final boolean mUseDevInputEventForAudioJack;
92 
WiredAccessoryManager(Context context, InputManagerService inputManager)93     public WiredAccessoryManager(Context context, InputManagerService inputManager) {
94         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
95         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryManager");
96         mWakeLock.setReferenceCounted(false);
97         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
98         mInputManager = inputManager;
99 
100         mUseDevInputEventForAudioJack =
101                 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
102 
103         mExtconObserver = new WiredAccessoryExtconObserver();
104         mObserver = new WiredAccessoryObserver();
105     }
106 
onSystemReady()107     private void onSystemReady() {
108         if (mUseDevInputEventForAudioJack) {
109             int switchValues = 0;
110             if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_HEADPHONE_INSERT)
111                     == 1) {
112                 switchValues |= SW_HEADPHONE_INSERT_BIT;
113             }
114             if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_MICROPHONE_INSERT)
115                     == 1) {
116                 switchValues |= SW_MICROPHONE_INSERT_BIT;
117             }
118             if (mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_LINEOUT_INSERT) == 1) {
119                 switchValues |= SW_LINEOUT_INSERT_BIT;
120             }
121             notifyWiredAccessoryChanged(
122                     0,
123                     switchValues,
124                     SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT,
125                     true /*isSynchronous*/);
126         }
127 
128 
129         if (ExtconUEventObserver.extconExists()) {
130             if (mUseDevInputEventForAudioJack) {
131                 Log.w(TAG, "Both input event and extcon are used for audio jack,"
132                         + " please just choose one.");
133             }
134             mExtconObserver.init();
135         } else {
136             mObserver.init();
137         }
138     }
139 
140     @Override
notifyWiredAccessoryChanged( long whenNanos, int switchValues, int switchMask)141     public void notifyWiredAccessoryChanged(
142             long whenNanos, int switchValues, int switchMask) {
143         notifyWiredAccessoryChanged(whenNanos, switchValues, switchMask, false /*isSynchronous*/);
144     }
145 
notifyWiredAccessoryChanged( long whenNanos, int switchValues, int switchMask, boolean isSynchronous)146     public void notifyWiredAccessoryChanged(
147             long whenNanos, int switchValues, int switchMask, boolean isSynchronous) {
148         if (LOG) {
149             Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos
150                     + " bits=" + switchCodeToString(switchValues, switchMask)
151                     + " mask=" + Integer.toHexString(switchMask));
152         }
153 
154         synchronized (mLock) {
155             int headset;
156             mSwitchValues = (mSwitchValues & ~switchMask) | switchValues;
157             switch (mSwitchValues &
158                     (SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_LINEOUT_INSERT_BIT)) {
159                 case 0:
160                     headset = 0;
161                     break;
162 
163                 case SW_HEADPHONE_INSERT_BIT:
164                     headset = BIT_HEADSET_NO_MIC;
165                     break;
166 
167                 case SW_LINEOUT_INSERT_BIT:
168                     headset = BIT_LINEOUT;
169                     break;
170 
171                 case SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT:
172                     headset = BIT_HEADSET;
173                     break;
174 
175                 case SW_MICROPHONE_INSERT_BIT:
176                     headset = BIT_HEADSET;
177                     break;
178 
179                 default:
180                     headset = 0;
181                     break;
182             }
183 
184             updateLocked(
185                     NAME_H2W,
186                     (mHeadsetState & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) | headset,
187                     isSynchronous);
188         }
189     }
190 
191     @Override
systemReady()192     public void systemReady() {
193         synchronized (mLock) {
194             mWakeLock.acquire();
195 
196             Message msg = mHandler.obtainMessage(MSG_SYSTEM_READY, 0, 0, null);
197             mHandler.sendMessage(msg);
198         }
199     }
200 
201     /**
202      * Compare the existing headset state with the new state and pass along accordingly. Note
203      * that this only supports a single headset at a time. Inserting both a usb and jacked headset
204      * results in support for the last one plugged in. Similarly, unplugging either is seen as
205      * unplugging all.
206      *
207      * @param newName  One of the NAME_xxx variables defined above.
208      * @param newState 0 or one of the BIT_xxx variables defined above.
209      * @param isSynchronous boolean to determine whether should happen sync or async
210      */
updateLocked(String newName, int newState, boolean isSynchronous)211     private void updateLocked(String newName, int newState, boolean isSynchronous) {
212         // Retain only relevant bits
213         int headsetState = newState & SUPPORTED_HEADSETS;
214         int usb_headset_anlg = headsetState & BIT_USB_HEADSET_ANLG;
215         int usb_headset_dgtl = headsetState & BIT_USB_HEADSET_DGTL;
216         int h2w_headset = headsetState & (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT);
217         boolean h2wStateChange = true;
218         boolean usbStateChange = true;
219         if (LOG) {
220             Slog.v(TAG, "newName=" + newName
221                     + " newState=" + newState
222                     + " headsetState=" + headsetState
223                     + " prev headsetState=" + mHeadsetState);
224         }
225 
226         if (mHeadsetState == headsetState) {
227             Log.e(TAG, "No state change.");
228             return;
229         }
230 
231         // reject all suspect transitions: only accept state changes from:
232         // - a: 0 headset to 1 headset
233         // - b: 1 headset to 0 headset
234         if (h2w_headset == (BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) {
235             Log.e(TAG, "Invalid combination, unsetting h2w flag");
236             h2wStateChange = false;
237         }
238         // - c: 0 usb headset to 1 usb headset
239         // - d: 1 usb headset to 0 usb headset
240         if (usb_headset_anlg == BIT_USB_HEADSET_ANLG && usb_headset_dgtl == BIT_USB_HEADSET_DGTL) {
241             Log.e(TAG, "Invalid combination, unsetting usb flag");
242             usbStateChange = false;
243         }
244         if (!h2wStateChange && !usbStateChange) {
245             Log.e(TAG, "invalid transition, returning ...");
246             return;
247         }
248 
249         if (isSynchronous) {
250             setDevicesState(headsetState, mHeadsetState, "");
251         } else {
252             mWakeLock.acquire();
253             Log.i(TAG, "MSG_NEW_DEVICE_STATE");
254             Message msg = mHandler.obtainMessage(MSG_NEW_DEVICE_STATE, headsetState,
255                     mHeadsetState, "");
256             mHandler.sendMessage(msg);
257         }
258 
259         mHeadsetState = headsetState;
260     }
261 
262     private final Handler mHandler = new Handler(Looper.myLooper(), null, true) {
263         @Override
264         public void handleMessage(Message msg) {
265             switch (msg.what) {
266                 case MSG_NEW_DEVICE_STATE:
267                     setDevicesState(msg.arg1, msg.arg2, (String) msg.obj);
268                     mWakeLock.release();
269                     break;
270                 case MSG_SYSTEM_READY:
271                     onSystemReady();
272                     mWakeLock.release();
273                     break;
274             }
275         }
276     };
277 
setDevicesState( int headsetState, int prevHeadsetState, String headsetName)278     private void setDevicesState(
279             int headsetState, int prevHeadsetState, String headsetName) {
280         synchronized (mLock) {
281             int allHeadsets = SUPPORTED_HEADSETS;
282             for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
283                 if ((curHeadset & allHeadsets) != 0) {
284                     setDeviceStateLocked(curHeadset, headsetState, prevHeadsetState, headsetName);
285                     allHeadsets &= ~curHeadset;
286                 }
287             }
288         }
289     }
290 
setDeviceStateLocked(int headset, int headsetState, int prevHeadsetState, String headsetName)291     private void setDeviceStateLocked(int headset,
292             int headsetState, int prevHeadsetState, String headsetName) {
293         if ((headsetState & headset) != (prevHeadsetState & headset)) {
294             int outDevice = 0;
295             int inDevice = 0;
296             int state;
297 
298             if ((headsetState & headset) != 0) {
299                 state = 1;
300             } else {
301                 state = 0;
302             }
303 
304             if (headset == BIT_HEADSET) {
305                 outDevice = AudioManager.DEVICE_OUT_WIRED_HEADSET;
306                 inDevice = AudioManager.DEVICE_IN_WIRED_HEADSET;
307             } else if (headset == BIT_HEADSET_NO_MIC) {
308                 outDevice = AudioManager.DEVICE_OUT_WIRED_HEADPHONE;
309             } else if (headset == BIT_LINEOUT) {
310                 outDevice = AudioManager.DEVICE_OUT_LINE;
311             } else if (headset == BIT_USB_HEADSET_ANLG) {
312                 outDevice = AudioManager.DEVICE_OUT_ANLG_DOCK_HEADSET;
313             } else if (headset == BIT_USB_HEADSET_DGTL) {
314                 outDevice = AudioManager.DEVICE_OUT_DGTL_DOCK_HEADSET;
315             } else if (headset == BIT_HDMI_AUDIO) {
316                 outDevice = AudioManager.DEVICE_OUT_HDMI;
317             } else {
318                 Slog.e(TAG, "setDeviceState() invalid headset type: " + headset);
319                 return;
320             }
321 
322             if (LOG) {
323                 Slog.v(TAG, "headsetName: " + headsetName +
324                         (state == 1 ? " connected" : " disconnected"));
325             }
326 
327             if (outDevice != 0) {
328                 mAudioManager.setWiredDeviceConnectionState(outDevice, state, "", headsetName);
329             }
330             if (inDevice != 0) {
331                 mAudioManager.setWiredDeviceConnectionState(inDevice, state, "", headsetName);
332             }
333         }
334     }
335 
switchCodeToString(int switchValues, int switchMask)336     private String switchCodeToString(int switchValues, int switchMask) {
337         StringBuilder sb = new StringBuilder();
338         if ((switchMask & SW_HEADPHONE_INSERT_BIT) != 0 &&
339                 (switchValues & SW_HEADPHONE_INSERT_BIT) != 0) {
340             sb.append("SW_HEADPHONE_INSERT ");
341         }
342         if ((switchMask & SW_MICROPHONE_INSERT_BIT) != 0 &&
343                 (switchValues & SW_MICROPHONE_INSERT_BIT) != 0) {
344             sb.append("SW_MICROPHONE_INSERT");
345         }
346         return sb.toString();
347     }
348 
349     class WiredAccessoryObserver extends UEventObserver {
350         private final List<UEventInfo> mUEventInfo;
351 
WiredAccessoryObserver()352         public WiredAccessoryObserver() {
353             mUEventInfo = makeObservedUEventList();
354         }
355 
init()356         void init() {
357             synchronized (mLock) {
358                 if (LOG) Slog.v(TAG, "init()");
359                 char[] buffer = new char[1024];
360 
361                 for (int i = 0; i < mUEventInfo.size(); ++i) {
362                     UEventInfo uei = mUEventInfo.get(i);
363                     try {
364                         int curState;
365                         FileReader file = new FileReader(uei.getSwitchStatePath());
366                         int len = file.read(buffer, 0, 1024);
367                         file.close();
368                         curState = Integer.parseInt((new String(buffer, 0, len)).trim());
369 
370                         if (curState > 0) {
371                             updateStateLocked(uei.getDevPath(), uei.getDevName(), curState);
372                         }
373                     } catch (FileNotFoundException e) {
374                         Slog.w(TAG, uei.getSwitchStatePath() +
375                                 " not found while attempting to determine initial switch state");
376                     } catch (Exception e) {
377                         Slog.e(TAG, "Error while attempting to determine initial switch state for "
378                                 + uei.getDevName(), e);
379                     }
380                 }
381             }
382 
383             // At any given time accessories could be inserted
384             // one on the board, one on the dock and one on HDMI:
385             // observe three UEVENTs
386             for (int i = 0; i < mUEventInfo.size(); ++i) {
387                 UEventInfo uei = mUEventInfo.get(i);
388                 startObserving("DEVPATH=" + uei.getDevPath());
389             }
390         }
391 
makeObservedUEventList()392         private List<UEventInfo> makeObservedUEventList() {
393             List<UEventInfo> retVal = new ArrayList<UEventInfo>();
394             UEventInfo uei;
395 
396             // Monitor h2w
397             if (!mUseDevInputEventForAudioJack) {
398                 uei = new UEventInfo(NAME_H2W, BIT_HEADSET, BIT_HEADSET_NO_MIC, BIT_LINEOUT);
399                 if (uei.checkSwitchExists()) {
400                     retVal.add(uei);
401                 } else {
402                     Slog.w(TAG, "This kernel does not have wired headset support");
403                 }
404             }
405 
406             // Monitor USB
407             uei = new UEventInfo(NAME_USB_AUDIO, BIT_USB_HEADSET_ANLG, BIT_USB_HEADSET_DGTL, 0);
408             if (uei.checkSwitchExists()) {
409                 retVal.add(uei);
410             } else {
411                 Slog.w(TAG, "This kernel does not have usb audio support");
412             }
413 
414             // Monitor HDMI
415             //
416             // If the kernel has support for the "hdmi_audio" switch, use that.  It will be
417             // signalled only when the HDMI driver has a video mode configured, and the downstream
418             // sink indicates support for audio in its EDID.
419             //
420             // If the kernel does not have an "hdmi_audio" switch, just fall back on the older
421             // "hdmi" switch instead.
422             uei = new UEventInfo(NAME_HDMI_AUDIO, BIT_HDMI_AUDIO, 0, 0);
423             if (uei.checkSwitchExists()) {
424                 retVal.add(uei);
425             } else {
426                 uei = new UEventInfo(NAME_HDMI, BIT_HDMI_AUDIO, 0, 0);
427                 if (uei.checkSwitchExists()) {
428                     retVal.add(uei);
429                 } else {
430                     Slog.w(TAG, "This kernel does not have HDMI audio support");
431                 }
432             }
433 
434             return retVal;
435         }
436 
437         @Override
onUEvent(UEventObserver.UEvent event)438         public void onUEvent(UEventObserver.UEvent event) {
439             if (LOG) Slog.v(TAG, "Headset UEVENT: " + event.toString());
440 
441             try {
442                 String devPath = event.get("DEVPATH");
443                 String name = event.get("SWITCH_NAME");
444                 int state = Integer.parseInt(event.get("SWITCH_STATE"));
445                 synchronized (mLock) {
446                     updateStateLocked(devPath, name, state);
447                 }
448             } catch (NumberFormatException e) {
449                 Slog.e(TAG, "Could not parse switch state from event " + event);
450             }
451         }
452 
updateStateLocked(String devPath, String name, int state)453         private void updateStateLocked(String devPath, String name, int state) {
454             for (int i = 0; i < mUEventInfo.size(); ++i) {
455                 UEventInfo uei = mUEventInfo.get(i);
456                 if (devPath.equals(uei.getDevPath())) {
457                     updateLocked(
458                             name,
459                             uei.computeNewHeadsetState(mHeadsetState, state),
460                             false /*isSynchronous*/);
461                     return;
462                 }
463             }
464         }
465 
466         private final class UEventInfo {
467             private final String mDevName;
468             private final int mState1Bits;
469             private final int mState2Bits;
470             private final int mStateNbits;
471 
UEventInfo(String devName, int state1Bits, int state2Bits, int stateNbits)472             public UEventInfo(String devName, int state1Bits, int state2Bits, int stateNbits) {
473                 mDevName = devName;
474                 mState1Bits = state1Bits;
475                 mState2Bits = state2Bits;
476                 mStateNbits = stateNbits;
477             }
478 
getDevName()479             public String getDevName() {
480                 return mDevName;
481             }
482 
getDevPath()483             public String getDevPath() {
484                 return String.format(Locale.US, "/devices/virtual/switch/%s", mDevName);
485             }
486 
getSwitchStatePath()487             public String getSwitchStatePath() {
488                 return String.format(Locale.US, "/sys/class/switch/%s/state", mDevName);
489             }
490 
checkSwitchExists()491             public boolean checkSwitchExists() {
492                 File f = new File(getSwitchStatePath());
493                 return f.exists();
494             }
495 
computeNewHeadsetState(int headsetState, int switchState)496             public int computeNewHeadsetState(int headsetState, int switchState) {
497                 int preserveMask = ~(mState1Bits | mState2Bits | mStateNbits);
498                 int setBits = ((switchState == 1) ? mState1Bits :
499                         ((switchState == 2) ? mState2Bits :
500                                 ((switchState == mStateNbits) ? mStateNbits : 0)));
501 
502                 return ((headsetState & preserveMask) | setBits);
503             }
504         }
505     }
506 
507     private class WiredAccessoryExtconObserver extends ExtconStateObserver<Pair<Integer, Integer>> {
508         private final List<ExtconInfo> mExtconInfos;
509 
WiredAccessoryExtconObserver()510         WiredAccessoryExtconObserver() {
511             mExtconInfos = ExtconInfo.getExtconInfoForTypes(new String[] {
512                     ExtconInfo.EXTCON_HEADPHONE,
513                     ExtconInfo.EXTCON_MICROPHONE,
514                     ExtconInfo.EXTCON_HDMI,
515                     ExtconInfo.EXTCON_LINE_OUT,
516             });
517         }
518 
init()519         private void init() {
520             for (ExtconInfo extconInfo : mExtconInfos) {
521                 Pair<Integer, Integer> state = null;
522                 try {
523                     state = parseStateFromFile(extconInfo);
524                 } catch (FileNotFoundException e) {
525                     Slog.w(TAG, extconInfo.getStatePath()
526                             + " not found while attempting to determine initial state", e);
527                 } catch (IOException e) {
528                     Slog.e(
529                             TAG,
530                             "Error reading " + extconInfo.getStatePath()
531                                     + " while attempting to determine initial state",
532                             e);
533                 }
534                 if (state != null) {
535                     updateState(extconInfo, extconInfo.getName(), state);
536                 }
537                 if (LOG) Slog.d(TAG, "observing " + extconInfo.getName());
538                 startObserving(extconInfo);
539             }
540 
541         }
542 
543         @Override
parseState(ExtconInfo extconInfo, String status)544         public Pair<Integer, Integer> parseState(ExtconInfo extconInfo, String status) {
545             if (LOG) Slog.v(TAG, "status  " + status);
546             int[] maskAndState = {0, 0};
547             // extcon event state changes from kernel4.9
548             // new state will be like STATE=MICROPHONE=1\nHEADPHONE=0
549             if (extconInfo.hasCableType(ExtconInfo.EXTCON_HEADPHONE)) {
550                 updateBit(maskAndState, BIT_HEADSET_NO_MIC, status, ExtconInfo.EXTCON_HEADPHONE);
551             }
552             if (extconInfo.hasCableType(ExtconInfo.EXTCON_MICROPHONE)) {
553                 updateBit(maskAndState, BIT_HEADSET, status, ExtconInfo.EXTCON_MICROPHONE);
554             }
555             if (extconInfo.hasCableType(ExtconInfo.EXTCON_HDMI)) {
556                 updateBit(maskAndState, BIT_HDMI_AUDIO, status, ExtconInfo.EXTCON_HDMI);
557             }
558             if (extconInfo.hasCableType(ExtconInfo.EXTCON_LINE_OUT)) {
559                 updateBit(maskAndState, BIT_LINEOUT, status, ExtconInfo.EXTCON_LINE_OUT);
560             }
561             if (LOG) Slog.v(TAG, "mask " + maskAndState[0] + " state " + maskAndState[1]);
562             return Pair.create(maskAndState[0], maskAndState[1]);
563         }
564 
565         @Override
updateState(ExtconInfo extconInfo, String name, Pair<Integer, Integer> maskAndState)566         public void updateState(ExtconInfo extconInfo, String name,
567                 Pair<Integer, Integer> maskAndState) {
568             synchronized (mLock) {
569                 int mask = maskAndState.first;
570                 int state = maskAndState.second;
571                 updateLocked(
572                         name,
573                         mHeadsetState & ~(mask & ~state) | (mask & state),
574                         false /*isSynchronous*/);
575                 return;
576             }
577         }
578     }
579 
580     /**
581      * Updates the mask bit at {@code position} to 1 and the state bit at {@code position} to true
582      * if {@code name=1}  or false if {}@code name=0} is contained in {@code state}.
583      */
updateBit(int[] maskAndState, int position, String state, String name)584     private static void updateBit(int[] maskAndState, int position, String state, String name) {
585         maskAndState[0] |= position;
586         if (state.contains(name + "=1")) {
587             maskAndState[0] |= position;
588             maskAndState[1] |= position;
589         } else if (state.contains(name + "=0")) {
590             maskAndState[0] |= position;
591             maskAndState[1] &= ~position;
592         }
593     }
594 }
595