1.systemserver.java
2.android_server_SystemServer_init1.cpp
3.System_init.cpp
4.systemserver.java
新建线程
class ServerThread extends Thread
{
……
public void run()
{
……
try {
Log.i(TAG, "Headset Observer");
// Listen for wired headset changes
headset = new HeadsetObserver(context); // new a thread to observer headset status
} catch (Throwable e) {
Log.e(TAG, "Failure starting HeadsetObserver", e);
}
}
}
5.启动服务并广播:HeadsetObserver.java
class HeadsetObserver extends UeventObserver
{
……
public HeadsetObserver(Context context)
{
……
startObserving(HEADSET_UEVENT_MATCH);
init(); // set initial status
}
}
6.AudioService.java中将接收广播
private class AudioServiceBroadcastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
……
else if (action.equals(Intent.ACTION_HEADSET_PLUG))
{
……
//if you first insert headset, will implement fellow code
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,AudioSystem.DEVICE_STATE_AVAILABLE,"");
}
}
}
7.设置设备连接1:状态android_media_AudioSystem.cpp
static JNINativeMethod gMethods[] = {
"setDeviceConnectionState","(IILjava/lang/String;)I",(void*)android_media_AudioSystem_setDeviceConnectionState},
};
8.设置连接状态2:AudioSystem.cpp
status_t AudioSystem::setDeviceConnectionState(audio_devices device,
device_connection_state state, const char *device_address)
{
const sp& aps = AudioSystem::get_audio_policy_service();
if (aps == 0) return PERMISSION_DENIED;
return aps->setDeviceConnectionState(device, state, device_address);
}
9.设置连接状态3:AudioPolicyManager.cpp
status_t
AudioPolicyManager::setDeviceConnectionState(AudioSystem::audio_devices device,
AudioSystem::device_connection_state state, const char *device_address)
{
……
// handle output devices
if (AudioSystem::isOutputDevice(device))
{
switch (state)
{
case AudioSystem::DEVICE_STATE_AVAILABLE:
……
if (AudioSystem::isBluetoothScoDevice(device))
{
……
}
else if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET ||
device == AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)
{
if (getDeviceForStrategy(STRATEGY_PHONE) == device &&
(mPhoneState == AudioSystem::MODE_IN_CALL ||
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_PHONE)))
{
newDevice = device;
}
else if ((getDeviceForStrategy(STRATEGY_SONIFICATION) & device) &&
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_SONIFICATION))
{
newDevice = getDeviceForStrategy(STRATEGY_SONIFICATION);
}
else if ((getDeviceForStrategy(STRATEGY_MEDIA) == device) &&
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_MEDIA))
{
newDevice = device;
}
else if (getDeviceForStrategy(STRATEGY_DTMF) == device &&
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_DTMF))
{
newDevice = device;
}
}
}
}
10.确定输出设备AudioPolicyManager.cpp
setOutputDevice(mHardwareOutput, newDevice);
11.最后通过alsa切换通路输出
alsa_default.cpp