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

Unverified Commit 6ba963bc authored by ezio84's avatar ezio84 Committed by Michael Bestas
Browse files

Screenshot: Append app name to filename



Co-authored-by: default avatarDroidFreak32 <rushabshah32@gmail.com>
Co-authored-by: default avatarLuK1337 <priv.luk@gmail.com>
Co-authored-by: default avatarMichael Bestas <mkbestas@lineageos.org>
Co-authored-by: default avatarWang Han <wanghan1995315@gmail.com>
Change-Id: Ic2a42536bf763a0c6f7fb9ac4bbbe84f73723f91
parent fdc567ff
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ constructor(
                screenshot.bitmap,
                screenshot.userHandle,
                screenshot.displayId,
                null,
            )
        future.addListener(
            {
+18 −5
Original line number Diff line number Diff line
@@ -67,6 +67,9 @@ public class ImageExporter {

    // ex: 'Screenshot_20201215-090626.png'
    private static final String FILENAME_PATTERN = "Screenshot_%1$tY%<tm%<td-%<tH%<tM%<tS.%2$s";
    // ex: 'Screenshot_20201215-090626_Settings.png'
    private static final String FILENAME_WITH_APP_NAME_PATTERN =
            "Screenshot_%1$tY%<tm%<td-%<tH%<tM%<tS_%2$s.%3$s";
    // ex: 'Screenshot_20201215-090626-display-1.png'
    private static final String CONNECTED_DISPLAY_FILENAME_PATTERN =
            "Screenshot_%1$tY%<tm%<td-%<tH%<tM%<tS-display-%2$d.%3$s";
@@ -151,14 +154,16 @@ public class ImageExporter {
     * @param executor  the thread for execution
     * @param bitmap    the bitmap to export
     * @param displayId the display id the bitmap comes from.
     * @param foregroundAppName the name of app running in foreground
     * @return a listenable future result
     */
    public ListenableFuture<Result> export(Executor executor, UUID requestId, Bitmap bitmap,
            UserHandle owner, int displayId) {
            UserHandle owner, int displayId, String foregroundAppName) {
        ZonedDateTime captureTime = ZonedDateTime.now(ZoneId.systemDefault());
        return export(executor,
                new Task(mResolver, requestId, bitmap, captureTime, mCompressFormat,
                        mQuality, owner, createFilename(captureTime, mCompressFormat, displayId)));
                        mQuality, owner, createFilename(captureTime, mCompressFormat, displayId,
                        foregroundAppName)));
    }

    /**
@@ -191,12 +196,14 @@ public class ImageExporter {
     *
     * @param executor the thread for execution
     * @param bitmap   the bitmap to export
     * @param foregroundAppName the name of app running in foreground
     * @return a listenable future result
     */
    public ListenableFuture<Result> export(Executor executor, UUID requestId, Bitmap bitmap,
            ZonedDateTime captureTime, UserHandle owner, int displayId) {
            ZonedDateTime captureTime, UserHandle owner, int displayId, String foregroundAppName) {
        return export(executor, new Task(mResolver, requestId, bitmap, captureTime, mCompressFormat,
                mQuality, owner, createFilename(captureTime, mCompressFormat, displayId)));
                mQuality, owner, createFilename(captureTime, mCompressFormat, displayId,
                foregroundAppName)));
    }

    /**
@@ -204,6 +211,7 @@ public class ImageExporter {
     *
     * @param executor the thread for execution
     * @param bitmap   the bitmap to export
     * @param foregroundAppName the name of app running in foreground
     * @return a listenable future result
     */
    ListenableFuture<Result> export(Executor executor, UUID requestId, Bitmap bitmap,
@@ -465,8 +473,13 @@ public class ImageExporter {
    }

    @VisibleForTesting
    static String createFilename(ZonedDateTime time, CompressFormat format, int displayId) {
    static String createFilename(ZonedDateTime time, CompressFormat format, int displayId,
            String foregroundAppName) {
        if (displayId == Display.DEFAULT_DISPLAY) {
            if (foregroundAppName != null) {
                return String.format(FILENAME_WITH_APP_NAME_PATTERN, time, foregroundAppName,
                        fileExtension(format));
            }
            return String.format(FILENAME_PATTERN, time, fileExtension(format));
        }
        return String.format(CONNECTED_DISPLAY_FILENAME_PATTERN, time, displayId,
+1 −1
Original line number Diff line number Diff line
@@ -646,7 +646,7 @@ public class LegacyScreenshotController implements InteractiveScreenshotHandler
            Consumer<Uri> finisher, Consumer<ImageExporter.Result> onResult) {
        ListenableFuture<ImageExporter.Result> future = mImageExporter.export(mBgExecutor,
                requestId, screenshot.getBitmap(), screenshot.getUserHandle(),
                mDisplay.getDisplayId());
                mDisplay.getDisplayId(), null);
        future.addListener(() -> {
            try {
                ImageExporter.Result result = future.get();
+10 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.Insets
@@ -78,6 +79,7 @@ internal constructor(
    private val screenshotHandler: TimeoutHandler,
    private val broadcastSender: BroadcastSender,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val packageManager: PackageManager,
    private val userManager: UserManager,
    private val assistContentRequester: AssistContentRequester,
    private val messageContainerController: MessageContainerController,
@@ -102,6 +104,7 @@ internal constructor(
    private var screenshotTakenInPortrait = false
    private var screenshotAnimation: Animator? = null
    private var packageName = ""
    private var packageLabel = ""

    /** Tracks config changes that require re-creating UI */
    private val configChanges =
@@ -161,6 +164,12 @@ internal constructor(
        Assert.isMainThread()
        screenshotHandler.resetTimeout()

        packageLabel = runCatching {
            val info = packageManager.getApplicationInfo(screenshot.packageNameString, 0)
            info.loadLabel(packageManager).toString()
        }.getOrDefault("")
        scrollCaptureExecutor.longScreenshotHolder.foregroundAppName = packageLabel

        val currentBitmap = screenshot.bitmap
        if (currentBitmap == null) {
            Log.e(TAG, "handleScreenshot: Screenshot bitmap was null")
@@ -496,6 +505,7 @@ internal constructor(
                screenshot.bitmap,
                screenshot.userHandle,
                display.displayId,
                packageLabel,
            )
        future.addListener(
            {
+1 −1
Original line number Diff line number Diff line
@@ -207,7 +207,7 @@ final class AppClipsViewModel extends ViewModel {

            // Export and save the screenshot in background.
            ListenableFuture<ImageExporter.Result> exportFuture = mImageExporter.export(mBgExecutor,
                    UUID.randomUUID(), screenshotBitmap, user, Display.DEFAULT_DISPLAY);
                    UUID.randomUUID(), screenshotBitmap, user, Display.DEFAULT_DISPLAY, null);

            // Get the result and update state on main thread.
            exportFuture.addListener(() -> {
Loading