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

Commit 8a91775d authored by James O'Leary's avatar James O'Leary Committed by Android (Google) Code Review
Browse files

Merge "Add completion handler for ScreenshotHelper" into qt-qpr1-dev

parents ae4ab469 fa5bb7af
Loading
Loading
Loading
Loading
+66 −11
Original line number Diff line number Diff line
package com.android.internal.util;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -13,6 +14,8 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;

import java.util.function.Consumer;

public class ScreenshotHelper {
    private static final String TAG = "ScreenshotHelper";

@@ -33,18 +36,59 @@ public class ScreenshotHelper {
        mContext = context;
    }

    /**
     * Request a screenshot be taken with a specific timeout.
     *
     * Added to support reducing unit test duration; the method variant without a timeout argument
     * is recommended for general use.
     *
     * @param screenshotType     The type of screenshot, for example either
     *                           {@link android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN}
     *                           or
     *                           {@link android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION}
     * @param hasStatus          {@code true} if the status bar is currently showing. {@code false}
     *                           if
     *                           not.
     * @param hasNav             {@code true} if the navigation bar is currently showing. {@code
     *                           false}
     *                           if not.
     * @param handler            A handler used in case the screenshot times out
     * @param completionConsumer Consumes `false` if a screenshot was not taken, and `true` if the
     *                           screenshot was taken.
     */
    public void takeScreenshot(final int screenshotType, final boolean hasStatus,
            final boolean hasNav, @NonNull Handler handler,
            @Nullable Consumer<Boolean> completionConsumer) {
        takeScreenshot(screenshotType, hasStatus, hasNav, SCREENSHOT_TIMEOUT_MS, handler,
                completionConsumer);
    }

    /**
     * Request a screenshot be taken.
     *
     * Added to support reducing unit test duration; the method variant without a timeout argument
     * is recommended for general use.
     *
     * @param screenshotType     The type of screenshot, for example either
     *                           {@link android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN}
     *                       or {@link android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION}
     * @param hasStatus {@code true} if the status bar is currently showing. {@code false} if not.
     * @param hasNav {@code true} if the navigation bar is currently showing. {@code false} if not.
     *                           or
     *                           {@link android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION}
     * @param hasStatus          {@code true} if the status bar is currently showing. {@code false}
     *                           if
     *                           not.
     * @param hasNav             {@code true} if the navigation bar is currently showing. {@code
     *                           false}
     *                           if not.
     * @param timeoutMs          If the screenshot hasn't been completed within this time period,
     *                           the screenshot attempt will be cancelled and `completionConsumer`
     *                           will be run.
     * @param handler            A handler used in case the screenshot times out
     * @param completionConsumer Consumes `false` if a screenshot was not taken, and `true` if the
     *                           screenshot was taken.
     */
    public void takeScreenshot(final int screenshotType, final boolean hasStatus,
            final boolean hasNav, @NonNull Handler handler) {
            final boolean hasNav, long timeoutMs, @NonNull Handler handler,
            @Nullable Consumer<Boolean> completionConsumer) {
        synchronized (mScreenshotLock) {
            if (mScreenshotConnection != null) {
                return;
@@ -54,7 +98,8 @@ public class ScreenshotHelper {
            final Intent serviceIntent = new Intent();

            final Runnable mScreenshotTimeout = new Runnable() {
                @Override public void run() {
                @Override
                public void run() {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != null) {
                            mContext.unbindService(mScreenshotConnection);
@@ -62,6 +107,9 @@ public class ScreenshotHelper {
                            notifyScreenshotError();
                        }
                    }
                    if (completionConsumer != null) {
                        completionConsumer.accept(false);
                    }
                }
            };

@@ -86,15 +134,22 @@ public class ScreenshotHelper {
                                        handler.removeCallbacks(mScreenshotTimeout);
                                    }
                                }
                                if (completionConsumer != null) {
                                    completionConsumer.accept(true);
                                }
                            }
                        };
                        msg.replyTo = new Messenger(h);
                        msg.arg1 = hasStatus ? 1 : 0;
                        msg.arg2 = hasNav ? 1 : 0;

                        try {
                            messenger.send(msg);
                        } catch (RemoteException e) {
                            Log.e(TAG, "Couldn't take screenshot: " + e);
                            if (completionConsumer != null) {
                                completionConsumer.accept(false);
                            }
                        }
                    }
                }
@@ -115,7 +170,7 @@ public class ScreenshotHelper {
                    Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
                    UserHandle.CURRENT)) {
                mScreenshotConnection = conn;
                handler.postDelayed(mScreenshotTimeout, SCREENSHOT_TIMEOUT_MS);
                handler.postDelayed(mScreenshotTimeout, timeoutMs);
            }
        }
    }
+28 −0
Original line number Diff line number Diff line
android_test {
    name: "ScreenshotHelperTests",

    srcs: [
        "src/**/*.java",
    ],
    
    static_libs: [
        "frameworks-base-testutils",
        "androidx.test.runner",
        "androidx.test.rules",
        "androidx.test.ext.junit",
        "mockito-target-minus-junit4",
        "platform-test-annotations",
    ],

    libs: [
        "android.test.runner",
        "android.test.base",
        "android.test.mock",
    ],

    platform_apis: true,
    test_suites: ["device-tests"],

    certificate: "platform",
}
+32 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:installLocation="internalOnly"
          package="com.android.internal.util"
          android:sharedUserId="android.uid.systemui" >

    <application >
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
            android:targetPackage="com.android.internal.util"
            android:label="Screenshot Helper Tests" />

            <protected-broadcast android:name="android.intent.action.USER_PRESENT" />

</manifest>
+98 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.util;

import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN;
import static android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.fail;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidJUnit4.class)
public final class ScreenshotHelperTest {
    private Context mContext;
    private ScreenshotHelper mScreenshotHelper;
    private Handler mHandler;


    @Before
    public void setUp() {
        // `ScreenshotHelper.notifyScreenshotError()` calls `Context.sendBroadcastAsUser()` and
        // `Context.bindServiceAsUser`.
        //
        // This raises a `SecurityException` if the device is locked. Calling either `Context`
        // method results in a broadcast of `android.intent.action. USER_PRESENT`. Only the system
        // process is allowed to broadcast that `Intent`.
        mContext = Mockito.spy(Context.class);
        Mockito.doNothing().when(mContext).sendBroadcastAsUser(any(), any());
        Mockito.doReturn(true).when(mContext).bindServiceAsUser(any(), any(), anyInt(), any());

        mHandler = new Handler(Looper.getMainLooper());
        mScreenshotHelper = new ScreenshotHelper(mContext);
    }

    @Test
    public void testFullscreenScreenshot() {
        mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, mHandler, null);
    }

    @Test
    public void testSelectedRegionScreenshot() {
        mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_SELECTED_REGION, false, false, mHandler,
                null);
    }

    @Test
    public void testScreenshotTimesOut() {
        long timeoutMs = 10;

        CountDownLatch lock = new CountDownLatch(1);
        mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, timeoutMs,
                mHandler,
                worked -> {
                    assertFalse(worked);
                    lock.countDown();
                });

        try {
            // Add tolerance for delay to prevent flakes.
            long awaitDurationMs = timeoutMs + 100;
            if (!lock.await(awaitDurationMs, TimeUnit.MILLISECONDS)) {
                fail("lock never freed");
            }
        } catch (InterruptedException e) {
            fail("lock interrupted");
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -614,7 +614,7 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
                    mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
                    MetricsLogger.action(mContext,
                            MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
                }
Loading