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

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

Disable lock screen and enable location in THM

In Test Harness Mode, the lock screen will be disabled so that tests can
start immediately, and location will be enabled (for use with CTS).

Bug: 80137798
Test: make, adb shell cmd testharness enable
Change-Id: I08f4c06865013d2cbdf7e95cf12fdf469c30b9e7
(cherry picked from commit fd0230bf)
parent abf224e8
Loading
Loading
Loading
Loading
+31 −9
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.debug.AdbManagerInternal;
import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.IBinder;
@@ -39,6 +40,7 @@ import android.util.Slog;

import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.internal.notification.SystemNotificationChannels;
import com.android.internal.widget.LockPatternUtils;
import com.android.server.LocalServices;
import com.android.server.PersistentDataBlockManagerInternal;
import com.android.server.SystemService;
@@ -95,6 +97,12 @@ public class TestHarnessModeService extends SystemService {
        super.onBootPhase(phase);
    }

    /**
     * Begin the setup for Test Harness Mode.
     *
     * <p>Note: This is just the things that <em>need</em> to be done before the device finishes
     * booting for the first time. Everything else should be done after the system is done booting.
     */
    private void setUpTestHarnessMode() {
        Slog.d(TAG, "Setting up test harness mode");
        byte[] testHarnessModeData = getPersistentDataBlock().getTestHarnessModeData();
@@ -105,9 +113,16 @@ public class TestHarnessModeService extends SystemService {
        // If there is data, we should set the device as provisioned, so that we skip the setup
        // wizard.
        setDeviceProvisioned();
        disableLockScreen();
        SystemProperties.set(TEST_HARNESS_MODE_PROPERTY, "1");
    }

    private void disableLockScreen() {
        UserInfo userInfo = getPrimaryUser();
        LockPatternUtils utils = new LockPatternUtils(getContext());
        utils.setLockScreenDisabled(true, userInfo.id);
    }

    private void completeTestHarnessModeSetup() {
        Slog.d(TAG, "Completing Test Harness Mode setup.");
        byte[] testHarnessModeData = getPersistentDataBlock().getTestHarnessModeData();
@@ -117,8 +132,8 @@ public class TestHarnessModeService extends SystemService {
        }
        try {
            setUpAdbFiles(PersistentData.fromBytes(testHarnessModeData));
            disableAutoSync();
            configureSettings();
            configureUser();
        } catch (SetUpTestHarnessModeException e) {
            Slog.e(TAG, "Failed to set up Test Harness Mode. Bad data.", e);
        } finally {
@@ -130,12 +145,6 @@ public class TestHarnessModeService extends SystemService {
        }
    }

    private void disableAutoSync() {
        UserInfo primaryUser = UserManager.get(getContext()).getPrimaryUser();
        ContentResolver
            .setMasterSyncAutomaticallyAsUser(false, primaryUser.getUserHandle().getIdentifier());
    }

    private void configureSettings() {
        ContentResolver cr = getContext().getContentResolver();

@@ -158,6 +167,20 @@ public class TestHarnessModeService extends SystemService {
        writeBytesToFile(persistentData.mAdbTempKeys, adbManager.getAdbTempKeysFile().toPath());
    }

    private void configureUser() {
        UserInfo primaryUser = getPrimaryUser();

        ContentResolver.setMasterSyncAutomaticallyAsUser(false, primaryUser.id);

        LocationManager locationManager = getContext().getSystemService(LocationManager.class);
        locationManager.setLocationEnabledForUser(true, primaryUser.getUserHandle());
    }

    private UserInfo getPrimaryUser() {
        UserManager userManager = UserManager.get(getContext());
        return userManager.getPrimaryUser();
    }

    private void writeBytesToFile(byte[] keys, Path adbKeys) {
        try {
            OutputStream fileOutputStream = Files.newOutputStream(adbKeys);
@@ -266,9 +289,8 @@ public class TestHarnessModeService extends SystemService {
        }

        private boolean isDeviceSecure() {
            UserInfo primaryUser = UserManager.get(getContext()).getPrimaryUser();
            KeyguardManager keyguardManager = getContext().getSystemService(KeyguardManager.class);
            return keyguardManager.isDeviceSecure(primaryUser.id);
            return keyguardManager.isDeviceSecure(getPrimaryUser().id);
        }

        private int handleEnable() {