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);
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      */
74     @UiThread
shareAsDataWithExplicitIntent(@ullable Rect crop, Intent intent)75     public void shareAsDataWithExplicitIntent(@Nullable Rect crop, Intent intent) {
76         addImageAndSendIntent(crop, intent, true);
77     }
78 
79     @UiThread
addImageAndSendIntent(@ullable Rect crop, Intent intent, boolean setData)80     private void addImageAndSendIntent(@Nullable Rect crop, Intent intent, boolean setData) {
81         if (mBitmapSupplier.get() == null) {
82             Log.e(TAG, "No snapshot available, not starting share.");
83             return;
84         }
85 
86         UI_HELPER_EXECUTOR.execute(() -> persistBitmapAndStartActivity(mContext,
87                 mBitmapSupplier.get(), crop, intent, (uri, intentForUri) -> {
88                     intentForUri.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
89                     if (setData) {
90                         intentForUri.setData(uri);
91                     } else {
92                         intentForUri.putExtra(EXTRA_STREAM, uri);
93                     }
94                     return new Intent[]{intentForUri};
95                 }, TAG));
96     }
97 
98     /**
99      * Share the image this api was constructed with.
100      */
101     @UiThread
startShareActivity(Rect crop)102     public void startShareActivity(Rect crop) {
103         ImageActionUtils.startShareActivity(mContext, mBitmapSupplier, crop, null, TAG);
104     }
105 
106     /**
107      * @param screenshot       to be saved to the media store.
108      * @param screenshotBounds the location of where the bitmap was laid out on the screen in
109      *                         screen coordinates.
110      * @param visibleInsets    that are used to draw the screenshot within the bounds.
111      * @param task             of the task that the screenshot was taken of.
112      */
saveScreenshot(Bitmap screenshot, Rect screenshotBounds, Insets visibleInsets, Task.TaskKey task)113     public void saveScreenshot(Bitmap screenshot, Rect screenshotBounds,
114             Insets visibleInsets, Task.TaskKey task) {
115         ImageActionUtils.saveScreenshot(mSystemUiProxy, screenshot, screenshotBounds, visibleInsets,
116                 task);
117     }
118 
119     /**
120      * Share the image when user taps on overview share targets.
121      */
122     @UiThread
shareImage(RectF rectF, ShortcutInfo shortcutInfo, AppTarget appTarget)123     public void shareImage(RectF rectF, ShortcutInfo shortcutInfo, AppTarget appTarget) {
124         ImageActionUtils.shareImage(mContext, mBitmapSupplier, rectF, shortcutInfo, appTarget, TAG);
125     }
126 }
127