android 4.2切换耳机,[Audio] Android 耳机切换

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。

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值