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

Commit e59de5a6 authored by Ugo Yu's avatar Ugo Yu Committed by Michael Bestas
Browse files

Limit the retry attemps on restarting Bluetooth

* Back off the delay time of resarting Bluetooth when
  there's continous turn on failures.
* Stop to restart Bluetooth if we are seeing back to
  back Bluetooth crashes, so we won't see endless
  diaglogs popping out.

Bug: 144572641
Test: Manual
Change-Id: Ifeffa214256eab61901c123fc88b037fc6d7dd50
parent be4662a2
Loading
Loading
Loading
Loading
+27 −21
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {

    private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
    //Maximum msec to wait for service restart
    private static final int SERVICE_RESTART_TIME_MS = 200;
    private static final int SERVICE_RESTART_TIME_MS = 400;
    //Maximum msec to wait for restart due to error
    private static final int ERROR_RESTART_TIME_MS = 3000;
    //Maximum msec to delay MESSAGE_USER_SWITCHED
@@ -1634,13 +1634,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        //     ActivityManager detects it.
                        // The waiting for (b) and (c) is accomplished by
                        // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
                        // message. On slower devices, that delay needs to be
                        // on the order of (2 * SERVICE_RESTART_TIME_MS).
                        // message. The delay time is backed off if Bluetooth
                        // continuously failed to turn on itself.
                        //
                        waitForOnOff(false, true);
                        Message restartMsg =
                                mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                        mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
                        mHandler.sendMessageDelayed(restartMsg, getServiceRestartMs());
                    }
                    break;

@@ -1819,7 +1819,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            waitForOnOff(false, true);
                            Message restartMsg =
                                    mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                            mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
                            mHandler.sendMessageDelayed(restartMsg, getServiceRestartMs());
                        }
                    }
                    if (newState == BluetoothAdapter.STATE_ON
@@ -1862,7 +1862,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        // Send a Bluetooth Restart message
                        Message restartMsg =
                                mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                        mHandler.sendMessageDelayed(restartMsg, SERVICE_RESTART_TIME_MS);
                        mHandler.sendMessageDelayed(restartMsg, getServiceRestartMs());
                    }

                    sendBluetoothServiceDownCallback();
@@ -1885,7 +1885,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    break;
                }
                case MESSAGE_RESTART_BLUETOOTH_SERVICE: {
                    Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE");
                    mErrorRecoveryRetryCounter++;
                    Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE: retry count="
                            + mErrorRecoveryRetryCounter);
                    if (mErrorRecoveryRetryCounter < MAX_ERROR_RESTART_RETRIES) {
                        /* Enable without persisting the setting as
                         it doesnt change when IBluetooth
                         service restarts */
@@ -1893,6 +1896,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        addActiveLog(BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTARTED,
                                mContext.getPackageName(), true);
                        handleEnable(mQuietEnable);
                    } else {
                        Slog.e(TAG, "Reach maximum retry to restart Bluetooth!");
                    }
                    break;
                }
                case MESSAGE_TIMEOUT_BIND: {
@@ -2331,13 +2337,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {

        mEnable = false;

        if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
        // Send a Bluetooth Restart message to reenable bluetooth
        Message restartMsg = mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
        mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
        } else {
            // todo: notify user to power down and power up phone to make bluetooth work.
        }
    }

    private boolean isBluetoothDisallowed() {
@@ -2373,6 +2375,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }
    }

    private int getServiceRestartMs() {
        return (mErrorRecoveryRetryCounter + 1) * SERVICE_RESTART_TIME_MS;
    }

    @Override
    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
        if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) {