Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7440fc2e authored by Jaikumar Ganesh's avatar Jaikumar Ganesh
Browse files

Wifi: Update code for new BT Apis.

Change-Id: I1deb0a9a1533958cdb79179bebd2d3b435b0b07c
parent 3f03496a
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -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;
@@ -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;
            }
@@ -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);
    }

+49 −15
Original line number Diff line number Diff line
@@ -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) {
                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