GlobalScreenshot.java revision b787a751760de544f0d83bb7ddf9405ae6e4e481
19112ec3039dda4186c6f957981237c0691db2269Winson Chung/* 29112ec3039dda4186c6f957981237c0691db2269Winson Chung * Copyright (C) 2011 The Android Open Source Project 39112ec3039dda4186c6f957981237c0691db2269Winson Chung * 49112ec3039dda4186c6f957981237c0691db2269Winson Chung * Licensed under the Apache License, Version 2.0 (the "License"); 59112ec3039dda4186c6f957981237c0691db2269Winson Chung * you may not use this file except in compliance with the License. 69112ec3039dda4186c6f957981237c0691db2269Winson Chung * You may obtain a copy of the License at 79112ec3039dda4186c6f957981237c0691db2269Winson Chung * 89112ec3039dda4186c6f957981237c0691db2269Winson Chung * http://www.apache.org/licenses/LICENSE-2.0 99112ec3039dda4186c6f957981237c0691db2269Winson Chung * 109112ec3039dda4186c6f957981237c0691db2269Winson Chung * Unless required by applicable law or agreed to in writing, software 119112ec3039dda4186c6f957981237c0691db2269Winson Chung * distributed under the License is distributed on an "AS IS" BASIS, 129112ec3039dda4186c6f957981237c0691db2269Winson Chung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139112ec3039dda4186c6f957981237c0691db2269Winson Chung * See the License for the specific language governing permissions and 149112ec3039dda4186c6f957981237c0691db2269Winson Chung * limitations under the License. 159112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 169112ec3039dda4186c6f957981237c0691db2269Winson Chung 179112ec3039dda4186c6f957981237c0691db2269Winson Chungpackage com.android.systemui.screenshot; 189112ec3039dda4186c6f957981237c0691db2269Winson Chung 199112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.Animator; 209112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.AnimatorListenerAdapter; 219112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.AnimatorSet; 229112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator; 239112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator.AnimatorUpdateListener; 24c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.Notification; 25c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.NotificationManager; 26c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.PendingIntent; 279837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwoodimport android.content.ContentResolver; 289112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.ContentValues; 299112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.Context; 30c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.content.Intent; 31c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.content.res.Resources; 329112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Bitmap; 339112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Canvas; 349112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Matrix; 359112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.PixelFormat; 36a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.graphics.PointF; 37a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.graphics.RectF; 389112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.net.Uri; 399112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.AsyncTask; 409112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.Environment; 4136c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chungimport android.os.Process; 429112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.ServiceManager; 439112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.provider.MediaStore; 449112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.util.DisplayMetrics; 459112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Display; 469112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.IWindowManager; 479112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.LayoutInflater; 489112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.MotionEvent; 499112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Surface; 509112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.View; 519112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.ViewGroup; 529112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.WindowManager; 53a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.view.animation.AccelerateInterpolator; 54a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.view.animation.DecelerateInterpolator; 559112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.FrameLayout; 569112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.ImageView; 579112ec3039dda4186c6f957981237c0691db2269Winson Chung 589112ec3039dda4186c6f957981237c0691db2269Winson Chungimport com.android.systemui.R; 599112ec3039dda4186c6f957981237c0691db2269Winson Chung 609112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.File; 619112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.OutputStream; 629112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.text.SimpleDateFormat; 639112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.util.Date; 649112ec3039dda4186c6f957981237c0691db2269Winson Chung 659112ec3039dda4186c6f957981237c0691db2269Winson Chung/** 669112ec3039dda4186c6f957981237c0691db2269Winson Chung * POD used in the AsyncTask which saves an image in the background. 679112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 689112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundData { 699112ec3039dda4186c6f957981237c0691db2269Winson Chung Context context; 709112ec3039dda4186c6f957981237c0691db2269Winson Chung Bitmap image; 7136c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung Uri imageUri; 72fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn Runnable finisher; 73a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung int iconSize; 749112ec3039dda4186c6f957981237c0691db2269Winson Chung int result; 759112ec3039dda4186c6f957981237c0691db2269Winson Chung} 769112ec3039dda4186c6f957981237c0691db2269Winson Chung 779112ec3039dda4186c6f957981237c0691db2269Winson Chung/** 789112ec3039dda4186c6f957981237c0691db2269Winson Chung * An AsyncTask that saves an image to the media store in the background. 799112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 809112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void, 819112ec3039dda4186c6f957981237c0691db2269Winson Chung SaveImageInBackgroundData> { 829112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String TAG = "SaveImageInBackgroundTask"; 839112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; 84753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; 85753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung private static final String SCREENSHOT_FILE_PATH_TEMPLATE = "%s/%s/%s"; 869112ec3039dda4186c6f957981237c0691db2269Winson Chung 87c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private int mNotificationId; 88c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private NotificationManager mNotificationManager; 89c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private Notification.Builder mNotificationBuilder; 90c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private Intent mLaunchIntent; 91c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private String mImageDir; 92c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private String mImageFileName; 93c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private String mImageFilePath; 94c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private String mImageDate; 95c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private long mImageTime; 96c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 97a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // WORKAROUND: We want the same notification across screenshots that we update so that we don't 98a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // spam a user's notification drawer. However, we only show the ticker for the saving state 99a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // and if the ticker text is the same as the previous notification, then it will not show. So 100a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // for now, we just add and remove a space from the ticker text to trigger the animation when 101a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // necessary. 102a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static boolean mTickerAddSpace; 103a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 104a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data, 105a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung NotificationManager nManager, int nId) { 106c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Resources r = context.getResources(); 107c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 108c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung // Prepare all the output metadata 109c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mImageTime = System.currentTimeMillis(); 110c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mImageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime)); 111c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mImageDir = Environment.getExternalStoragePublicDirectory( 112c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Environment.DIRECTORY_PICTURES).getAbsolutePath(); 113c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, mImageDate); 114c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mImageFilePath = String.format(SCREENSHOT_FILE_PATH_TEMPLATE, mImageDir, 115c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung SCREENSHOTS_DIR_NAME, mImageFileName); 116c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 117a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Create the large notification icon 118a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung int imageWidth = data.image.getWidth(); 119a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung int imageHeight = data.image.getHeight(); 120a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung int iconWidth = data.iconSize; 121a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung int iconHeight = data.iconSize; 122a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung if (imageWidth > imageHeight) { 123a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung iconWidth = (int) (((float) iconHeight / imageHeight) * imageWidth); 124a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } else { 125a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung iconHeight = (int) (((float) iconWidth / imageWidth) * imageHeight); 126a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } 127a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung Bitmap rawIcon = Bitmap.createScaledBitmap(data.image, iconWidth, iconHeight, true); 128a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung Bitmap croppedIcon = Bitmap.createBitmap(rawIcon, (iconWidth - data.iconSize) / 2, 129a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung (iconHeight - data.iconSize) / 2, data.iconSize, data.iconSize); 130a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 131c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung // Show the intermediate notification 132a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mTickerAddSpace = !mTickerAddSpace; 133c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationId = nId; 134c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationBuilder = new Notification.Builder(context) 135a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung .setLargeIcon(croppedIcon) 136a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung .setTicker(r.getString(R.string.screenshot_saving_ticker) 137a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung + (mTickerAddSpace ? " " : "")) 138c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentTitle(r.getString(R.string.screenshot_saving_title)) 139c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentText(r.getString(R.string.screenshot_saving_text)) 140b787a751760de544f0d83bb7ddf9405ae6e4e481Winson Chung .setSmallIcon(R.drawable.stat_notify_image) 141c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setWhen(System.currentTimeMillis()); 142c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Notification n = mNotificationBuilder.getNotification(); 143c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung n.flags |= Notification.FLAG_NO_CLEAR; 144c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 145c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationManager = nManager; 146c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationManager.notify(nId, n); 147c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung } 148c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 1499112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 1509112ec3039dda4186c6f957981237c0691db2269Winson Chung protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) { 1519112ec3039dda4186c6f957981237c0691db2269Winson Chung if (params.length != 1) return null; 1529112ec3039dda4186c6f957981237c0691db2269Winson Chung 15336c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung // By default, AsyncTask sets the worker thread to have background thread priority, so bump 15436c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung // it back up so that we save a little quicker. 15536c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); 15636c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung 1579112ec3039dda4186c6f957981237c0691db2269Winson Chung Context context = params[0].context; 1589112ec3039dda4186c6f957981237c0691db2269Winson Chung Bitmap image = params[0].image; 1599112ec3039dda4186c6f957981237c0691db2269Winson Chung 160d859fa399133da32705415e138c897f263ae99adWinson Chung try { 1619112ec3039dda4186c6f957981237c0691db2269Winson Chung // Save the screenshot to the MediaStore 1629112ec3039dda4186c6f957981237c0691db2269Winson Chung ContentValues values = new ContentValues(); 1639837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood ContentResolver resolver = context.getContentResolver(); 164c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath); 165c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName); 166c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName); 167c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime); 168c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_ADDED, mImageTime); 169c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, mImageTime); 1709112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png"); 1719837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 1729112ec3039dda4186c6f957981237c0691db2269Winson Chung 1739837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood OutputStream out = resolver.openOutputStream(uri); 1749112ec3039dda4186c6f957981237c0691db2269Winson Chung image.compress(Bitmap.CompressFormat.PNG, 100, out); 1759112ec3039dda4186c6f957981237c0691db2269Winson Chung out.flush(); 1769112ec3039dda4186c6f957981237c0691db2269Winson Chung out.close(); 1779112ec3039dda4186c6f957981237c0691db2269Winson Chung 1789837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood // update file size in the database 1799837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood values.clear(); 180c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length()); 1819837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood resolver.update(uri, values, null, null); 1829837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood 18336c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung params[0].imageUri = uri; 1849112ec3039dda4186c6f957981237c0691db2269Winson Chung params[0].result = 0; 185d859fa399133da32705415e138c897f263ae99adWinson Chung } catch (Exception e) { 186d859fa399133da32705415e138c897f263ae99adWinson Chung // IOException/UnsupportedOperationException may be thrown if external storage is not 187d859fa399133da32705415e138c897f263ae99adWinson Chung // mounted 1889112ec3039dda4186c6f957981237c0691db2269Winson Chung params[0].result = 1; 1899112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1909112ec3039dda4186c6f957981237c0691db2269Winson Chung 1919112ec3039dda4186c6f957981237c0691db2269Winson Chung return params[0]; 1929112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 1939112ec3039dda4186c6f957981237c0691db2269Winson Chung 1949112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 1959112ec3039dda4186c6f957981237c0691db2269Winson Chung protected void onPostExecute(SaveImageInBackgroundData params) { 1969112ec3039dda4186c6f957981237c0691db2269Winson Chung if (params.result > 0) { 1979112ec3039dda4186c6f957981237c0691db2269Winson Chung // Show a message that we've failed to save the image to disk 198c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager); 1999112ec3039dda4186c6f957981237c0691db2269Winson Chung } else { 200c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung // Show the final notification to indicate screenshot saved 201c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Resources r = params.context.getResources(); 202c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 20336c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung // Create the intent to show the screenshot in gallery 20436c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung mLaunchIntent = new Intent(Intent.ACTION_VIEW); 20536c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung mLaunchIntent.setDataAndType(params.imageUri, "image/png"); 20636c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung mLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 20736c9e29cc6554fc1fe34565b93c2280f6a6b4cf9Winson Chung 208c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationBuilder 209c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentTitle(r.getString(R.string.screenshot_saved_title)) 210c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentText(r.getString(R.string.screenshot_saved_text)) 211c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentIntent(PendingIntent.getActivity(params.context, 0, mLaunchIntent, 0)) 212c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setWhen(System.currentTimeMillis()) 213c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setAutoCancel(true); 214c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 215c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Notification n = mNotificationBuilder.getNotification(); 216c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung n.flags &= ~Notification.FLAG_NO_CLEAR; 217c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationManager.notify(mNotificationId, n); 2189112ec3039dda4186c6f957981237c0691db2269Winson Chung } 219fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn params.finisher.run(); 2209112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 2219112ec3039dda4186c6f957981237c0691db2269Winson Chung} 2229112ec3039dda4186c6f957981237c0691db2269Winson Chung 2239112ec3039dda4186c6f957981237c0691db2269Winson Chung/** 2249112ec3039dda4186c6f957981237c0691db2269Winson Chung * TODO: 2259112ec3039dda4186c6f957981237c0691db2269Winson Chung * - Performance when over gl surfaces? Ie. Gallery 2269112ec3039dda4186c6f957981237c0691db2269Winson Chung * - what do we say in the Toast? Which icon do we get if the user uses another 2279112ec3039dda4186c6f957981237c0691db2269Winson Chung * type of gallery? 2289112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 2299112ec3039dda4186c6f957981237c0691db2269Winson Chungclass GlobalScreenshot { 2309112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String TAG = "GlobalScreenshot"; 231c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private static final int SCREENSHOT_NOTIFICATION_ID = 789; 232a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final int SCREENSHOT_FADE_IN_DURATION = 250; 233a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final int SCREENSHOT_FADE_OUT_DELAY = 750; 234a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final int SCREENSHOT_FADE_OUT_DURATION = 500; 235a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final int SCREENSHOT_FAST_FADE_OUT_DURATION = 350; 2369112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final float BACKGROUND_ALPHA = 0.65f; 237c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private static final float SCREENSHOT_SCALE_FUDGE = 0.075f; // To account for the border padding 238a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final float SCREENSHOT_SCALE = 0.55f; 239a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final float SCREENSHOT_FADE_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.975f; 240a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private static final float SCREENSHOT_FADE_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.925f; 2419112ec3039dda4186c6f957981237c0691db2269Winson Chung 2429112ec3039dda4186c6f957981237c0691db2269Winson Chung private Context mContext; 2439112ec3039dda4186c6f957981237c0691db2269Winson Chung private LayoutInflater mLayoutInflater; 2449112ec3039dda4186c6f957981237c0691db2269Winson Chung private IWindowManager mIWindowManager; 2459112ec3039dda4186c6f957981237c0691db2269Winson Chung private WindowManager mWindowManager; 2469112ec3039dda4186c6f957981237c0691db2269Winson Chung private WindowManager.LayoutParams mWindowLayoutParams; 247c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung private NotificationManager mNotificationManager; 2489112ec3039dda4186c6f957981237c0691db2269Winson Chung private Display mDisplay; 2499112ec3039dda4186c6f957981237c0691db2269Winson Chung private DisplayMetrics mDisplayMetrics; 2509112ec3039dda4186c6f957981237c0691db2269Winson Chung private Matrix mDisplayMatrix; 2519112ec3039dda4186c6f957981237c0691db2269Winson Chung 2529112ec3039dda4186c6f957981237c0691db2269Winson Chung private Bitmap mScreenBitmap; 2539112ec3039dda4186c6f957981237c0691db2269Winson Chung private View mScreenshotLayout; 2549112ec3039dda4186c6f957981237c0691db2269Winson Chung private ImageView mBackgroundView; 2559112ec3039dda4186c6f957981237c0691db2269Winson Chung private FrameLayout mScreenshotContainerView; 2569112ec3039dda4186c6f957981237c0691db2269Winson Chung private ImageView mScreenshotView; 2579112ec3039dda4186c6f957981237c0691db2269Winson Chung 2589112ec3039dda4186c6f957981237c0691db2269Winson Chung private AnimatorSet mScreenshotAnimation; 2599112ec3039dda4186c6f957981237c0691db2269Winson Chung 260a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private int mStatusBarIconSize; 261a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private int mNotificationIconSize; 262a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private float mDropOffsetX; 263a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private float mDropOffsetY; 264a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private float mBgPadding; 265a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private float mBgPaddingScale; 266a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 2679112ec3039dda4186c6f957981237c0691db2269Winson Chung 2689112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 2699112ec3039dda4186c6f957981237c0691db2269Winson Chung * @param context everything needs a context :( 2709112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 2719112ec3039dda4186c6f957981237c0691db2269Winson Chung public GlobalScreenshot(Context context) { 272a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung Resources r = context.getResources(); 2739112ec3039dda4186c6f957981237c0691db2269Winson Chung mContext = context; 2749112ec3039dda4186c6f957981237c0691db2269Winson Chung mLayoutInflater = (LayoutInflater) 2759112ec3039dda4186c6f957981237c0691db2269Winson Chung context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2769112ec3039dda4186c6f957981237c0691db2269Winson Chung 2779112ec3039dda4186c6f957981237c0691db2269Winson Chung // Inflate the screenshot layout 2789112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix = new Matrix(); 2799112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout = mLayoutInflater.inflate(R.layout.global_screenshot, null); 2809112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background); 2819112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView = (FrameLayout) mScreenshotLayout.findViewById(R.id.global_screenshot_container); 2829112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot); 2839112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.setFocusable(true); 2849112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() { 2859112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 2869112ec3039dda4186c6f957981237c0691db2269Winson Chung public boolean onTouch(View v, MotionEvent event) { 2879112ec3039dda4186c6f957981237c0691db2269Winson Chung // Intercept and ignore all touch events 2889112ec3039dda4186c6f957981237c0691db2269Winson Chung return true; 2899112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2909112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 2919112ec3039dda4186c6f957981237c0691db2269Winson Chung 2929112ec3039dda4186c6f957981237c0691db2269Winson Chung // Setup the window that we are going to use 2939112ec3039dda4186c6f957981237c0691db2269Winson Chung mIWindowManager = IWindowManager.Stub.asInterface( 2949112ec3039dda4186c6f957981237c0691db2269Winson Chung ServiceManager.getService(Context.WINDOW_SERVICE)); 2959112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowLayoutParams = new WindowManager.LayoutParams( 2969112ec3039dda4186c6f957981237c0691db2269Winson Chung ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, 2979112ec3039dda4186c6f957981237c0691db2269Winson Chung WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY, 2989112ec3039dda4186c6f957981237c0691db2269Winson Chung WindowManager.LayoutParams.FLAG_FULLSCREEN 2999112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED 3009112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 3019112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, 3029112ec3039dda4186c6f957981237c0691db2269Winson Chung PixelFormat.TRANSLUCENT); 3039112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowLayoutParams.setTitle("ScreenshotAnimation"); 3049112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 305c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mNotificationManager = 306c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 3079112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplay = mWindowManager.getDefaultDisplay(); 308a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDisplayMetrics = new DisplayMetrics(); 309a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDisplay.getRealMetrics(mDisplayMetrics); 310a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 311a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Get the various target sizes 312a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mStatusBarIconSize = 313a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung r.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_icon_size); 314a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mNotificationIconSize = 315a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height); 316a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDropOffsetX = r.getDimensionPixelSize(R.dimen.global_screenshot_drop_offset_x); 317a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDropOffsetY = r.getDimensionPixelSize(R.dimen.global_screenshot_drop_offset_y); 318a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 319a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Scale has to account for both sides of the bg 320a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding); 321a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBgPaddingScale = (2f * mBgPadding) / mDisplayMetrics.widthPixels; 3229112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3239112ec3039dda4186c6f957981237c0691db2269Winson Chung 3249112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 3259112ec3039dda4186c6f957981237c0691db2269Winson Chung * Creates a new worker thread and saves the screenshot to the media store. 3269112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 327fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn private void saveScreenshotInWorkerThread(Runnable finisher) { 3289112ec3039dda4186c6f957981237c0691db2269Winson Chung SaveImageInBackgroundData data = new SaveImageInBackgroundData(); 3299112ec3039dda4186c6f957981237c0691db2269Winson Chung data.context = mContext; 3309112ec3039dda4186c6f957981237c0691db2269Winson Chung data.image = mScreenBitmap; 331a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung data.iconSize = mNotificationIconSize; 332fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn data.finisher = finisher; 333a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung new SaveImageInBackgroundTask(mContext, data, mNotificationManager, 334a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung SCREENSHOT_NOTIFICATION_ID).execute(data); 3359112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3369112ec3039dda4186c6f957981237c0691db2269Winson Chung 3379112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 3389112ec3039dda4186c6f957981237c0691db2269Winson Chung * @return the current display rotation in degrees 3399112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 3409112ec3039dda4186c6f957981237c0691db2269Winson Chung private float getDegreesForRotation(int value) { 3419112ec3039dda4186c6f957981237c0691db2269Winson Chung switch (value) { 3429112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_90: 343a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung return 360f - 90f; 3449112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_180: 345a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung return 360f - 180f; 3469112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_270: 347a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung return 360f - 270f; 3489112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3499112ec3039dda4186c6f957981237c0691db2269Winson Chung return 0f; 3509112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3519112ec3039dda4186c6f957981237c0691db2269Winson Chung 3529112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 3539112ec3039dda4186c6f957981237c0691db2269Winson Chung * Takes a screenshot of the current display and shows an animation. 3549112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 355a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { 3569112ec3039dda4186c6f957981237c0691db2269Winson Chung // We need to orient the screenshot correctly (and the Surface api seems to take screenshots 3579112ec3039dda4186c6f957981237c0691db2269Winson Chung // only in the natural orientation of the device :!) 3589112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplay.getRealMetrics(mDisplayMetrics); 3599112ec3039dda4186c6f957981237c0691db2269Winson Chung float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; 3609112ec3039dda4186c6f957981237c0691db2269Winson Chung float degrees = getDegreesForRotation(mDisplay.getRotation()); 3619112ec3039dda4186c6f957981237c0691db2269Winson Chung boolean requiresRotation = (degrees > 0); 3629112ec3039dda4186c6f957981237c0691db2269Winson Chung if (requiresRotation) { 3639112ec3039dda4186c6f957981237c0691db2269Winson Chung // Get the dimensions of the device in its native orientation 3649112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.reset(); 3659112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.preRotate(-degrees); 3669112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.mapPoints(dims); 3679112ec3039dda4186c6f957981237c0691db2269Winson Chung dims[0] = Math.abs(dims[0]); 3689112ec3039dda4186c6f957981237c0691db2269Winson Chung dims[1] = Math.abs(dims[1]); 3699112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3709112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]); 3719112ec3039dda4186c6f957981237c0691db2269Winson Chung if (requiresRotation) { 3729112ec3039dda4186c6f957981237c0691db2269Winson Chung // Rotate the screenshot to the current orientation 3739112ec3039dda4186c6f957981237c0691db2269Winson Chung Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, 3749112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); 3759112ec3039dda4186c6f957981237c0691db2269Winson Chung Canvas c = new Canvas(ss); 3769112ec3039dda4186c6f957981237c0691db2269Winson Chung c.translate(ss.getWidth() / 2, ss.getHeight() / 2); 377a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung c.rotate(degrees); 3789112ec3039dda4186c6f957981237c0691db2269Winson Chung c.translate(-dims[0] / 2, -dims[1] / 2); 3799112ec3039dda4186c6f957981237c0691db2269Winson Chung c.drawBitmap(mScreenBitmap, 0, 0, null); 3806311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn c.setBitmap(null); 3819112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenBitmap = ss; 3829112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3839112ec3039dda4186c6f957981237c0691db2269Winson Chung 3849112ec3039dda4186c6f957981237c0691db2269Winson Chung // If we couldn't take the screenshot, notify the user 3859112ec3039dda4186c6f957981237c0691db2269Winson Chung if (mScreenBitmap == null) { 386c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung notifyScreenshotError(mContext, mNotificationManager); 387fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn finisher.run(); 3889112ec3039dda4186c6f957981237c0691db2269Winson Chung return; 3899112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3909112ec3039dda4186c6f957981237c0691db2269Winson Chung 391a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Optimizations 392a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenBitmap.setHasAlpha(false); 393a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenBitmap.prepareToDraw(); 394a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 3959112ec3039dda4186c6f957981237c0691db2269Winson Chung // Start the post-screenshot animation 396a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, 397a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung statusBarVisible, navBarVisible); 3989112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3999112ec3039dda4186c6f957981237c0691db2269Winson Chung 4009112ec3039dda4186c6f957981237c0691db2269Winson Chung 4019112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 4029112ec3039dda4186c6f957981237c0691db2269Winson Chung * Starts the animation after taking the screenshot 4039112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 404a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible, 405a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung boolean navBarVisible) { 4069112ec3039dda4186c6f957981237c0691db2269Winson Chung // Add the view for the animation 4079112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotView.setImageBitmap(mScreenBitmap); 4089112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.requestFocus(); 4099112ec3039dda4186c6f957981237c0691db2269Winson Chung 4109112ec3039dda4186c6f957981237c0691db2269Winson Chung // Setup the animation with the screenshot just taken 4119112ec3039dda4186c6f957981237c0691db2269Winson Chung if (mScreenshotAnimation != null) { 4129112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.end(); 4139112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4149112ec3039dda4186c6f957981237c0691db2269Winson Chung 4159112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); 4169112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator screenshotFadeInAnim = createScreenshotFadeInAnimation(); 417a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung ValueAnimator screenshotFadeOutAnim = createScreenshotFadeOutAnimation(w, h, 418a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung statusBarVisible, navBarVisible); 4199112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation = new AnimatorSet(); 4209112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.play(screenshotFadeInAnim).before(screenshotFadeOutAnim); 4219112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { 4229112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 4239112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationEnd(Animator animation) { 4249112ec3039dda4186c6f957981237c0691db2269Winson Chung // Save the screenshot once we have a bit of time now 425fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn saveScreenshotInWorkerThread(finisher); 4269112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager.removeView(mScreenshotLayout); 4279112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4289112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 429a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotLayout.post(new Runnable() { 430a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung @Override 431a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung public void run() { 432a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 433a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.buildLayer(); 434a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotAnimation.start(); 435a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } 436a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung }); 4379112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4389112ec3039dda4186c6f957981237c0691db2269Winson Chung private ValueAnimator createScreenshotFadeInAnimation() { 4399112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 440a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.setInterpolator(new AccelerateInterpolator(1.5f)); 4419112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setDuration(SCREENSHOT_FADE_IN_DURATION); 4429112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addListener(new AnimatorListenerAdapter() { 4439112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 4449112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationStart(Animator animation) { 445a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBackgroundView.setAlpha(0f); 4469112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setVisibility(View.VISIBLE); 447a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setTranslationX(0f); 448c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung mScreenshotContainerView.setTranslationY(0f); 449a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleX(SCREENSHOT_FADE_IN_MIN_SCALE); 450a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleY(SCREENSHOT_FADE_IN_MIN_SCALE); 451a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setAlpha(0f); 4529112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setVisibility(View.VISIBLE); 4539112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4549112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 4559112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addUpdateListener(new AnimatorUpdateListener() { 4569112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 4579112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationUpdate(ValueAnimator animation) { 4589112ec3039dda4186c6f957981237c0691db2269Winson Chung float t = ((Float) animation.getAnimatedValue()).floatValue(); 459a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float scaleT = (SCREENSHOT_FADE_IN_MIN_SCALE) 460a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung + (float) t * (SCREENSHOT_SCALE - SCREENSHOT_FADE_IN_MIN_SCALE); 461a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBackgroundView.setAlpha(t * BACKGROUND_ALPHA); 4629112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleX(scaleT); 4639112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleY(scaleT); 464a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setAlpha(t); 4659112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4669112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 4679112ec3039dda4186c6f957981237c0691db2269Winson Chung return anim; 4689112ec3039dda4186c6f957981237c0691db2269Winson Chung } 469a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung private ValueAnimator createScreenshotFadeOutAnimation(int w, int h, boolean statusBarVisible, 470a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung boolean navBarVisible) { 471a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 472a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.setInterpolator(new DecelerateInterpolator(0.5f)); 4739112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setStartDelay(SCREENSHOT_FADE_OUT_DELAY); 4749112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addListener(new AnimatorListenerAdapter() { 4759112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 4769112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationEnd(Animator animation) { 4779112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setVisibility(View.GONE); 4789112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setVisibility(View.GONE); 479a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setLayerType(View.LAYER_TYPE_NONE, null); 4809112ec3039dda4186c6f957981237c0691db2269Winson Chung } 4819112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 482a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 483a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung if (!statusBarVisible || !navBarVisible) { 484a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // There is no status bar/nav bar, so just fade the screenshot away in place 485a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.setDuration(SCREENSHOT_FAST_FADE_OUT_DURATION); 486a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.addUpdateListener(new AnimatorUpdateListener() { 487a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung @Override 488a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung public void onAnimationUpdate(ValueAnimator animation) { 489a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float t = ((Float) animation.getAnimatedValue()).floatValue(); 490a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float scaleT = (SCREENSHOT_FADE_OUT_MIN_SCALE) 491a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung + (float) (1f - t) * (SCREENSHOT_SCALE - SCREENSHOT_FADE_OUT_MIN_SCALE); 492a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); 493a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setAlpha((1f - t) * BACKGROUND_ALPHA); 494a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleX(scaleT); 495a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleY(scaleT); 496a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } 497a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung }); 498a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } else { 499a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Determine the bounds of how to scale 500a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float halfScreenWidth = (w - 2f * mBgPadding) / 2f; 501a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float halfScreenHeight = (h - 2f * mBgPadding) / 2f; 502a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung final RectF finalBounds = new RectF(mDropOffsetX, mDropOffsetY, 503a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDropOffsetX + mStatusBarIconSize, 504a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mDropOffsetY + mStatusBarIconSize); 505a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung final PointF currentPos = new PointF(0f, 0f); 506a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung final PointF finalPos = new PointF(-halfScreenWidth + finalBounds.centerX(), 507a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung -halfScreenHeight + finalBounds.centerY()); 508a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung final DecelerateInterpolator d = new DecelerateInterpolator(2f); 509a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Note: since the scale origin is in the center of the view, divide difference by 2 510a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float tmpMinScale = 0f; 511a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung if (w > h) { 512a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung tmpMinScale = finalBounds.width() / (2f * w); 513a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } else { 514a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung tmpMinScale = finalBounds.height() / (2f * h); 5159112ec3039dda4186c6f957981237c0691db2269Winson Chung } 516a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung final float minScale = tmpMinScale; 517a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung 518a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung // Animate the screenshot to the status bar 519a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.setDuration(SCREENSHOT_FADE_OUT_DURATION); 520a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung anim.addUpdateListener(new AnimatorUpdateListener() { 521a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung @Override 522a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung public void onAnimationUpdate(ValueAnimator animation) { 523a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float t = ((Float) animation.getAnimatedValue()).floatValue(); 524a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung float scaleT = minScale 525a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung + (float) (1f - t) * (SCREENSHOT_SCALE - minScale - mBgPaddingScale) 526a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung + mBgPaddingScale; 527a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setAlpha(d.getInterpolation(1f - t)); 528a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setTranslationX(d.getInterpolation(t) * finalPos.x); 529a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setTranslationY(d.getInterpolation(t) * finalPos.y); 530a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleX(scaleT); 531a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mScreenshotContainerView.setScaleY(scaleT); 532a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); 533a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } 534a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung }); 535a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung } 5369112ec3039dda4186c6f957981237c0691db2269Winson Chung return anim; 5379112ec3039dda4186c6f957981237c0691db2269Winson Chung } 538c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 539c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung static void notifyScreenshotError(Context context, NotificationManager nManager) { 540c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Resources r = context.getResources(); 541c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung 542c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung // Clear all existing notification, compose the new notification and show it 543c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung Notification n = new Notification.Builder(context) 544c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setTicker(r.getString(R.string.screenshot_failed_title)) 545c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentTitle(r.getString(R.string.screenshot_failed_title)) 546c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setContentText(r.getString(R.string.screenshot_failed_text)) 547b787a751760de544f0d83bb7ddf9405ae6e4e481Winson Chung .setSmallIcon(R.drawable.stat_notify_image_error) 548c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setWhen(System.currentTimeMillis()) 549c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .setAutoCancel(true) 550c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung .getNotification(); 551c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung nManager.notify(SCREENSHOT_NOTIFICATION_ID, n); 552c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung } 5539112ec3039dda4186c6f957981237c0691db2269Winson Chung} 554