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

Commit 54c6ea7f authored by Xin Li's avatar Xin Li
Browse files

Merge QQ3A.200605.002 into master

Bug: 158095402
Merged-In: I5e28537689d1a53e99f91eafd494eb3a0c890f46
Change-Id: I436ef37a509cbe56d02c6d8d3a38c776f4b4883f
parents 57e4e9be 8f7ff007
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -1196,10 +1196,11 @@ public final class BluetoothAdapter {
    public boolean factoryReset() {
        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                return mService.factoryReset();
            if (mService != null && mService.factoryReset()
                    && mManagerService != null && mManagerService.onFactoryReset()) {
                return true;
            }
            Log.e(TAG, "factoryReset(): IBluetooth Service is null");
            Log.e(TAG, "factoryReset(): Setting persist.bluetooth.factoryreset to retry later");
            SystemProperties.set("persist.bluetooth.factoryreset", "true");
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
+60 −30
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@@ -266,6 +267,46 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                }
            };

    public boolean onFactoryReset() {
        // Wait for stable state if bluetooth is temporary state.
        int state = getState();
        if (state == BluetoothAdapter.STATE_BLE_TURNING_ON
                || state == BluetoothAdapter.STATE_TURNING_ON
                || state == BluetoothAdapter.STATE_TURNING_OFF) {
            if (!waitForState(Set.of(BluetoothAdapter.STATE_BLE_ON, BluetoothAdapter.STATE_ON))) {
                return false;
            }
        }

        // Clear registered LE apps to force shut-off Bluetooth
        clearBleApps();
        state = getState();
        try {
            mBluetoothLock.readLock().lock();
            if (mBluetooth == null) {
                return false;
            }
            if (state == BluetoothAdapter.STATE_BLE_ON) {
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(), false);
                mBluetooth.onBrEdrDown();
                return true;
            } else if (state == BluetoothAdapter.STATE_ON) {
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(), false);
                mBluetooth.disable();
                return true;
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "Unable to shutdown Bluetooth", e);
        } finally {
            mBluetoothLock.readLock().unlock();
        }
        return false;
    }

    public void onAirplaneModeChanged() {
        synchronized (this) {
            if (isBluetoothPersistedStateOn()) {
@@ -1920,9 +1961,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }

                    if (!mEnable) {
                        waitForOnOff(true, false);
                        waitForState(Set.of(BluetoothAdapter.STATE_ON));
                        handleDisable();
                        waitForOnOff(false, false);
                        waitForState(Set.of(BluetoothAdapter.STATE_OFF,
                                BluetoothAdapter.STATE_TURNING_ON,
                                BluetoothAdapter.STATE_TURNING_OFF,
                                BluetoothAdapter.STATE_BLE_TURNING_ON,
                                BluetoothAdapter.STATE_BLE_ON,
                                BluetoothAdapter.STATE_BLE_TURNING_OFF));
                    }
                    break;
                }
@@ -1954,7 +2000,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            == BluetoothAdapter.STATE_OFF)) {
                        if (mEnable) {
                            Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting.");
                            waitForOnOff(false, true);
                            waitForState(Set.of(BluetoothAdapter.STATE_OFF));
                            Message restartMsg =
                                    mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                            mHandler.sendMessageDelayed(restartMsg, getServiceRestartMs());
@@ -2083,7 +2129,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            mState = BluetoothAdapter.STATE_TURNING_ON;
                        }

                        waitForOnOff(true, false);
                        waitForState(Set.of(BluetoothAdapter.STATE_ON));

                        if (mState == BluetoothAdapter.STATE_TURNING_ON) {
                            bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
@@ -2098,7 +2144,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
                                BluetoothAdapter.STATE_TURNING_OFF);

                        boolean didDisableTimeout = !waitForOnOff(false, true);
                        boolean didDisableTimeout =
                                !waitForState(Set.of(BluetoothAdapter.STATE_OFF));

                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
                                BluetoothAdapter.STATE_OFF);
@@ -2345,12 +2392,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }
    }

    /**
     *  if on is true, wait for state become ON
     *  if off is true, wait for state become OFF
     *  if both on and off are false, wait for state not ON
     */
    private boolean waitForOnOff(boolean on, boolean off) {
    private boolean waitForState(Set<Integer> states) {
        int i = 0;
        while (i < 10) {
            try {
@@ -2358,33 +2400,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                if (mBluetooth == null) {
                    break;
                }
                if (on) {
                    if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) {
                        return true;
                    }
                } else if (off) {
                    if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) {
                if (states.contains(mBluetooth.getState())) {
                    return true;
                }
                } else {
                    if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) {
                        return true;
                    }
                }
            } catch (RemoteException e) {
                Slog.e(TAG, "getState()", e);
                break;
            } finally {
                mBluetoothLock.readLock().unlock();
            }
            if (on || off) {
            SystemClock.sleep(300);
            } else {
                SystemClock.sleep(50);
            }
            i++;
        }
        Slog.e(TAG, "waitForOnOff time out");
        Slog.e(TAG, "waitForState " + states + " time out");
        return false;
    }

@@ -2445,7 +2473,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                mContext.getPackageName(), false);
        handleDisable();

        waitForOnOff(false, true);
        waitForState(Set.of(BluetoothAdapter.STATE_OFF));

        sendBluetoothServiceDownCallback();

@@ -2613,6 +2641,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                return "USER_SWITCH";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING:
                return "RESTORE_USER_SETTING";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET:
                return "FACTORY_RESET";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_UNSPECIFIED:
            default: return "UNKNOWN[" + reason + "]";
        }