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