blob: 5041617fb718e249d60fa2e06f4a61f5437ba71b [file] [log] [blame]
Winson Chung9112ec32011-06-27 13:15:32 -07001/*
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
17package com.android.systemui.screenshot;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
Winson Chung9112ec32011-06-27 13:15:32 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungc57ccf02011-10-13 15:04:59 -070024import android.app.Notification;
Chris Wren3745a3d2012-05-22 15:11:52 -040025import android.app.Notification.BigPictureStyle;
Winson Chungc57ccf02011-10-13 15:04:59 -070026import android.app.NotificationManager;
27import android.app.PendingIntent;
Mike Lockwood98377342011-07-26 13:55:16 -040028import android.content.ContentResolver;
Winson Chung9112ec32011-06-27 13:15:32 -070029import android.content.ContentValues;
30import android.content.Context;
Winson Chungc57ccf02011-10-13 15:04:59 -070031import android.content.Intent;
32import android.content.res.Resources;
Winson Chung9112ec32011-06-27 13:15:32 -070033import android.graphics.Bitmap;
34import android.graphics.Canvas;
Chris Wren3745a3d2012-05-22 15:11:52 -040035import android.graphics.ColorMatrix;
36import android.graphics.ColorMatrixColorFilter;
Winson Chung9112ec32011-06-27 13:15:32 -070037import android.graphics.Matrix;
Chris Wren3745a3d2012-05-22 15:11:52 -040038import android.graphics.Paint;
Winson Chung9112ec32011-06-27 13:15:32 -070039import android.graphics.PixelFormat;
Winson Chunga63bb842011-10-17 10:26:28 -070040import android.graphics.PointF;
Eino-Ville Talvalae6909582012-03-01 11:01:38 -080041import android.media.MediaActionSound;
Winson Chung9112ec32011-06-27 13:15:32 -070042import android.net.Uri;
43import android.os.AsyncTask;
Winson Chung9112ec32011-06-27 13:15:32 -070044import android.os.Environment;
Winson Chung36c9e292011-10-18 13:48:38 -070045import android.os.Process;
Winson Chung9112ec32011-06-27 13:15:32 -070046import android.provider.MediaStore;
47import android.util.DisplayMetrics;
Winson Chung5cc9a312013-02-11 14:58:45 -080048import android.util.Log;
Winson Chung9112ec32011-06-27 13:15:32 -070049import android.view.Display;
Winson Chung9112ec32011-06-27 13:15:32 -070050import android.view.LayoutInflater;
51import android.view.MotionEvent;
52import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080053import android.view.SurfaceControl;
Winson Chung9112ec32011-06-27 13:15:32 -070054import android.view.View;
55import android.view.ViewGroup;
56import android.view.WindowManager;
Winson Chung22ca0952011-10-20 19:44:32 -070057import android.view.animation.Interpolator;
Winson Chung9112ec32011-06-27 13:15:32 -070058import android.widget.ImageView;
Winson Chung8d513ea2011-12-01 14:39:12 -080059
Winson Chung9112ec32011-06-27 13:15:32 -070060import com.android.systemui.R;
61
62import java.io.File;
Winson Chung9112ec32011-06-27 13:15:32 -070063import java.io.OutputStream;
Winson Chung9112ec32011-06-27 13:15:32 -070064import java.text.SimpleDateFormat;
65import java.util.Date;
66
67/**
68 * POD used in the AsyncTask which saves an image in the background.
69 */
70class SaveImageInBackgroundData {
71 Context context;
72 Bitmap image;
Winson Chung36c9e292011-10-18 13:48:38 -070073 Uri imageUri;
Dianne Hackbornfc8fa632011-08-17 16:20:47 -070074 Runnable finisher;
Winson Chunga63bb842011-10-17 10:26:28 -070075 int iconSize;
Winson Chung9112ec32011-06-27 13:15:32 -070076 int result;
Winson Chung0e6232c2013-01-22 14:11:46 -080077
78 void clearImage() {
Winson Chung0e6232c2013-01-22 14:11:46 -080079 image = null;
80 imageUri = null;
81 iconSize = 0;
82 }
Winson Chung3a5a7742013-04-01 16:04:28 -070083 void clearContext() {
84 context = null;
85 }
Winson Chung9112ec32011-06-27 13:15:32 -070086}
87
88/**
89 * An AsyncTask that saves an image to the media store in the background.
90 */
91class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void,
92 SaveImageInBackgroundData> {
Winson Chung5cc9a312013-02-11 14:58:45 -080093 private static final String TAG = "SaveImageInBackgroundTask";
94
Winson Chung9112ec32011-06-27 13:15:32 -070095 private static final String SCREENSHOTS_DIR_NAME = "Screenshots";
Winson Chung753e40b2011-07-25 17:10:21 -070096 private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png";
Winson Chung224848f2012-07-18 16:47:51 -070097 private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)";
Winson Chung9112ec32011-06-27 13:15:32 -070098
Jeff Sharkey80b54de2013-01-16 15:07:37 -080099 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 Chungc57ccf02011-10-13 15:04:59 -0700109
Winson Chunga63bb842011-10-17 10:26:28 -0700110 // 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 Chungc57ccf02011-10-13 15:04:59 -0700119 Resources r = context.getResources();
120
121 // Prepare all the output metadata
122 mImageTime = System.currentTimeMillis();
Romain Guy8279acb2011-11-29 13:56:25 -0800123 String imageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime));
Romain Guy8279acb2011-11-29 13:56:25 -0800124 mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate);
Jeff Sharkey149e02c2013-01-16 14:38:49 -0800125
Jeff Sharkey80b54de2013-01-16 15:07:37 -0800126 mScreenshotDir = new File(Environment.getExternalStoragePublicDirectory(
Jeff Sharkey149e02c2013-01-16 14:38:49 -0800127 Environment.DIRECTORY_PICTURES), SCREENSHOTS_DIR_NAME);
Jeff Sharkey80b54de2013-01-16 15:07:37 -0800128 mImageFilePath = new File(mScreenshotDir, mImageFileName).getAbsolutePath();
Winson Chungc57ccf02011-10-13 15:04:59 -0700129
Winson Chunga63bb842011-10-17 10:26:28 -0700130 // Create the large notification icon
Doris Liu5aa011b2012-11-06 16:29:46 -0800131 mImageWidth = data.image.getWidth();
132 mImageHeight = data.image.getHeight();
Chris Wren3745a3d2012-05-22 15:11:52 -0400133 int iconSize = data.iconSize;
134
Doris Liu5aa011b2012-11-06 16:29:46 -0800135 final int shortSide = mImageWidth < mImageHeight ? mImageWidth : mImageHeight;
Chris Wren3745a3d2012-05-22 15:11:52 -0400136 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 Liu5aa011b2012-11-06 16:29:46 -0800143 matrix.postTranslate((shortSide - mImageWidth) / 2,
144 (shortSide - mImageHeight) / 2);
Chris Wren3745a3d2012-05-22 15:11:52 -0400145 c.drawBitmap(data.image, matrix, paint);
146 c.drawColor(0x40FFFFFF);
Winson Chung5cc9a312013-02-11 14:58:45 -0800147 c.setBitmap(null);
Chris Wren3745a3d2012-05-22 15:11:52 -0400148
149 Bitmap croppedIcon = Bitmap.createScaledBitmap(preview, iconSize, iconSize, true);
Winson Chunga63bb842011-10-17 10:26:28 -0700150
Winson Chungc57ccf02011-10-13 15:04:59 -0700151 // Show the intermediate notification
Winson Chunga63bb842011-10-17 10:26:28 -0700152 mTickerAddSpace = !mTickerAddSpace;
Winson Chungc57ccf02011-10-13 15:04:59 -0700153 mNotificationId = nId;
Winson Chung9c99aee2011-11-14 14:12:54 -0800154 mNotificationManager = nManager;
Winson Chungc57ccf02011-10-13 15:04:59 -0700155 mNotificationBuilder = new Notification.Builder(context)
Winson Chunga63bb842011-10-17 10:26:28 -0700156 .setTicker(r.getString(R.string.screenshot_saving_ticker)
157 + (mTickerAddSpace ? " " : ""))
Winson Chungc57ccf02011-10-13 15:04:59 -0700158 .setContentTitle(r.getString(R.string.screenshot_saving_title))
159 .setContentText(r.getString(R.string.screenshot_saving_text))
Winson Chungb787a752011-10-19 18:31:53 -0700160 .setSmallIcon(R.drawable.stat_notify_image)
Winson Chungc57ccf02011-10-13 15:04:59 -0700161 .setWhen(System.currentTimeMillis());
Chris Wren3745a3d2012-05-22 15:11:52 -0400162
163 mNotificationStyle = new Notification.BigPictureStyle()
164 .bigPicture(preview);
165 mNotificationBuilder.setStyle(mNotificationStyle);
166
167 Notification n = mNotificationBuilder.build();
Winson Chungc57ccf02011-10-13 15:04:59 -0700168 n.flags |= Notification.FLAG_NO_CLEAR;
Winson Chungc57ccf02011-10-13 15:04:59 -0700169 mNotificationManager.notify(nId, n);
Winson Chung9c99aee2011-11-14 14:12:54 -0800170
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 Wren3745a3d2012-05-22 15:11:52 -0400175 // But we still don't set it for the expanded view, allowing the smallIcon to show here.
176 mNotificationStyle.bigLargeIcon(null);
Winson Chungc57ccf02011-10-13 15:04:59 -0700177 }
178
Winson Chung9112ec32011-06-27 13:15:32 -0700179 @Override
180 protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) {
181 if (params.length != 1) return null;
Winson Chung0e6232c2013-01-22 14:11:46 -0800182 if (isCancelled()) {
183 params[0].clearImage();
Winson Chung3a5a7742013-04-01 16:04:28 -0700184 params[0].clearContext();
Winson Chung0e6232c2013-01-22 14:11:46 -0800185 return null;
186 }
Winson Chung9112ec32011-06-27 13:15:32 -0700187
Winson Chung36c9e292011-10-18 13:48:38 -0700188 // 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 Chung9112ec32011-06-27 13:15:32 -0700192 Context context = params[0].context;
193 Bitmap image = params[0].image;
Chris Wren3745a3d2012-05-22 15:11:52 -0400194 Resources r = context.getResources();
Winson Chung9112ec32011-06-27 13:15:32 -0700195
Winson Chungd859fa32011-07-22 12:19:52 -0700196 try {
Jeff Sharkey80b54de2013-01-16 15:07:37 -0800197 // Create screenshot directory if it doesn't exist
198 mScreenshotDir.mkdirs();
199
Winson Chung9112ec32011-06-27 13:15:32 -0700200 // Save the screenshot to the MediaStore
201 ContentValues values = new ContentValues();
Mike Lockwood98377342011-07-26 13:55:16 -0400202 ContentResolver resolver = context.getContentResolver();
Winson Chungc57ccf02011-10-13 15:04:59 -0700203 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 Chung9112ec32011-06-27 13:15:32 -0700209 values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
Doris Liu5aa011b2012-11-06 16:29:46 -0800210 values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth);
211 values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight);
Mike Lockwood98377342011-07-26 13:55:16 -0400212 Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Winson Chung9112ec32011-06-27 13:15:32 -0700213
Winson Chung224848f2012-07-18 16:47:51 -0700214 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 Wren3745a3d2012-05-22 15:11:52 -0400217 Intent sharingIntent = new Intent(Intent.ACTION_SEND);
218 sharingIntent.setType("image/png");
219 sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
Winson Chung224848f2012-07-18 16:47:51 -0700220 sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
Daniel Sandler046fddf2012-05-31 15:40:25 -0400221
222 Intent chooserIntent = Intent.createChooser(sharingIntent, null);
Dianne Hackborn1927ae82012-06-22 15:21:36 -0700223 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
Daniel Sandler046fddf2012-05-31 15:40:25 -0400224 | Intent.FLAG_ACTIVITY_NEW_TASK);
225
Chris Wren3745a3d2012-05-22 15:11:52 -0400226 mNotificationBuilder.addAction(R.drawable.ic_menu_share,
227 r.getString(com.android.internal.R.string.share),
Daniel Sandler046fddf2012-05-31 15:40:25 -0400228 PendingIntent.getActivity(context, 0, chooserIntent,
229 PendingIntent.FLAG_CANCEL_CURRENT));
Chris Wren3745a3d2012-05-22 15:11:52 -0400230
Mike Lockwood98377342011-07-26 13:55:16 -0400231 OutputStream out = resolver.openOutputStream(uri);
Winson Chung9112ec32011-06-27 13:15:32 -0700232 image.compress(Bitmap.CompressFormat.PNG, 100, out);
233 out.flush();
234 out.close();
235
Mike Lockwood98377342011-07-26 13:55:16 -0400236 // update file size in the database
237 values.clear();
Winson Chungc57ccf02011-10-13 15:04:59 -0700238 values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
Mike Lockwood98377342011-07-26 13:55:16 -0400239 resolver.update(uri, values, null, null);
240
Winson Chung36c9e292011-10-18 13:48:38 -0700241 params[0].imageUri = uri;
Winson Chungcd6a9ef2013-01-16 14:59:18 -0800242 params[0].image = null;
Winson Chung9112ec32011-06-27 13:15:32 -0700243 params[0].result = 0;
Winson Chungd859fa32011-07-22 12:19:52 -0700244 } catch (Exception e) {
245 // IOException/UnsupportedOperationException may be thrown if external storage is not
246 // mounted
Winson Chung0e6232c2013-01-22 14:11:46 -0800247 params[0].clearImage();
Winson Chung9112ec32011-06-27 13:15:32 -0700248 params[0].result = 1;
249 }
250
Winson Chungcd6a9ef2013-01-16 14:59:18 -0800251 // Recycle the bitmap data
252 if (image != null) {
253 image.recycle();
254 }
255
Winson Chung9112ec32011-06-27 13:15:32 -0700256 return params[0];
Romain Guy8279acb2011-11-29 13:56:25 -0800257 }
Winson Chung9112ec32011-06-27 13:15:32 -0700258
259 @Override
260 protected void onPostExecute(SaveImageInBackgroundData params) {
Winson Chung0e6232c2013-01-22 14:11:46 -0800261 if (isCancelled()) {
262 params.finisher.run();
263 params.clearImage();
Winson Chung3a5a7742013-04-01 16:04:28 -0700264 params.clearContext();
Winson Chung0e6232c2013-01-22 14:11:46 -0800265 return;
266 }
267
Winson Chung9112ec32011-06-27 13:15:32 -0700268 if (params.result > 0) {
269 // Show a message that we've failed to save the image to disk
Winson Chungc57ccf02011-10-13 15:04:59 -0700270 GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager);
Winson Chung9112ec32011-06-27 13:15:32 -0700271 } else {
Winson Chungc57ccf02011-10-13 15:04:59 -0700272 // Show the final notification to indicate screenshot saved
273 Resources r = params.context.getResources();
274
Winson Chung36c9e292011-10-18 13:48:38 -0700275 // Create the intent to show the screenshot in gallery
Romain Guy8279acb2011-11-29 13:56:25 -0800276 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
277 launchIntent.setDataAndType(params.imageUri, "image/png");
278 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chung36c9e292011-10-18 13:48:38 -0700279
Winson Chungc57ccf02011-10-13 15:04:59 -0700280 mNotificationBuilder
Winson Chungc57ccf02011-10-13 15:04:59 -0700281 .setContentTitle(r.getString(R.string.screenshot_saved_title))
282 .setContentText(r.getString(R.string.screenshot_saved_text))
Romain Guy8279acb2011-11-29 13:56:25 -0800283 .setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0))
Winson Chungc57ccf02011-10-13 15:04:59 -0700284 .setWhen(System.currentTimeMillis())
285 .setAutoCancel(true);
286
Chris Wren3745a3d2012-05-22 15:11:52 -0400287 Notification n = mNotificationBuilder.build();
Winson Chungc57ccf02011-10-13 15:04:59 -0700288 n.flags &= ~Notification.FLAG_NO_CLEAR;
289 mNotificationManager.notify(mNotificationId, n);
Winson Chung9112ec32011-06-27 13:15:32 -0700290 }
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700291 params.finisher.run();
Winson Chung3a5a7742013-04-01 16:04:28 -0700292 params.clearContext();
Romain Guy8279acb2011-11-29 13:56:25 -0800293 }
Winson Chung9112ec32011-06-27 13:15:32 -0700294}
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 */
302class GlobalScreenshot {
Winson Chung5cc9a312013-02-11 14:58:45 -0800303 private static final String TAG = "GlobalScreenshot";
304
Winson Chungc57ccf02011-10-13 15:04:59 -0700305 private static final int SCREENSHOT_NOTIFICATION_ID = 789;
Winson Chung22ca0952011-10-20 19:44:32 -0700306 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 Chung9112ec32011-06-27 13:15:32 -0700318
319 private Context mContext;
Winson Chung9112ec32011-06-27 13:15:32 -0700320 private WindowManager mWindowManager;
321 private WindowManager.LayoutParams mWindowLayoutParams;
Winson Chungc57ccf02011-10-13 15:04:59 -0700322 private NotificationManager mNotificationManager;
Winson Chung9112ec32011-06-27 13:15:32 -0700323 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 Chung9112ec32011-06-27 13:15:32 -0700330 private ImageView mScreenshotView;
Winson Chung22ca0952011-10-20 19:44:32 -0700331 private ImageView mScreenshotFlash;
Winson Chung9112ec32011-06-27 13:15:32 -0700332
333 private AnimatorSet mScreenshotAnimation;
334
Winson Chunga63bb842011-10-17 10:26:28 -0700335 private int mNotificationIconSize;
Winson Chunga63bb842011-10-17 10:26:28 -0700336 private float mBgPadding;
337 private float mBgPaddingScale;
338
Winson Chung0e6232c2013-01-22 14:11:46 -0800339 private AsyncTask<SaveImageInBackgroundData, Void, SaveImageInBackgroundData> mSaveInBgTask;
340
Eino-Ville Talvalae6909582012-03-01 11:01:38 -0800341 private MediaActionSound mCameraSound;
Winson Chung8d513ea2011-12-01 14:39:12 -0800342
Winson Chung9112ec32011-06-27 13:15:32 -0700343
344 /**
345 * @param context everything needs a context :(
346 */
347 public GlobalScreenshot(Context context) {
Winson Chunga63bb842011-10-17 10:26:28 -0700348 Resources r = context.getResources();
Winson Chung9112ec32011-06-27 13:15:32 -0700349 mContext = context;
Romain Guy8279acb2011-11-29 13:56:25 -0800350 LayoutInflater layoutInflater = (LayoutInflater)
Winson Chung9112ec32011-06-27 13:15:32 -0700351 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
352
353 // Inflate the screenshot layout
Winson Chung9112ec32011-06-27 13:15:32 -0700354 mDisplayMatrix = new Matrix();
Romain Guy8279acb2011-11-29 13:56:25 -0800355 mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null);
Winson Chung9112ec32011-06-27 13:15:32 -0700356 mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background);
Winson Chung9112ec32011-06-27 13:15:32 -0700357 mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot);
Winson Chung22ca0952011-10-20 19:44:32 -0700358 mScreenshotFlash = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_flash);
Winson Chung9112ec32011-06-27 13:15:32 -0700359 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 Chung9112ec32011-06-27 13:15:32 -0700369 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 Chung9112ec32011-06-27 13:15:32 -0700374 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
375 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
376 PixelFormat.TRANSLUCENT);
Winson Chung9112ec32011-06-27 13:15:32 -0700377 mWindowLayoutParams.setTitle("ScreenshotAnimation");
378 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Winson Chungc57ccf02011-10-13 15:04:59 -0700379 mNotificationManager =
380 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Winson Chung9112ec32011-06-27 13:15:32 -0700381 mDisplay = mWindowManager.getDefaultDisplay();
Winson Chunga63bb842011-10-17 10:26:28 -0700382 mDisplayMetrics = new DisplayMetrics();
383 mDisplay.getRealMetrics(mDisplayMetrics);
384
385 // Get the various target sizes
Winson Chunga63bb842011-10-17 10:26:28 -0700386 mNotificationIconSize =
387 r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
Winson Chunga63bb842011-10-17 10:26:28 -0700388
389 // Scale has to account for both sides of the bg
390 mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding);
Winson Chung22ca0952011-10-20 19:44:32 -0700391 mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels;
Winson Chung8d513ea2011-12-01 14:39:12 -0800392
393 // Setup the Camera shutter sound
Eino-Ville Talvalae6909582012-03-01 11:01:38 -0800394 mCameraSound = new MediaActionSound();
395 mCameraSound.load(MediaActionSound.SHUTTER_CLICK);
Winson Chung9112ec32011-06-27 13:15:32 -0700396 }
397
398 /**
399 * Creates a new worker thread and saves the screenshot to the media store.
400 */
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700401 private void saveScreenshotInWorkerThread(Runnable finisher) {
Winson Chung9112ec32011-06-27 13:15:32 -0700402 SaveImageInBackgroundData data = new SaveImageInBackgroundData();
403 data.context = mContext;
404 data.image = mScreenBitmap;
Winson Chunga63bb842011-10-17 10:26:28 -0700405 data.iconSize = mNotificationIconSize;
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700406 data.finisher = finisher;
Winson Chung0e6232c2013-01-22 14:11:46 -0800407 if (mSaveInBgTask != null) {
408 mSaveInBgTask.cancel(false);
409 }
410 mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager,
Winson Chunga63bb842011-10-17 10:26:28 -0700411 SCREENSHOT_NOTIFICATION_ID).execute(data);
Winson Chung9112ec32011-06-27 13:15:32 -0700412 }
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 Chunga63bb842011-10-17 10:26:28 -0700420 return 360f - 90f;
Winson Chung9112ec32011-06-27 13:15:32 -0700421 case Surface.ROTATION_180:
Winson Chunga63bb842011-10-17 10:26:28 -0700422 return 360f - 180f;
Winson Chung9112ec32011-06-27 13:15:32 -0700423 case Surface.ROTATION_270:
Winson Chunga63bb842011-10-17 10:26:28 -0700424 return 360f - 270f;
Winson Chung9112ec32011-06-27 13:15:32 -0700425 }
426 return 0f;
427 }
428
429 /**
430 * Takes a screenshot of the current display and shows an animation.
431 */
Winson Chunga63bb842011-10-17 10:26:28 -0700432 void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {
Winson Chung9112ec32011-06-27 13:15:32 -0700433 // 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 Chunga46d7782012-01-04 16:43:10 -0800447
448 // Take the screenshot
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800449 mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
Winson Chunga46d7782012-01-04 16:43:10 -0800450 if (mScreenBitmap == null) {
451 notifyScreenshotError(mContext, mNotificationManager);
452 finisher.run();
453 return;
454 }
455
Winson Chung9112ec32011-06-27 13:15:32 -0700456 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 Chunga63bb842011-10-17 10:26:28 -0700462 c.rotate(degrees);
Winson Chung9112ec32011-06-27 13:15:32 -0700463 c.translate(-dims[0] / 2, -dims[1] / 2);
464 c.drawBitmap(mScreenBitmap, 0, 0, null);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -0700465 c.setBitmap(null);
Winson Chung5cc9a312013-02-11 14:58:45 -0800466 // Recycle the previous bitmap
467 mScreenBitmap.recycle();
Winson Chung9112ec32011-06-27 13:15:32 -0700468 mScreenBitmap = ss;
469 }
470
Winson Chunga63bb842011-10-17 10:26:28 -0700471 // Optimizations
472 mScreenBitmap.setHasAlpha(false);
473 mScreenBitmap.prepareToDraw();
474
Winson Chung9112ec32011-06-27 13:15:32 -0700475 // Start the post-screenshot animation
Winson Chunga63bb842011-10-17 10:26:28 -0700476 startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
477 statusBarVisible, navBarVisible);
Winson Chung9112ec32011-06-27 13:15:32 -0700478 }
479
480
481 /**
482 * Starts the animation after taking the screenshot
483 */
Winson Chunga63bb842011-10-17 10:26:28 -0700484 private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible,
485 boolean navBarVisible) {
Winson Chung9112ec32011-06-27 13:15:32 -0700486 // 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 Chung0e6232c2013-01-22 14:11:46 -0800493 mScreenshotAnimation.removeAllListeners();
Winson Chung9112ec32011-06-27 13:15:32 -0700494 }
495
496 mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
Winson Chung22ca0952011-10-20 19:44:32 -0700497 ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation();
498 ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h,
Winson Chunga63bb842011-10-17 10:26:28 -0700499 statusBarVisible, navBarVisible);
Winson Chung9112ec32011-06-27 13:15:32 -0700500 mScreenshotAnimation = new AnimatorSet();
Winson Chung22ca0952011-10-20 19:44:32 -0700501 mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim);
Winson Chung9112ec32011-06-27 13:15:32 -0700502 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 Hackbornfc8fa632011-08-17 16:20:47 -0700506 saveScreenshotInWorkerThread(finisher);
Winson Chung9112ec32011-06-27 13:15:32 -0700507 mWindowManager.removeView(mScreenshotLayout);
Winson Chungcd6a9ef2013-01-16 14:59:18 -0800508
509 // Clear any references to the bitmap
510 mScreenBitmap = null;
511 mScreenshotView.setImageBitmap(null);
Winson Chung9112ec32011-06-27 13:15:32 -0700512 }
513 });
Winson Chunga63bb842011-10-17 10:26:28 -0700514 mScreenshotLayout.post(new Runnable() {
515 @Override
516 public void run() {
Winson Chung8d513ea2011-12-01 14:39:12 -0800517 // Play the shutter sound to notify that we've taken a screenshot
Eino-Ville Talvalae6909582012-03-01 11:01:38 -0800518 mCameraSound.play(MediaActionSound.SHUTTER_CLICK);
Winson Chung8d513ea2011-12-01 14:39:12 -0800519
Romain Guy8279acb2011-11-29 13:56:25 -0800520 mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
521 mScreenshotView.buildLayer();
Winson Chunga63bb842011-10-17 10:26:28 -0700522 mScreenshotAnimation.start();
523 }
524 });
Winson Chung9112ec32011-06-27 13:15:32 -0700525 }
Winson Chung22ca0952011-10-20 19:44:32 -0700526 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 Chung9112ec32011-06-27 13:15:32 -0700550 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung22ca0952011-10-20 19:44:32 -0700551 anim.setDuration(SCREENSHOT_DROP_IN_DURATION);
Winson Chung9112ec32011-06-27 13:15:32 -0700552 anim.addListener(new AnimatorListenerAdapter() {
553 @Override
554 public void onAnimationStart(Animator animation) {
Romain Guy8279acb2011-11-29 13:56:25 -0800555 mBackgroundView.setAlpha(0f);
Winson Chung9112ec32011-06-27 13:15:32 -0700556 mBackgroundView.setVisibility(View.VISIBLE);
Romain Guy8279acb2011-11-29 13:56:25 -0800557 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 Chung22ca0952011-10-20 19:44:32 -0700564 mScreenshotFlash.setVisibility(View.VISIBLE);
565 }
566 @Override
567 public void onAnimationEnd(android.animation.Animator animation) {
568 mScreenshotFlash.setVisibility(View.GONE);
Winson Chung9112ec32011-06-27 13:15:32 -0700569 }
570 });
571 anim.addUpdateListener(new AnimatorUpdateListener() {
572 @Override
573 public void onAnimationUpdate(ValueAnimator animation) {
Romain Guy8279acb2011-11-29 13:56:25 -0800574 float t = (Float) animation.getAnimatedValue();
Winson Chung22ca0952011-10-20 19:44:32 -0700575 float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale)
Romain Guy8279acb2011-11-29 13:56:25 -0800576 - scaleInterpolator.getInterpolation(t)
Winson Chung22ca0952011-10-20 19:44:32 -0700577 * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE);
Romain Guy8279acb2011-11-29 13:56:25 -0800578 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 Chung9112ec32011-06-27 13:15:32 -0700583 }
584 });
585 return anim;
586 }
Winson Chung22ca0952011-10-20 19:44:32 -0700587 private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible,
Winson Chunga63bb842011-10-17 10:26:28 -0700588 boolean navBarVisible) {
589 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung22ca0952011-10-20 19:44:32 -0700590 anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY);
Winson Chung9112ec32011-06-27 13:15:32 -0700591 anim.addListener(new AnimatorListenerAdapter() {
592 @Override
593 public void onAnimationEnd(Animator animation) {
594 mBackgroundView.setVisibility(View.GONE);
Romain Guy8279acb2011-11-29 13:56:25 -0800595 mScreenshotView.setVisibility(View.GONE);
596 mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null);
Winson Chung9112ec32011-06-27 13:15:32 -0700597 }
598 });
Winson Chunga63bb842011-10-17 10:26:28 -0700599
600 if (!statusBarVisible || !navBarVisible) {
601 // There is no status bar/nav bar, so just fade the screenshot away in place
Winson Chung22ca0952011-10-20 19:44:32 -0700602 anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION);
Winson Chunga63bb842011-10-17 10:26:28 -0700603 anim.addUpdateListener(new AnimatorUpdateListener() {
604 @Override
605 public void onAnimationUpdate(ValueAnimator animation) {
Romain Guy8279acb2011-11-29 13:56:25 -0800606 float t = (Float) animation.getAnimatedValue();
Winson Chung22ca0952011-10-20 19:44:32 -0700607 float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale)
Romain Guy8279acb2011-11-29 13:56:25 -0800608 - 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 Chunga63bb842011-10-17 10:26:28 -0700613 }
614 });
615 } else {
Winson Chung22ca0952011-10-20 19:44:32 -0700616 // 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 Chunga63bb842011-10-17 10:26:28 -0700630 // Determine the bounds of how to scale
631 float halfScreenWidth = (w - 2f * mBgPadding) / 2f;
632 float halfScreenHeight = (h - 2f * mBgPadding) / 2f;
Winson Chung22ca0952011-10-20 19:44:32 -0700633 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 Chunga63bb842011-10-17 10:26:28 -0700637
638 // Animate the screenshot to the status bar
Winson Chung22ca0952011-10-20 19:44:32 -0700639 anim.setDuration(SCREENSHOT_DROP_OUT_DURATION);
Winson Chunga63bb842011-10-17 10:26:28 -0700640 anim.addUpdateListener(new AnimatorUpdateListener() {
641 @Override
642 public void onAnimationUpdate(ValueAnimator animation) {
Romain Guy8279acb2011-11-29 13:56:25 -0800643 float t = (Float) animation.getAnimatedValue();
Winson Chung22ca0952011-10-20 19:44:32 -0700644 float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale)
Romain Guy8279acb2011-11-29 13:56:25 -0800645 - scaleInterpolator.getInterpolation(t)
Winson Chung22ca0952011-10-20 19:44:32 -0700646 * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE);
Romain Guy8279acb2011-11-29 13:56:25 -0800647 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 Chunga63bb842011-10-17 10:26:28 -0700653 }
654 });
655 }
Winson Chung9112ec32011-06-27 13:15:32 -0700656 return anim;
657 }
Winson Chungc57ccf02011-10-13 15:04:59 -0700658
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 Chung224848f2012-07-18 16:47:51 -0700663 Notification.Builder b = new Notification.Builder(context)
Winson Chungc57ccf02011-10-13 15:04:59 -0700664 .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 Chungb787a752011-10-19 18:31:53 -0700667 .setSmallIcon(R.drawable.stat_notify_image_error)
Winson Chungc57ccf02011-10-13 15:04:59 -0700668 .setWhen(System.currentTimeMillis())
Winson Chung224848f2012-07-18 16:47:51 -0700669 .setAutoCancel(true);
670 Notification n =
671 new Notification.BigTextStyle(b)
672 .bigText(r.getString(R.string.screenshot_failed_text))
673 .build();
Winson Chungc57ccf02011-10-13 15:04:59 -0700674 nManager.notify(SCREENSHOT_NOTIFICATION_ID, n);
675 }
Winson Chung9112ec32011-06-27 13:15:32 -0700676}