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

Commit dfca3dbd authored by William Hester's avatar William Hester
Browse files

Consolidate point where ADB is enabled for Test Harness Mode

Test Harness Mode relies on enabling ADB upon boot. However, currently,
there appears to be a race condition when enabling ADB. Test Harness
Mode enables ADB in PHASE_BOOT_COMPLETED, and ADB sets itself up in
PHASE_ACTIVITY_MANAGER_READY, both by writing a setting. It appears that
sometimes, the setting for initializing ADB's state commits *after* the
setting to enable ADB for Test Harness Mode, which means that ADB turns
on for a brief moment, then off until the device is manually fixed.

This changes it so that the AdbService checks the Test Harness Mode
property when initializing ADB state, always enabling ADB on boot if
Test Harness Mode is enabled.

Bug: 232813060
Test: tested with user and userdebug builds, both enable adb correctly
Change-Id: I518415dda275bc3d563820ead611e5f017368c95
parent f012e832
Loading
Loading
Loading
Loading
+19 −15
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.testharness.TestHarnessModeService;

import java.io.File;
import java.io.FileDescriptor;
@@ -163,18 +164,8 @@ public class AdbService extends IAdbManager.Stub {
        }
    }

    private void initAdbState() {
    private void registerContentObservers() {
        try {
            /*
             * Use the normal bootmode persistent prop to maintain state of adb across
             * all boot modes.
             */
            mIsAdbUsbEnabled = containsFunction(
                    SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY, ""),
                    UsbManager.USB_FUNCTION_ADB);
            mIsAdbWifiEnabled = "1".equals(
                    SystemProperties.get(WIFI_PERSISTENT_CONFIG_PROPERTY, "0"));

            // register observer to listen for settings changes
            mObserver = new AdbSettingsObserver();
            mContentResolver.registerContentObserver(
@@ -184,7 +175,7 @@ public class AdbService extends IAdbManager.Stub {
                    Settings.Global.getUriFor(Settings.Global.ADB_WIFI_ENABLED),
                    false, mObserver);
        } catch (Exception e) {
            Slog.e(TAG, "Error in initAdbState", e);
            Slog.e(TAG, "Error in registerContentObservers", e);
        }
    }

@@ -248,7 +239,7 @@ public class AdbService extends IAdbManager.Stub {
        mContentResolver = context.getContentResolver();
        mDebuggingManager = new AdbDebuggingManager(context);

        initAdbState();
        registerContentObservers();
        LocalServices.addService(AdbManagerInternal.class, new AdbManagerInternalImpl());
    }

@@ -259,10 +250,23 @@ public class AdbService extends IAdbManager.Stub {
    public void systemReady() {
        if (DEBUG) Slog.d(TAG, "systemReady");

        /*
         * Use the normal bootmode persistent prop to maintain state of adb across
         * all boot modes.
         */
        mIsAdbUsbEnabled = containsFunction(
                SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY, ""),
                UsbManager.USB_FUNCTION_ADB);
        boolean shouldEnableAdbUsb = mIsAdbUsbEnabled
                || SystemProperties.getBoolean(
                        TestHarnessModeService.TEST_HARNESS_MODE_PROPERTY, false);
        mIsAdbWifiEnabled = "1".equals(
                SystemProperties.get(WIFI_PERSISTENT_CONFIG_PROPERTY, "0"));

        // make sure the ADB_ENABLED setting value matches the current state
        try {
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, mIsAdbUsbEnabled ? 1 : 0);
                    Settings.Global.ADB_ENABLED, shouldEnableAdbUsb ? 1 : 0);
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_WIFI_ENABLED, mIsAdbWifiEnabled ? 1 : 0);
        } catch (SecurityException e) {
@@ -272,7 +276,7 @@ public class AdbService extends IAdbManager.Stub {
    }

    /**
     * Callend in response to {@code SystemService.PHASE_BOOT_COMPLETED} from {@code SystemServer}.
     * Called in response to {@code SystemService.PHASE_BOOT_COMPLETED} from {@code SystemServer}.
     */
    public void bootCompleted() {
        if (DEBUG) Slog.d(TAG, "boot completed");
+1 −0
Original line number Diff line number Diff line
williamhester@google.com
+3 −3
Original line number Diff line number Diff line
@@ -69,8 +69,8 @@ import java.util.Set;
 * automatic updates, etc.) are all disabled by default but may be re-enabled by the user.
 */
public class TestHarnessModeService extends SystemService {
    public static final String TEST_HARNESS_MODE_PROPERTY = "persist.sys.test_harness";
    private static final String TAG = TestHarnessModeService.class.getSimpleName();
    private static final String TEST_HARNESS_MODE_PROPERTY = "persist.sys.test_harness";

    private PersistentDataBlockManagerInternal mPersistentDataBlockManagerInternal;

@@ -168,9 +168,9 @@ public class TestHarnessModeService extends SystemService {
            Slog.d(TAG, "Restarted adbd");
        }

        // Disable the TTL for ADB keys before enabling ADB
        // Disable the TTL for ADB keys before ADB is enabled as a part of AdbService's
        // initialization.
        Settings.Global.putLong(cr, Settings.Global.ADB_ALLOWED_CONNECTION_TIME, 0);
        Settings.Global.putInt(cr, Settings.Global.ADB_ENABLED, 1);
        Settings.Global.putInt(cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
        Settings.Global.putInt(cr, Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 0);
        Settings.Global.putInt(