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

Commit bd306bab authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes Id36f4d43,I5c97a8f6 into main

* changes:
  SystemServer: stop repeating getPackageName
  SystemServer: Static import of BluetoothProtoEnums
parents 4a0bc439 3732d90d
Loading
Loading
Loading
Loading
+57 −98
Original line number Diff line number Diff line
@@ -23,6 +23,17 @@ import static android.bluetooth.BluetoothAdapter.STATE_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_TURNING_ON;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_CRASH;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_DISALLOWED;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTARTED;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_START_ERROR;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT;
import static android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH;
import static android.os.PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED;

import static com.android.modules.utils.build.SdkLevel.isAtLeastV;
@@ -34,7 +45,6 @@ import android.annotation.RequiresPermission;
import android.app.ActivityManager;
import android.app.BroadcastOptions;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.BluetoothStatusCodes;
import android.bluetooth.IBluetooth;
import android.bluetooth.IBluetoothCallback;
@@ -302,9 +312,7 @@ class BluetoothManagerService {
        // DISALLOW_BLUETOOTH can only be set by DO or PO on the system user.
        // Only trigger once instead of for all users
        if (UserHandle.SYSTEM.equals(userHandle) && newBluetoothDisallowed) {
            sendDisableMsg(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_DISALLOWED,
                    mContext.getPackageName());
            sendDisableMsg(ENABLE_DISABLE_REASON_DISALLOWED);
        }
    }

@@ -328,17 +336,11 @@ class BluetoothManagerService {
                return false;
            }
            if (state == STATE_BLE_ON) {
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(),
                        false);
                addActiveLog(ENABLE_DISABLE_REASON_FACTORY_RESET, false);
                mAdapter.stopBle(mContext.getAttributionSource());
                return true;
            } else if (state == STATE_ON) {
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(),
                        false);
                addActiveLog(ENABLE_DISABLE_REASON_FACTORY_RESET, false);
                mAdapter.disable(mContext.getAttributionSource());
                return true;
            }
@@ -492,10 +494,7 @@ class BluetoothManagerService {
                    mAdapterLock.readLock().lock();
                    try {
                        if (mAdapter != null) {
                            addActiveLog(
                                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE,
                                    mContext.getPackageName(),
                                    false);
                            addActiveLog(ENABLE_DISABLE_REASON_AIRPLANE_MODE, false);
                            mAdapter.stopBle(mContext.getAttributionSource());
                            mEnable = false;
                            mEnableExternal = false;
@@ -506,15 +505,10 @@ class BluetoothManagerService {
                        mAdapterLock.readLock().unlock();
                    }
                } else if (st == STATE_ON) {
                    sendDisableMsg(
                            BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE,
                            mContext.getPackageName());
                    sendDisableMsg(ENABLE_DISABLE_REASON_AIRPLANE_MODE);
                }
            } else if (mEnableExternal) {
                sendEnableMsg(
                        mQuietEnableExternal,
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE,
                        mContext.getPackageName());
                sendEnableMsg(mQuietEnableExternal, ENABLE_DISABLE_REASON_AIRPLANE_MODE);
            } else if (st != STATE_ON) {
                autoOnSetupTimer();
            }
@@ -523,15 +517,10 @@ class BluetoothManagerService {

    private void handleSatelliteModeChanged(boolean isSatelliteModeOn) {
        if (shouldBluetoothBeOn(isSatelliteModeOn) && getState() != STATE_ON) {
            sendEnableMsg(
                    mQuietEnableExternal,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE,
                    mContext.getPackageName());
            sendEnableMsg(mQuietEnableExternal, ENABLE_DISABLE_REASON_SATELLITE_MODE);
        } else if (!shouldBluetoothBeOn(isSatelliteModeOn) && getState() != STATE_OFF) {
            AutoOnFeature.pause();
            sendDisableMsg(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE,
                    mContext.getPackageName());
            sendDisableMsg(ENABLE_DISABLE_REASON_SATELLITE_MODE);
        } else if (!isSatelliteModeOn
                && !shouldBluetoothBeOn(isSatelliteModeOn)
                && getState() != STATE_ON) {
@@ -927,11 +916,7 @@ class BluetoothManagerService {
                        mAdapterLock.readLock().lock();
                        try {
                            if (mAdapter != null) {
                                addActiveLog(
                                        BluetoothProtoEnums
                                                .ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                                        mContext.getPackageName(),
                                        false);
                                addActiveLog(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, false);
                                mAdapter.stopBle(mContext.getAttributionSource());
                            }
                        } catch (RemoteException e) {
@@ -1016,11 +1001,7 @@ class BluetoothManagerService {
        }
        synchronized (mReceiver) {
            // waive WRITE_SECURE_SETTINGS permission check
            sendEnableMsg(
                    false,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                    packageName,
                    true);
            sendEnableMsg(false, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName, true);
        }
        return true;
    }
@@ -1050,10 +1031,7 @@ class BluetoothManagerService {
                disableBleScanMode();
            }
            if (!mEnableExternal) {
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                        packageName,
                        false);
                addActiveLog(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName, false);
                sendBrEdrDownCallback();
            }
        }
@@ -1156,10 +1134,7 @@ class BluetoothManagerService {
        synchronized (mReceiver) {
            mQuietEnableExternal = true;
            mEnableExternal = true;
            sendEnableMsg(
                    true,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                    packageName);
            sendEnableMsg(true, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }
@@ -1188,10 +1163,7 @@ class BluetoothManagerService {
            } finally {
                Binder.restoreCallingIdentity(callingIdentity);
            }
            sendEnableMsg(
                    false,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                    packageName);
            sendEnableMsg(false, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }
@@ -1218,8 +1190,7 @@ class BluetoothManagerService {
                setBluetoothPersistedState(BLUETOOTH_OFF);
            }
            mEnableExternal = false;
            sendDisableMsg(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
            sendDisableMsg(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }
@@ -1287,10 +1258,7 @@ class BluetoothManagerService {
        final boolean isSafeMode = mContext.getPackageManager().isSafeMode();
        if (mEnableExternal && isBluetoothPersistedStateOnBluetooth() && !isSafeMode) {
            Log.i(TAG, "internalHandleOnBootPhase: Auto-enabling Bluetooth.");
            sendEnableMsg(
                    mQuietEnableExternal,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT,
                    mContext.getPackageName());
            sendEnableMsg(mQuietEnableExternal, ENABLE_DISABLE_REASON_SYSTEM_BOOT);
        } else if (!isNameAndAddressSet()) {
            Log.i(TAG, "internalHandleOnBootPhase: Getting adapter name and address");
            mHandler.sendEmptyMessage(MESSAGE_GET_NAME_AND_ADDRESS);
@@ -1676,17 +1644,12 @@ class BluetoothManagerService {
                        Log.d(TAG, "MESSAGE_RESTORE_USER_SETTING: set Bluetooth state to disabled");
                        setBluetoothPersistedState(BLUETOOTH_OFF);
                        mEnableExternal = false;
                        sendDisableMsg(
                                BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING,
                                mContext.getPackageName());
                        sendDisableMsg(ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    } else if ((msg.arg1 == RESTORE_SETTING_TO_ON) && !mEnable) {
                        Log.d(TAG, "MESSAGE_RESTORE_USER_SETTING: set Bluetooth state to enabled");
                        mQuietEnableExternal = false;
                        mEnableExternal = true;
                        sendEnableMsg(
                                false,
                                BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING,
                                mContext.getPackageName());
                        sendEnableMsg(false, ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    } else {
                        Log.w(
                                TAG,
@@ -1823,10 +1786,7 @@ class BluetoothManagerService {

                    // log the unexpected crash
                    addCrashLog();
                    addActiveLog(
                            BluetoothProtoEnums.ENABLE_DISABLE_REASON_CRASH,
                            mContext.getPackageName(),
                            false);
                    addActiveLog(ENABLE_DISABLE_REASON_CRASH, false);
                    if (mEnable) {
                        mEnable = false;
                        mHandler.sendEmptyMessageDelayed(
@@ -1860,10 +1820,7 @@ class BluetoothManagerService {
                         * it doesn't change when IBluetooth
                         * service restarts */
                        mEnable = true;
                        addActiveLog(
                                BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTARTED,
                                mContext.getPackageName(),
                                true);
                        addActiveLog(ENABLE_DISABLE_REASON_RESTARTED, true);
                        handleEnable(mQuietEnable);
                    } else {
                        mAdapterLock.writeLock().lock();
@@ -1945,10 +1902,7 @@ class BluetoothManagerService {
            assert (mState.oneOf(STATE_ON));

            // disable
            addActiveLog(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH,
                    mContext.getPackageName(),
                    false);
            addActiveLog(ENABLE_DISABLE_REASON_USER_SWITCH, false);
            handleDisable();
            // Pbap service need receive STATE_TURNING_OFF intent to close
            bluetoothStateChangeHandler(STATE_ON, STATE_TURNING_OFF);
@@ -1971,10 +1925,7 @@ class BluetoothManagerService {
            mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
            mState.set(STATE_OFF);
            // enable
            addActiveLog(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH,
                    mContext.getPackageName(),
                    true);
            addActiveLog(ENABLE_DISABLE_REASON_USER_SWITCH, true);
            // mEnable flag could have been reset on stopBle. Reenable it.
            mEnable = true;
            handleEnable(mQuietEnable);
@@ -2120,11 +2071,19 @@ class BluetoothManagerService {
        return mState.waitForState(getSyncTimeout(), states);
    }

    private void sendDisableMsg(int reason) {
        sendDisableMsg(reason, mContext.getPackageName());
    }

    private void sendDisableMsg(int reason, String packageName) {
        mHandler.sendEmptyMessage(MESSAGE_DISABLE);
        addActiveLog(reason, packageName, false);
    }

    private void sendEnableMsg(boolean quietMode, int reason) {
        sendEnableMsg(quietMode, reason, mContext.getPackageName());
    }

    private void sendEnableMsg(boolean quietMode, int reason, String packageName) {
        sendEnableMsg(quietMode, reason, packageName, false);
    }
@@ -2135,6 +2094,10 @@ class BluetoothManagerService {
        mLastEnabledTime = SystemClock.elapsedRealtime();
    }

    private void addActiveLog(int reason, boolean enable) {
        addActiveLog(reason, mContext.getPackageName(), enable);
    }

    private void addActiveLog(int reason, String packageName, boolean enable) {
        ActiveLog lastActiveLog = mActiveLogs.peekLast();
        synchronized (mActiveLogs) {
@@ -2213,10 +2176,7 @@ class BluetoothManagerService {
        SystemClock.sleep(500);

        // disable
        addActiveLog(
                BluetoothProtoEnums.ENABLE_DISABLE_REASON_START_ERROR,
                mContext.getPackageName(),
                false);
        addActiveLog(ENABLE_DISABLE_REASON_START_ERROR, false);
        handleDisable();

        waitForState(STATE_OFF);
@@ -2490,29 +2450,28 @@ class BluetoothManagerService {

    private static String getEnableDisableReasonString(int reason) {
        switch (reason) {
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST:
            case ENABLE_DISABLE_REASON_APPLICATION_REQUEST:
                return "APPLICATION_REQUEST";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE:
            case ENABLE_DISABLE_REASON_AIRPLANE_MODE:
                return "AIRPLANE_MODE";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_DISALLOWED:
            case ENABLE_DISABLE_REASON_DISALLOWED:
                return "DISALLOWED";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTARTED:
            case ENABLE_DISABLE_REASON_RESTARTED:
                return "RESTARTED";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_START_ERROR:
            case ENABLE_DISABLE_REASON_START_ERROR:
                return "START_ERROR";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT:
            case ENABLE_DISABLE_REASON_SYSTEM_BOOT:
                return "SYSTEM_BOOT";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_CRASH:
            case ENABLE_DISABLE_REASON_CRASH:
                return "CRASH";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH:
            case ENABLE_DISABLE_REASON_USER_SWITCH:
                return "USER_SWITCH";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING:
            case ENABLE_DISABLE_REASON_RESTORE_USER_SETTING:
                return "RESTORE_USER_SETTING";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET:
            case ENABLE_DISABLE_REASON_FACTORY_RESET:
                return "FACTORY_RESET";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_INIT_FLAGS_CHANGED:
                return "INIT_FLAGS_CHANGED";
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_UNSPECIFIED:
            case ENABLE_DISABLE_REASON_SATELLITE_MODE:
                return "SATELLITE MODE";
            default:
                return "UNKNOWN[" + reason + "]";
        }