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

Commit 1901b92f authored by Vadim Tryshev's avatar Vadim Tryshev Committed by android-build-merger
Browse files

Merge "Adopt shell permission only to set and unset time limit." into ub-launcher3-qt-dev

am: 4c8d7a04

Change-Id: Id226c9a9acbc9f9daba7d5eccc6f6bf407649626
parents 6d1bbbcd 4c8d7a04
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -31,14 +31,14 @@ public class DigitalWellBeingToastTest extends AbstractQuickStepTest {
                mTargetContext.getSystemService(UsageStatsManager.class);
        final int observerId = 0;

        getInstrumentation().getUiAutomation().adoptShellPermissionIdentity();
        try {
            final String[] packages = new String[]{CALCULATOR_PACKAGE};

            // Set time limit for app.
            runWithShellPermission(() ->
                    usageStatsManager.registerAppUsageLimitObserver(observerId, packages,
                            Duration.ofSeconds(600), Duration.ofSeconds(300),
                    PendingIntent.getActivity(mTargetContext, -1, new Intent(), 0));
                            PendingIntent.getActivity(mTargetContext, -1, new Intent(), 0)));

            mLauncher.pressHome();
            final DigitalWellBeingToast toast = getToast();
@@ -47,13 +47,14 @@ public class DigitalWellBeingToastTest extends AbstractQuickStepTest {
            assertEquals("Toast text: ", "5 minutes left today", toast.getTextView().getText());

            // Unset time limit for app.
            usageStatsManager.unregisterAppUsageLimitObserver(observerId);
            runWithShellPermission(
                    () -> usageStatsManager.unregisterAppUsageLimitObserver(observerId));

            mLauncher.pressHome();
            assertFalse("Toast is visible", getToast().isShown());
        } finally {
            usageStatsManager.unregisterAppUsageLimitObserver(observerId);
            getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
            runWithShellPermission(
                    () -> usageStatsManager.unregisterAppUsageLimitObserver(observerId));
        }
    }

@@ -73,4 +74,14 @@ public class DigitalWellBeingToastTest extends AbstractQuickStepTest {
    private TaskView getLatestTask(Launcher launcher) {
        return launcher.<RecentsView>getOverviewPanel().getTaskViewAt(0);
    }

    private void runWithShellPermission(Runnable action) {
        getInstrumentation().getUiAutomation().adoptShellPermissionIdentity();
        try {
            action.run();
        } finally {
            getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
        }

    }
}