Merge "Bluetooth: BLE app tracking fixes" am: 2174731449 am: 3d8d5dbb56

am: 56853771ae

Change-Id: I2c0ac6fd2047643ae3c1e6450397b2cd9eed7e94
This commit is contained in:
Marie Janssen
2016-11-12 00:05:19 +00:00
committed by android-build-merger

View File

@@ -56,6 +56,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -115,7 +116,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
private static final int SERVICE_IBLUETOOTHGATT = 2; private static final int SERVICE_IBLUETOOTHGATT = 2;
private final Context mContext; private final Context mContext;
private static int mBleAppCount = 0;
// Locks are not provided for mName and mAddress. // Locks are not provided for mName and mAddress.
// They are accessed in handler or broadcast receiver, same thread context. // They are accessed in handler or broadcast receiver, same thread context.
@@ -207,10 +207,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (isAirplaneModeOn()) { if (isAirplaneModeOn()) {
// Clear registered LE apps to force shut-off // Clear registered LE apps to force shut-off
synchronized (this) { clearBleApps();
mBleAppCount = 0;
mBleApps.clear();
}
if (st == BluetoothAdapter.STATE_BLE_ON) { if (st == BluetoothAdapter.STATE_BLE_ON) {
//if state is BLE_ON make sure you trigger disableBLE part //if state is BLE_ON make sure you trigger disableBLE part
try { try {
@@ -451,28 +448,28 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
class ClientDeathRecipient implements IBinder.DeathRecipient { class ClientDeathRecipient implements IBinder.DeathRecipient {
public void binderDied() { public void binderDied() {
if (DBG) Slog.d(TAG, "Binder is dead - unregister Ble App"); if (DBG) Slog.d(TAG, "Binder is dead - unregister Ble App");
if (mBleAppCount > 0) --mBleAppCount; if (isBleAppPresent()) {
// Nothing to do, another app is here.
if (mBleAppCount == 0) { return;
if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash"); }
try { if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash");
mBluetoothLock.readLock().lock(); try {
if (mBluetooth != null && mBluetoothLock.readLock().lock();
mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { if (mBluetooth != null &&
mEnable = false; mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
mBluetooth.onBrEdrDown(); mEnable = false;
} mBluetooth.onBrEdrDown();
} catch (RemoteException e) {
Slog.e(TAG,"Unable to call onBrEdrDown", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} catch (RemoteException e) {
Slog.e(TAG,"Unable to call onBrEdrDown", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} }
} }
/** Internal death rec list */ /** Internal death rec list */
Map<IBinder, ClientDeathRecipient> mBleApps = new HashMap<IBinder, ClientDeathRecipient>(); Map<IBinder, ClientDeathRecipient> mBleApps = new ConcurrentHashMap<IBinder, ClientDeathRecipient>();
@Override @Override
public boolean isBleScanAlwaysAvailable() { public boolean isBleScanAlwaysAvailable() {
@@ -492,17 +489,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
ContentObserver contentObserver = new ContentObserver(null) { ContentObserver contentObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
if (!isBleScanAlwaysAvailable()) { if (isBleScanAlwaysAvailable()) {
disableBleScanMode(); // Nothing to do
clearBleApps(); return;
try { }
mBluetoothLock.readLock().lock(); // BLE scan is not available.
if (mBluetooth != null) mBluetooth.onBrEdrDown(); disableBleScanMode();
} catch (RemoteException e) { clearBleApps();
Slog.e(TAG, "error when disabling bluetooth", e); try {
} finally { mBluetoothLock.readLock().lock();
mBluetoothLock.readLock().unlock(); if (mBluetooth != null) mBluetooth.onBrEdrDown();
} } catch (RemoteException e) {
Slog.e(TAG, "error when disabling bluetooth", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} }
}; };
@@ -538,9 +538,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
throw new IllegalArgumentException("Wake lock is already dead."); throw new IllegalArgumentException("Wake lock is already dead.");
} }
mBleApps.put(token, deathRec); mBleApps.put(token, deathRec);
synchronized (this) {
++mBleAppCount;
}
if (DBG) Slog.d(TAG, "Registered for death Notification"); if (DBG) Slog.d(TAG, "Registered for death Notification");
} }
@@ -550,31 +547,26 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
// Unregister death recipient as the app goes away. // Unregister death recipient as the app goes away.
token.unlinkToDeath(r, 0); token.unlinkToDeath(r, 0);
mBleApps.remove(token); mBleApps.remove(token);
synchronized (this) {
if (mBleAppCount > 0) --mBleAppCount;
}
if (DBG) Slog.d(TAG, "Unregistered for death Notification"); if (DBG) Slog.d(TAG, "Unregistered for death Notification");
} }
} }
if (DBG) Slog.d(TAG, "Updated BleAppCount" + mBleAppCount); int appCount = mBleApps.size();
if (mBleAppCount == 0 && mEnable) { if (DBG) Slog.d(TAG, appCount + " registered Ble Apps");
if (appCount == 0 && mEnable) {
disableBleScanMode(); disableBleScanMode();
} }
return mBleAppCount; return appCount;
} }
// Clear all apps using BLE scan only mode. // Clear all apps using BLE scan only mode.
private void clearBleApps() { private void clearBleApps() {
synchronized (this) { mBleApps.clear();
mBleApps.clear();
mBleAppCount = 0;
}
} }
/** @hide*/ /** @hide*/
public boolean isBleAppPresent() { public boolean isBleAppPresent() {
if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleAppCount); if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleApps.size());
return (mBleAppCount > 0); return mBleApps.size() > 0;
} }
/** /**
@@ -1384,12 +1376,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) && if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
(newState == BluetoothAdapter.STATE_OFF) && (newState == BluetoothAdapter.STATE_OFF) &&
(mBluetooth != null) && mEnable) { (mBluetooth != null) && mEnable) {
recoverBluetoothServiceFromError(); recoverBluetoothServiceFromError(false);
} }
if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
(newState == BluetoothAdapter.STATE_BLE_ON) && (newState == BluetoothAdapter.STATE_BLE_ON) &&
(mBluetooth != null) && mEnable) { (mBluetooth != null) && mEnable) {
recoverBluetoothServiceFromError(); recoverBluetoothServiceFromError(true);
} }
// If we tried to enable BT while BT was in the process of shutting down, // If we tried to enable BT while BT was in the process of shutting down,
// wait for the BT process to fully tear down and then force a restart // wait for the BT process to fully tear down and then force a restart
@@ -1805,7 +1797,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
quietMode ? 1 : 0, 0)); quietMode ? 1 : 0, 0));
} }
private void recoverBluetoothServiceFromError() { private void recoverBluetoothServiceFromError(boolean clearBle) {
Slog.e(TAG,"recoverBluetoothServiceFromError"); Slog.e(TAG,"recoverBluetoothServiceFromError");
try { try {
mBluetoothLock.readLock().lock(); mBluetoothLock.readLock().lock();
@@ -1843,6 +1835,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState = BluetoothAdapter.STATE_OFF; mState = BluetoothAdapter.STATE_OFF;
if (clearBle) {
clearBleApps();
}
mEnable = false; mEnable = false;
if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) { if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {