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

Commit 79b693f4 authored by Joseph Pirozzo's avatar Joseph Pirozzo Committed by Sanket Agarwal
Browse files

Autoconnect Car Profiles

Update profiles used in Car to enable and require PRIORITY_AUTO_CONNECT
to automatically connect when Adapter turns on.

Bug: 27899874
Change-Id: I33bf7cabe959b47954e3aced2af8a5ce8444b9ad
parent cb934fae
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@
    <uses-permission android:name="android.permission.BLUETOOTH_STACK" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"/>
    <uses-permission android:name="android.permission.MANAGE_USERS"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.google.android.gallery3d.permission.GALLERY_PROVIDER"/>
    <uses-permission android:name="com.android.gallery3d.permission.GALLERY_PROVIDER"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
+105 −17
Original line number Diff line number Diff line
@@ -1680,11 +1680,13 @@ public class AdapterService extends Service {
        }

        for (BluetoothDevice device : bondedDevices) {
            if (headsetClientService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT){
            debugLog("autoConnectHeadsetClient() - Connecting Headset Client with " +
                device.toString());
            headsetClientService.connect(device);
            }
        }
    }

    private void autoConnectA2dpSink() {
         A2dpSinkService a2dpSinkService = A2dpSinkService.getA2dpSinkService();
@@ -1694,10 +1696,12 @@ public class AdapterService extends Service {
         }

         for (BluetoothDevice device : bondedDevices) {
             if (a2dpSinkService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
                 debugLog("autoConnectA2dpSink() - Connecting A2DP Sink with " + device.toString());
                 a2dpSinkService.connect(device);
             }
         }
     }

     private void autoConnectPbapClient(){
         PbapClientService pbapClientService = PbapClientService.getPbapClientService();
@@ -1706,8 +1710,9 @@ public class AdapterService extends Service {
             return;
         }
         for (BluetoothDevice device : bondedDevices) {
             if (pbapClientService.getPriority(device) >= BluetoothProfile.PRIORITY_ON ){
                 debugLog("autoConnectPbapClient() - Connecting PBAP Client with " + device.toString());
             if (pbapClientService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
                 debugLog("autoConnectPbapClient() - Connecting PBAP Client with " +
                         device.toString());
                 pbapClientService.connect(device);
             }
         }
@@ -1766,8 +1771,49 @@ public class AdapterService extends Service {
        }
     }

    private void adjustOtherSinkPriorities(A2dpService a2dpService,
             BluetoothDevice connectedDevice) {
         for (BluetoothDevice device : getBondedDevices()) {
             if (a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT &&
                 !device.equals(connectedDevice)) {
                 a2dpService.setPriority(device, BluetoothProfile.PRIORITY_ON);
             }
         }
     }

    private void adjustOtherHeadsetClientPriorities(HeadsetClientService hsService,
            BluetoothDevice connectedDevice) {
        for (BluetoothDevice device : getBondedDevices()) {
           if (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT &&
               !device.equals(connectedDevice)) {
               hsService.setPriority(device, BluetoothProfile.PRIORITY_ON);
           }
        }
     }

    private void adjustOtherA2dpSinkPriorities(A2dpSinkService a2dpService,
            BluetoothDevice connectedDevice) {
        for (BluetoothDevice device : getBondedDevices()) {
            if (a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT &&
                !device.equals(connectedDevice)) {
                a2dpService.setPriority(device, BluetoothProfile.PRIORITY_ON);
            }
        }
    }

    private void adjustOtherPbapClientPriorities(PbapClientService pbapService,
            BluetoothDevice connectedDevice) {
        for (BluetoothDevice device : getBondedDevices()) {
           if (pbapService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT &&
               !device.equals(connectedDevice)) {
               pbapService.setPriority(device, BluetoothProfile.PRIORITY_ON);
           }
        }
     }

    void setProfileAutoConnectionPriority (BluetoothDevice device, int profileId){
         if (profileId == BluetoothProfile.HEADSET) {
        switch (profileId) {
            case BluetoothProfile.HEADSET:
                HeadsetService  hsService = HeadsetService.getHeadsetService();
                List<BluetoothDevice> deviceList = hsService.getConnectedDevices();
                if ((hsService != null) &&
@@ -1775,6 +1821,48 @@ public class AdapterService extends Service {
                    adjustOtherHeadsetPriorities(hsService, deviceList);
                    hsService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
                }
                break;

            case BluetoothProfile.A2DP:
                A2dpService a2dpService = A2dpService.getA2dpService();
                if ((a2dpService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT !=
                        a2dpService.getPriority(device))) {
                    adjustOtherSinkPriorities(a2dpService, device);
                    a2dpService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
                }
                break;

           case BluetoothProfile.A2DP_SINK:
                A2dpSinkService a2dpSinkService = A2dpSinkService.getA2dpSinkService();
                if ((a2dpSinkService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT !=
                        a2dpSinkService.getPriority(device))) {
                    adjustOtherA2dpSinkPriorities(a2dpSinkService, device);
                    a2dpSinkService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
                }
                break;

            case BluetoothProfile.HEADSET_CLIENT:
                HeadsetClientService headsetClientService =
                        HeadsetClientService.getHeadsetClientService();
                if ((headsetClientService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT !=
                        headsetClientService.getPriority(device))) {
                    adjustOtherHeadsetClientPriorities(headsetClientService, device);
                    headsetClientService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
                }
                break;

            case BluetoothProfile.PBAP_CLIENT:
                PbapClientService pbapClientService = PbapClientService.getPbapClientService();
                if ((pbapClientService != null) && (BluetoothProfile.PRIORITY_AUTO_CONNECT !=
                        pbapClientService.getPriority(device))) {
                    adjustOtherPbapClientPriorities(pbapClientService, device);
                    pbapClientService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
                }
                break;

            default:
                Log.w(TAG, "Attempting to set Auto Connect priority on invalid profile");
                break;
         }
    }

+0 −16
Original line number Diff line number Diff line
@@ -62,22 +62,6 @@ public class PbapClientService extends ProfileService {
    private static PbapClientService sPbapClientService;
    private PbapBroadcastReceiver mPbapBroadcastReceiver = new PbapBroadcastReceiver();

    private Account getAccount(BluetoothDevice device) {
        Account account = null;
        Account[] accounts =  mAccountManager.
                getAccountsByType("com.android.bluetooth.pbapclient");
        for (Account acc : accounts) {
            if (acc.name.equals(device.getAddress())) {
                 account = acc;
            }
        }
        if (account == null) {
            account = new Account(device.getAddress(), "com.android.bluetooth.pbapclient");
            mAccountManager.addAccountExplicitly(account, null, null);
        }
        return account;
    }

    @Override
    protected String getName() {
        return TAG;
+10 −7
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ import java.lang.Thread;
 */
public class PbapPCEClient  implements PbapHandler.PbapListener {
    private static final String TAG = "PbapPCEClient";
    private static final boolean DBG = true;
    private static final boolean DBG = false;
    private final Queue<PullRequest> mPendingRequests = new ArrayDeque<PullRequest>();
    private BluetoothDevice mDevice;
    private BluetoothPbapClient mClient;
@@ -186,8 +186,6 @@ public class PbapPCEClient implements PbapHandler.PbapListener {
                        if (oldState != BluetoothProfile.STATE_DISCONNECTED) {
                            return;
                        }
                        onConnectionStateChanged(device, oldState,
                                BluetoothProfile.STATE_CONNECTING);
                        handleConnect(device);
                    } else {
                        Log.e(TAG, "Invalid instance in Connection Handler:Connect");
@@ -221,6 +219,7 @@ public class PbapPCEClient implements PbapHandler.PbapListener {
        }

        private void handleConnect(BluetoothDevice device) {
          Log.d(TAG,"HANDLECONNECT" + device);
            if (device == null) {
                throw new IllegalStateException(TAG + ":Connect with null device!");
            } else if (mDevice != null && !mDevice.equals(device)) {
@@ -236,10 +235,14 @@ public class PbapPCEClient implements PbapHandler.PbapListener {
            }
            // Update the device.
            mDevice = device;
            mClient = new BluetoothPbapClient(mDevice, mAccount, mHandler);
            onConnectionStateChanged(mDevice,BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTING);
            // Add the account. This should give us a place to stash the data.
            mAccount = new Account(device.getAddress(), mContext.getString(R.string.pbap_account_type));
            mContactHandler.obtainMessage(ContactHandler.EVENT_ADD_ACCOUNT,mAccount).sendToTarget();
            mAccount = new Account(device.getAddress(),
                    mContext.getString(R.string.pbap_account_type));
            mContactHandler.obtainMessage(ContactHandler.EVENT_ADD_ACCOUNT, mAccount)
                    .sendToTarget();
            mClient = new BluetoothPbapClient(mDevice, mAccount, mHandler);
            downloadPhoneBook();
            downloadCallLogs();
            mClient.connect();
@@ -420,7 +423,7 @@ public class PbapPCEClient implements PbapHandler.PbapListener {
                }
                return true;
            }
            throw new IllegalStateException(TAG + ":Failed to add account!");
            return false;
        }

        private boolean removeAccount(Account acc) {