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; |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 20 | import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 21 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 22 | import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_ACTION_INTENT; |
| 23 | import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_CANCEL_NOTIFICATION; |
| 24 | import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_DISALLOW_ENTER_PIP; |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 25 | import static com.android.systemui.statusbar.phone.StatusBar.SYSTEM_DIALOG_REASON_SCREENSHOT; |
| 26 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 27 | import android.animation.Animator; |
| 28 | import android.animation.AnimatorListenerAdapter; |
| 29 | import android.animation.AnimatorSet; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 30 | import android.animation.ValueAnimator; |
| 31 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 32 | import android.app.ActivityOptions; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 33 | import android.app.Notification; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 34 | import android.app.Notification.BigPictureStyle; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 35 | import android.app.NotificationManager; |
| 36 | import android.app.PendingIntent; |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 37 | import android.app.admin.DevicePolicyManager; |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
Alison Cichowlas | 09e5ea3 | 2018-09-07 15:52:24 -0400 | [diff] [blame] | 39 | import android.content.ClipData; |
| 40 | import android.content.ClipDescription; |
Alison Cichowlas | f280653 | 2018-03-06 19:08:18 -0500 | [diff] [blame] | 41 | import android.content.ComponentName; |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 42 | import android.content.ContentResolver; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 43 | import android.content.Context; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 44 | import android.content.Intent; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 45 | import android.content.res.Configuration; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 46 | import android.content.res.Resources; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 47 | import android.graphics.Bitmap; |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 48 | import android.graphics.Canvas; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 49 | import android.graphics.Color; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 50 | import android.graphics.ColorMatrix; |
| 51 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 52 | import android.graphics.Matrix; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 53 | import android.graphics.Paint; |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 54 | import android.graphics.Picture; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 55 | import android.graphics.PixelFormat; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 56 | import android.graphics.PointF; |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 57 | import android.graphics.Rect; |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 58 | import android.media.MediaActionSound; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 59 | import android.net.Uri; |
| 60 | import android.os.AsyncTask; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 61 | import android.os.Environment; |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 62 | import android.os.PowerManager; |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 63 | import android.os.Process; |
phweiss | ec3d9ca | 2017-02-09 16:57:39 +0100 | [diff] [blame] | 64 | import android.os.UserHandle; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 65 | import android.provider.MediaStore; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 66 | import android.text.TextUtils; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 67 | import android.util.DisplayMetrics; |
Dan Sandler | 150651b | 2017-06-14 10:43:07 -0400 | [diff] [blame] | 68 | import android.util.Slog; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 69 | import android.view.Display; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 70 | import android.view.LayoutInflater; |
| 71 | import android.view.MotionEvent; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 72 | import android.view.SurfaceControl; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 73 | import android.view.View; |
| 74 | import android.view.ViewGroup; |
| 75 | import android.view.WindowManager; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 76 | import android.view.animation.Interpolator; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 77 | import android.widget.ImageView; |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 78 | import android.widget.Toast; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 79 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 80 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 81 | import com.android.systemui.R; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 82 | import com.android.systemui.SysUiServiceProvider; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 83 | import com.android.systemui.SystemUI; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 84 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
| 85 | import com.android.systemui.statusbar.phone.StatusBar; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 86 | import com.android.systemui.util.NotificationChannels; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 87 | |
Jeff Sharkey | 7bba507 | 2018-11-02 14:37:07 -0600 | [diff] [blame] | 88 | import libcore.io.IoUtils; |
| 89 | |
| 90 | import java.io.IOException; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 91 | import java.io.OutputStream; |
Victoria Lease | e45e1f2 | 2013-09-30 15:33:43 -0700 | [diff] [blame] | 92 | import java.text.DateFormat; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 93 | import java.text.SimpleDateFormat; |
| 94 | import java.util.Date; |
| 95 | |
| 96 | /** |
| 97 | * POD used in the AsyncTask which saves an image in the background. |
| 98 | */ |
| 99 | class SaveImageInBackgroundData { |
| 100 | Context context; |
| 101 | Bitmap image; |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 102 | Uri imageUri; |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 103 | Runnable finisher; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 104 | int iconSize; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 105 | int previewWidth; |
| 106 | int previewheight; |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 107 | int errorMsgResId; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 108 | |
| 109 | void clearImage() { |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 110 | image = null; |
| 111 | imageUri = null; |
| 112 | iconSize = 0; |
| 113 | } |
Winson Chung | 3a5a774 | 2013-04-01 16:04:28 -0700 | [diff] [blame] | 114 | void clearContext() { |
| 115 | context = null; |
| 116 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * An AsyncTask that saves an image to the media store in the background. |
| 121 | */ |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 122 | class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> { |
Dan Sandler | 150651b | 2017-06-14 10:43:07 -0400 | [diff] [blame] | 123 | private static final String TAG = "SaveImageInBackgroundTask"; |
Winson Chung | 5cc9a31 | 2013-02-11 14:58:45 -0800 | [diff] [blame] | 124 | |
Winson Chung | 753e40b | 2011-07-25 17:10:21 -0700 | [diff] [blame] | 125 | private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 126 | private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)"; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 127 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 128 | private final SaveImageInBackgroundData mParams; |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 129 | private final NotificationManager mNotificationManager; |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 130 | private final Notification.Builder mNotificationBuilder, mPublicNotificationBuilder; |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 131 | private final String mImageFileName; |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 132 | private final long mImageTime; |
| 133 | private final BigPictureStyle mNotificationStyle; |
| 134 | private final int mImageWidth; |
| 135 | private final int mImageHeight; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 136 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 137 | SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data, |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 138 | NotificationManager nManager) { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 139 | Resources r = context.getResources(); |
| 140 | |
| 141 | // Prepare all the output metadata |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 142 | mParams = data; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 143 | mImageTime = System.currentTimeMillis(); |
Winson Chung | b9c74ac | 2015-04-03 10:01:40 -0700 | [diff] [blame] | 144 | String imageDate = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(mImageTime)); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 145 | mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate); |
Jeff Sharkey | 149e02c | 2013-01-16 14:38:49 -0800 | [diff] [blame] | 146 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 147 | // Create the large notification icon |
Doris Liu | 5aa011b | 2012-11-06 16:29:46 -0800 | [diff] [blame] | 148 | mImageWidth = data.image.getWidth(); |
| 149 | mImageHeight = data.image.getHeight(); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 150 | int iconSize = data.iconSize; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 151 | int previewWidth = data.previewWidth; |
| 152 | int previewHeight = data.previewheight; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 153 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 154 | Paint paint = new Paint(); |
| 155 | ColorMatrix desat = new ColorMatrix(); |
| 156 | desat.setSaturation(0.25f); |
| 157 | paint.setColorFilter(new ColorMatrixColorFilter(desat)); |
| 158 | Matrix matrix = new Matrix(); |
Winson Chung | c6caff8 | 2015-05-27 12:41:29 -0700 | [diff] [blame] | 159 | int overlayColor = 0x40FFFFFF; |
| 160 | |
Winson Chung | c6caff8 | 2015-05-27 12:41:29 -0700 | [diff] [blame] | 161 | matrix.setTranslate((previewWidth - mImageWidth) / 2, (previewHeight - mImageHeight) / 2); |
chaviw | a053926 | 2017-12-12 14:46:40 -0800 | [diff] [blame] | 162 | Bitmap picture = generateAdjustedHwBitmap(data.image, previewWidth, previewHeight, matrix, |
| 163 | paint, overlayColor); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 164 | |
Winson Chung | c6caff8 | 2015-05-27 12:41:29 -0700 | [diff] [blame] | 165 | // Note, we can't use the preview for the small icon, since it is non-square |
| 166 | float scale = (float) iconSize / Math.min(mImageWidth, mImageHeight); |
Winson Chung | c6caff8 | 2015-05-27 12:41:29 -0700 | [diff] [blame] | 167 | matrix.setScale(scale, scale); |
| 168 | matrix.postTranslate((iconSize - (scale * mImageWidth)) / 2, |
| 169 | (iconSize - (scale * mImageHeight)) / 2); |
chaviw | a053926 | 2017-12-12 14:46:40 -0800 | [diff] [blame] | 170 | Bitmap icon = generateAdjustedHwBitmap(data.image, iconSize, iconSize, matrix, paint, |
| 171 | overlayColor); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 172 | |
Winson Chung | 9c99aee | 2011-11-14 14:12:54 -0800 | [diff] [blame] | 173 | mNotificationManager = nManager; |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 174 | final long now = System.currentTimeMillis(); |
| 175 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 176 | // Setup the notification |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 177 | mNotificationStyle = new Notification.BigPictureStyle() |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 178 | .bigPicture(picture.createAshmemBitmap()); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 179 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 180 | // The public notification will show similar info but with the actual screenshot omitted |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 181 | mPublicNotificationBuilder = |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 182 | new Notification.Builder(context, NotificationChannels.SCREENSHOTS_HEADSUP) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 183 | .setContentTitle(r.getString(R.string.screenshot_saving_title)) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 184 | .setSmallIcon(R.drawable.stat_notify_image) |
| 185 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 186 | .setWhen(now) |
| 187 | .setShowWhen(true) |
| 188 | .setColor(r.getColor( |
| 189 | com.android.internal.R.color.system_notification_accent_color)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 190 | SystemUI.overrideNotificationAppName(context, mPublicNotificationBuilder, true); |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 191 | |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 192 | mNotificationBuilder = new Notification.Builder(context, |
| 193 | NotificationChannels.SCREENSHOTS_HEADSUP) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 194 | .setContentTitle(r.getString(R.string.screenshot_saving_title)) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 195 | .setSmallIcon(R.drawable.stat_notify_image) |
| 196 | .setWhen(now) |
Dan Sandler | d52ea0c | 2016-04-14 10:16:28 -0400 | [diff] [blame] | 197 | .setShowWhen(true) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 198 | .setColor(r.getColor(com.android.internal.R.color.system_notification_accent_color)) |
| 199 | .setStyle(mNotificationStyle) |
| 200 | .setPublicVersion(mPublicNotificationBuilder.build()); |
| 201 | mNotificationBuilder.setFlag(Notification.FLAG_NO_CLEAR, true); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 202 | SystemUI.overrideNotificationAppName(context, mNotificationBuilder, true); |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 203 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 204 | mNotificationManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT, |
| 205 | mNotificationBuilder.build()); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * NOTE: The following code prepares the notification builder for updating the notification |
| 209 | * after the screenshot has been written to disk. |
| 210 | */ |
Winson Chung | 9c99aee | 2011-11-14 14:12:54 -0800 | [diff] [blame] | 211 | |
| 212 | // On the tablet, the large icon makes the notification appear as if it is clickable (and |
| 213 | // on small devices, the large icon is not shown) so defer showing the large icon until |
| 214 | // we compose the final post-save notification below. |
Winson Chung | 1b87be4 | 2015-06-02 12:18:05 -0700 | [diff] [blame] | 215 | mNotificationBuilder.setLargeIcon(icon.createAshmemBitmap()); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 216 | // But we still don't set it for the expanded view, allowing the smallIcon to show here. |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 217 | mNotificationStyle.bigLargeIcon((Bitmap) null); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 218 | } |
| 219 | |
chaviw | a053926 | 2017-12-12 14:46:40 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Generates a new hardware bitmap with specified values, copying the content from the passed |
| 222 | * in bitmap. |
| 223 | */ |
| 224 | private Bitmap generateAdjustedHwBitmap(Bitmap bitmap, int width, int height, Matrix matrix, |
| 225 | Paint paint, int color) { |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 226 | Picture picture = new Picture(); |
| 227 | Canvas canvas = picture.beginRecording(width, height); |
chaviw | a053926 | 2017-12-12 14:46:40 -0800 | [diff] [blame] | 228 | canvas.drawColor(color); |
| 229 | canvas.drawBitmap(bitmap, matrix, paint); |
John Reck | 519ad48 | 2018-02-12 17:08:48 -0800 | [diff] [blame] | 230 | picture.endRecording(); |
| 231 | return Bitmap.createBitmap(picture); |
chaviw | a053926 | 2017-12-12 14:46:40 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 234 | @Override |
Jeff Sharkey | 7bba507 | 2018-11-02 14:37:07 -0600 | [diff] [blame] | 235 | protected Void doInBackground(Void... paramsUnused) { |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 236 | if (isCancelled()) { |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 237 | return null; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 238 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 239 | |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 240 | // By default, AsyncTask sets the worker thread to have background thread priority, so bump |
| 241 | // it back up so that we save a little quicker. |
| 242 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
| 243 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 244 | Context context = mParams.context; |
| 245 | Bitmap image = mParams.image; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 246 | Resources r = context.getResources(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 247 | |
Winson Chung | d859fa3 | 2011-07-22 12:19:52 -0700 | [diff] [blame] | 248 | try { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 249 | // Save the screenshot to the MediaStore |
Jeff Sharkey | 7bba507 | 2018-11-02 14:37:07 -0600 | [diff] [blame] | 250 | final MediaStore.PendingParams params = new MediaStore.PendingParams( |
| 251 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mImageFileName, "image/png"); |
| 252 | params.setPrimaryDirectory(Environment.DIRECTORY_PICTURES); |
| 253 | params.setSecondaryDirectory(Environment.DIRECTORY_SCREENSHOTS); |
| 254 | |
| 255 | final Uri uri = MediaStore.createPending(context, params); |
| 256 | final MediaStore.PendingSession session = MediaStore.openPending(context, uri); |
| 257 | try { |
| 258 | try (OutputStream out = session.openOutputStream()) { |
| 259 | if (!image.compress(Bitmap.CompressFormat.PNG, 100, out)) { |
| 260 | throw new IOException("Failed to compress"); |
| 261 | } |
| 262 | } |
| 263 | session.publish(); |
| 264 | } catch (Exception e) { |
| 265 | session.abandon(); |
| 266 | throw e; |
| 267 | } finally { |
| 268 | IoUtils.closeQuietly(session); |
| 269 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 270 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 271 | // Note: Both the share and edit actions are proxied through ActionProxyReceiver in |
| 272 | // order to do some common work like dismissing the keyguard and sending |
| 273 | // closeSystemWindows |
| 274 | |
| 275 | // Create a share intent, this will always go through the chooser activity first which |
| 276 | // should not trigger auto-enter PiP |
Victoria Lease | e45e1f2 | 2013-09-30 15:33:43 -0700 | [diff] [blame] | 277 | String subjectDate = DateFormat.getDateTimeInstance().format(new Date(mImageTime)); |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 278 | String subject = String.format(SCREENSHOT_SHARE_SUBJECT_TEMPLATE, subjectDate); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 279 | Intent sharingIntent = new Intent(Intent.ACTION_SEND); |
| 280 | sharingIntent.setType("image/png"); |
| 281 | sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); |
Alison Cichowlas | 09e5ea3 | 2018-09-07 15:52:24 -0400 | [diff] [blame] | 282 | // Include URI in ClipData also, so that grantPermission picks it up. |
| 283 | // We don't use setData here because some apps interpret this as "to:". |
| 284 | ClipData clipdata = new ClipData(new ClipDescription("content", |
| 285 | new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}), |
| 286 | new ClipData.Item(uri)); |
| 287 | sharingIntent.setClipData(clipdata); |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 288 | sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject); |
Alison Cichowlas | 4f1297a | 2018-01-30 11:40:36 -0500 | [diff] [blame] | 289 | sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Daniel Sandler | 046fddf | 2012-05-31 15:40:25 -0400 | [diff] [blame] | 290 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 291 | PendingIntent chooserAction = PendingIntent.getBroadcast(context, 0, |
| 292 | new Intent(context, GlobalScreenshot.TargetChosenReceiver.class), |
| 293 | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); |
| 294 | Intent sharingChooserIntent = Intent.createChooser(sharingIntent, null, |
| 295 | chooserAction.getIntentSender()) |
Alison Cichowlas | 09e5ea3 | 2018-09-07 15:52:24 -0400 | [diff] [blame] | 296 | .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) |
| 297 | .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 298 | |
| 299 | // Create a share action for the notification |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 300 | PendingIntent shareAction = PendingIntent.getBroadcastAsUser(context, 0, |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 301 | new Intent(context, GlobalScreenshot.ActionProxyReceiver.class) |
| 302 | .putExtra(EXTRA_ACTION_INTENT, sharingChooserIntent) |
| 303 | .putExtra(EXTRA_DISALLOW_ENTER_PIP, true), |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 304 | PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.SYSTEM); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 305 | Notification.Action.Builder shareActionBuilder = new Notification.Action.Builder( |
| 306 | R.drawable.ic_screenshot_share, |
| 307 | r.getString(com.android.internal.R.string.share), shareAction); |
| 308 | mNotificationBuilder.addAction(shareActionBuilder.build()); |
Daniel Sandler | 046fddf | 2012-05-31 15:40:25 -0400 | [diff] [blame] | 309 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 310 | // Create an edit intent, if a specific package is provided as the editor, then launch |
| 311 | // that directly |
| 312 | String editorPackage = context.getString(R.string.config_screenshotEditor); |
Alison Cichowlas | 134f2cc | 2018-01-19 19:16:31 -0500 | [diff] [blame] | 313 | Intent editIntent = new Intent(Intent.ACTION_EDIT); |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 314 | if (!TextUtils.isEmpty(editorPackage)) { |
| 315 | editIntent.setComponent(ComponentName.unflattenFromString(editorPackage)); |
| 316 | } |
Alison Cichowlas | 134f2cc | 2018-01-19 19:16:31 -0500 | [diff] [blame] | 317 | editIntent.setType("image/png"); |
Alison Cichowlas | 4f1297a | 2018-01-30 11:40:36 -0500 | [diff] [blame] | 318 | editIntent.setData(uri); |
| 319 | editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 320 | editIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
Alison Cichowlas | 134f2cc | 2018-01-19 19:16:31 -0500 | [diff] [blame] | 321 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 322 | // Create a edit action |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 323 | PendingIntent editAction = PendingIntent.getBroadcastAsUser(context, 1, |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 324 | new Intent(context, GlobalScreenshot.ActionProxyReceiver.class) |
| 325 | .putExtra(EXTRA_ACTION_INTENT, editIntent) |
| 326 | .putExtra(EXTRA_CANCEL_NOTIFICATION, editIntent.getComponent() != null), |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 327 | PendingIntent.FLAG_CANCEL_CURRENT, UserHandle.SYSTEM); |
Alison Cichowlas | 134f2cc | 2018-01-19 19:16:31 -0500 | [diff] [blame] | 328 | Notification.Action.Builder editActionBuilder = new Notification.Action.Builder( |
| 329 | R.drawable.ic_screenshot_edit, |
| 330 | r.getString(com.android.internal.R.string.screenshot_edit), editAction); |
| 331 | mNotificationBuilder.addAction(editActionBuilder.build()); |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 332 | |
Alison Cichowlas | f280653 | 2018-03-06 19:08:18 -0500 | [diff] [blame] | 333 | // Create a delete action for the notification |
| 334 | PendingIntent deleteAction = PendingIntent.getBroadcast(context, 0, |
| 335 | new Intent(context, GlobalScreenshot.DeleteScreenshotReceiver.class) |
| 336 | .putExtra(GlobalScreenshot.SCREENSHOT_URI_ID, uri.toString()), |
| 337 | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); |
| 338 | Notification.Action.Builder deleteActionBuilder = new Notification.Action.Builder( |
| 339 | R.drawable.ic_screenshot_delete, |
| 340 | r.getString(com.android.internal.R.string.delete), deleteAction); |
| 341 | mNotificationBuilder.addAction(deleteActionBuilder.build()); |
| 342 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 343 | mParams.imageUri = uri; |
| 344 | mParams.image = null; |
| 345 | mParams.errorMsgResId = 0; |
Winson Chung | d859fa3 | 2011-07-22 12:19:52 -0700 | [diff] [blame] | 346 | } catch (Exception e) { |
| 347 | // IOException/UnsupportedOperationException may be thrown if external storage is not |
| 348 | // mounted |
Dan Sandler | 150651b | 2017-06-14 10:43:07 -0400 | [diff] [blame] | 349 | Slog.e(TAG, "unable to save screenshot", e); |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 350 | mParams.clearImage(); |
| 351 | mParams.errorMsgResId = R.string.screenshot_failed_to_save_text; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Winson Chung | cd6a9ef | 2013-01-16 14:59:18 -0800 | [diff] [blame] | 354 | // Recycle the bitmap data |
| 355 | if (image != null) { |
| 356 | image.recycle(); |
| 357 | } |
| 358 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 359 | return null; |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 360 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 361 | |
| 362 | @Override |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 363 | protected void onPostExecute(Void params) { |
| 364 | if (mParams.errorMsgResId != 0) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 365 | // Show a message that we've failed to save the image to disk |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 366 | GlobalScreenshot.notifyScreenshotError(mParams.context, mNotificationManager, |
| 367 | mParams.errorMsgResId); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 368 | } else { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 369 | // Show the final notification to indicate screenshot saved |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 370 | Context context = mParams.context; |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 371 | Resources r = context.getResources(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 372 | |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 373 | // Create the intent to show the screenshot in gallery |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 374 | Intent launchIntent = new Intent(Intent.ACTION_VIEW); |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 375 | launchIntent.setDataAndType(mParams.imageUri, "image/png"); |
Jeff Sharkey | 72b84a2 | 2016-12-01 17:10:27 -0700 | [diff] [blame] | 376 | launchIntent.setFlags( |
| 377 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 378 | |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 379 | final long now = System.currentTimeMillis(); |
| 380 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 381 | // Update the text and the icon for the existing notification |
| 382 | mPublicNotificationBuilder |
| 383 | .setContentTitle(r.getString(R.string.screenshot_saved_title)) |
| 384 | .setContentText(r.getString(R.string.screenshot_saved_text)) |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 385 | .setContentIntent(PendingIntent.getActivity(mParams.context, 0, launchIntent, 0)) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 386 | .setWhen(now) |
| 387 | .setAutoCancel(true) |
| 388 | .setColor(context.getColor( |
| 389 | com.android.internal.R.color.system_notification_accent_color)); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 390 | mNotificationBuilder |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 391 | .setContentTitle(r.getString(R.string.screenshot_saved_title)) |
| 392 | .setContentText(r.getString(R.string.screenshot_saved_text)) |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 393 | .setContentIntent(PendingIntent.getActivity(mParams.context, 0, launchIntent, 0)) |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 394 | .setWhen(now) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 395 | .setAutoCancel(true) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 396 | .setColor(context.getColor( |
| 397 | com.android.internal.R.color.system_notification_accent_color)) |
| 398 | .setPublicVersion(mPublicNotificationBuilder.build()) |
| 399 | .setFlag(Notification.FLAG_NO_CLEAR, false); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 400 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 401 | mNotificationManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT, |
| 402 | mNotificationBuilder.build()); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 403 | } |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 404 | mParams.finisher.run(); |
| 405 | mParams.clearContext(); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 406 | } |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 407 | |
| 408 | @Override |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 409 | protected void onCancelled(Void params) { |
| 410 | // If we are cancelled while the task is running in the background, we may get null params. |
| 411 | // The finisher is expected to always be called back, so just use the baked-in params from |
| 412 | // the ctor in any case. |
| 413 | mParams.finisher.run(); |
| 414 | mParams.clearImage(); |
| 415 | mParams.clearContext(); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 416 | |
| 417 | // Cancel the posted notification |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 418 | mNotificationManager.cancel(SystemMessage.NOTE_GLOBAL_SCREENSHOT); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 419 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 423 | * An AsyncTask that deletes an image from the media store in the background. |
| 424 | */ |
| 425 | class DeleteImageInBackgroundTask extends AsyncTask<Uri, Void, Void> { |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 426 | private Context mContext; |
| 427 | |
| 428 | DeleteImageInBackgroundTask(Context context) { |
| 429 | mContext = context; |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | protected Void doInBackground(Uri... params) { |
| 434 | if (params.length != 1) return null; |
| 435 | |
| 436 | Uri screenshotUri = params[0]; |
| 437 | ContentResolver resolver = mContext.getContentResolver(); |
| 438 | resolver.delete(screenshotUri, null, null); |
| 439 | return null; |
| 440 | } |
| 441 | } |
| 442 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 443 | class GlobalScreenshot { |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 444 | static final String SCREENSHOT_URI_ID = "android:screenshot_uri_id"; |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 445 | static final String EXTRA_ACTION_INTENT = "android:screenshot_action_intent"; |
| 446 | static final String EXTRA_CANCEL_NOTIFICATION = "android:screenshot_cancel_notification"; |
| 447 | static final String EXTRA_DISALLOW_ENTER_PIP = "android:screenshot_disallow_enter_pip"; |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 448 | |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 449 | private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130; |
| 450 | private static final int SCREENSHOT_DROP_IN_DURATION = 430; |
| 451 | private static final int SCREENSHOT_DROP_OUT_DELAY = 500; |
| 452 | private static final int SCREENSHOT_DROP_OUT_DURATION = 430; |
| 453 | private static final int SCREENSHOT_DROP_OUT_SCALE_DURATION = 370; |
| 454 | private static final int SCREENSHOT_FAST_DROP_OUT_DURATION = 320; |
| 455 | private static final float BACKGROUND_ALPHA = 0.5f; |
| 456 | private static final float SCREENSHOT_SCALE = 1f; |
| 457 | private static final float SCREENSHOT_DROP_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.725f; |
| 458 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.45f; |
| 459 | private static final float SCREENSHOT_FAST_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.6f; |
| 460 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET = 0f; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 461 | private final int mPreviewWidth; |
| 462 | private final int mPreviewHeight; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 463 | |
| 464 | private Context mContext; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 465 | private WindowManager mWindowManager; |
| 466 | private WindowManager.LayoutParams mWindowLayoutParams; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 467 | private NotificationManager mNotificationManager; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 468 | private Display mDisplay; |
| 469 | private DisplayMetrics mDisplayMetrics; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 470 | |
| 471 | private Bitmap mScreenBitmap; |
| 472 | private View mScreenshotLayout; |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 473 | private ScreenshotSelectorView mScreenshotSelectorView; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 474 | private ImageView mBackgroundView; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 475 | private ImageView mScreenshotView; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 476 | private ImageView mScreenshotFlash; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 477 | |
| 478 | private AnimatorSet mScreenshotAnimation; |
| 479 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 480 | private int mNotificationIconSize; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 481 | private float mBgPadding; |
| 482 | private float mBgPaddingScale; |
| 483 | |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 484 | private AsyncTask<Void, Void, Void> mSaveInBgTask; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 485 | |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 486 | private MediaActionSound mCameraSound; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 487 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * @param context everything needs a context :( |
| 491 | */ |
| 492 | public GlobalScreenshot(Context context) { |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 493 | Resources r = context.getResources(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 494 | mContext = context; |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 495 | LayoutInflater layoutInflater = (LayoutInflater) |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 496 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 497 | |
| 498 | // Inflate the screenshot layout |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 499 | mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 500 | mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 501 | mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 502 | mScreenshotFlash = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_flash); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 503 | mScreenshotSelectorView = (ScreenshotSelectorView) mScreenshotLayout.findViewById( |
| 504 | R.id.global_screenshot_selector); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 505 | mScreenshotLayout.setFocusable(true); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 506 | mScreenshotSelectorView.setFocusable(true); |
| 507 | mScreenshotSelectorView.setFocusableInTouchMode(true); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 508 | mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() { |
| 509 | @Override |
| 510 | public boolean onTouch(View v, MotionEvent event) { |
| 511 | // Intercept and ignore all touch events |
| 512 | return true; |
| 513 | } |
| 514 | }); |
| 515 | |
| 516 | // Setup the window that we are going to use |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 517 | mWindowLayoutParams = new WindowManager.LayoutParams( |
| 518 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 519 | WindowManager.LayoutParams.TYPE_SCREENSHOT, |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 520 | WindowManager.LayoutParams.FLAG_FULLSCREEN |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 521 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 522 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, |
| 523 | PixelFormat.TRANSLUCENT); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 524 | mWindowLayoutParams.setTitle("ScreenshotAnimation"); |
Adrian Roos | d122977 | 2018-03-21 14:46:10 +0100 | [diff] [blame] | 525 | mWindowLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 526 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 527 | mNotificationManager = |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 528 | (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 529 | mDisplay = mWindowManager.getDefaultDisplay(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 530 | mDisplayMetrics = new DisplayMetrics(); |
| 531 | mDisplay.getRealMetrics(mDisplayMetrics); |
| 532 | |
| 533 | // Get the various target sizes |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 534 | mNotificationIconSize = |
| 535 | r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 536 | |
| 537 | // Scale has to account for both sides of the bg |
| 538 | mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 539 | mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 540 | |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 541 | // determine the optimal preview size |
| 542 | int panelWidth = 0; |
| 543 | try { |
| 544 | panelWidth = r.getDimensionPixelSize(R.dimen.notification_panel_width); |
| 545 | } catch (Resources.NotFoundException e) { |
Dan Sandler | deb0d11 | 2014-08-07 09:21:06 -0400 | [diff] [blame] | 546 | } |
| 547 | if (panelWidth <= 0) { |
| 548 | // includes notification_panel_width==match_parent (-1) |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 549 | panelWidth = mDisplayMetrics.widthPixels; |
| 550 | } |
| 551 | mPreviewWidth = panelWidth; |
| 552 | mPreviewHeight = r.getDimensionPixelSize(R.dimen.notification_max_height); |
| 553 | |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 554 | // Setup the Camera shutter sound |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 555 | mCameraSound = new MediaActionSound(); |
| 556 | mCameraSound.load(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Creates a new worker thread and saves the screenshot to the media store. |
| 561 | */ |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 562 | private void saveScreenshotInWorkerThread(Runnable finisher) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 563 | SaveImageInBackgroundData data = new SaveImageInBackgroundData(); |
| 564 | data.context = mContext; |
| 565 | data.image = mScreenBitmap; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 566 | data.iconSize = mNotificationIconSize; |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 567 | data.finisher = finisher; |
Chris Wren | f63185e | 2014-07-31 13:52:39 +0100 | [diff] [blame] | 568 | data.previewWidth = mPreviewWidth; |
| 569 | data.previewheight = mPreviewHeight; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 570 | if (mSaveInBgTask != null) { |
| 571 | mSaveInBgTask.cancel(false); |
| 572 | } |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 573 | mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager) |
Winson | c7aa65b | 2016-03-10 12:52:27 -0800 | [diff] [blame] | 574 | .execute(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 578 | * Takes a screenshot of the current display and shows an animation. |
| 579 | */ |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 580 | private void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible, |
| 581 | Rect crop) { |
| 582 | int rot = mDisplay.getRotation(); |
| 583 | int width = crop.width(); |
| 584 | int height = crop.height(); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 585 | |
| 586 | // Take the screenshot |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 587 | mScreenBitmap = SurfaceControl.screenshot(crop, width, height, rot); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 588 | if (mScreenBitmap == null) { |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 589 | notifyScreenshotError(mContext, mNotificationManager, |
| 590 | R.string.screenshot_failed_to_capture_text); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 591 | finisher.run(); |
| 592 | return; |
| 593 | } |
| 594 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 595 | // Optimizations |
| 596 | mScreenBitmap.setHasAlpha(false); |
| 597 | mScreenBitmap.prepareToDraw(); |
| 598 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 599 | // Start the post-screenshot animation |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 600 | startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, |
| 601 | statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 604 | void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { |
| 605 | mDisplay.getRealMetrics(mDisplayMetrics); |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 606 | takeScreenshot(finisher, statusBarVisible, navBarVisible, |
| 607 | new Rect(0, 0, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels)); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Displays a screenshot selector |
| 612 | */ |
| 613 | void takeScreenshotPartial(final Runnable finisher, final boolean statusBarVisible, |
| 614 | final boolean navBarVisible) { |
| 615 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); |
| 616 | mScreenshotSelectorView.setOnTouchListener(new View.OnTouchListener() { |
| 617 | @Override |
| 618 | public boolean onTouch(View v, MotionEvent event) { |
| 619 | ScreenshotSelectorView view = (ScreenshotSelectorView) v; |
| 620 | switch (event.getAction()) { |
| 621 | case MotionEvent.ACTION_DOWN: |
| 622 | view.startSelection((int) event.getX(), (int) event.getY()); |
| 623 | return true; |
| 624 | case MotionEvent.ACTION_MOVE: |
| 625 | view.updateSelection((int) event.getX(), (int) event.getY()); |
| 626 | return true; |
| 627 | case MotionEvent.ACTION_UP: |
| 628 | view.setVisibility(View.GONE); |
| 629 | mWindowManager.removeView(mScreenshotLayout); |
| 630 | final Rect rect = view.getSelectionRect(); |
| 631 | if (rect != null) { |
| 632 | if (rect.width() != 0 && rect.height() != 0) { |
| 633 | // Need mScreenshotLayout to handle it after the view disappears |
| 634 | mScreenshotLayout.post(new Runnable() { |
| 635 | public void run() { |
| 636 | takeScreenshot(finisher, statusBarVisible, navBarVisible, |
chaviw | a69e0a7 | 2017-11-29 17:55:12 -0800 | [diff] [blame] | 637 | rect); |
Muyuan Li | 6ca619f | 2016-03-08 13:30:42 -0800 | [diff] [blame] | 638 | } |
| 639 | }); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | view.stopSelection(); |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | return false; |
| 648 | } |
| 649 | }); |
| 650 | mScreenshotLayout.post(new Runnable() { |
| 651 | @Override |
| 652 | public void run() { |
| 653 | mScreenshotSelectorView.setVisibility(View.VISIBLE); |
| 654 | mScreenshotSelectorView.requestFocus(); |
| 655 | } |
| 656 | }); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Cancels screenshot request |
| 661 | */ |
| 662 | void stopScreenshot() { |
| 663 | // If the selector layer still presents on screen, we remove it and resets its state. |
| 664 | if (mScreenshotSelectorView.getSelectionRect() != null) { |
| 665 | mWindowManager.removeView(mScreenshotLayout); |
| 666 | mScreenshotSelectorView.stopSelection(); |
| 667 | } |
| 668 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 669 | |
| 670 | /** |
| 671 | * Starts the animation after taking the screenshot |
| 672 | */ |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 673 | private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible, |
| 674 | boolean navBarVisible) { |
Geoffrey Pitsch | ed5de31 | 2017-06-27 11:42:12 -0400 | [diff] [blame] | 675 | // If power save is on, show a toast so there is some visual indication that a screenshot |
| 676 | // has been taken. |
| 677 | PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 678 | if (powerManager.isPowerSaveMode()) { |
| 679 | Toast.makeText(mContext, R.string.screenshot_saved_title, Toast.LENGTH_SHORT).show(); |
| 680 | } |
| 681 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 682 | // Add the view for the animation |
| 683 | mScreenshotView.setImageBitmap(mScreenBitmap); |
| 684 | mScreenshotLayout.requestFocus(); |
| 685 | |
| 686 | // Setup the animation with the screenshot just taken |
| 687 | if (mScreenshotAnimation != null) { |
Dan Sandler | d9cd20b | 2015-11-13 15:01:44 -0500 | [diff] [blame] | 688 | if (mScreenshotAnimation.isStarted()) { |
| 689 | mScreenshotAnimation.end(); |
| 690 | } |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 691 | mScreenshotAnimation.removeAllListeners(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 695 | ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation(); |
| 696 | ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 697 | statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 698 | mScreenshotAnimation = new AnimatorSet(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 699 | mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 700 | mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { |
| 701 | @Override |
| 702 | public void onAnimationEnd(Animator animation) { |
| 703 | // Save the screenshot once we have a bit of time now |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 704 | saveScreenshotInWorkerThread(finisher); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 705 | mWindowManager.removeView(mScreenshotLayout); |
Winson Chung | cd6a9ef | 2013-01-16 14:59:18 -0800 | [diff] [blame] | 706 | |
| 707 | // Clear any references to the bitmap |
| 708 | mScreenBitmap = null; |
| 709 | mScreenshotView.setImageBitmap(null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 710 | } |
| 711 | }); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 712 | mScreenshotLayout.post(new Runnable() { |
| 713 | @Override |
| 714 | public void run() { |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 715 | // Play the shutter sound to notify that we've taken a screenshot |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 716 | mCameraSound.play(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 717 | |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 718 | mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 719 | mScreenshotView.buildLayer(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 720 | mScreenshotAnimation.start(); |
| 721 | } |
| 722 | }); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 723 | } |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 724 | private ValueAnimator createScreenshotDropInAnimation() { |
| 725 | final float flashPeakDurationPct = ((float) (SCREENSHOT_FLASH_TO_PEAK_DURATION) |
| 726 | / SCREENSHOT_DROP_IN_DURATION); |
| 727 | final float flashDurationPct = 2f * flashPeakDurationPct; |
| 728 | final Interpolator flashAlphaInterpolator = new Interpolator() { |
| 729 | @Override |
| 730 | public float getInterpolation(float x) { |
| 731 | // Flash the flash view in and out quickly |
| 732 | if (x <= flashDurationPct) { |
| 733 | return (float) Math.sin(Math.PI * (x / flashDurationPct)); |
| 734 | } |
| 735 | return 0; |
| 736 | } |
| 737 | }; |
| 738 | final Interpolator scaleInterpolator = new Interpolator() { |
| 739 | @Override |
| 740 | public float getInterpolation(float x) { |
| 741 | // We start scaling when the flash is at it's peak |
| 742 | if (x < flashPeakDurationPct) { |
| 743 | return 0; |
| 744 | } |
| 745 | return (x - flashDurationPct) / (1f - flashDurationPct); |
| 746 | } |
| 747 | }; |
Beth Thibodeau | d98a945 | 2019-03-08 14:32:51 -0500 | [diff] [blame] | 748 | |
| 749 | Resources r = mContext.getResources(); |
| 750 | if ((r.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) |
| 751 | == Configuration.UI_MODE_NIGHT_YES) { |
| 752 | mScreenshotView.getBackground().setTint(Color.BLACK); |
| 753 | } else { |
| 754 | mScreenshotView.getBackground().setTintList(null); |
| 755 | } |
| 756 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 757 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 758 | anim.setDuration(SCREENSHOT_DROP_IN_DURATION); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 759 | anim.addListener(new AnimatorListenerAdapter() { |
| 760 | @Override |
| 761 | public void onAnimationStart(Animator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 762 | mBackgroundView.setAlpha(0f); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 763 | mBackgroundView.setVisibility(View.VISIBLE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 764 | mScreenshotView.setAlpha(0f); |
| 765 | mScreenshotView.setTranslationX(0f); |
| 766 | mScreenshotView.setTranslationY(0f); |
| 767 | mScreenshotView.setScaleX(SCREENSHOT_SCALE + mBgPaddingScale); |
| 768 | mScreenshotView.setScaleY(SCREENSHOT_SCALE + mBgPaddingScale); |
| 769 | mScreenshotView.setVisibility(View.VISIBLE); |
| 770 | mScreenshotFlash.setAlpha(0f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 771 | mScreenshotFlash.setVisibility(View.VISIBLE); |
| 772 | } |
| 773 | @Override |
| 774 | public void onAnimationEnd(android.animation.Animator animation) { |
| 775 | mScreenshotFlash.setVisibility(View.GONE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 776 | } |
| 777 | }); |
| 778 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 779 | @Override |
| 780 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 781 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 782 | float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 783 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 784 | * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 785 | mBackgroundView.setAlpha(scaleInterpolator.getInterpolation(t) * BACKGROUND_ALPHA); |
| 786 | mScreenshotView.setAlpha(t); |
| 787 | mScreenshotView.setScaleX(scaleT); |
| 788 | mScreenshotView.setScaleY(scaleT); |
| 789 | mScreenshotFlash.setAlpha(flashAlphaInterpolator.getInterpolation(t)); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 790 | } |
| 791 | }); |
| 792 | return anim; |
| 793 | } |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 794 | private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 795 | boolean navBarVisible) { |
| 796 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 797 | anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 798 | anim.addListener(new AnimatorListenerAdapter() { |
| 799 | @Override |
| 800 | public void onAnimationEnd(Animator animation) { |
| 801 | mBackgroundView.setVisibility(View.GONE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 802 | mScreenshotView.setVisibility(View.GONE); |
| 803 | mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 804 | } |
| 805 | }); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 806 | |
| 807 | if (!statusBarVisible || !navBarVisible) { |
| 808 | // 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] | 809 | anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 810 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 811 | @Override |
| 812 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 813 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 814 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 815 | - t * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_FAST_DROP_OUT_MIN_SCALE); |
| 816 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 817 | mScreenshotView.setAlpha(1f - t); |
| 818 | mScreenshotView.setScaleX(scaleT); |
| 819 | mScreenshotView.setScaleY(scaleT); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 820 | } |
| 821 | }); |
| 822 | } else { |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 823 | // In the case where there is a status bar, animate to the origin of the bar (top-left) |
| 824 | final float scaleDurationPct = (float) SCREENSHOT_DROP_OUT_SCALE_DURATION |
| 825 | / SCREENSHOT_DROP_OUT_DURATION; |
| 826 | final Interpolator scaleInterpolator = new Interpolator() { |
| 827 | @Override |
| 828 | public float getInterpolation(float x) { |
| 829 | if (x < scaleDurationPct) { |
| 830 | // Decelerate, and scale the input accordingly |
| 831 | return (float) (1f - Math.pow(1f - (x / scaleDurationPct), 2f)); |
| 832 | } |
| 833 | return 1f; |
| 834 | } |
| 835 | }; |
| 836 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 837 | // Determine the bounds of how to scale |
| 838 | float halfScreenWidth = (w - 2f * mBgPadding) / 2f; |
| 839 | float halfScreenHeight = (h - 2f * mBgPadding) / 2f; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 840 | final float offsetPct = SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET; |
| 841 | final PointF finalPos = new PointF( |
| 842 | -halfScreenWidth + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenWidth, |
| 843 | -halfScreenHeight + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenHeight); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 844 | |
| 845 | // Animate the screenshot to the status bar |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 846 | anim.setDuration(SCREENSHOT_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 847 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 848 | @Override |
| 849 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 850 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 851 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 852 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 853 | * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 854 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 855 | mScreenshotView.setAlpha(1f - scaleInterpolator.getInterpolation(t)); |
| 856 | mScreenshotView.setScaleX(scaleT); |
| 857 | mScreenshotView.setScaleY(scaleT); |
| 858 | mScreenshotView.setTranslationX(t * finalPos.x); |
| 859 | mScreenshotView.setTranslationY(t * finalPos.y); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 860 | } |
| 861 | }); |
| 862 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 863 | return anim; |
| 864 | } |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 865 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 866 | static void notifyScreenshotError(Context context, NotificationManager nManager, int msgResId) { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 867 | Resources r = context.getResources(); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 868 | String errorMsg = r.getString(msgResId); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 869 | |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 870 | // Repurpose the existing notification to notify the user of the error |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 871 | Notification.Builder b = new Notification.Builder(context, NotificationChannels.ALERTS) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 872 | .setTicker(r.getString(R.string.screenshot_failed_title)) |
| 873 | .setContentTitle(r.getString(R.string.screenshot_failed_title)) |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 874 | .setContentText(errorMsg) |
Winson Chung | b787a75 | 2011-10-19 18:31:53 -0700 | [diff] [blame] | 875 | .setSmallIcon(R.drawable.stat_notify_image_error) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 876 | .setWhen(System.currentTimeMillis()) |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 877 | .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen |
Dan Sandler | 59f0258 | 2014-03-21 12:04:19 -0400 | [diff] [blame] | 878 | .setCategory(Notification.CATEGORY_ERROR) |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 879 | .setAutoCancel(true) |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 880 | .setColor(context.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 881 | com.android.internal.R.color.system_notification_accent_color)); |
phweiss | ec3d9ca | 2017-02-09 16:57:39 +0100 | [diff] [blame] | 882 | final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( |
| 883 | Context.DEVICE_POLICY_SERVICE); |
| 884 | final Intent intent = dpm.createAdminSupportIntent( |
| 885 | DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 886 | if (intent != null) { |
| 887 | final PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 888 | context, 0, intent, 0, null, UserHandle.CURRENT); |
| 889 | b.setContentIntent(pendingIntent); |
| 890 | } |
| 891 | |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 892 | SystemUI.overrideNotificationAppName(context, b, true); |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 893 | |
| 894 | Notification n = new Notification.BigTextStyle(b) |
| 895 | .bigText(errorMsg) |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 896 | .build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 897 | nManager.notify(SystemMessage.NOTE_GLOBAL_SCREENSHOT, n); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 898 | } |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 899 | |
| 900 | /** |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 901 | * Receiver to proxy the share or edit intent, used to clean up the notification and send |
| 902 | * appropriate signals to the system (ie. to dismiss the keyguard if necessary). |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 903 | */ |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 904 | public static class ActionProxyReceiver extends BroadcastReceiver { |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 905 | @Override |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 906 | public void onReceive(Context context, final Intent intent) { |
| 907 | Runnable startActivityRunnable = () -> { |
| 908 | ActivityManagerWrapper.getInstance().closeSystemWindows( |
| 909 | SYSTEM_DIALOG_REASON_SCREENSHOT); |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 910 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 911 | Intent actionIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT); |
| 912 | if (intent.getBooleanExtra(EXTRA_CANCEL_NOTIFICATION, false)) { |
| 913 | cancelScreenshotNotification(context); |
| 914 | } |
| 915 | ActivityOptions opts = ActivityOptions.makeBasic(); |
| 916 | opts.setDisallowEnterPictureInPictureWhileLaunching( |
| 917 | intent.getBooleanExtra(EXTRA_DISALLOW_ENTER_PIP, false)); |
Winson Chung | 6c454f6 | 2018-07-09 15:55:56 -0700 | [diff] [blame] | 918 | context.startActivityAsUser(actionIntent, opts.toBundle(), UserHandle.CURRENT); |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 919 | }; |
| 920 | StatusBar statusBar = SysUiServiceProvider.getComponent(context, StatusBar.class); |
| 921 | statusBar.executeRunnableDismissingKeyguard(startActivityRunnable, null, |
| 922 | true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */); |
Winson Chung | ed376a3 | 2017-09-07 14:05:42 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 927 | * Removes the notification for a screenshot after a share target is chosen. |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 928 | */ |
| 929 | public static class TargetChosenReceiver extends BroadcastReceiver { |
| 930 | @Override |
| 931 | public void onReceive(Context context, Intent intent) { |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 932 | // Clear the notification only after the user has chosen a share action |
| 933 | cancelScreenshotNotification(context); |
Adam Powell | d4d3415 | 2015-04-17 12:13:40 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 936 | |
| 937 | /** |
| 938 | * Removes the last screenshot. |
| 939 | */ |
| 940 | public static class DeleteScreenshotReceiver extends BroadcastReceiver { |
| 941 | @Override |
| 942 | public void onReceive(Context context, Intent intent) { |
Winson | e559171 | 2016-02-18 13:45:54 -0800 | [diff] [blame] | 943 | if (!intent.hasExtra(SCREENSHOT_URI_ID)) { |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 947 | // Clear the notification when the image is deleted |
| 948 | cancelScreenshotNotification(context); |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 949 | |
| 950 | // And delete the image from the media store |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 951 | final Uri uri = Uri.parse(intent.getStringExtra(SCREENSHOT_URI_ID)); |
Winson Chung | 8858e6e | 2015-05-27 17:18:56 -0700 | [diff] [blame] | 952 | new DeleteImageInBackgroundTask(context).execute(uri); |
| 953 | } |
| 954 | } |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 955 | |
| 956 | private static void cancelScreenshotNotification(Context context) { |
| 957 | final NotificationManager nm = |
| 958 | (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); |
| 959 | nm.cancel(SystemMessage.NOTE_GLOBAL_SCREENSHOT); |
| 960 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 961 | } |