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 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.AnimatorSet; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
| 23 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 24 | import android.app.Notification; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 25 | import android.app.Notification.BigPictureStyle; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 26 | import android.app.NotificationManager; |
| 27 | import android.app.PendingIntent; |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 28 | import android.content.ContentResolver; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 29 | import android.content.ContentValues; |
| 30 | import android.content.Context; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.res.Resources; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
| 34 | import android.graphics.Canvas; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 35 | import android.graphics.ColorMatrix; |
| 36 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 37 | import android.graphics.Matrix; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 38 | import android.graphics.Paint; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 39 | import android.graphics.PixelFormat; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 40 | import android.graphics.PointF; |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 41 | import android.media.MediaActionSound; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 42 | import android.net.Uri; |
| 43 | import android.os.AsyncTask; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 44 | import android.os.Environment; |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 45 | import android.os.Process; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 46 | import android.provider.MediaStore; |
| 47 | import android.util.DisplayMetrics; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 48 | import android.view.Display; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
| 50 | import android.view.MotionEvent; |
| 51 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 52 | import android.view.SurfaceControl; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 53 | import android.view.View; |
| 54 | import android.view.ViewGroup; |
| 55 | import android.view.WindowManager; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 56 | import android.view.animation.Interpolator; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 57 | import android.widget.ImageView; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 58 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 59 | import com.android.systemui.R; |
| 60 | |
| 61 | import java.io.File; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 62 | import java.io.OutputStream; |
Victoria Lease | e45e1f2 | 2013-09-30 15:33:43 -0700 | [diff] [blame] | 63 | import java.text.DateFormat; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 64 | import java.text.SimpleDateFormat; |
| 65 | import java.util.Date; |
| 66 | |
| 67 | /** |
| 68 | * POD used in the AsyncTask which saves an image in the background. |
| 69 | */ |
| 70 | class SaveImageInBackgroundData { |
| 71 | Context context; |
| 72 | Bitmap image; |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 73 | Uri imageUri; |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 74 | Runnable finisher; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 75 | int iconSize; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 76 | int result; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 77 | |
| 78 | void clearImage() { |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 79 | image = null; |
| 80 | imageUri = null; |
| 81 | iconSize = 0; |
| 82 | } |
Winson Chung | 3a5a774 | 2013-04-01 16:04:28 -0700 | [diff] [blame] | 83 | void clearContext() { |
| 84 | context = null; |
| 85 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * An AsyncTask that saves an image to the media store in the background. |
| 90 | */ |
| 91 | class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void, |
| 92 | SaveImageInBackgroundData> { |
Winson Chung | 5cc9a31 | 2013-02-11 14:58:45 -0800 | [diff] [blame] | 93 | private static final String TAG = "SaveImageInBackgroundTask"; |
| 94 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 95 | private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; |
Winson Chung | 753e40b | 2011-07-25 17:10:21 -0700 | [diff] [blame] | 96 | private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 97 | private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)"; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 98 | |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 99 | private final int mNotificationId; |
| 100 | private final NotificationManager mNotificationManager; |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 101 | private final Notification.Builder mNotificationBuilder, mPublicNotificationBuilder; |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 102 | private final File mScreenshotDir; |
| 103 | private final String mImageFileName; |
| 104 | private final String mImageFilePath; |
| 105 | private final long mImageTime; |
| 106 | private final BigPictureStyle mNotificationStyle; |
| 107 | private final int mImageWidth; |
| 108 | private final int mImageHeight; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 109 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 110 | // WORKAROUND: We want the same notification across screenshots that we update so that we don't |
| 111 | // spam a user's notification drawer. However, we only show the ticker for the saving state |
| 112 | // and if the ticker text is the same as the previous notification, then it will not show. So |
| 113 | // for now, we just add and remove a space from the ticker text to trigger the animation when |
| 114 | // necessary. |
| 115 | private static boolean mTickerAddSpace; |
| 116 | |
| 117 | SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data, |
| 118 | NotificationManager nManager, int nId) { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 119 | Resources r = context.getResources(); |
| 120 | |
| 121 | // Prepare all the output metadata |
| 122 | mImageTime = System.currentTimeMillis(); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 123 | String imageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime)); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 124 | mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate); |
Jeff Sharkey | 149e02c | 2013-01-16 14:38:49 -0800 | [diff] [blame] | 125 | |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 126 | mScreenshotDir = new File(Environment.getExternalStoragePublicDirectory( |
Jeff Sharkey | 149e02c | 2013-01-16 14:38:49 -0800 | [diff] [blame] | 127 | Environment.DIRECTORY_PICTURES), SCREENSHOTS_DIR_NAME); |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 128 | mImageFilePath = new File(mScreenshotDir, mImageFileName).getAbsolutePath(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 129 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 130 | // Create the large notification icon |
Doris Liu | 5aa011b | 2012-11-06 16:29:46 -0800 | [diff] [blame] | 131 | mImageWidth = data.image.getWidth(); |
| 132 | mImageHeight = data.image.getHeight(); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 133 | int iconSize = data.iconSize; |
| 134 | |
Doris Liu | 5aa011b | 2012-11-06 16:29:46 -0800 | [diff] [blame] | 135 | final int shortSide = mImageWidth < mImageHeight ? mImageWidth : mImageHeight; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 136 | Bitmap preview = Bitmap.createBitmap(shortSide, shortSide, data.image.getConfig()); |
| 137 | Canvas c = new Canvas(preview); |
| 138 | Paint paint = new Paint(); |
| 139 | ColorMatrix desat = new ColorMatrix(); |
| 140 | desat.setSaturation(0.25f); |
| 141 | paint.setColorFilter(new ColorMatrixColorFilter(desat)); |
| 142 | Matrix matrix = new Matrix(); |
Doris Liu | 5aa011b | 2012-11-06 16:29:46 -0800 | [diff] [blame] | 143 | matrix.postTranslate((shortSide - mImageWidth) / 2, |
| 144 | (shortSide - mImageHeight) / 2); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 145 | c.drawBitmap(data.image, matrix, paint); |
| 146 | c.drawColor(0x40FFFFFF); |
Winson Chung | 5cc9a31 | 2013-02-11 14:58:45 -0800 | [diff] [blame] | 147 | c.setBitmap(null); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 148 | |
| 149 | Bitmap croppedIcon = Bitmap.createScaledBitmap(preview, iconSize, iconSize, true); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 150 | |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 151 | // Show the intermediate notification |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 152 | mTickerAddSpace = !mTickerAddSpace; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 153 | mNotificationId = nId; |
Winson Chung | 9c99aee | 2011-11-14 14:12:54 -0800 | [diff] [blame] | 154 | mNotificationManager = nManager; |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 155 | final long now = System.currentTimeMillis(); |
| 156 | |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 157 | mNotificationBuilder = new Notification.Builder(context) |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 158 | .setTicker(r.getString(R.string.screenshot_saving_ticker) |
| 159 | + (mTickerAddSpace ? " " : "")) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 160 | .setContentTitle(r.getString(R.string.screenshot_saving_title)) |
| 161 | .setContentText(r.getString(R.string.screenshot_saving_text)) |
Winson Chung | b787a75 | 2011-10-19 18:31:53 -0700 | [diff] [blame] | 162 | .setSmallIcon(R.drawable.stat_notify_image) |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 163 | .setWhen(now); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 164 | |
| 165 | mNotificationStyle = new Notification.BigPictureStyle() |
| 166 | .bigPicture(preview); |
| 167 | mNotificationBuilder.setStyle(mNotificationStyle); |
| 168 | |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 169 | // For "public" situations we want to show all the same info but |
| 170 | // omit the actual screenshot image. |
| 171 | mPublicNotificationBuilder = new Notification.Builder(context) |
| 172 | .setContentTitle(r.getString(R.string.screenshot_saving_title)) |
| 173 | .setContentText(r.getString(R.string.screenshot_saving_text)) |
| 174 | .setSmallIcon(R.drawable.stat_notify_image) |
| 175 | .setWhen(now); |
| 176 | |
| 177 | mNotificationBuilder.setPublicVersion(mPublicNotificationBuilder.build()); |
| 178 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 179 | Notification n = mNotificationBuilder.build(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 180 | n.flags |= Notification.FLAG_NO_CLEAR; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 181 | mNotificationManager.notify(nId, n); |
Winson Chung | 9c99aee | 2011-11-14 14:12:54 -0800 | [diff] [blame] | 182 | |
| 183 | // On the tablet, the large icon makes the notification appear as if it is clickable (and |
| 184 | // on small devices, the large icon is not shown) so defer showing the large icon until |
| 185 | // we compose the final post-save notification below. |
| 186 | mNotificationBuilder.setLargeIcon(croppedIcon); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 187 | // But we still don't set it for the expanded view, allowing the smallIcon to show here. |
| 188 | mNotificationStyle.bigLargeIcon(null); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 191 | @Override |
| 192 | protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) { |
| 193 | if (params.length != 1) return null; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 194 | if (isCancelled()) { |
| 195 | params[0].clearImage(); |
Winson Chung | 3a5a774 | 2013-04-01 16:04:28 -0700 | [diff] [blame] | 196 | params[0].clearContext(); |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 197 | return null; |
| 198 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 199 | |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 200 | // By default, AsyncTask sets the worker thread to have background thread priority, so bump |
| 201 | // it back up so that we save a little quicker. |
| 202 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
| 203 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 204 | Context context = params[0].context; |
| 205 | Bitmap image = params[0].image; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 206 | Resources r = context.getResources(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 207 | |
Winson Chung | d859fa3 | 2011-07-22 12:19:52 -0700 | [diff] [blame] | 208 | try { |
Jeff Sharkey | 80b54de | 2013-01-16 15:07:37 -0800 | [diff] [blame] | 209 | // Create screenshot directory if it doesn't exist |
| 210 | mScreenshotDir.mkdirs(); |
| 211 | |
Marco Nelissen | 9b64ca7 | 2013-08-21 13:13:29 -0700 | [diff] [blame] | 212 | // media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds |
| 213 | // for DATE_TAKEN |
Mike Lockwoood | 4761124 | 2013-05-02 09:24:37 -0700 | [diff] [blame] | 214 | long dateSeconds = mImageTime / 1000; |
| 215 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 216 | // Save the screenshot to the MediaStore |
| 217 | ContentValues values = new ContentValues(); |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 218 | ContentResolver resolver = context.getContentResolver(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 219 | values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath); |
| 220 | values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName); |
| 221 | values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName); |
Marco Nelissen | 9b64ca7 | 2013-08-21 13:13:29 -0700 | [diff] [blame] | 222 | values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime); |
Mike Lockwoood | 4761124 | 2013-05-02 09:24:37 -0700 | [diff] [blame] | 223 | values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds); |
| 224 | values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 225 | values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png"); |
Doris Liu | 5aa011b | 2012-11-06 16:29:46 -0800 | [diff] [blame] | 226 | values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth); |
| 227 | values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight); |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 228 | Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 229 | |
Victoria Lease | e45e1f2 | 2013-09-30 15:33:43 -0700 | [diff] [blame] | 230 | String subjectDate = DateFormat.getDateTimeInstance().format(new Date(mImageTime)); |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 231 | String subject = String.format(SCREENSHOT_SHARE_SUBJECT_TEMPLATE, subjectDate); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 232 | Intent sharingIntent = new Intent(Intent.ACTION_SEND); |
| 233 | sharingIntent.setType("image/png"); |
| 234 | sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 235 | sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject); |
Daniel Sandler | 046fddf | 2012-05-31 15:40:25 -0400 | [diff] [blame] | 236 | |
| 237 | Intent chooserIntent = Intent.createChooser(sharingIntent, null); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 238 | chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
Daniel Sandler | 046fddf | 2012-05-31 15:40:25 -0400 | [diff] [blame] | 239 | | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 240 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 241 | mNotificationBuilder.addAction(R.drawable.ic_menu_share, |
| 242 | r.getString(com.android.internal.R.string.share), |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 243 | PendingIntent.getActivity(context, 0, chooserIntent, |
Daniel Sandler | 046fddf | 2012-05-31 15:40:25 -0400 | [diff] [blame] | 244 | PendingIntent.FLAG_CANCEL_CURRENT)); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 245 | |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 246 | OutputStream out = resolver.openOutputStream(uri); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 247 | image.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 248 | out.flush(); |
| 249 | out.close(); |
| 250 | |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 251 | // update file size in the database |
| 252 | values.clear(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 253 | values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length()); |
Mike Lockwood | 9837734 | 2011-07-26 13:55:16 -0400 | [diff] [blame] | 254 | resolver.update(uri, values, null, null); |
| 255 | |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 256 | params[0].imageUri = uri; |
Winson Chung | cd6a9ef | 2013-01-16 14:59:18 -0800 | [diff] [blame] | 257 | params[0].image = null; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 258 | params[0].result = 0; |
Winson Chung | d859fa3 | 2011-07-22 12:19:52 -0700 | [diff] [blame] | 259 | } catch (Exception e) { |
| 260 | // IOException/UnsupportedOperationException may be thrown if external storage is not |
| 261 | // mounted |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 262 | params[0].clearImage(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 263 | params[0].result = 1; |
| 264 | } |
| 265 | |
Winson Chung | cd6a9ef | 2013-01-16 14:59:18 -0800 | [diff] [blame] | 266 | // Recycle the bitmap data |
| 267 | if (image != null) { |
| 268 | image.recycle(); |
| 269 | } |
| 270 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 271 | return params[0]; |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 272 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 273 | |
| 274 | @Override |
| 275 | protected void onPostExecute(SaveImageInBackgroundData params) { |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 276 | if (isCancelled()) { |
| 277 | params.finisher.run(); |
| 278 | params.clearImage(); |
Winson Chung | 3a5a774 | 2013-04-01 16:04:28 -0700 | [diff] [blame] | 279 | params.clearContext(); |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 283 | if (params.result > 0) { |
| 284 | // Show a message that we've failed to save the image to disk |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 285 | GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 286 | } else { |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 287 | // Show the final notification to indicate screenshot saved |
| 288 | Resources r = params.context.getResources(); |
| 289 | |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 290 | // Create the intent to show the screenshot in gallery |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 291 | Intent launchIntent = new Intent(Intent.ACTION_VIEW); |
| 292 | launchIntent.setDataAndType(params.imageUri, "image/png"); |
| 293 | launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Winson Chung | 36c9e29 | 2011-10-18 13:48:38 -0700 | [diff] [blame] | 294 | |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 295 | final long now = System.currentTimeMillis(); |
| 296 | |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 297 | mNotificationBuilder |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 298 | .setContentTitle(r.getString(R.string.screenshot_saved_title)) |
| 299 | .setContentText(r.getString(R.string.screenshot_saved_text)) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 300 | .setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0)) |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 301 | .setWhen(now) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 302 | .setAutoCancel(true); |
| 303 | |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 304 | // Update the text in the public version as well |
| 305 | mPublicNotificationBuilder |
| 306 | .setContentTitle(r.getString(R.string.screenshot_saved_title)) |
| 307 | .setContentText(r.getString(R.string.screenshot_saved_text)) |
| 308 | .setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0)) |
| 309 | .setWhen(now) |
| 310 | .setAutoCancel(true); |
| 311 | |
| 312 | mNotificationBuilder.setPublicVersion(mPublicNotificationBuilder.build()); |
| 313 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 314 | Notification n = mNotificationBuilder.build(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 315 | n.flags &= ~Notification.FLAG_NO_CLEAR; |
| 316 | mNotificationManager.notify(mNotificationId, n); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 317 | } |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 318 | params.finisher.run(); |
Winson Chung | 3a5a774 | 2013-04-01 16:04:28 -0700 | [diff] [blame] | 319 | params.clearContext(); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 320 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * TODO: |
| 325 | * - Performance when over gl surfaces? Ie. Gallery |
| 326 | * - what do we say in the Toast? Which icon do we get if the user uses another |
| 327 | * type of gallery? |
| 328 | */ |
| 329 | class GlobalScreenshot { |
Winson Chung | 5cc9a31 | 2013-02-11 14:58:45 -0800 | [diff] [blame] | 330 | private static final String TAG = "GlobalScreenshot"; |
| 331 | |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 332 | private static final int SCREENSHOT_NOTIFICATION_ID = 789; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 333 | private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130; |
| 334 | private static final int SCREENSHOT_DROP_IN_DURATION = 430; |
| 335 | private static final int SCREENSHOT_DROP_OUT_DELAY = 500; |
| 336 | private static final int SCREENSHOT_DROP_OUT_DURATION = 430; |
| 337 | private static final int SCREENSHOT_DROP_OUT_SCALE_DURATION = 370; |
| 338 | private static final int SCREENSHOT_FAST_DROP_OUT_DURATION = 320; |
| 339 | private static final float BACKGROUND_ALPHA = 0.5f; |
| 340 | private static final float SCREENSHOT_SCALE = 1f; |
| 341 | private static final float SCREENSHOT_DROP_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.725f; |
| 342 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.45f; |
| 343 | private static final float SCREENSHOT_FAST_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.6f; |
| 344 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET = 0f; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 345 | |
| 346 | private Context mContext; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 347 | private WindowManager mWindowManager; |
| 348 | private WindowManager.LayoutParams mWindowLayoutParams; |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 349 | private NotificationManager mNotificationManager; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 350 | private Display mDisplay; |
| 351 | private DisplayMetrics mDisplayMetrics; |
| 352 | private Matrix mDisplayMatrix; |
| 353 | |
| 354 | private Bitmap mScreenBitmap; |
| 355 | private View mScreenshotLayout; |
| 356 | private ImageView mBackgroundView; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 357 | private ImageView mScreenshotView; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 358 | private ImageView mScreenshotFlash; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 359 | |
| 360 | private AnimatorSet mScreenshotAnimation; |
| 361 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 362 | private int mNotificationIconSize; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 363 | private float mBgPadding; |
| 364 | private float mBgPaddingScale; |
| 365 | |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 366 | private AsyncTask<SaveImageInBackgroundData, Void, SaveImageInBackgroundData> mSaveInBgTask; |
| 367 | |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 368 | private MediaActionSound mCameraSound; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 369 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * @param context everything needs a context :( |
| 373 | */ |
| 374 | public GlobalScreenshot(Context context) { |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 375 | Resources r = context.getResources(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 376 | mContext = context; |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 377 | LayoutInflater layoutInflater = (LayoutInflater) |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 378 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 379 | |
| 380 | // Inflate the screenshot layout |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 381 | mDisplayMatrix = new Matrix(); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 382 | mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 383 | mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 384 | mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 385 | mScreenshotFlash = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_flash); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 386 | mScreenshotLayout.setFocusable(true); |
| 387 | mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() { |
| 388 | @Override |
| 389 | public boolean onTouch(View v, MotionEvent event) { |
| 390 | // Intercept and ignore all touch events |
| 391 | return true; |
| 392 | } |
| 393 | }); |
| 394 | |
| 395 | // Setup the window that we are going to use |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 396 | mWindowLayoutParams = new WindowManager.LayoutParams( |
| 397 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, |
| 398 | WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY, |
| 399 | WindowManager.LayoutParams.FLAG_FULLSCREEN |
| 400 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 401 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 402 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, |
| 403 | PixelFormat.TRANSLUCENT); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 404 | mWindowLayoutParams.setTitle("ScreenshotAnimation"); |
| 405 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 406 | mNotificationManager = |
| 407 | (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 408 | mDisplay = mWindowManager.getDefaultDisplay(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 409 | mDisplayMetrics = new DisplayMetrics(); |
| 410 | mDisplay.getRealMetrics(mDisplayMetrics); |
| 411 | |
| 412 | // Get the various target sizes |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 413 | mNotificationIconSize = |
| 414 | r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 415 | |
| 416 | // Scale has to account for both sides of the bg |
| 417 | mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 418 | mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels; |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 419 | |
| 420 | // Setup the Camera shutter sound |
Eino-Ville Talvala | e690958 | 2012-03-01 11:01:38 -0800 | [diff] [blame] | 421 | mCameraSound = new MediaActionSound(); |
| 422 | mCameraSound.load(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Creates a new worker thread and saves the screenshot to the media store. |
| 427 | */ |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 428 | private void saveScreenshotInWorkerThread(Runnable finisher) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 429 | SaveImageInBackgroundData data = new SaveImageInBackgroundData(); |
| 430 | data.context = mContext; |
| 431 | data.image = mScreenBitmap; |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 432 | data.iconSize = mNotificationIconSize; |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 433 | data.finisher = finisher; |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 434 | if (mSaveInBgTask != null) { |
| 435 | mSaveInBgTask.cancel(false); |
| 436 | } |
| 437 | mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 438 | SCREENSHOT_NOTIFICATION_ID).execute(data); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @return the current display rotation in degrees |
| 443 | */ |
| 444 | private float getDegreesForRotation(int value) { |
| 445 | switch (value) { |
| 446 | case Surface.ROTATION_90: |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 447 | return 360f - 90f; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 448 | case Surface.ROTATION_180: |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 449 | return 360f - 180f; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 450 | case Surface.ROTATION_270: |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 451 | return 360f - 270f; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 452 | } |
| 453 | return 0f; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Takes a screenshot of the current display and shows an animation. |
| 458 | */ |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 459 | void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 460 | // We need to orient the screenshot correctly (and the Surface api seems to take screenshots |
| 461 | // only in the natural orientation of the device :!) |
| 462 | mDisplay.getRealMetrics(mDisplayMetrics); |
| 463 | float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; |
| 464 | float degrees = getDegreesForRotation(mDisplay.getRotation()); |
| 465 | boolean requiresRotation = (degrees > 0); |
| 466 | if (requiresRotation) { |
| 467 | // Get the dimensions of the device in its native orientation |
| 468 | mDisplayMatrix.reset(); |
| 469 | mDisplayMatrix.preRotate(-degrees); |
| 470 | mDisplayMatrix.mapPoints(dims); |
| 471 | dims[0] = Math.abs(dims[0]); |
| 472 | dims[1] = Math.abs(dims[1]); |
| 473 | } |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 474 | |
| 475 | // Take the screenshot |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 476 | mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]); |
Winson Chung | a46d778 | 2012-01-04 16:43:10 -0800 | [diff] [blame] | 477 | if (mScreenBitmap == null) { |
| 478 | notifyScreenshotError(mContext, mNotificationManager); |
| 479 | finisher.run(); |
| 480 | return; |
| 481 | } |
| 482 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 483 | if (requiresRotation) { |
| 484 | // Rotate the screenshot to the current orientation |
| 485 | Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, |
| 486 | mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); |
| 487 | Canvas c = new Canvas(ss); |
| 488 | c.translate(ss.getWidth() / 2, ss.getHeight() / 2); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 489 | c.rotate(degrees); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 490 | c.translate(-dims[0] / 2, -dims[1] / 2); |
| 491 | c.drawBitmap(mScreenBitmap, 0, 0, null); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 492 | c.setBitmap(null); |
Winson Chung | 5cc9a31 | 2013-02-11 14:58:45 -0800 | [diff] [blame] | 493 | // Recycle the previous bitmap |
| 494 | mScreenBitmap.recycle(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 495 | mScreenBitmap = ss; |
| 496 | } |
| 497 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 498 | // Optimizations |
| 499 | mScreenBitmap.setHasAlpha(false); |
| 500 | mScreenBitmap.prepareToDraw(); |
| 501 | |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 502 | // Start the post-screenshot animation |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 503 | startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, |
| 504 | statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | /** |
| 509 | * Starts the animation after taking the screenshot |
| 510 | */ |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 511 | private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible, |
| 512 | boolean navBarVisible) { |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 513 | // Add the view for the animation |
| 514 | mScreenshotView.setImageBitmap(mScreenBitmap); |
| 515 | mScreenshotLayout.requestFocus(); |
| 516 | |
| 517 | // Setup the animation with the screenshot just taken |
| 518 | if (mScreenshotAnimation != null) { |
| 519 | mScreenshotAnimation.end(); |
Winson Chung | 0e6232c | 2013-01-22 14:11:46 -0800 | [diff] [blame] | 520 | mScreenshotAnimation.removeAllListeners(); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 524 | ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation(); |
| 525 | ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 526 | statusBarVisible, navBarVisible); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 527 | mScreenshotAnimation = new AnimatorSet(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 528 | mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 529 | mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { |
| 530 | @Override |
| 531 | public void onAnimationEnd(Animator animation) { |
| 532 | // Save the screenshot once we have a bit of time now |
Dianne Hackborn | fc8fa63 | 2011-08-17 16:20:47 -0700 | [diff] [blame] | 533 | saveScreenshotInWorkerThread(finisher); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 534 | mWindowManager.removeView(mScreenshotLayout); |
Winson Chung | cd6a9ef | 2013-01-16 14:59:18 -0800 | [diff] [blame] | 535 | |
| 536 | // Clear any references to the bitmap |
| 537 | mScreenBitmap = null; |
| 538 | mScreenshotView.setImageBitmap(null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 539 | } |
| 540 | }); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 541 | mScreenshotLayout.post(new Runnable() { |
| 542 | @Override |
| 543 | public void run() { |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 544 | // 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] | 545 | mCameraSound.play(MediaActionSound.SHUTTER_CLICK); |
Winson Chung | 8d513ea | 2011-12-01 14:39:12 -0800 | [diff] [blame] | 546 | |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 547 | mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 548 | mScreenshotView.buildLayer(); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 549 | mScreenshotAnimation.start(); |
| 550 | } |
| 551 | }); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 552 | } |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 553 | private ValueAnimator createScreenshotDropInAnimation() { |
| 554 | final float flashPeakDurationPct = ((float) (SCREENSHOT_FLASH_TO_PEAK_DURATION) |
| 555 | / SCREENSHOT_DROP_IN_DURATION); |
| 556 | final float flashDurationPct = 2f * flashPeakDurationPct; |
| 557 | final Interpolator flashAlphaInterpolator = new Interpolator() { |
| 558 | @Override |
| 559 | public float getInterpolation(float x) { |
| 560 | // Flash the flash view in and out quickly |
| 561 | if (x <= flashDurationPct) { |
| 562 | return (float) Math.sin(Math.PI * (x / flashDurationPct)); |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | }; |
| 567 | final Interpolator scaleInterpolator = new Interpolator() { |
| 568 | @Override |
| 569 | public float getInterpolation(float x) { |
| 570 | // We start scaling when the flash is at it's peak |
| 571 | if (x < flashPeakDurationPct) { |
| 572 | return 0; |
| 573 | } |
| 574 | return (x - flashDurationPct) / (1f - flashDurationPct); |
| 575 | } |
| 576 | }; |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 577 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 578 | anim.setDuration(SCREENSHOT_DROP_IN_DURATION); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 579 | anim.addListener(new AnimatorListenerAdapter() { |
| 580 | @Override |
| 581 | public void onAnimationStart(Animator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 582 | mBackgroundView.setAlpha(0f); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 583 | mBackgroundView.setVisibility(View.VISIBLE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 584 | mScreenshotView.setAlpha(0f); |
| 585 | mScreenshotView.setTranslationX(0f); |
| 586 | mScreenshotView.setTranslationY(0f); |
| 587 | mScreenshotView.setScaleX(SCREENSHOT_SCALE + mBgPaddingScale); |
| 588 | mScreenshotView.setScaleY(SCREENSHOT_SCALE + mBgPaddingScale); |
| 589 | mScreenshotView.setVisibility(View.VISIBLE); |
| 590 | mScreenshotFlash.setAlpha(0f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 591 | mScreenshotFlash.setVisibility(View.VISIBLE); |
| 592 | } |
| 593 | @Override |
| 594 | public void onAnimationEnd(android.animation.Animator animation) { |
| 595 | mScreenshotFlash.setVisibility(View.GONE); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 596 | } |
| 597 | }); |
| 598 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 599 | @Override |
| 600 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 601 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 602 | float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 603 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 604 | * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 605 | mBackgroundView.setAlpha(scaleInterpolator.getInterpolation(t) * BACKGROUND_ALPHA); |
| 606 | mScreenshotView.setAlpha(t); |
| 607 | mScreenshotView.setScaleX(scaleT); |
| 608 | mScreenshotView.setScaleY(scaleT); |
| 609 | mScreenshotFlash.setAlpha(flashAlphaInterpolator.getInterpolation(t)); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 610 | } |
| 611 | }); |
| 612 | return anim; |
| 613 | } |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 614 | private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible, |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 615 | boolean navBarVisible) { |
| 616 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 617 | anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 618 | anim.addListener(new AnimatorListenerAdapter() { |
| 619 | @Override |
| 620 | public void onAnimationEnd(Animator animation) { |
| 621 | mBackgroundView.setVisibility(View.GONE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 622 | mScreenshotView.setVisibility(View.GONE); |
| 623 | mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null); |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 624 | } |
| 625 | }); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 626 | |
| 627 | if (!statusBarVisible || !navBarVisible) { |
| 628 | // 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] | 629 | anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 630 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 631 | @Override |
| 632 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 633 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 634 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 635 | - t * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_FAST_DROP_OUT_MIN_SCALE); |
| 636 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 637 | mScreenshotView.setAlpha(1f - t); |
| 638 | mScreenshotView.setScaleX(scaleT); |
| 639 | mScreenshotView.setScaleY(scaleT); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 640 | } |
| 641 | }); |
| 642 | } else { |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 643 | // In the case where there is a status bar, animate to the origin of the bar (top-left) |
| 644 | final float scaleDurationPct = (float) SCREENSHOT_DROP_OUT_SCALE_DURATION |
| 645 | / 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 | |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 657 | // Determine the bounds of how to scale |
| 658 | float halfScreenWidth = (w - 2f * mBgPadding) / 2f; |
| 659 | float halfScreenHeight = (h - 2f * mBgPadding) / 2f; |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 660 | final float offsetPct = SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET; |
| 661 | final PointF finalPos = new PointF( |
| 662 | -halfScreenWidth + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenWidth, |
| 663 | -halfScreenHeight + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenHeight); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 664 | |
| 665 | // Animate the screenshot to the status bar |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 666 | anim.setDuration(SCREENSHOT_DROP_OUT_DURATION); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 667 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 668 | @Override |
| 669 | public void onAnimationUpdate(ValueAnimator animation) { |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 670 | float t = (Float) animation.getAnimatedValue(); |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 671 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 672 | - scaleInterpolator.getInterpolation(t) |
Winson Chung | 22ca095 | 2011-10-20 19:44:32 -0700 | [diff] [blame] | 673 | * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE); |
Romain Guy | 8279acb | 2011-11-29 13:56:25 -0800 | [diff] [blame] | 674 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); |
| 675 | mScreenshotView.setAlpha(1f - scaleInterpolator.getInterpolation(t)); |
| 676 | mScreenshotView.setScaleX(scaleT); |
| 677 | mScreenshotView.setScaleY(scaleT); |
| 678 | mScreenshotView.setTranslationX(t * finalPos.x); |
| 679 | mScreenshotView.setTranslationY(t * finalPos.y); |
Winson Chung | a63bb84 | 2011-10-17 10:26:28 -0700 | [diff] [blame] | 680 | } |
| 681 | }); |
| 682 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 683 | return anim; |
| 684 | } |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 685 | |
| 686 | static void notifyScreenshotError(Context context, NotificationManager nManager) { |
| 687 | Resources r = context.getResources(); |
| 688 | |
| 689 | // Clear all existing notification, compose the new notification and show it |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 690 | Notification.Builder b = new Notification.Builder(context) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 691 | .setTicker(r.getString(R.string.screenshot_failed_title)) |
| 692 | .setContentTitle(r.getString(R.string.screenshot_failed_title)) |
| 693 | .setContentText(r.getString(R.string.screenshot_failed_text)) |
Winson Chung | b787a75 | 2011-10-19 18:31:53 -0700 | [diff] [blame] | 694 | .setSmallIcon(R.drawable.stat_notify_image_error) |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 695 | .setWhen(System.currentTimeMillis()) |
Dan Sandler | 156e97f | 2014-01-30 11:14:07 -0500 | [diff] [blame] | 696 | .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen |
Winson Chung | 224848f | 2012-07-18 16:47:51 -0700 | [diff] [blame] | 697 | .setAutoCancel(true); |
| 698 | Notification n = |
| 699 | new Notification.BigTextStyle(b) |
| 700 | .bigText(r.getString(R.string.screenshot_failed_text)) |
| 701 | .build(); |
Winson Chung | c57ccf0 | 2011-10-13 15:04:59 -0700 | [diff] [blame] | 702 | nManager.notify(SCREENSHOT_NOTIFICATION_ID, n); |
| 703 | } |
Winson Chung | 9112ec3 | 2011-06-27 13:15:32 -0700 | [diff] [blame] | 704 | } |