1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.quickstep; 18 19 import static android.content.Intent.EXTRA_STREAM; 20 import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION; 21 22 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; 23 import static com.android.quickstep.util.ImageActionUtils.persistBitmapAndStartActivity; 24 25 import android.app.prediction.AppTarget; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.pm.ShortcutInfo; 29 import android.graphics.Bitmap; 30 import android.graphics.Insets; 31 import android.graphics.Rect; 32 import android.graphics.RectF; 33 import android.util.Log; 34 35 import androidx.annotation.Nullable; 36 import androidx.annotation.UiThread; 37 38 import com.android.launcher3.BuildConfig; 39 import com.android.quickstep.util.ImageActionUtils; 40 import com.android.systemui.shared.recents.model.Task; 41 42 import java.util.function.Supplier; 43 44 /** 45 * Contains image selection functions necessary to complete overview action button functions. 46 */ 47 public class ImageActionsApi { 48 49 private static final String TAG = BuildConfig.APPLICATION_ID + "ImageActionsApi"; 50 51 protected final Context mContext; 52 protected final Supplier<Bitmap> mBitmapSupplier; 53 protected final SystemUiProxy mSystemUiProxy; 54 ImageActionsApi(Context context, Supplier<Bitmap> bitmapSupplier)55 public ImageActionsApi(Context context, Supplier<Bitmap> bitmapSupplier) { 56 mContext = context; 57 mBitmapSupplier = bitmapSupplier; 58 mSystemUiProxy = SystemUiProxy.INSTANCE.get(context); 59 } 60 61 /** 62 * Share the image this api was constructed with using the provided intent. The implementation 63 * should add an {@link Intent#EXTRA_STREAM} with the URI pointing to the image to the intent. 64 */ 65 @UiThread shareWithExplicitIntent(@ullable Rect crop, Intent intent)66 public void shareWithExplicitIntent(@Nullable Rect crop, Intent intent) { 67 addImageAndSendIntent(crop, intent, false, null /* exceptionCallback */); 68 } 69 70 /** 71 * Share the image this api was constructed with using the provided intent. The implementation 72 * should set the intent's data field to the URI pointing to the image. 73 * @param exceptionCallback An optional callback to be called when the intent can't be resolved 74 */ 75 @UiThread shareAsDataWithExplicitIntent(@ullable Rect crop, Intent intent, @Nullable Runnable exceptionCallback)76 public void shareAsDataWithExplicitIntent(@Nullable Rect crop, Intent intent, 77 @Nullable Runnable exceptionCallback) { 78 addImageAndSendIntent(crop, intent, true, exceptionCallback); 79 } 80 addImageAndSendIntent(@ullable Rect crop, Intent intent, boolean setData, @Nullable Runnable exceptionCallback)81 private void addImageAndSendIntent(@Nullable Rect crop, Intent intent, boolean setData, 82 @Nullable Runnable exceptionCallback) { 83 84 UI_HELPER_EXECUTOR.execute(() -> { 85 Bitmap bitmap = mBitmapSupplier.get(); 86 if (bitmap == null) { 87 Log.e(TAG, "No snapshot available, not starting share."); 88 return; 89 } 90 persistBitmapAndStartActivity(mContext, 91 bitmap, crop, intent, (uri, intentForUri) -> { 92 intentForUri.addFlags(FLAG_GRANT_READ_URI_PERMISSION); 93 if (setData) { 94 intentForUri.setData(uri); 95 } else { 96 intentForUri.putExtra(EXTRA_STREAM, uri); 97 } 98 return new Intent[]{intentForUri}; 99 }, TAG, exceptionCallback); 100 }); 101 } 102 103 /** 104 * Share the image this api was constructed with. 105 */ 106 @UiThread startShareActivity(Rect crop)107 public void startShareActivity(Rect crop) { 108 ImageActionUtils.startShareActivity(mContext, mBitmapSupplier, crop, null, TAG); 109 } 110 111 /** 112 * @param screenshot to be saved to the media store. 113 * @param screenshotBounds the location of where the bitmap was laid out on the screen in 114 * screen coordinates. 115 * @param visibleInsets that are used to draw the screenshot within the bounds. 116 * @param task of the task that the screenshot was taken of. 117 */ saveScreenshot(Bitmap screenshot, Rect screenshotBounds, Insets visibleInsets, Task.TaskKey task)118 public void saveScreenshot(Bitmap screenshot, Rect screenshotBounds, 119 Insets visibleInsets, Task.TaskKey task) { 120 ImageActionUtils.saveScreenshot(mSystemUiProxy, screenshot, screenshotBounds, visibleInsets, 121 task); 122 } 123 124 /** 125 * Share the image when user taps on overview share targets. 126 */ 127 @UiThread shareImage(RectF rectF, ShortcutInfo shortcutInfo, AppTarget appTarget)128 public void shareImage(RectF rectF, ShortcutInfo shortcutInfo, AppTarget appTarget) { 129 ImageActionUtils.shareImage(mContext, mBitmapSupplier, rectF, shortcutInfo, appTarget, TAG); 130 } 131 } 132