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