diff options
-rw-r--r-- | services/java/com/android/server/WifiService.java | 26 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/WifiStateMachine.java | 64 |
2 files changed, 60 insertions, 30 deletions
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java index 760aa43..19f56a8 100644 --- a/services/java/com/android/server/WifiService.java +++ b/services/java/com/android/server/WifiService.java @@ -21,7 +21,9 @@ import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.bluetooth.BluetoothA2dp; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothProfile; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; @@ -86,6 +88,7 @@ public class WifiService extends IWifiManager.Stub { private AlarmManager mAlarmManager; private PendingIntent mIdleIntent; + private BluetoothA2dp mBluetoothA2dp; private static final int IDLE_REQUEST = 0; private boolean mScreenOff; private boolean mDeviceIdle; @@ -182,7 +185,7 @@ public class WifiService extends IWifiManager.Stub { * something other than scanning, we reset this to 0. */ private int mNumScansSinceNetworkStateChange; - + /** * Temporary for computing UIDS that are responsible for starting WIFI. * Protected by mWifiStateTracker lock. @@ -888,17 +891,10 @@ public class WifiService extends IWifiManager.Stub { return; } mPluggedType = pluggedType; - } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) { - BluetoothA2dp a2dp = new BluetoothA2dp(mContext); - Set<BluetoothDevice> sinks = a2dp.getConnectedSinks(); - boolean isBluetoothPlaying = false; - for (BluetoothDevice sink : sinks) { - if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) { - isBluetoothPlaying = true; - } - } - mWifiStateMachine.setBluetoothScanMode(isBluetoothPlaying); - + } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) { + int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, + BluetoothA2dp.STATE_NOT_PLAYING); + mWifiStateMachine.setBluetoothScanMode(state == BluetoothA2dp.STATE_PLAYING); } else { return; } @@ -958,7 +954,7 @@ public class WifiService extends IWifiManager.Stub { } mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource); } - + private void updateWifiState() { boolean wifiEnabled = getPersistedWifiEnabled(); boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden.get(); @@ -999,7 +995,7 @@ public class WifiService extends IWifiManager.Stub { intentFilter.addAction(Intent.ACTION_SCREEN_OFF); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); intentFilter.addAction(ACTION_DEVICE_IDLE); - intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED); + intentFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); mContext.registerReceiver(mReceiver, intentFilter); } @@ -1214,7 +1210,7 @@ public class WifiService extends IWifiManager.Stub { // Be aggressive about adding new locks into the accounted state... // we want to over-report rather than under-report. reportStartWorkSource(); - + updateWifiState(); return true; } diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java index 7e26028..e82c003 100644 --- a/wifi/java/android/net/wifi/WifiStateMachine.java +++ b/wifi/java/android/net/wifi/WifiStateMachine.java @@ -63,9 +63,11 @@ import android.util.EventLog; import android.util.Log; import android.util.Slog; import android.app.backup.IBackupManager; +import android.bluetooth.BluetoothA2dp; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; -import android.bluetooth.BluetoothA2dp; +import android.bluetooth.BluetoothProfile; import android.content.ContentResolver; import android.content.Intent; import android.content.Context; @@ -444,8 +446,14 @@ public class WifiStateMachine extends HierarchicalStateMachine { mInterfaceName = SystemProperties.get("wifi.interface", "tiwlan0"); mSupplicantStateTracker = new SupplicantStateTracker(context, getHandler()); - mBluetoothHeadset = new BluetoothHeadset(mContext, null); mLinkProperties = new LinkProperties(); + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter != null) { + adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, + BluetoothProfile.A2DP); + adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, + BluetoothProfile.HEADSET); + } mNetworkInfo.setIsAvailable(false); mLinkProperties.clear(); @@ -1278,24 +1286,50 @@ public class WifiStateMachine extends HierarchicalStateMachine { * * @return Whether to disable coexistence mode. */ - private boolean shouldDisableCoexistenceMode() { - int state = mBluetoothHeadset.getState(mBluetoothHeadset.getCurrentHeadset()); - return state == BluetoothHeadset.STATE_DISCONNECTED; + private synchronized boolean shouldDisableCoexistenceMode() { + Set<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices(); + + return (devices.size() != 0 ? true : false); } - private void checkIsBluetoothPlaying() { + private synchronized void checkIsBluetoothPlaying() { boolean isBluetoothPlaying = false; - Set<BluetoothDevice> connected = mBluetoothA2dp.getConnectedSinks(); + if (mBluetoothA2dp != null) { + Set<BluetoothDevice> connected = mBluetoothA2dp.getConnectedDevices(); - for (BluetoothDevice device : connected) { - if (mBluetoothA2dp.getSinkState(device) == BluetoothA2dp.STATE_PLAYING) { - isBluetoothPlaying = true; - break; + for (BluetoothDevice device : connected) { + if (mBluetoothA2dp.isA2dpPlaying(device)) { + isBluetoothPlaying = true; + break; + } } } setBluetoothScanMode(isBluetoothPlaying); } + private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = + new BluetoothProfile.ServiceListener() { + public void onServiceConnected(int profile, BluetoothProfile proxy) { + synchronized (WifiStateMachine.this) { + if (profile == BluetoothProfile.HEADSET) { + mBluetoothHeadset = (BluetoothHeadset) proxy; + } else if (profile == BluetoothProfile.A2DP) { + mBluetoothA2dp = (BluetoothA2dp)proxy; + } + } + } + + public void onServiceDisconnected(int profile) { + synchronized (WifiStateMachine.this) { + if (profile == BluetoothProfile.HEADSET) { + mBluetoothHeadset = null; + } else if (profile == BluetoothProfile.A2DP) { + mBluetoothA2dp = null; + } + } + } + }; + private void sendScanResultsAvailableBroadcast() { if (!ActivityManagerNative.isSystemReady()) return; @@ -1905,9 +1939,6 @@ public class WifiStateMachine extends HierarchicalStateMachine { //TODO: initialize and fix multicast filtering //mWM.initializeMulticastFiltering(); - if (mBluetoothA2dp == null) { - mBluetoothA2dp = new BluetoothA2dp(mContext); - } checkIsBluetoothPlaying(); sendSupplicantConnectionChangedBroadcast(true); @@ -2458,7 +2489,10 @@ public class WifiStateMachine extends HierarchicalStateMachine { mModifiedBluetoothCoexistenceMode = false; mPowerMode = DRIVER_POWER_MODE_AUTO; - if (shouldDisableCoexistenceMode()) { + // TODO(): Incorporate the else part in the state machine + // If mBluetoothHeadset == null, means it not conencted to the + // service yet. + if (mBluetoothHeadset != null && shouldDisableCoexistenceMode()) { /* * There are problems setting the Wi-Fi driver's power * mode to active when bluetooth coexistence mode is |