Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.systemui.screenshot; |
| 18 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 19 | import static android.content.Context.NOTIFICATION_SERVICE; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 20 | import static android.os.AsyncTask.THREAD_POOL_EXECUTOR; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 21 | import static android.provider.DeviceConfig.NAMESPACE_SYSTEMUI; |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 22 | import static android.view.View.VISIBLE; |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 23 | import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 24 | |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 25 | import static com.android.internal.config.sysui.SystemUiDeviceConfigFlags.SCREENSHOT_CORNER_FLOW; |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 26 | import static com.android.systemui.statusbar.phone.StatusBar.SYSTEM_DIALOG_REASON_SCREENSHOT; |
| 27 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 28 | import android.animation.Animator; |
| 29 | import android.animation.AnimatorListenerAdapter; |
| 30 | import android.animation.AnimatorSet; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 31 | import android.animation.ValueAnimator; |
| 32 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 33 | import android.annotation.Nullable; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 34 | import android.app.ActivityManager; |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 35 | import android.app.ActivityOptions; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 36 | import android.app.Notification; |
| 37 | import android.app.NotificationManager; |
| 38 | import android.app.PendingIntent; |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 39 | import android.app.admin.DevicePolicyManager; |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Alison Cichowlas | f280653 | 2018-03-06 19:08:18 -0500 | [diff] [blame] | 41 | import android.content.ComponentName; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 42 | import android.content.Context; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 43 | import android.content.Intent; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 44 | import android.content.res.Configuration; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 45 | import android.content.res.Resources; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 47 | import android.graphics.Color; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 48 | import android.graphics.PixelFormat; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 49 | import android.graphics.PointF; |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 50 | import android.graphics.Rect; |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 51 | import android.media.MediaActionSound; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 52 | import android.net.Uri; |
| 53 | import android.os.AsyncTask; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 54 | import android.os.Handler; |
| 55 | import android.os.Looper; |
| 56 | import android.os.Message; |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 57 | import android.os.PowerManager; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 58 | import android.os.SystemClock; |
phweiss | ec3d9ca | 2017-02-09 16:57:39 +0100 | [diff] [blame] | 59 | import android.os.UserHandle; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 60 | import android.provider.DeviceConfig; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 61 | import android.util.DisplayMetrics; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 62 | import android.util.Log; |
Dan Sandler | 150651b | 2017-06-14 10:43:07 -0400 | [diff] [blame] | 63 | import android.util.Slog; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 64 | import android.view.Display; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 65 | import android.view.LayoutInflater; |
| 66 | import android.view.MotionEvent; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 67 | import android.view.SurfaceControl; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 68 | import android.view.View; |
| 69 | import android.view.ViewGroup; |
| 70 | import android.view.WindowManager; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 71 | import android.view.animation.Interpolator; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 72 | import android.widget.ImageView; |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 73 | import android.widget.LinearLayout; |
| 74 | import android.widget.TextView; |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 75 | import android.widget.Toast; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 76 | |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 77 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 78 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 79 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 80 | import com.android.systemui.SystemUI; |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 81 | import com.android.systemui.SystemUIFactory; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 82 | import com.android.systemui.dagger.qualifiers.MainResources; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 83 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
| 84 | import com.android.systemui.statusbar.phone.StatusBar; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 85 | import com.android.systemui.util.NotificationChannels; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 86 | |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 87 | import java.util.Collections; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 88 | import java.util.List; |
Garfield Tan | a2242397 | 2019-11-04 17:40:26 -0800 | [diff] [blame] | 89 | import java.util.Optional; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 90 | import java.util.concurrent.CompletableFuture; |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 91 | import java.util.concurrent.ExecutionException; |
| 92 | import java.util.concurrent.TimeUnit; |
| 93 | import java.util.concurrent.TimeoutException; |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 94 | import java.util.function.Consumer; |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 95 | |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 96 | import javax.inject.Inject; |
| 97 | import javax.inject.Singleton; |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 98 | |
Garfield Tan | a2242397 | 2019-11-04 17:40:26 -0800 | [diff] [blame] | 99 | import dagger.Lazy; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 102 | * Class for handling device screen shots |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 103 | */ |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 104 | @Singleton |
| 105 | public class GlobalScreenshot { |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 106 | |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 107 | /** |
| 108 | * POD used in the AsyncTask which saves an image in the background. |
| 109 | */ |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 110 | static class SaveImageInBackgroundData { |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 111 | public Context context; |
| 112 | public Bitmap image; |
| 113 | public Uri imageUri; |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 114 | public Consumer<Uri> finisher; |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 115 | public GlobalScreenshot.ActionsReadyListener mActionsReadyListener; |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 116 | public int iconSize; |
| 117 | public int previewWidth; |
| 118 | public int previewheight; |
| 119 | public int errorMsgResId; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 120 | |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 121 | void clearImage() { |
| 122 | image = null; |
| 123 | imageUri = null; |
| 124 | iconSize = 0; |
| 125 | } |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 126 | |
Sunny Goyal | af7cddd | 2019-11-05 17:49:17 -0800 | [diff] [blame] | 127 | void clearContext() { |
| 128 | context = null; |
| 129 | } |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 132 | abstract static class ActionsReadyListener { |
| 133 | abstract void onActionsReady(PendingIntent shareAction, PendingIntent editAction); |
| 134 | } |
| 135 | |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 136 | // These strings are used for communicating the action invoked to |
| 137 | // ScreenshotNotificationSmartActionsProvider. |
| 138 | static final String EXTRA_ACTION_TYPE = "android:screenshot_action_type"; |
| 139 | static final String EXTRA_ID = "android:screenshot_id"; |
| 140 | static final String ACTION_TYPE_DELETE = "Delete"; |
| 141 | static final String ACTION_TYPE_SHARE = "Share"; |
| 142 | static final String ACTION_TYPE_EDIT = "Edit"; |
| 143 | static final String EXTRA_SMART_ACTIONS_ENABLED = "android:smart_actions_enabled"; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 144 | static final String EXTRA_ACTION_INTENT = "android:screenshot_action_intent"; |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 145 | |
| 146 | static final String SCREENSHOT_URI_ID = "android:screenshot_uri_id"; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 147 | static final String EXTRA_CANCEL_NOTIFICATION = "android:screenshot_cancel_notification"; |
| 148 | static final String EXTRA_DISALLOW_ENTER_PIP = "android:screenshot_disallow_enter_pip"; |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 149 | |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 150 | private static final String TAG = "GlobalScreenshot"; |
| 151 | |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 152 | private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130; |
| 153 | private static final int SCREENSHOT_DROP_IN_DURATION = 430; |
| 154 | private static final int SCREENSHOT_DROP_OUT_DELAY = 500; |
| 155 | private static final int SCREENSHOT_DROP_OUT_DURATION = 430; |
| 156 | private static final int SCREENSHOT_DROP_OUT_SCALE_DURATION = 370; |
| 157 | private static final int SCREENSHOT_FAST_DROP_OUT_DURATION = 320; |
| 158 | private static final float BACKGROUND_ALPHA = 0.5f; |
| 159 | private static final float SCREENSHOT_SCALE = 1f; |
| 160 | private static final float SCREENSHOT_DROP_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.725f; |
| 161 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.45f; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 162 | private static final float SCREENSHOT_CORNER_MIN_SCALE = SCREENSHOT_SCALE * 0.2f; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 163 | private static final float SCREENSHOT_FAST_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.6f; |
| 164 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET = 0f; |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 165 | private static final float SCREENSHOT_CORNER_MIN_SCALE_OFFSET = .1f; |
| 166 | private static final long SCREENSHOT_CORNER_TIMEOUT_MILLIS = 8000; |
| 167 | private static final int MESSAGE_CORNER_TIMEOUT = 2; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 168 | private final int mPreviewWidth; |
| 169 | private final int mPreviewHeight; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 170 | |
| 171 | private Context mContext; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 172 | private WindowManager mWindowManager; |
| 173 | private WindowManager.LayoutParams mWindowLayoutParams; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 174 | private NotificationManager mNotificationManager; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 175 | private Display mDisplay; |
| 176 | private DisplayMetrics mDisplayMetrics; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 177 | |
| 178 | private Bitmap mScreenBitmap; |
| 179 | private View mScreenshotLayout; |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 180 | private ScreenshotSelectorView mScreenshotSelectorView; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 181 | private ImageView mBackgroundView; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 182 | private ImageView mScreenshotView; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 183 | private ImageView mScreenshotFlash; |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 184 | private LinearLayout mActionsView; |
| 185 | private TextView mShareAction; |
| 186 | private TextView mEditAction; |
| 187 | private TextView mScrollAction; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 188 | |
| 189 | private AnimatorSet mScreenshotAnimation; |
| 190 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 191 | private int mNotificationIconSize; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 192 | private float mBgPadding; |
| 193 | private float mBgPaddingScale; |
| 194 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 195 | private AsyncTask<Void, Void, Void> mSaveInBgTask; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 196 | |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 197 | private MediaActionSound mCameraSound; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 198 | |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 199 | private final Handler mScreenshotHandler = new Handler(Looper.getMainLooper()) { |
| 200 | @Override |
| 201 | public void handleMessage(Message msg) { |
| 202 | switch (msg.what) { |
| 203 | case MESSAGE_CORNER_TIMEOUT: |
| 204 | GlobalScreenshot.this.clearScreenshot(); |
| 205 | break; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | }; |
| 211 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 212 | /** |
| 213 | * @param context everything needs a context :( |
| 214 | */ |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 215 | @Inject |
| 216 | public GlobalScreenshot(Context context, @MainResources Resources resources, |
| 217 | LayoutInflater layoutInflater) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 218 | mContext = context; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 219 | |
| 220 | // Inflate the screenshot layout |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 221 | mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null); |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 222 | mBackgroundView = mScreenshotLayout.findViewById(R.id.global_screenshot_background); |
| 223 | mScreenshotView = mScreenshotLayout.findViewById(R.id.global_screenshot); |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 224 | mActionsView = mScreenshotLayout.findViewById(R.id.global_screenshot_actions); |
| 225 | |
| 226 | mShareAction = (TextView) layoutInflater.inflate( |
| 227 | R.layout.global_screenshot_action_chip, mActionsView, false); |
| 228 | mEditAction = (TextView) layoutInflater.inflate( |
| 229 | R.layout.global_screenshot_action_chip, mActionsView, false); |
| 230 | mScrollAction = (TextView) layoutInflater.inflate( |
| 231 | R.layout.global_screenshot_action_chip, mActionsView, false); |
| 232 | |
| 233 | mShareAction.setText(com.android.internal.R.string.share); |
| 234 | mEditAction.setText(com.android.internal.R.string.screenshot_edit); |
| 235 | mScrollAction.setText("Scroll"); // TODO (mkephart): Add to resources and translate |
| 236 | |
| 237 | mActionsView.addView(mShareAction); |
| 238 | mActionsView.addView(mEditAction); |
| 239 | mActionsView.addView(mScrollAction); |
| 240 | |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 241 | mScreenshotFlash = mScreenshotLayout.findViewById(R.id.global_screenshot_flash); |
| 242 | mScreenshotSelectorView = mScreenshotLayout.findViewById(R.id.global_screenshot_selector); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 243 | mScreenshotLayout.setFocusable(true); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 244 | mScreenshotSelectorView.setFocusable(true); |
| 245 | mScreenshotSelectorView.setFocusableInTouchMode(true); |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 246 | mScreenshotLayout.setOnTouchListener((v, event) -> { |
| 247 | // Intercept and ignore all touch events |
| 248 | return true; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 249 | }); |
| 250 | |
| 251 | // Setup the window that we are going to use |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 252 | mWindowLayoutParams = new WindowManager.LayoutParams( |
| 253 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 254 | WindowManager.LayoutParams.TYPE_SCREENSHOT, |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 255 | WindowManager.LayoutParams.FLAG_FULLSCREEN |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 256 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 257 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 258 | PixelFormat.TRANSLUCENT); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 259 | mWindowLayoutParams.setTitle("ScreenshotAnimation"); |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 260 | mWindowLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 261 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 262 | mNotificationManager = |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 263 | (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 264 | mDisplay = mWindowManager.getDefaultDisplay(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 265 | mDisplayMetrics = new DisplayMetrics(); |
| 266 | mDisplay.getRealMetrics(mDisplayMetrics); |
| 267 | |
| 268 | // Get the various target sizes |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 269 | mNotificationIconSize = |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 270 | resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 271 | |
| 272 | // Scale has to account for both sides of the bg |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 273 | mBgPadding = (float) resources.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding); |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 274 | mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 275 | |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 276 | // determine the optimal preview size |
| 277 | int panelWidth = 0; |
| 278 | try { |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 279 | panelWidth = resources.getDimensionPixelSize(R.dimen.notification_panel_width); |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 280 | } catch (Resources.NotFoundException e) { |
Dan Sandler | deb0d11 | 2014-08-07 09:21:06 -0400 | [diff] [blame] | 281 | } |
| 282 | if (panelWidth <= 0) { |
| 283 | // includes notification_panel_width==match_parent (-1) |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 284 | panelWidth = mDisplayMetrics.widthPixels; |
| 285 | } |
| 286 | mPreviewWidth = panelWidth; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 287 | mPreviewHeight = resources.getDimensionPixelSize(R.dimen.notification_max_height); |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 288 | |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 289 | // Setup the Camera shutter sound |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 290 | mCameraSound = new MediaActionSound(); |
| 291 | mCameraSound.load(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Creates a new worker thread and saves the screenshot to the media store. |
| 296 | */ |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 297 | private void saveScreenshotInWorkerThread( |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 298 | Consumer<Uri> finisher, @Nullable ActionsReadyListener actionsReadyListener) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 299 | SaveImageInBackgroundData data = new SaveImageInBackgroundData(); |
| 300 | data.context = mContext; |
| 301 | data.image = mScreenBitmap; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 302 | data.iconSize = mNotificationIconSize; |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 303 | data.finisher = finisher; |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 304 | data.mActionsReadyListener = actionsReadyListener; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 305 | data.previewWidth = mPreviewWidth; |
| 306 | data.previewheight = mPreviewHeight; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 307 | if (mSaveInBgTask != null) { |
| 308 | mSaveInBgTask.cancel(false); |
| 309 | } |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 310 | mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager) |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 311 | .execute(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 314 | private void saveScreenshotInWorkerThread(Consumer<Uri> finisher) { |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 315 | saveScreenshotInWorkerThread(finisher, null); |
| 316 | } |
| 317 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 318 | /** |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 319 | * Takes a screenshot of the current display and shows an animation. |
| 320 | */ |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 321 | private void takeScreenshot(Consumer<Uri> finisher, boolean statusBarVisible, |
| 322 | boolean navBarVisible, Rect crop) { |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 323 | int rot = mDisplay.getRotation(); |
| 324 | int width = crop.width(); |
| 325 | int height = crop.height(); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 326 | |
| 327 | // Take the screenshot |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 328 | mScreenBitmap = SurfaceControl.screenshot(crop, width, height, rot); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 329 | if (mScreenBitmap == null) { |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 330 | notifyScreenshotError(mContext, mNotificationManager, |
| 331 | R.string.screenshot_failed_to_capture_text); |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 332 | finisher.accept(null); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 336 | // Optimizations |
| 337 | mScreenBitmap.setHasAlpha(false); |
| 338 | mScreenBitmap.prepareToDraw(); |
| 339 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 340 | // Start the post-screenshot animation |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 341 | startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, |
| 342 | statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 345 | void takeScreenshot(Consumer<Uri> finisher, boolean statusBarVisible, boolean navBarVisible) { |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 346 | mDisplay.getRealMetrics(mDisplayMetrics); |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 347 | takeScreenshot(finisher, statusBarVisible, navBarVisible, |
| 348 | new Rect(0, 0, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels)); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Displays a screenshot selector |
| 353 | */ |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 354 | void takeScreenshotPartial(final Consumer<Uri> finisher, final boolean statusBarVisible, |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 355 | final boolean navBarVisible) { |
| 356 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); |
| 357 | mScreenshotSelectorView.setOnTouchListener(new View.OnTouchListener() { |
| 358 | @Override |
| 359 | public boolean onTouch(View v, MotionEvent event) { |
| 360 | ScreenshotSelectorView view = (ScreenshotSelectorView) v; |
| 361 | switch (event.getAction()) { |
| 362 | case MotionEvent.ACTION_DOWN: |
| 363 | view.startSelection((int) event.getX(), (int) event.getY()); |
| 364 | return true; |
| 365 | case MotionEvent.ACTION_MOVE: |
| 366 | view.updateSelection((int) event.getX(), (int) event.getY()); |
| 367 | return true; |
| 368 | case MotionEvent.ACTION_UP: |
| 369 | view.setVisibility(View.GONE); |
| 370 | mWindowManager.removeView(mScreenshotLayout); |
| 371 | final Rect rect = view.getSelectionRect(); |
| 372 | if (rect != null) { |
| 373 | if (rect.width() != 0 && rect.height() != 0) { |
| 374 | // Need mScreenshotLayout to handle it after the view disappears |
| 375 | mScreenshotLayout.post(new Runnable() { |
| 376 | public void run() { |
| 377 | takeScreenshot(finisher, statusBarVisible, navBarVisible, |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 378 | rect); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 379 | } |
| 380 | }); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | view.stopSelection(); |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | return false; |
| 389 | } |
| 390 | }); |
| 391 | mScreenshotLayout.post(new Runnable() { |
| 392 | @Override |
| 393 | public void run() { |
| 394 | mScreenshotSelectorView.setVisibility(View.VISIBLE); |
| 395 | mScreenshotSelectorView.requestFocus(); |
| 396 | } |
| 397 | }); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Cancels screenshot request |
| 402 | */ |
| 403 | void stopScreenshot() { |
| 404 | // If the selector layer still presents on screen, we remove it and resets its state. |
| 405 | if (mScreenshotSelectorView.getSelectionRect() != null) { |
| 406 | mWindowManager.removeView(mScreenshotLayout); |
| 407 | mScreenshotSelectorView.stopSelection(); |
| 408 | } |
| 409 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 410 | |
| 411 | /** |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 412 | * Clears current screenshot |
| 413 | */ |
| 414 | private void clearScreenshot() { |
| 415 | if (mScreenshotLayout.isAttachedToWindow()) { |
| 416 | mWindowManager.removeView(mScreenshotLayout); |
| 417 | } |
| 418 | |
| 419 | // Clear any references to the bitmap |
| 420 | mScreenBitmap = null; |
| 421 | mScreenshotView.setImageBitmap(null); |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 422 | mActionsView.setVisibility(View.GONE); |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 423 | mBackgroundView.setVisibility(View.GONE); |
| 424 | mScreenshotView.setVisibility(View.GONE); |
| 425 | mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 426 | } |
| 427 | |
| 428 | /** |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 429 | * Starts the animation after taking the screenshot |
| 430 | */ |
Aran Ink | eb565fc | 2019-11-15 15:52:31 -0500 | [diff] [blame] | 431 | private void startAnimation(final Consumer<Uri> finisher, int w, int h, |
| 432 | boolean statusBarVisible, boolean navBarVisible) { |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 433 | // If power save is on, show a toast so there is some visual indication that a screenshot |
| 434 | // has been taken. |
| 435 | PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 436 | if (powerManager.isPowerSaveMode()) { |
| 437 | Toast.makeText(mContext, R.string.screenshot_saved_title, Toast.LENGTH_SHORT).show(); |
| 438 | } |
| 439 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 440 | // Add the view for the animation |
| 441 | mScreenshotView.setImageBitmap(mScreenBitmap); |
| 442 | mScreenshotLayout.requestFocus(); |
| 443 | |
| 444 | // Setup the animation with the screenshot just taken |
| 445 | if (mScreenshotAnimation != null) { |
Dan Sandler | d9cd20b | 2015-11-13 15:01:44 -0500 | [diff] [blame] | 446 | if (mScreenshotAnimation.isStarted()) { |
| 447 | mScreenshotAnimation.end(); |
| 448 | } |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 449 | mScreenshotAnimation.removeAllListeners(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 452 | boolean useCornerFlow = |
| 453 | DeviceConfig.getBoolean(NAMESPACE_SYSTEMUI, SCREENSHOT_CORNER_FLOW, false); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 454 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 455 | ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation(); |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 456 | ValueAnimator screenshotFadeOutAnim = useCornerFlow |
| 457 | ? createScreenshotToCornerAnimation(w, h) |
| 458 | : createScreenshotDropOutAnimation(w, h, statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 459 | mScreenshotAnimation = new AnimatorSet(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 460 | mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 461 | mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { |
| 462 | @Override |
| 463 | public void onAnimationEnd(Animator animation) { |
| 464 | // Save the screenshot once we have a bit of time now |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 465 | if (!useCornerFlow) { |
| 466 | saveScreenshotInWorkerThread(finisher); |
| 467 | clearScreenshot(); |
| 468 | } else { |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 469 | saveScreenshotInWorkerThread(finisher, new ActionsReadyListener() { |
| 470 | @Override |
| 471 | void onActionsReady(PendingIntent shareAction, PendingIntent editAction) { |
| 472 | mScreenshotHandler.post(() -> |
| 473 | createScreenshotActionsShadeAnimation(shareAction, editAction) |
| 474 | .start()); |
| 475 | } |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 476 | }); |
| 477 | mScreenshotHandler.sendMessageDelayed( |
| 478 | mScreenshotHandler.obtainMessage(MESSAGE_CORNER_TIMEOUT), |
| 479 | SCREENSHOT_CORNER_TIMEOUT_MILLIS); |
| 480 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 481 | } |
| 482 | }); |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 483 | mScreenshotHandler.post(() -> { |
| 484 | // Play the shutter sound to notify that we've taken a screenshot |
| 485 | mCameraSound.play(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 486 | |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 487 | mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 488 | mScreenshotView.buildLayer(); |
| 489 | mScreenshotAnimation.start(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 490 | }); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 491 | } |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 492 | |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 493 | private ValueAnimator createScreenshotDropInAnimation() { |
| 494 | final float flashPeakDurationPct = ((float) (SCREENSHOT_FLASH_TO_PEAK_DURATION) |
| 495 | / SCREENSHOT_DROP_IN_DURATION); |
| 496 | final float flashDurationPct = 2f * flashPeakDurationPct; |
| 497 | final Interpolator flashAlphaInterpolator = new Interpolator() { |
| 498 | @Override |
| 499 | public float getInterpolation(float x) { |
| 500 | // Flash the flash view in and out quickly |
| 501 | if (x <= flashDurationPct) { |
| 502 | return (float) Math.sin(Math.PI * (x / flashDurationPct)); |
| 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | }; |
| 507 | final Interpolator scaleInterpolator = new Interpolator() { |
| 508 | @Override |
| 509 | public float getInterpolation(float x) { |
| 510 | // We start scaling when the flash is at it's peak |
| 511 | if (x < flashPeakDurationPct) { |
| 512 | return 0; |
| 513 | } |
| 514 | return (x - flashDurationPct) / (1f - flashDurationPct); |
| 515 | } |
| 516 | }; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 517 | |
| 518 | Resources r = mContext.getResources(); |
| 519 | if ((r.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) |
| 520 | == Configuration.UI_MODE_NIGHT_YES) { |
| 521 | mScreenshotView.getBackground().setTint(Color.BLACK); |
| 522 | } else { |
| 523 | mScreenshotView.getBackground().setTintList(null); |
| 524 | } |
| 525 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 526 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 527 | anim.setDuration(SCREENSHOT_DROP_IN_DURATION); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 528 | anim.addListener(new AnimatorListenerAdapter() { |
| 529 | @Override |
| 530 | public void onAnimationStart(Animator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 531 | mBackgroundView.setAlpha(0f); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 532 | mBackgroundView.setVisibility(View.VISIBLE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 533 | mScreenshotView.setAlpha(0f); |
| 534 | mScreenshotView.setTranslationX(0f); |
| 535 | mScreenshotView.setTranslationY(0f); |
| 536 | mScreenshotView.setScaleX(SCREENSHOT_SCALE + mBgPaddingScale); |
| 537 | mScreenshotView.setScaleY(SCREENSHOT_SCALE + mBgPaddingScale); |
| 538 | mScreenshotView.setVisibility(View.VISIBLE); |
| 539 | mScreenshotFlash.setAlpha(0f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 540 | mScreenshotFlash.setVisibility(View.VISIBLE); |
| 541 | } |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 542 | |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 543 | @Override |
| 544 | public void onAnimationEnd(android.animation.Animator animation) { |
| 545 | mScreenshotFlash.setVisibility(View.GONE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 546 | } |
| 547 | }); |
| 548 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 549 | @Override |
| 550 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 551 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 552 | float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale) |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 553 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 554 | * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 555 | mBackgroundView.setAlpha(scaleInterpolator.getInterpolation(t) * BACKGROUND_ALPHA); |
| 556 | mScreenshotView.setAlpha(t); |
| 557 | mScreenshotView.setScaleX(scaleT); |
| 558 | mScreenshotView.setScaleY(scaleT); |
| 559 | mScreenshotFlash.setAlpha(flashAlphaInterpolator.getInterpolation(t)); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 560 | } |
| 561 | }); |
| 562 | return anim; |
| 563 | } |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 564 | |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 565 | private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 566 | boolean navBarVisible) { |
| 567 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 568 | anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 569 | anim.addListener(new AnimatorListenerAdapter() { |
| 570 | @Override |
| 571 | public void onAnimationEnd(Animator animation) { |
| 572 | mBackgroundView.setVisibility(View.GONE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 573 | mScreenshotView.setVisibility(View.GONE); |
| 574 | mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 575 | } |
| 576 | }); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 577 | |
| 578 | if (!statusBarVisible || !navBarVisible) { |
| 579 | // There is no status bar/nav bar, so just fade the screenshot away in place |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 580 | anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 581 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 582 | @Override |
| 583 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 584 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 585 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 586 | - t * (SCREENSHOT_DROP_IN_MIN_SCALE |
| 587 | - SCREENSHOT_FAST_DROP_OUT_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 588 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 589 | mScreenshotView.setAlpha(1f - t); |
| 590 | mScreenshotView.setScaleX(scaleT); |
| 591 | mScreenshotView.setScaleY(scaleT); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 592 | } |
| 593 | }); |
| 594 | } else { |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 595 | // In the case where there is a status bar, animate to the origin of the bar (top-left) |
| 596 | final float scaleDurationPct = (float) SCREENSHOT_DROP_OUT_SCALE_DURATION |
| 597 | / SCREENSHOT_DROP_OUT_DURATION; |
| 598 | final Interpolator scaleInterpolator = new Interpolator() { |
| 599 | @Override |
| 600 | public float getInterpolation(float x) { |
| 601 | if (x < scaleDurationPct) { |
| 602 | // Decelerate, and scale the input accordingly |
| 603 | return (float) (1f - Math.pow(1f - (x / scaleDurationPct), 2f)); |
| 604 | } |
| 605 | return 1f; |
| 606 | } |
| 607 | }; |
| 608 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 609 | // Determine the bounds of how to scale |
| 610 | float halfScreenWidth = (w - 2f * mBgPadding) / 2f; |
| 611 | float halfScreenHeight = (h - 2f * mBgPadding) / 2f; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 612 | final float offsetPct = SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET; |
| 613 | final PointF finalPos = new PointF( |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 614 | -halfScreenWidth |
| 615 | + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenWidth, |
| 616 | -halfScreenHeight |
| 617 | + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenHeight); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 618 | |
| 619 | // Animate the screenshot to the status bar |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 620 | anim.setDuration(SCREENSHOT_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 621 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 622 | @Override |
| 623 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 624 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 625 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 626 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 627 | * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 628 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 629 | mScreenshotView.setAlpha(1f - scaleInterpolator.getInterpolation(t)); |
| 630 | mScreenshotView.setScaleX(scaleT); |
| 631 | mScreenshotView.setScaleY(scaleT); |
| 632 | mScreenshotView.setTranslationX(t * finalPos.x); |
| 633 | mScreenshotView.setTranslationY(t * finalPos.y); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 634 | } |
| 635 | }); |
| 636 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 637 | return anim; |
| 638 | } |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 639 | |
Miranda Kephart | ea2e67a | 2019-10-29 11:52:56 -0400 | [diff] [blame] | 640 | private ValueAnimator createScreenshotToCornerAnimation(int w, int h) { |
| 641 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
| 642 | anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY); |
| 643 | |
| 644 | final float scaleDurationPct = |
| 645 | (float) SCREENSHOT_DROP_OUT_SCALE_DURATION / SCREENSHOT_DROP_OUT_DURATION; |
| 646 | final Interpolator scaleInterpolator = new Interpolator() { |
| 647 | @Override |
| 648 | public float getInterpolation(float x) { |
| 649 | if (x < scaleDurationPct) { |
| 650 | // Decelerate, and scale the input accordingly |
| 651 | return (float) (1f - Math.pow(1f - (x / scaleDurationPct), 2f)); |
| 652 | } |
| 653 | return 1f; |
| 654 | } |
| 655 | }; |
| 656 | |
| 657 | // Determine the bounds of how to scale |
| 658 | float halfScreenWidth = (w - 2f * mBgPadding) / 2f; |
| 659 | float halfScreenHeight = (h - 2f * mBgPadding) / 2f; |
| 660 | final float offsetPct = SCREENSHOT_CORNER_MIN_SCALE_OFFSET; |
| 661 | final PointF finalPos = new PointF( |
| 662 | -halfScreenWidth + (SCREENSHOT_CORNER_MIN_SCALE + offsetPct) * halfScreenWidth, |
| 663 | halfScreenHeight - (SCREENSHOT_CORNER_MIN_SCALE + offsetPct) * halfScreenHeight); |
| 664 | |
| 665 | // Animate the screenshot to the bottom left corner |
| 666 | anim.setDuration(SCREENSHOT_DROP_OUT_DURATION); |
| 667 | anim.addUpdateListener(animation -> { |
| 668 | float t = (Float) animation.getAnimatedValue(); |
| 669 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
| 670 | - scaleInterpolator.getInterpolation(t) |
| 671 | * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_CORNER_MIN_SCALE); |
| 672 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 673 | mScreenshotView.setScaleX(scaleT); |
| 674 | mScreenshotView.setScaleY(scaleT); |
| 675 | mScreenshotView.setTranslationX(t * finalPos.x); |
| 676 | mScreenshotView.setTranslationY(t * finalPos.y); |
| 677 | }); |
| 678 | return anim; |
| 679 | } |
| 680 | |
Miranda Kephart | 0148b28 | 2019-10-31 15:36:21 -0400 | [diff] [blame^] | 681 | private ValueAnimator createScreenshotActionsShadeAnimation( |
| 682 | PendingIntent shareAction, PendingIntent editAction) { |
| 683 | ValueAnimator animator = ValueAnimator.ofFloat(0, 1); |
| 684 | mActionsView.setY(mDisplayMetrics.heightPixels); |
| 685 | mActionsView.setVisibility(VISIBLE); |
| 686 | mActionsView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| 687 | float actionsViewHeight = mActionsView.getMeasuredHeight(); |
| 688 | float screenshotStartHeight = mScreenshotView.getTranslationY(); |
| 689 | |
| 690 | animator.addUpdateListener(animation -> { |
| 691 | float t = animation.getAnimatedFraction(); |
| 692 | mScreenshotView.setTranslationY(screenshotStartHeight - actionsViewHeight * t); |
| 693 | mActionsView.setY(mDisplayMetrics.heightPixels - actionsViewHeight * t); |
| 694 | }); |
| 695 | animator.addListener(new AnimatorListenerAdapter() { |
| 696 | @Override |
| 697 | public void onAnimationEnd(Animator animation) { |
| 698 | super.onAnimationEnd(animation); |
| 699 | mScreenshotView.requestFocus(); |
| 700 | mShareAction.setOnClickListener(v -> { |
| 701 | try { |
| 702 | shareAction.send(); |
| 703 | clearScreenshot(); |
| 704 | } catch (PendingIntent.CanceledException e) { |
| 705 | Log.e(TAG, "Share intent cancelled", e); |
| 706 | } |
| 707 | }); |
| 708 | mEditAction.setOnClickListener(v -> { |
| 709 | try { |
| 710 | editAction.send(); |
| 711 | clearScreenshot(); |
| 712 | } catch (PendingIntent.CanceledException e) { |
| 713 | Log.e(TAG, "Edit intent cancelled", e); |
| 714 | } |
| 715 | }); |
| 716 | Toast scrollNotImplemented = Toast.makeText( |
| 717 | mContext, "Not implemented", Toast.LENGTH_SHORT); |
| 718 | mScrollAction.setOnClickListener(v -> scrollNotImplemented.show()); |
| 719 | } |
| 720 | }); |
| 721 | return animator; |
| 722 | } |
| 723 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 724 | static void notifyScreenshotError(Context context, NotificationManager nManager, int msgResId) { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 725 | Resources r = context.getResources(); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 726 | String errorMsg = r.getString(msgResId); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 727 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 728 | // Repurpose the existing notification to notify the user of the error |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 729 | Notification.Builder b = new Notification.Builder(context, NotificationChannels.ALERTS) |
Miranda Kephart | ca6c5eb | 2019-11-14 12:44:11 -0500 | [diff] [blame] | 730 | .setTicker(r.getString(R.string.screenshot_failed_title)) |
| 731 | .setContentTitle(r.getString(R.string.screenshot_failed_title)) |
| 732 | .setContentText(errorMsg) |
| 733 | .setSmallIcon(R.drawable.stat_notify_image_error) |
| 734 | .setWhen(System.currentTimeMillis()) |
| 735 | .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen |
| 736 | .setCategory(Notification.CATEGORY_ERROR) |
| 737 | .setAutoCancel(true) |
| 738 | .setColor(context.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 739 | com.android.internal.R.color.system_notification_accent_color)); |
phweiss | ec3d9ca | 2017-02-09 16:57:39 +0100 | [diff] [blame] | 740 | final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( |
| 741 | Context.DEVICE_POLICY_SERVICE); |
| 742 | final Intent intent = dpm.createAdminSupportIntent( |
| 743 | DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 744 | if (intent != null) { |
| 745 | final PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 746 | context, 0, intent, 0, null, UserHandle.CURRENT); |
| 747 | b.setContentIntent(pendingIntent); |
| 748 | } |
| 749 | |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 750 | SystemUI.overrideNotificationAppName(context, b, true); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 751 | |
| 752 | Notification n = new Notification.BigTextStyle(b) |
| 753 | .bigText(errorMsg) |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 754 | .build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 755 | nManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT, n); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 756 | } |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 757 | |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 758 | @VisibleForTesting |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 759 | static CompletableFuture<List<Notification.Action>> getSmartActionsFuture(String screenshotId, |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 760 | Bitmap image, ScreenshotNotificationSmartActionsProvider smartActionsProvider, |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 761 | boolean smartActionsEnabled, boolean isManagedProfile) { |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 762 | if (!smartActionsEnabled) { |
| 763 | Slog.i(TAG, "Screenshot Intelligence not enabled, returning empty list."); |
| 764 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 765 | } |
| 766 | if (image.getConfig() != Bitmap.Config.HARDWARE) { |
| 767 | Slog.w(TAG, String.format( |
| 768 | "Bitmap expected: Hardware, Bitmap found: %s. Returning empty list.", |
| 769 | image.getConfig())); |
| 770 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 771 | } |
| 772 | |
| 773 | Slog.d(TAG, "Screenshot from a managed profile: " + isManagedProfile); |
| 774 | CompletableFuture<List<Notification.Action>> smartActionsFuture; |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 775 | long startTimeMs = SystemClock.uptimeMillis(); |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 776 | try { |
| 777 | ActivityManager.RunningTaskInfo runningTask = |
| 778 | ActivityManagerWrapper.getInstance().getRunningTask(); |
| 779 | ComponentName componentName = |
| 780 | (runningTask != null && runningTask.topActivity != null) |
| 781 | ? runningTask.topActivity |
| 782 | : new ComponentName("", ""); |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 783 | smartActionsFuture = smartActionsProvider.getActions(screenshotId, image, |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 784 | componentName, |
| 785 | isManagedProfile); |
| 786 | } catch (Throwable e) { |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 787 | long waitTimeMs = SystemClock.uptimeMillis() - startTimeMs; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 788 | smartActionsFuture = CompletableFuture.completedFuture(Collections.emptyList()); |
| 789 | Slog.e(TAG, "Failed to get future for screenshot notification smart actions.", e); |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 790 | notifyScreenshotOp(screenshotId, smartActionsProvider, |
| 791 | ScreenshotNotificationSmartActionsProvider.ScreenshotOp.REQUEST_SMART_ACTIONS, |
| 792 | ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus.ERROR, |
| 793 | waitTimeMs); |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 794 | } |
| 795 | return smartActionsFuture; |
| 796 | } |
| 797 | |
| 798 | @VisibleForTesting |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 799 | static List<Notification.Action> getSmartActions(String screenshotId, |
| 800 | CompletableFuture<List<Notification.Action>> smartActionsFuture, int timeoutMs, |
| 801 | ScreenshotNotificationSmartActionsProvider smartActionsProvider) { |
| 802 | long startTimeMs = SystemClock.uptimeMillis(); |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 803 | try { |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 804 | List<Notification.Action> actions = smartActionsFuture.get(timeoutMs, |
| 805 | TimeUnit.MILLISECONDS); |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 806 | long waitTimeMs = SystemClock.uptimeMillis() - startTimeMs; |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 807 | Slog.d(TAG, String.format("Wait time for smart actions: %d ms", |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 808 | waitTimeMs)); |
| 809 | notifyScreenshotOp(screenshotId, smartActionsProvider, |
| 810 | ScreenshotNotificationSmartActionsProvider.ScreenshotOp.WAIT_FOR_SMART_ACTIONS, |
| 811 | ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus.SUCCESS, |
| 812 | waitTimeMs); |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 813 | return actions; |
| 814 | } catch (Throwable e) { |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 815 | long waitTimeMs = SystemClock.uptimeMillis() - startTimeMs; |
| 816 | Slog.d(TAG, "Failed to obtain screenshot notification smart actions.", e); |
| 817 | ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus status = |
| 818 | (e instanceof TimeoutException) |
| 819 | ? ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus.TIMEOUT |
| 820 | : ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus.ERROR; |
| 821 | notifyScreenshotOp(screenshotId, smartActionsProvider, |
| 822 | ScreenshotNotificationSmartActionsProvider.ScreenshotOp.WAIT_FOR_SMART_ACTIONS, |
| 823 | status, waitTimeMs); |
Satakshi | d2010f2 | 2019-10-29 10:57:43 -0700 | [diff] [blame] | 824 | return Collections.emptyList(); |
| 825 | } |
| 826 | } |
| 827 | |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 828 | static void notifyScreenshotOp(String screenshotId, |
| 829 | ScreenshotNotificationSmartActionsProvider smartActionsProvider, |
| 830 | ScreenshotNotificationSmartActionsProvider.ScreenshotOp op, |
| 831 | ScreenshotNotificationSmartActionsProvider.ScreenshotOpStatus status, long durationMs) { |
| 832 | try { |
| 833 | smartActionsProvider.notifyOp(screenshotId, op, status, durationMs); |
| 834 | } catch (Throwable e) { |
| 835 | Slog.e(TAG, "Error in notifyScreenshotOp: ", e); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static void notifyScreenshotAction(Context context, String screenshotId, String action, |
| 840 | boolean isSmartAction) { |
| 841 | try { |
| 842 | ScreenshotNotificationSmartActionsProvider provider = |
| 843 | SystemUIFactory.getInstance().createScreenshotNotificationSmartActionsProvider( |
| 844 | context, THREAD_POOL_EXECUTOR, new Handler()); |
| 845 | provider.notifyAction(screenshotId, action, isSmartAction); |
| 846 | } catch (Throwable e) { |
| 847 | Slog.e(TAG, "Error in notifyScreenshotAction: ", e); |
| 848 | } |
| 849 | } |
| 850 | |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 851 | /** |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 852 | * Receiver to proxy the share or edit intent, used to clean up the notification and send |
| 853 | * appropriate signals to the system (ie. to dismiss the keyguard if necessary). |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 854 | */ |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 855 | public static class ActionProxyReceiver extends BroadcastReceiver { |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 856 | static final int CLOSE_WINDOWS_TIMEOUT_MILLIS = 3000; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 857 | private final StatusBar mStatusBar; |
| 858 | |
| 859 | @Inject |
Garfield Tan | a2242397 | 2019-11-04 17:40:26 -0800 | [diff] [blame] | 860 | public ActionProxyReceiver(Optional<Lazy<StatusBar>> statusBarLazy) { |
| 861 | Lazy<StatusBar> statusBar = statusBarLazy.orElse(null); |
| 862 | mStatusBar = statusBar != null ? statusBar.get() : null; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 863 | } |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 864 | |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 865 | @Override |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 866 | public void onReceive(Context context, final Intent intent) { |
| 867 | Runnable startActivityRunnable = () -> { |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 868 | try { |
| 869 | ActivityManagerWrapper.getInstance().closeSystemWindows( |
| 870 | SYSTEM_DIALOG_REASON_SCREENSHOT).get( |
| 871 | CLOSE_WINDOWS_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 872 | } catch (TimeoutException | InterruptedException | ExecutionException e) { |
| 873 | Slog.e(TAG, "Unable to share screenshot", e); |
| 874 | return; |
| 875 | } |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 876 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 877 | Intent actionIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT); |
| 878 | if (intent.getBooleanExtra(EXTRA_CANCEL_NOTIFICATION, false)) { |
| 879 | cancelScreenshotNotification(context); |
| 880 | } |
| 881 | ActivityOptions opts = ActivityOptions.makeBasic(); |
| 882 | opts.setDisallowEnterPictureInPictureWhileLaunching( |
| 883 | intent.getBooleanExtra(EXTRA_DISALLOW_ENTER_PIP, false)); |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 884 | context.startActivityAsUser(actionIntent, opts.toBundle(), UserHandle.CURRENT); |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 885 | }; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 886 | |
Garfield Tan | a2242397 | 2019-11-04 17:40:26 -0800 | [diff] [blame] | 887 | if (mStatusBar != null) { |
| 888 | mStatusBar.executeRunnableDismissingKeyguard(startActivityRunnable, null, |
| 889 | true /* dismissShade */, true /* afterKeyguardGone */, |
| 890 | true /* deferred */); |
| 891 | } else { |
| 892 | startActivityRunnable.run(); |
| 893 | } |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 894 | |
| 895 | if (intent.getBooleanExtra(EXTRA_SMART_ACTIONS_ENABLED, false)) { |
| 896 | String actionType = Intent.ACTION_EDIT.equals(intent.getAction()) ? ACTION_TYPE_EDIT |
| 897 | : ACTION_TYPE_SHARE; |
| 898 | notifyScreenshotAction(context, intent.getStringExtra(EXTRA_ID), |
| 899 | actionType, false); |
| 900 | } |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
| 904 | /** |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 905 | * Removes the notification for a screenshot after a share target is chosen. |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 906 | */ |
| 907 | public static class TargetChosenReceiver extends BroadcastReceiver { |
| 908 | @Override |
| 909 | public void onReceive(Context context, Intent intent) { |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 910 | // Clear the notification only after the user has chosen a share action |
| 911 | cancelScreenshotNotification(context); |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 914 | |
| 915 | /** |
| 916 | * Removes the last screenshot. |
| 917 | */ |
| 918 | public static class DeleteScreenshotReceiver extends BroadcastReceiver { |
| 919 | @Override |
| 920 | public void onReceive(Context context, Intent intent) { |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 921 | if (!intent.hasExtra(SCREENSHOT_URI_ID)) { |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 922 | return; |
| 923 | } |
| 924 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 925 | // Clear the notification when the image is deleted |
| 926 | cancelScreenshotNotification(context); |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 927 | |
| 928 | // And delete the image from the media store |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 929 | final Uri uri = Uri.parse(intent.getStringExtra(SCREENSHOT_URI_ID)); |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 930 | new DeleteImageInBackgroundTask(context).execute(uri); |
Satakshi | aaf6953 | 2019-11-07 17:54:24 -0800 | [diff] [blame] | 931 | if (intent.getBooleanExtra(EXTRA_SMART_ACTIONS_ENABLED, false)) { |
| 932 | notifyScreenshotAction(context, intent.getStringExtra(EXTRA_ID), |
| 933 | ACTION_TYPE_DELETE, |
| 934 | false); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Executes the smart action tapped by the user in the notification. |
| 941 | */ |
| 942 | public static class SmartActionsReceiver extends BroadcastReceiver { |
| 943 | @Override |
| 944 | public void onReceive(Context context, Intent intent) { |
| 945 | PendingIntent actionIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT); |
| 946 | ActivityOptions opts = ActivityOptions.makeBasic(); |
| 947 | context.startActivityAsUser(actionIntent.getIntent(), opts.toBundle(), |
| 948 | UserHandle.CURRENT); |
| 949 | |
| 950 | Slog.d(TAG, "Screenshot notification smart action is invoked."); |
| 951 | notifyScreenshotAction(context, intent.getStringExtra(EXTRA_ID), |
| 952 | intent.getStringExtra(EXTRA_ACTION_TYPE), |
| 953 | true); |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 956 | |
| 957 | private static void cancelScreenshotNotification(Context context) { |
| 958 | final NotificationManager nm = |
| 959 | (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); |
| 960 | nm.cancel(SystemMessage.NOTE_GLOBAL_SCREENSHOT); |
| 961 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 962 | } |