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

Commit 78cb0998 authored by Ugo Yu's avatar Ugo Yu
Browse files

Independent methods to enable/disable BLE mode

* Use IBluetoothManager.enableBle() and disableBle() to toggle BLE only
  mode instead of updateBleAppCount() then enable().
* Fix BluetoothAdapter.disableBle sometime won't disable Bluetooth.
* Add active log if Bluetooth is disabled via disableBle().

Bug: 139454316
Test: enableBle() -> disableBle() and check Bluetooth is OFF.
Change-Id: I173d5fed1b47ff68a6504741e25754e65cbd1455
parent 7b97603a
Loading
Loading
Loading
Loading
+7 −30
Original line number Diff line number Diff line
@@ -916,24 +916,12 @@ public final class BluetoothAdapter {
        if (!isBleScanAlwaysAvailable()) {
            return false;
        }

        int state = getLeState();
        if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_BLE_ON) {
        String packageName = ActivityThread.currentPackageName();
            if (DBG) {
                Log.d(TAG, "disableBLE(): de-registering " + packageName);
            }
        try {
                mManagerService.updateBleAppCount(mToken, false, packageName);
            return mManagerService.disableBle(packageName, mToken);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
            return true;
        }

        if (DBG) {
            Log.d(TAG, "disableBLE(): Already disabled");
        }
        return false;
    }

@@ -973,20 +961,9 @@ public final class BluetoothAdapter {
        if (!isBleScanAlwaysAvailable()) {
            return false;
        }

        try {
        String packageName = ActivityThread.currentPackageName();
            mManagerService.updateBleAppCount(mToken, true, packageName);
            if (isLeEnabled()) {
                if (DBG) {
                    Log.d(TAG, "enableBLE(): Bluetooth already enabled");
                }
                return true;
            }
            if (DBG) {
                Log.d(TAG, "enableBLE(): Calling enable");
            }
            return mManagerService.enable(packageName);
        try {
            return mManagerService.enableBle(packageName, mToken);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
+107 −61
Original line number Diff line number Diff line
@@ -740,13 +740,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }
    }

    public int updateBleAppCount(IBinder token, boolean enable, String packageName) {
        // Check if packageName belongs to callingUid
        final int callingUid = Binder.getCallingUid();
        final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
        if (!isCallerSystem) {
            checkPackage(callingUid, packageName);
        }
    private int updateBleAppCount(IBinder token, boolean enable, String packageName) {
        ClientDeathRecipient r = mBleApps.get(token);
        if (r == null && enable) {
            ClientDeathRecipient deathRec = new ClientDeathRecipient(packageName);
@@ -771,13 +765,94 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        if (DBG) {
            Slog.d(TAG, appCount + " registered Ble Apps");
        }
        if (appCount == 0 && mEnable) {
        return appCount;
    }

    private boolean checkBluetoothPermissions(String packageName, boolean requireForeground) {
        if (isBluetoothDisallowed()) {
            if (DBG) {
                Slog.d(TAG, "checkBluetoothPermissions: bluetooth disallowed");
            }
            return false;
        }
        // Check if packageName belongs to callingUid
        final int callingUid = Binder.getCallingUid();
        final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
        if (!isCallerSystem) {
            checkPackage(callingUid, packageName);

            if (requireForeground && !checkIfCallerIsForegroundUser()) {
                Slog.w(TAG, "Not allowed for non-active and non system user");
                return false;
            }

            mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                    "Need BLUETOOTH ADMIN permission");
        }
        return true;
    }

    public boolean enableBle(String packageName, IBinder token) throws RemoteException {
        if (!checkBluetoothPermissions(packageName, false)) {
            if (DBG) {
                Slog.d(TAG, "enableBle(): bluetooth disallowed");
            }
            return false;
        }

        if (DBG) {
            Slog.d(TAG, "enableBle(" + packageName + "):  mBluetooth =" + mBluetooth
                    + " mBinding = " + mBinding + " mState = "
                    + BluetoothAdapter.nameForState(mState));
        }
        updateBleAppCount(token, true, packageName);

        if (mState == BluetoothAdapter.STATE_ON
                || mState == BluetoothAdapter.STATE_BLE_ON
                || mState == BluetoothAdapter.STATE_TURNING_ON
                || mState == BluetoothAdapter.STATE_TURNING_OFF) {
            Log.d(TAG, "enableBLE(): Bluetooth already enabled");
            return true;
        }
        synchronized (mReceiver) {
            // waive WRITE_SECURE_SETTINGS permission check
            sendEnableMsg(false,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }

    public boolean disableBle(String packageName, IBinder token) throws RemoteException {
        if (!checkBluetoothPermissions(packageName, false)) {
            if (DBG) {
                Slog.d(TAG, "disableBLE(): bluetooth disallowed");
            }
            return false;
        }

        if (DBG) {
            Slog.d(TAG, "disableBle(" + packageName + "):  mBluetooth =" + mBluetooth
                    + " mBinding = " + mBinding + " mState = "
                    + BluetoothAdapter.nameForState(mState));
        }

        if (mState == BluetoothAdapter.STATE_OFF) {
            Slog.d(TAG, "disableBLE(): Already disabled");
            return false;
        }
        updateBleAppCount(token, false, packageName);

        if (mState == BluetoothAdapter.STATE_BLE_ON && !isBleAppPresent()) {
            if (mEnable) {
                disableBleScanMode();
            }
        if (appCount == 0 && !mEnableExternal) {
            if (!mEnableExternal) {
                addActiveLog(BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                        packageName, false);
                sendBrEdrDownCallback();
            }
        return appCount;
        }
        return true;
    }

    // Clear all apps using BLE scan only mode.
@@ -855,29 +930,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    }

    public boolean enableNoAutoConnect(String packageName) {
        if (isBluetoothDisallowed()) {
        if (!checkBluetoothPermissions(packageName, false)) {
            if (DBG) {
                Slog.d(TAG, "enableNoAutoConnect(): not enabling - bluetooth disallowed");
            }
            return false;
        }

        // Check if packageName belongs to callingUid
        final int callingUid = Binder.getCallingUid();
        final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
        if (!isCallerSystem) {
            checkPackage(callingUid, packageName);
        }

        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                "Need BLUETOOTH ADMIN permission");

        if (DBG) {
            Slog.d(TAG, "enableNoAutoConnect():  mBluetooth =" + mBluetooth + " mBinding = "
                    + mBinding);
        }
        int callingAppId = UserHandle.getAppId(callingUid);

        int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
        if (callingAppId != Process.NFC_UID) {
            throw new SecurityException("no permission to enable Bluetooth quietly");
        }
@@ -892,33 +957,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    }

    public boolean enable(String packageName) throws RemoteException {
        final int callingUid = Binder.getCallingUid();
        final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;

        if (isBluetoothDisallowed()) {
        if (!checkBluetoothPermissions(packageName, true)) {
            if (DBG) {
                Slog.d(TAG, "enable(): not enabling - bluetooth disallowed");
            }
            return false;
        }

        if (!callerSystem) {
            // Check if packageName belongs to callingUid
            checkPackage(callingUid, packageName);

            if (!checkIfCallerIsForegroundUser()) {
                Slog.w(TAG, "enable(): not allowed for non-active and non system user");
                return false;
            }

            mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                    "Need BLUETOOTH ADMIN permission");

            if (!isEnabled() && mWirelessConsentRequired && startConsentUiIfNeeded(packageName,
        final int callingUid = Binder.getCallingUid();
        final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
        if (!callerSystem && !isEnabled() && mWirelessConsentRequired
                && startConsentUiIfNeeded(packageName,
                callingUid, BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
            return false;
        }
        }

        if (DBG) {
            Slog.d(TAG, "enable(" + packageName + "):  mBluetooth =" + mBluetooth + " mBinding = "
@@ -939,26 +991,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    }

    public boolean disable(String packageName, boolean persist) throws RemoteException {
        final int callingUid = Binder.getCallingUid();
        final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;

        if (!callerSystem) {
            // Check if packageName belongs to callingUid
            checkPackage(callingUid, packageName);

            if (!checkIfCallerIsForegroundUser()) {
                Slog.w(TAG, "disable(): not allowed for non-active and non system user");
        if (!checkBluetoothPermissions(packageName, true)) {
            if (DBG) {
                Slog.d(TAG, "disable(): not disabling - bluetooth disallowed");
            }
            return false;
        }

            mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                    "Need BLUETOOTH ADMIN permission");

            if (isEnabled() && mWirelessConsentRequired && startConsentUiIfNeeded(packageName,
        final int callingUid = Binder.getCallingUid();
        final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
        if (!callerSystem && isEnabled() && mWirelessConsentRequired
                && startConsentUiIfNeeded(packageName,
                callingUid, BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
            return false;
        }
        }

        if (DBG) {
            Slog.d(TAG, "disable(): mBluetooth = " + mBluetooth + " mBinding = " + mBinding);