com/android/server/input/InputManagerService
com_android_server_input_InputManagerService.cpp
NativeInputManager::notifySwitch()
回调 InputManagerService.java
notifyJackSwitchChanged()或notifyLidSwitchChanged()
// Native callback.
public void notifyJackSwitchChanged(long whenNanos,int switchCode, boolean jackOpen) {
mCallbacks.notifyJackSwitchChanged(whenNanos, switchCode, jackOpen);
}mCallbacks是在创建InputManagerService()时传进的参数,见WindowManagerService.java
final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
final InputMonitor mInputMonitor = new InputMonitor(this);
private WindowManagerService(Context context, PowerManagerService pm,
boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore) {
mInputManager = new InputManagerService(context, mInputMonitor);
InputMonitor.java
final class InputMonitor implements InputManagerService.Callbacks {
private final WindowManagerService mService;
public InputMonitor(WindowManagerService service) {
mService = service;
}
/* Notifies that the Jack switch changed state. */
public void notifyJackSwitchChanged(long whenNanos,int switchCode, boolean jackOpen) {
mService.mPolicy.notifyJackSwitchChanged(whenNanos, switchCode, jackOpen);
}通过PolicyManager.makeNewWindowManager()可以知道其时是调用了PhoneWindowManager()的函数。
最后通过handleMessage调到sendIntents(), sentIntent(),最后调到AudioManager::setWiredDeviceConnectionState()
AudioManager的setWiredDeviceConnectionState实际是调用AudioService的setWiredDeviceConnectionState方法。
public void setWiredDeviceConnectionState(int device, int state, String name) {
synchronized (mConnectedDevices) {
int delay = checkSendBecomingNoisyIntent(device, state);
queueMsgUnderWakeLock(mAudioHandler,
MSG_SET_WIRED_DEVICE_CONNECTION_STATE,
device,
state,
name,
delay);
}
}最终会发送到上层一个广播:
private void sendDeviceConnectionIntent(int device, int state, String name)
{
Intent intent = new Intent();
intent.putExtra("state", state);
intent.putExtra("name", name);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
int connType = 0;
if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) {
connType = AudioRoutesInfo.MAIN_HEADSET;
intent.setAction(Intent.ACTION_HEADSET_PLUG);
intent.putExtra("microphone", 1);
} else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) {
......
synchronized (mCurAudioRoutes) {
if (connType != 0) {
int newConn = mCurAudioRoutes.mMainType;
if (state != 0) {
newConn |= connType;
} else {
newConn &= ~connType;
}
if (newConn != mCurAudioRoutes.mMainType) {
mCurAudioRoutes.mMainType = newConn;
sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES,
SENDMSG_NOOP, 0, 0, null, 0);
}
}
}
ActivityManagerNative.broadcastStickyIntent(intent, null);
另外,除了InputManagerService,还有WiredAccessoryObserver服务可以获得耳机插入,USB,HDMI的插入,在2.1时该服务为HeadsetObserver。
参考: