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

Unverified Commit 62cd2e66 authored by Luca Stefani's avatar Luca Stefani Committed by Michael Bestas
Browse files

fixup! SystemUI: screenshot: Add delete action chip intent

Change-Id: I5c972b827ed01934d709aef6f6e87011a7e74434
parent 7fda88d0
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.res.R
import com.android.systemui.screenshot.DeleteScreenshotReceiver.EXTRA_SCREENSHOT_URI_ID
import com.android.systemui.screenshot.scroll.LongScreenshotActivity
import com.android.systemui.shared.Flags.usePreferredImageEditor
import java.util.function.Consumer
@@ -134,14 +133,16 @@ constructor(
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
    }

    fun createDelete(rawUri: Uri, context: Context): PendingIntent {
        return PendingIntent.getBroadcast(context, rawUri.toString().hashCode(),
                Intent(context, DeleteScreenshotReceiver::class.java)
                        .putExtra(EXTRA_SCREENSHOT_URI_ID, rawUri.toString())
                        .addFlags(Intent.FLAG_RECEIVER_FOREGROUND),
                        (PendingIntent.FLAG_CANCEL_CURRENT
                        or PendingIntent.FLAG_ONE_SHOT
                        or PendingIntent.FLAG_IMMUTABLE))
    fun createDelete(rawUri: Uri): PendingIntent {
        val intent = Intent(context, DeleteScreenshotReceiver::class.java).apply {
            data = rawUri
        }
        return PendingIntent.getBroadcast(
            context,
            rawUri.hashCode(),
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_ONE_SHOT,
        )
    }

    /** @return an Intent to start the LongScreenshotActivity */
+0 −70
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.systemui.screenshot;

import static com.android.systemui.screenshot.DeleteScreenshotReceiver.EXTRA_SCREENSHOT_URI_ID;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_ACTION_TYPE;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_ID;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_SMART_ACTIONS_ENABLED;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.android.systemui.dagger.qualifiers.Background;

import java.util.concurrent.Executor;

import javax.inject.Inject;

/**
 * Removes the file at a provided URI.
 */
public class DeleteScreenshotReceiver extends BroadcastReceiver {
    public static final String EXTRA_SCREENSHOT_URI_ID = "android:screenshot_uri_id";

    private final ScreenshotSmartActions mScreenshotSmartActions;
    private final Executor mBackgroundExecutor;

    @Inject
    public DeleteScreenshotReceiver(ScreenshotSmartActions screenshotSmartActions,
            @Background Executor backgroundExecutor) {
        mScreenshotSmartActions = screenshotSmartActions;
        mBackgroundExecutor = backgroundExecutor;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (!intent.hasExtra(EXTRA_SCREENSHOT_URI_ID)) {
            return;
        }

        // And delete the image from the media store
        final Uri uri = Uri.parse(intent.getStringExtra(EXTRA_SCREENSHOT_URI_ID));
        mBackgroundExecutor.execute(() -> {
            ContentResolver resolver = context.getContentResolver();
            resolver.delete(uri, null, null);
        });
        if (intent.getBooleanExtra(EXTRA_SMART_ACTIONS_ENABLED, false)) {
            mScreenshotSmartActions.notifyScreenshotAction(
                    intent.getStringExtra(EXTRA_ID), intent.getStringExtra(EXTRA_ACTION_TYPE),
                    false, null);
        }
    }
}
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 * Copyright (C) 2025 The LineageOS 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.systemui.screenshot

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.android.systemui.dagger.qualifiers.Background
import java.util.concurrent.Executor
import javax.inject.Inject

/**
 * Removes the file at a provided URI.
 */
class DeleteScreenshotReceiver @Inject constructor(
    @Background private val backgroundExecutor: Executor
) : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val uri = intent.data ?: return

        // Delete the image from the media store
        backgroundExecutor.execute {
            context.contentResolver.delete(uri, null, null)
        }

    }
}
+1 −1
Original line number Diff line number Diff line
@@ -155,7 +155,7 @@ constructor(
            uiEventLogger.log(SCREENSHOT_DELETE_TAPPED, 0, request.packageNameString)
            onDeferrableActionTapped { result ->
                actionExecutor.sendPendingIntent(
                    actionIntentCreator.createDelete(result.uri, context)
                    actionIntentCreator.createDelete(result.uri)
                )
            }
        }
+6 −42
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 * Copyright (C) 2025 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,16 +17,10 @@

package com.android.systemui.screenshot;

import static com.android.systemui.screenshot.DeleteScreenshotReceiver.EXTRA_SCREENSHOT_URI_ID;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_ACTION_TYPE;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_ID;
import static com.android.systemui.screenshot.SmartActionsReceiver.EXTRA_SMART_ACTIONS_ENABLED;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@@ -57,8 +52,6 @@ import java.util.concurrent.Executor;
@SmallTest
public class DeleteScreenshotReceiverTest extends SysuiTestCase {

    @Mock
    private ScreenshotSmartActions mMockScreenshotSmartActions;
    @Mock
    private Executor mMockExecutor;

@@ -68,8 +61,7 @@ public class DeleteScreenshotReceiverTest extends SysuiTestCase {
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mDeleteScreenshotReceiver =
                new DeleteScreenshotReceiver(mMockScreenshotSmartActions, mMockExecutor);
        mDeleteScreenshotReceiver = new DeleteScreenshotReceiver(mMockExecutor);
    }

    @Test
@@ -79,15 +71,10 @@ public class DeleteScreenshotReceiverTest extends SysuiTestCase {
        mDeleteScreenshotReceiver.onReceive(mContext, intent);

        verify(mMockExecutor, never()).execute(any(Runnable.class));
        verify(mMockScreenshotSmartActions, never()).notifyScreenshotAction(
                any(String.class), any(String.class), anyBoolean(),
                any(Intent.class));
    }

    @Test
    public void testFileDeleted() {
        DeleteScreenshotReceiver deleteScreenshotReceiver =
                new DeleteScreenshotReceiver(mMockScreenshotSmartActions, mFakeExecutor);
        ContentResolver contentResolver = mContext.getContentResolver();
        final Uri testUri = contentResolver.insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, getFakeContentValues());
@@ -97,41 +84,18 @@ public class DeleteScreenshotReceiverTest extends SysuiTestCase {
            Cursor cursor =
                    contentResolver.query(testUri, null, null, null, null);
            assertEquals(1, cursor.getCount());
            Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class)
                    .putExtra(EXTRA_SCREENSHOT_URI_ID, testUri.toString());
            Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class);
            intent.setData(testUri);

            deleteScreenshotReceiver.onReceive(mContext, intent);
            mDeleteScreenshotReceiver.onReceive(mContext, intent);
            int runCount = mFakeExecutor.runAllReady();

            assertEquals(1, runCount);
            cursor =
                    contentResolver.query(testUri, null, null, null, null);
            cursor = contentResolver.query(testUri, null, null, null, null);
            assertEquals(0, cursor.getCount());
        } finally {
            contentResolver.delete(testUri, null, null);
        }

        // ensure smart actions not called by default
        verify(mMockScreenshotSmartActions, never()).notifyScreenshotAction(
                any(String.class), any(String.class), anyBoolean(), any(Intent.class));
    }

    @Test
    public void testNotifyScreenshotAction() {
        Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class);
        String uriString = "testUri";
        String testId = "testID";
        String testActionType = "testActionType";
        intent.putExtra(EXTRA_SCREENSHOT_URI_ID, uriString);
        intent.putExtra(EXTRA_ID, testId);
        intent.putExtra(EXTRA_SMART_ACTIONS_ENABLED, true);
        intent.putExtra(EXTRA_ACTION_TYPE, testActionType);

        mDeleteScreenshotReceiver.onReceive(mContext, intent);

        verify(mMockExecutor).execute(any(Runnable.class));
        verify(mMockScreenshotSmartActions).notifyScreenshotAction(testId,
                testActionType, false, null);
    }

    private static ContentValues getFakeContentValues() {