Merge "Send device connection intents from AudioService" into jb-dev

This commit is contained in:
Eric Laurent
2012-05-31 08:22:50 -07:00
committed by Android (Google) Code Review
5 changed files with 251 additions and 134 deletions

View File

@ -33,7 +33,11 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
@ -65,6 +69,10 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
private final BluetoothAdapter mAdapter; private final BluetoothAdapter mAdapter;
private int mTargetA2dpState; private int mTargetA2dpState;
private BluetoothDevice mPlayingA2dpDevice; private BluetoothDevice mPlayingA2dpDevice;
private IntentBroadcastHandler mIntentBroadcastHandler;
private final WakeLock mWakeLock;
private static final int MSG_CONNECTION_STATE_CHANGED = 0;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
@ -131,6 +139,11 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
public BluetoothA2dpService(Context context, BluetoothService bluetoothService) { public BluetoothA2dpService(Context context, BluetoothService bluetoothService) {
mContext = context; mContext = context;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BluetoothA2dpService");
mIntentBroadcastHandler = new IntentBroadcastHandler();
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mBluetoothService = bluetoothService; mBluetoothService = bluetoothService;
@ -514,17 +527,15 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
adjustOtherSinkPriorities(device); adjustOtherSinkPriorities(device);
} }
Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); int delay = mAudioManager.setBluetoothA2dpDeviceConnectionState(device, state);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcast(intent, BLUETOOTH_PERM);
if (DBG) log("A2DP state : device: " + device + " State:" + prevState + "->" + state); mWakeLock.acquire();
mIntentBroadcastHandler.sendMessageDelayed(mIntentBroadcastHandler.obtainMessage(
mBluetoothService.sendConnectionStateChange(device, BluetoothProfile.A2DP, state, MSG_CONNECTION_STATE_CHANGED,
prevState); prevState,
state,
device),
delay);
} }
} }
@ -586,6 +597,34 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
} }
} }
/** Handles A2DP connection state change intent broadcasts. */
private class IntentBroadcastHandler extends Handler {
private void onConnectionStateChanged(BluetoothDevice device, int prevState, int state) {
Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcast(intent, BLUETOOTH_PERM);
if (DBG) log("A2DP state : device: " + device + " State:" + prevState + "->" + state);
mBluetoothService.sendConnectionStateChange(device, BluetoothProfile.A2DP, state,
prevState);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CONNECTION_STATE_CHANGED:
onConnectionStateChanged((BluetoothDevice) msg.obj, msg.arg1, msg.arg2);
mWakeLock.release();
break;
}
}
}
@Override @Override
protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);

View File

@ -19,6 +19,7 @@ package android.media;
import android.annotation.SdkConstant; import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SdkConstant.SdkConstantType;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -2376,6 +2377,42 @@ public class AudioManager {
} }
} }
/**
* Indicate wired accessory connection state change.
* @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx)
* @param state new connection state: 1 connected, 0 disconnected
* @param name device name
* {@hide}
*/
public void setWiredDeviceConnectionState(int device, int state, String name) {
IAudioService service = getService();
try {
service.setWiredDeviceConnectionState(device, state, name);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setWiredDeviceConnectionState "+e);
}
}
/**
* Indicate A2DP sink connection state change.
* @param device Bluetooth device connected/disconnected
* @param state new connection state (BluetoothProfile.STATE_xxx)
* @return a delay in ms that the caller should wait before broadcasting
* BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED intent.
* {@hide}
*/
public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state) {
IAudioService service = getService();
int delay = 0;
try {
delay = service.setBluetoothA2dpDeviceConnectionState(device, state);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setBluetoothA2dpDeviceConnectionState "+e);
} finally {
return delay;
}
}
/** {@hide} */ /** {@hide} */
public IRingtonePlayer getRingtonePlayer() { public IRingtonePlayer getRingtonePlayer() {
try { try {

View File

@ -135,6 +135,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
private static final int MSG_RCDISPLAY_UPDATE = 13; private static final int MSG_RCDISPLAY_UPDATE = 13;
private static final int MSG_SET_ALL_VOLUMES = 14; private static final int MSG_SET_ALL_VOLUMES = 14;
private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 15; private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 15;
private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 16;
private static final int MSG_SET_A2DP_CONNECTION_STATE = 17;
// flags for MSG_PERSIST_VOLUME indicating if current and/or last audible volume should be // flags for MSG_PERSIST_VOLUME indicating if current and/or last audible volume should be
@ -442,15 +444,9 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
// Register for device connection intent broadcasts. // Register for device connection intent broadcasts.
IntentFilter intentFilter = IntentFilter intentFilter =
new IntentFilter(Intent.ACTION_HEADSET_PLUG); new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
intentFilter.addAction(Intent.ACTION_DOCK_EVENT); intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
intentFilter.addAction(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG);
intentFilter.addAction(Intent.ACTION_DIGITAL_AUDIO_DOCK_PLUG);
intentFilter.addAction(Intent.ACTION_HDMI_AUDIO_PLUG);
intentFilter.addAction(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG); intentFilter.addAction(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG);
intentFilter.addAction(Intent.ACTION_USB_AUDIO_DEVICE_PLUG); intentFilter.addAction(Intent.ACTION_USB_AUDIO_DEVICE_PLUG);
intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED); intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
@ -1961,7 +1957,19 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
deviceList = a2dp.getConnectedDevices(); deviceList = a2dp.getConnectedDevices();
if (deviceList.size() > 0) { if (deviceList.size() > 0) {
btDevice = deviceList.get(0); btDevice = deviceList.get(0);
handleA2dpConnectionStateChange(btDevice, a2dp.getConnectionState(btDevice)); synchronized (mConnectedDevices) {
int state = a2dp.getConnectionState(btDevice);
int delay = checkSendBecomingNoisyIntent(
AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
(state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0);
sendMsg(mAudioHandler,
MSG_SET_A2DP_CONNECTION_STATE,
SENDMSG_QUEUE,
state,
0,
btDevice,
delay);
}
} }
break; break;
@ -2262,6 +2270,36 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
return device; return device;
} }
public void setWiredDeviceConnectionState(int device, int state, String name) {
synchronized (mConnectedDevices) {
int delay = checkSendBecomingNoisyIntent(device, state);
sendMsg(mAudioHandler,
MSG_SET_WIRED_DEVICE_CONNECTION_STATE,
SENDMSG_QUEUE,
device,
state,
name,
delay);
}
}
public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state)
{
int delay;
synchronized (mConnectedDevices) {
delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
(state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0);
sendMsg(mAudioHandler,
MSG_SET_A2DP_CONNECTION_STATE,
SENDMSG_QUEUE,
state,
0,
device,
delay);
}
return delay;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Inner classes // Inner classes
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -2959,6 +2997,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
case MSG_BT_HEADSET_CNCT_FAILED: case MSG_BT_HEADSET_CNCT_FAILED:
resetBluetoothSco(); resetBluetoothSco();
break; break;
case MSG_SET_WIRED_DEVICE_CONNECTION_STATE:
onSetWiredDeviceConnectionState(msg.arg1, msg.arg2, (String)msg.obj);
break;
case MSG_SET_A2DP_CONNECTION_STATE:
onSetA2dpConnectionState((BluetoothDevice)msg.obj, msg.arg1);
break;
} }
} }
} }
@ -3020,7 +3066,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
// must be called synchronized on mConnectedDevices // must be called synchronized on mConnectedDevices
private void makeA2dpDeviceUnavailableNow(String address) { private void makeA2dpDeviceUnavailableNow(String address) {
sendBecomingNoisyIntent();
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
AudioSystem.DEVICE_STATE_UNAVAILABLE, AudioSystem.DEVICE_STATE_UNAVAILABLE,
address); address);
@ -3050,7 +3095,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT);
} }
private void handleA2dpConnectionStateChange(BluetoothDevice btDevice, int state) private void onSetA2dpConnectionState(BluetoothDevice btDevice, int state)
{ {
if (btDevice == null) { if (btDevice == null) {
return; return;
@ -3116,6 +3161,76 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
return false; return false;
} }
// Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only
// sent if none of these devices is connected.
int mBecomingNoisyIntentDevices =
AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE |
AudioSystem.DEVICE_OUT_ALL_A2DP;
// must be called before removing the device from mConnectedDevices
private int checkSendBecomingNoisyIntent(int device, int state) {
int delay = 0;
if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) {
int devices = 0;
for (int dev : mConnectedDevices.keySet()) {
if ((dev & mBecomingNoisyIntentDevices) != 0) {
devices |= dev;
}
}
if (devices == device) {
delay = 1000;
sendBecomingNoisyIntent();
}
}
if (mAudioHandler.hasMessages(MSG_SET_A2DP_CONNECTION_STATE) ||
mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) {
delay = 1000;
}
return 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);
if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) {
intent.setAction(Intent.ACTION_HEADSET_PLUG);
intent.putExtra("microphone", 1);
} else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) {
intent.setAction(Intent.ACTION_HEADSET_PLUG);
intent.putExtra("microphone", 0);
} else if (device == AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET) {
intent.setAction(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG);
} else if (device == AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET) {
intent.setAction(Intent.ACTION_DIGITAL_AUDIO_DOCK_PLUG);
} else if (device == AudioSystem.DEVICE_OUT_AUX_DIGITAL) {
intent.setAction(Intent.ACTION_HDMI_AUDIO_PLUG);
}
ActivityManagerNative.broadcastStickyIntent(intent, null);
}
private void onSetWiredDeviceConnectionState(int device, int state, String name)
{
synchronized (mConnectedDevices) {
if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) ||
(device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE))) {
setBluetoothA2dpOnInt(true);
}
handleDeviceConnection((state == 1), device, "");
if ((state != 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) ||
(device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE))) {
setBluetoothA2dpOnInt(false);
}
sendDeviceConnectionIntent(device, state, name);
}
}
/* cache of the address of the last dock the device was connected to */ /* cache of the address of the last dock the device was connected to */
private String mDockAddress; private String mDockAddress;
@ -3151,12 +3266,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
config = AudioSystem.FORCE_NONE; config = AudioSystem.FORCE_NONE;
} }
AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config);
} else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE,
BluetoothProfile.STATE_DISCONNECTED);
BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
handleA2dpConnectionStateChange(btDevice, state);
} else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE,
BluetoothProfile.STATE_DISCONNECTED); BluetoothProfile.STATE_DISCONNECTED);
@ -3197,43 +3306,9 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
} }
} }
} }
} else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
state = intent.getIntExtra("state", 0);
int microphone = intent.getIntExtra("microphone", 0);
if (microphone != 0) {
device = AudioSystem.DEVICE_OUT_WIRED_HEADSET;
} else {
device = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE;
}
// enable A2DP before notifying headset disconnection to avoid glitches
if (state == 0) {
setBluetoothA2dpOnInt(true);
}
handleDeviceConnection((state == 1), device, "");
// disable A2DP after notifying headset connection to avoid glitches
if (state != 0) {
setBluetoothA2dpOnInt(false);
}
} else if (action.equals(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG)) {
state = intent.getIntExtra("state", 0);
Log.v(TAG, "Broadcast Receiver: Got ACTION_ANALOG_AUDIO_DOCK_PLUG, state = "+state);
handleDeviceConnection((state == 1), AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET, "");
} else if (action.equals(Intent.ACTION_HDMI_AUDIO_PLUG)) {
state = intent.getIntExtra("state", 0);
Log.v(TAG, "Broadcast Receiver: Got ACTION_HDMI_AUDIO_PLUG, state = "+state);
handleDeviceConnection((state == 1), AudioSystem.DEVICE_OUT_AUX_DIGITAL, "");
} else if (action.equals(Intent.ACTION_DIGITAL_AUDIO_DOCK_PLUG)) {
state = intent.getIntExtra("state", 0);
Log.v(TAG,
"Broadcast Receiver Got ACTION_DIGITAL_AUDIO_DOCK_PLUG, state = " + state);
handleDeviceConnection((state == 1), AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET, "");
} else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG) || } else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG) ||
action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) { action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) {
state = intent.getIntExtra("state", 0); state = intent.getIntExtra("state", 0);
if (state == 0) {
sendBecomingNoisyIntent();
}
int alsaCard = intent.getIntExtra("card", -1); int alsaCard = intent.getIntExtra("card", -1);
int alsaDevice = intent.getIntExtra("device", -1); int alsaDevice = intent.getIntExtra("device", -1);
String params = (alsaCard == -1 && alsaDevice == -1 ? "" String params = (alsaCard == -1 && alsaDevice == -1 ? ""

View File

@ -17,6 +17,7 @@
package android.media; package android.media;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName; import android.content.ComponentName;
import android.media.IAudioFocusDispatcher; import android.media.IAudioFocusDispatcher;
import android.media.IRemoteControlClient; import android.media.IRemoteControlClient;
@ -133,4 +134,7 @@ interface IAudioService {
void setRingtonePlayer(IRingtonePlayer player); void setRingtonePlayer(IRingtonePlayer player);
IRingtonePlayer getRingtonePlayer(); IRingtonePlayer getRingtonePlayer();
int getMasterStreamType(); int getMasterStreamType();
void setWiredDeviceConnectionState(int device, int state, String name);
int setBluetoothA2dpDeviceConnectionState(in BluetoothDevice device, int state);
} }

View File

@ -139,11 +139,14 @@ class WiredAccessoryObserver extends UEventObserver {
private final Context mContext; private final Context mContext;
private final WakeLock mWakeLock; // held while there is a pending route change private final WakeLock mWakeLock; // held while there is a pending route change
private final AudioManager mAudioManager;
public WiredAccessoryObserver(Context context) { public WiredAccessoryObserver(Context context) {
mContext = context; mContext = context;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryObserver"); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WiredAccessoryObserver");
mWakeLock.setReferenceCounted(false); mWakeLock.setReferenceCounted(false);
mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
context.registerReceiver(new BootCompletedReceiver(), context.registerReceiver(new BootCompletedReceiver(),
new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null); new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
@ -250,106 +253,65 @@ class WiredAccessoryObserver extends UEventObserver {
mPrevHeadsetState = mHeadsetState; mPrevHeadsetState = mHeadsetState;
mHeadsetState = headsetState; mHeadsetState = headsetState;
if (headsetState == 0) {
if (mContext.getResources().getBoolean(
com.android.internal.R.bool.config_sendAudioBecomingNoisy)) {
Intent intent = new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
mContext.sendBroadcast(intent);
}
// It can take hundreds of ms flush the audio pipeline after
// apps pause audio playback, but audio route changes are
// immediate, so delay the route change by 1000ms.
// This could be improved once the audio sub-system provides an
// interface to clear the audio pipeline.
delay = 1000;
} else {
// Insert the same delay for headset connection so that the connection event is not
// broadcast before the disconnection event in case of fast removal/insertion
if (mHandler.hasMessages(0)) {
delay = 1000;
}
}
mWakeLock.acquire(); mWakeLock.acquire();
mHandler.sendMessageDelayed(mHandler.obtainMessage(0, mHandler.sendMessage(mHandler.obtainMessage(0,
mHeadsetState, mHeadsetState,
mPrevHeadsetState, mPrevHeadsetState,
mHeadsetName), mHeadsetName));
delay);
} }
private synchronized final void sendIntents(int headsetState, int prevHeadsetState, String headsetName) { private synchronized final void setDevicesState(int headsetState,
int prevHeadsetState,
String headsetName) {
int allHeadsets = SUPPORTED_HEADSETS; int allHeadsets = SUPPORTED_HEADSETS;
for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) { for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
if ((curHeadset & allHeadsets) != 0) { if ((curHeadset & allHeadsets) != 0) {
sendIntent(curHeadset, headsetState, prevHeadsetState, headsetName); setDeviceState(curHeadset, headsetState, prevHeadsetState, headsetName);
allHeadsets &= ~curHeadset; allHeadsets &= ~curHeadset;
} }
} }
} }
private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) { private final void setDeviceState(int headset,
int headsetState,
int prevHeadsetState,
String headsetName) {
if ((headsetState & headset) != (prevHeadsetState & headset)) { if ((headsetState & headset) != (prevHeadsetState & headset)) {
int device;
int state;
int state = 0;
if ((headsetState & headset) != 0) { if ((headsetState & headset) != 0) {
state = 1; state = 1;
} else {
state = 0;
} }
if((headset == BIT_USB_HEADSET_ANLG) || (headset == BIT_USB_HEADSET_DGTL) ||
(headset == BIT_HDMI_AUDIO)) {
Intent intent;
// Pack up the values and broadcast them to everyone if (headset == BIT_HEADSET) {
if (headset == BIT_USB_HEADSET_ANLG) { device = AudioManager.DEVICE_OUT_WIRED_HEADSET;
intent = new Intent(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG); } else if (headset == BIT_HEADSET_NO_MIC){
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); device = AudioManager.DEVICE_OUT_WIRED_HEADPHONE;
intent.putExtra("state", state); } else if (headset == BIT_USB_HEADSET_ANLG) {
intent.putExtra("name", headsetName); device = AudioManager.DEVICE_OUT_ANLG_DOCK_HEADSET;
ActivityManagerNative.broadcastStickyIntent(intent, null);
} else if (headset == BIT_USB_HEADSET_DGTL) { } else if (headset == BIT_USB_HEADSET_DGTL) {
intent = new Intent(Intent.ACTION_DIGITAL_AUDIO_DOCK_PLUG); device = AudioManager.DEVICE_OUT_DGTL_DOCK_HEADSET;
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
intent.putExtra("state", state);
intent.putExtra("name", headsetName);
ActivityManagerNative.broadcastStickyIntent(intent, null);
} else if (headset == BIT_HDMI_AUDIO) { } else if (headset == BIT_HDMI_AUDIO) {
intent = new Intent(Intent.ACTION_HDMI_AUDIO_PLUG); device = AudioManager.DEVICE_OUT_AUX_DIGITAL;
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); } else {
intent.putExtra("state", state); Slog.e(TAG, "setDeviceState() invalid headset type: "+headset);
intent.putExtra("name", headsetName); return;
ActivityManagerNative.broadcastStickyIntent(intent, null);
} }
if (LOG) Slog.v(TAG, "Intent.ACTION_USB_HEADSET_PLUG: state: "+state+" name: "+headsetName); if (LOG)
// TODO: Should we require a permission? Slog.v(TAG, "device "+headsetName+((state == 1) ? " connected" : " disconnected"));
}
if((headset == BIT_HEADSET) || (headset == BIT_HEADSET_NO_MIC)) {
// Pack up the values and broadcast them to everyone mAudioManager.setWiredDeviceConnectionState(device, state, headsetName);
Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
//int state = 0;
int microphone = 0;
if ((headset & HEADSETS_WITH_MIC) != 0) {
microphone = 1;
}
intent.putExtra("state", state);
intent.putExtra("name", headsetName);
intent.putExtra("microphone", microphone);
if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
// TODO: Should we require a permission?
ActivityManagerNative.broadcastStickyIntent(intent, null);
}
} }
} }
private final Handler mHandler = new Handler() { private final Handler mHandler = new Handler() {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
sendIntents(msg.arg1, msg.arg2, (String)msg.obj); setDevicesState(msg.arg1, msg.arg2, (String)msg.obj);
mWakeLock.release(); mWakeLock.release();
} }
}; };