OSDN Git Service

Autoconnect Car Profiles
authorJoseph Pirozzo <pirozzoj@google.com>
Fri, 1 Apr 2016 23:51:31 +0000 (16:51 -0700)
committerSanket Agarwal <sanketa@google.com>
Mon, 4 Apr 2016 18:26:18 +0000 (18:26 +0000)
Update profiles used in Car to enable and require PRIORITY_AUTO_CONNECT
to automatically connect when Adapter turns on.

Bug: 27899874
Change-Id: I33bf7cabe959b47954e3aced2af8a5ce8444b9ad

AndroidManifest.xml
src/com/android/bluetooth/btservice/AdapterService.java
src/com/android/bluetooth/pbapclient/PbapClientService.java
src/com/android/bluetooth/pbapclient/PbapPCEClient.java

index b76548e..a5ebf18 100644 (file)
@@ -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" />
index a2f0777..fe4cc04 100644 (file)
@@ -1680,9 +1680,11 @@ public class AdapterService extends Service {
         }
 
         for (BluetoothDevice device : bondedDevices) {
-             debugLog("autoConnectHeadsetClient() - Connecting Headset Client with " +
-                 device.toString());
-             headsetClientService.connect(device);
+            if (headsetClientService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT){
+            debugLog("autoConnectHeadsetClient() - Connecting Headset Client with " +
+                device.toString());
+            headsetClientService.connect(device);
+            }
         }
     }
 
@@ -1694,8 +1696,10 @@ public class AdapterService extends Service {
          }
 
          for (BluetoothDevice device : bondedDevices) {
-             debugLog("autoConnectA2dpSink() - Connecting A2DP Sink with " + device.toString());
-             a2dpSinkService.connect(device);
+             if (a2dpSinkService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
+                 debugLog("autoConnectA2dpSink() - Connecting A2DP Sink with " + device.toString());
+                 a2dpSinkService.connect(device);
+             }
          }
      }
 
@@ -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);
              }
          }
@@ -1757,7 +1762,7 @@ public class AdapterService extends Service {
     }
 
      private void adjustOtherHeadsetPriorities(HeadsetService  hsService,
-                                                    List<BluetoothDevice> connectedDeviceList) {
+             List<BluetoothDevice> connectedDeviceList) {
         for (BluetoothDevice device : getBondedDevices()) {
            if (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT &&
                !connectedDeviceList.contains(device)) {
@@ -1766,16 +1771,99 @@ public class AdapterService extends Service {
         }
      }
 
-     void setProfileAutoConnectionPriority (BluetoothDevice device, int profileId){
-         if (profileId == BluetoothProfile.HEADSET) {
-             HeadsetService  hsService = HeadsetService.getHeadsetService();
-             List<BluetoothDevice> deviceList = hsService.getConnectedDevices();
-             if ((hsService != null) &&
-                (BluetoothProfile.PRIORITY_AUTO_CONNECT != hsService.getPriority(device))){
-                 adjustOtherHeadsetPriorities(hsService, deviceList);
-                 hsService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);
+    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){
+        switch (profileId) {
+            case BluetoothProfile.HEADSET:
+                HeadsetService  hsService = HeadsetService.getHeadsetService();
+                List<BluetoothDevice> deviceList = hsService.getConnectedDevices();
+                if ((hsService != null) &&
+                   (BluetoothProfile.PRIORITY_AUTO_CONNECT != hsService.getPriority(device))) {
+                    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;
+         }
     }
 
      boolean cancelBondProcess(BluetoothDevice device) {
@@ -1796,7 +1884,7 @@ public class AdapterService extends Service {
         return true;
     }
 
-     int getBondState(BluetoothDevice device) {
+    int getBondState(BluetoothDevice device) {
         enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
         DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
         if (deviceProp == null) {
index 3ffdd83..8af451c 100644 (file)
@@ -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;
index 8c86023..d04fabc 100644 (file)
@@ -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) {