diff options
author | 2019-11-15 12:41:09 -0800 | |
---|---|---|
committer | 2019-12-13 10:49:16 -0800 | |
commit | 22209940e8d0847d052cd5b5fbc8e4ba62fcb4e4 (patch) | |
tree | 06764bee4ba72ef93a89bc1041f1a16d3664a58c | |
parent | e1ca0743cc3fc33f1992a279c7e619c191876a16 (diff) |
Pipe featureId from app to noteOp in BT code
FeatureIds are not yet available, hence in BTManager we assume always a
"null" featureId.
The effect of this change is that for apps that opt into using
featureIds, they will have one BluetoothAdapter per feature, not one per
process as before. In my testing this caused no problem. Most apps
won't use featureIds, hence for most apps there is no change in
behavior.
Test: used bluetooth
Bug: 136595429
Change-Id: Ic40326ea331c60f764f213bb2673cb4c49a81604
-rw-r--r-- | core/java/android/bluetooth/BluetoothAdapter.java | 14 | ||||
-rw-r--r-- | core/java/android/bluetooth/BluetoothManager.java | 30 | ||||
-rw-r--r-- | core/java/android/bluetooth/le/BluetoothLeScanner.java | 20 |
3 files changed, 47 insertions, 17 deletions
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 3f8cb627e382..291d1d99fdc9 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -847,7 +847,8 @@ public final class BluetoothAdapter { } synchronized (mLock) { if (sBluetoothLeScanner == null) { - sBluetoothLeScanner = new BluetoothLeScanner(mManagerService); + sBluetoothLeScanner = new BluetoothLeScanner(mManagerService, getOpPackageName(), + getFeatureId()); } } return sBluetoothLeScanner; @@ -1637,6 +1638,15 @@ public final class BluetoothAdapter { return ActivityThread.currentOpPackageName(); } + private String getFeatureId() { + // Workaround for legacy API for getting a BluetoothAdapter not + // passing a context + if (mContext != null) { + return null; + } + return null; + } + /** * Start the remote device discovery process. * <p>The discovery process usually involves an inquiry scan of about 12 @@ -1674,7 +1684,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.startDiscovery(getOpPackageName()); + return mService.startDiscovery(getOpPackageName(), getFeatureId()); } } catch (RemoteException e) { Log.e(TAG, "", e); diff --git a/core/java/android/bluetooth/BluetoothManager.java b/core/java/android/bluetooth/BluetoothManager.java index adedff3e9386..cff4c2d75dbc 100644 --- a/core/java/android/bluetooth/BluetoothManager.java +++ b/core/java/android/bluetooth/BluetoothManager.java @@ -22,7 +22,9 @@ import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.content.Context; import android.content.pm.PackageManager; +import android.os.IBinder; import android.os.RemoteException; +import android.os.ServiceManager; import android.util.Log; import java.util.ArrayList; @@ -60,22 +62,34 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - context = context.getApplicationContext(); - if (context == null) { - throw new IllegalArgumentException( - "context not associated with any application (using a mock context?)"); + if (null == null) { + context = context.getApplicationContext(); + if (context == null) { + throw new IllegalArgumentException( + "context not associated with any application (using a mock context?)"); + } + + mAdapter = BluetoothAdapter.getDefaultAdapter(); + } else { + IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE); + if (b != null) { + mAdapter = new BluetoothAdapter(IBluetoothManager.Stub.asInterface(b)); + } else { + Log.e(TAG, "Bluetooth binder is null"); + mAdapter = null; + } } - // Legacy api - getDefaultAdapter does not take in the context - mAdapter = BluetoothAdapter.getDefaultAdapter(); + + // Context is not initialized in constructor if (mAdapter != null) { mAdapter.setContext(context); } } /** - * Get the default BLUETOOTH Adapter for this device. + * Get the BLUETOOTH Adapter for this device. * - * @return the default BLUETOOTH Adapter + * @return the BLUETOOTH Adapter */ public BluetoothAdapter getAdapter() { return mAdapter; diff --git a/core/java/android/bluetooth/le/BluetoothLeScanner.java b/core/java/android/bluetooth/le/BluetoothLeScanner.java index ac126ae0969d..9a17346334d1 100644 --- a/core/java/android/bluetooth/le/BluetoothLeScanner.java +++ b/core/java/android/bluetooth/le/BluetoothLeScanner.java @@ -21,7 +21,6 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; -import android.app.ActivityThread; import android.app.PendingIntent; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothGatt; @@ -84,17 +83,25 @@ public final class BluetoothLeScanner { private BluetoothAdapter mBluetoothAdapter; private final Map<ScanCallback, BleScanCallbackWrapper> mLeScanClients; + private final String mOpPackageName; + private final String mFeatureId; + /** * Use {@link BluetoothAdapter#getBluetoothLeScanner()} instead. * * @param bluetoothManager BluetoothManager that conducts overall Bluetooth Management. + * @param opPackageName The opPackageName of the context this object was created from + * @param featureId The featureId of the context this object was created from * @hide */ - public BluetoothLeScanner(IBluetoothManager bluetoothManager) { + public BluetoothLeScanner(IBluetoothManager bluetoothManager, + @NonNull String opPackageName, @Nullable String featureId) { mBluetoothManager = bluetoothManager; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mHandler = new Handler(Looper.getMainLooper()); mLeScanClients = new HashMap<ScanCallback, BleScanCallbackWrapper>(); + mOpPackageName = opPackageName; + mFeatureId = featureId; } /** @@ -246,8 +253,8 @@ public final class BluetoothLeScanner { wrapper.startRegistration(); } else { try { - gatt.startScanForIntent(callbackIntent, settings, filters, - ActivityThread.currentOpPackageName()); + gatt.startScanForIntent(callbackIntent, settings, filters, mOpPackageName, + mFeatureId); } catch (RemoteException e) { return ScanCallback.SCAN_FAILED_INTERNAL_ERROR; } @@ -288,7 +295,7 @@ public final class BluetoothLeScanner { IBluetoothGatt gatt; try { gatt = mBluetoothManager.getBluetoothGatt(); - gatt.stopScanForIntent(callbackIntent, ActivityThread.currentOpPackageName()); + gatt.stopScanForIntent(callbackIntent, mOpPackageName); } catch (RemoteException e) { } } @@ -448,8 +455,7 @@ public final class BluetoothLeScanner { } else { mScannerId = scannerId; mBluetoothGatt.startScan(mScannerId, mSettings, mFilters, - mResultStorages, - ActivityThread.currentOpPackageName()); + mResultStorages, mOpPackageName, mFeatureId); } } catch (RemoteException e) { Log.e(TAG, "fail to start le scan: " + e); |