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
This commit is contained in:
Philip P. Moltmann
2019-11-15 12:41:09 -08:00
parent e1ca0743cc
commit 22209940e8
3 changed files with 47 additions and 17 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);