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

Commit d1a3e7ff authored by Anushree Ganjam's avatar Anushree Ganjam
Browse files

Move bitmapSupplier.get() call to a worker thread.

bitmapSupplier.get() is called twice which is unnecessary.
Call bitmapSupplier.get() in the constructor instead.

Bug: 232850298
Test: Manual.
OverView - shareImageToTarget https://b.corp.google.com/issues/232850298#comment8
Screenshot share from Toast - https://b.corp.google.com/issues/232850298#comment7

Change-Id: I920f89b90fafcc0d51b1b948fd76fdc9aaee4c52
parent 6663ebba
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -78,16 +78,17 @@ public class ImageActionsApi {
        addImageAndSendIntent(crop, intent, true, exceptionCallback);
    }

    @UiThread
    private void addImageAndSendIntent(@Nullable Rect crop, Intent intent, boolean setData,
            @Nullable Runnable exceptionCallback) {
        if (mBitmapSupplier.get() == null) {

        UI_HELPER_EXECUTOR.execute(() -> {
            Bitmap bitmap = mBitmapSupplier.get();
            if (bitmap == null) {
                Log.e(TAG, "No snapshot available, not starting share.");
                return;
            }

        UI_HELPER_EXECUTOR.execute(() -> persistBitmapAndStartActivity(mContext,
                mBitmapSupplier.get(), crop, intent, (uri, intentForUri) -> {
            persistBitmapAndStartActivity(mContext,
                    bitmap, crop, intent, (uri, intentForUri) -> {
                    intentForUri.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
                    if (setData) {
                        intentForUri.setData(uri);
@@ -95,7 +96,8 @@ public class ImageActionsApi {
                        intentForUri.putExtra(EXTRA_STREAM, uri);
                    }
                    return new Intent[]{intentForUri};
                }, TAG, exceptionCallback));
                }, TAG, exceptionCallback);
        });
    }

    /**
+48 −46
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import android.net.Uri;
import android.util.Log;
import android.view.View;

import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.core.content.FileProvider;

@@ -86,21 +85,23 @@ public class ImageActionUtils {
     * Launch the activity to share image for overview sharing. This is to share cropped bitmap
     * with specific share targets (with shortcutInfo and appTarget) rendered in overview.
     */
    @UiThread
    public static void shareImage(Context context, Supplier<Bitmap> bitmapSupplier, RectF rectF,
            ShortcutInfo shortcutInfo, AppTarget appTarget, String tag) {
        if (bitmapSupplier.get() == null) {
        UI_HELPER_EXECUTOR.execute(() -> {
            Bitmap bitmap = bitmapSupplier.get();
            if (bitmap == null) {
                return;
            }
            Rect crop = new Rect();
            rectF.round(crop);
            Intent intent = new Intent();
        Uri uri =  getImageUri(bitmapSupplier.get(), crop, context, tag);
            Uri uri = getImageUri(bitmap, crop, context, tag);
            ClipData clipdata = new ClipData(new ClipDescription("content",
                    new String[]{"image/png"}),
                    new ClipData.Item(uri));
            intent.setAction(Intent.ACTION_SEND)
            .setComponent(new ComponentName(appTarget.getPackageName(), appTarget.getClassName()))
                    .setComponent(
                            new ComponentName(appTarget.getPackageName(), appTarget.getClassName()))
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .addFlags(FLAG_GRANT_READ_URI_PERMISSION)
                    .setType("image/png")
@@ -115,38 +116,39 @@ public class ImageActionUtils {
            } else {
                context.startActivity(intent);
            }
        });
    }

    /**
     * Launch the activity to share image.
     */
    @UiThread
    public static void startShareActivity(Context context, Supplier<Bitmap> bitmapSupplier,
            Rect crop, Intent intent, String tag) {
        if (bitmapSupplier.get() == null) {
        UI_HELPER_EXECUTOR.execute(() -> {
            Bitmap bitmap = bitmapSupplier.get();
            if (bitmap == null) {
                Log.e(tag, "No snapshot available, not starting share.");
                return;
            }

        UI_HELPER_EXECUTOR.execute(() -> persistBitmapAndStartActivity(context,
                bitmapSupplier.get(), crop, intent, ImageActionUtils::getShareIntentForImageUri,
                tag));
            persistBitmapAndStartActivity(context, bitmap, crop, intent,
                    ImageActionUtils::getShareIntentForImageUri, tag);
        });
    }

    /**
     * Launch the activity to share image with shared element transition.
     */
    @UiThread
    public static void startShareActivity(Context context, Supplier<Bitmap> bitmapSupplier,
            Rect crop, Intent intent, String tag, View sharedElement) {
        if (bitmapSupplier.get() == null) {
        UI_HELPER_EXECUTOR.execute(() -> {
            Bitmap bitmap = bitmapSupplier.get();
            if (bitmap == null) {
                Log.e(tag, "No snapshot available, not starting share.");
                return;
            }

        UI_HELPER_EXECUTOR.execute(() -> persistBitmapAndStartActivity(context,
                bitmapSupplier.get(), crop, intent, ImageActionUtils::getShareIntentForImageUri,
                tag, sharedElement));
            persistBitmapAndStartActivity(context, bitmap,
                    crop, intent, ImageActionUtils::getShareIntentForImageUri, tag, sharedElement);
        });
    }

    /**