GlobalScreenshot.java revision 9112ec3039dda4186c6f957981237c0691db2269
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.ObjectAnimator; 239112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.TimeInterpolator; 249112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator; 259112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator.AnimatorUpdateListener; 269112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.app.Activity; 279112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.ContentValues; 289112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.Context; 299112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Bitmap; 309112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Canvas; 319112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Matrix; 329112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.PixelFormat; 339112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.media.MediaScannerConnection; 349112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.net.Uri; 359112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.AsyncTask; 369112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.Binder; 379112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.Environment; 389112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.ServiceManager; 399112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.provider.MediaStore; 409112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.util.DisplayMetrics; 419112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.util.Log; 429112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Display; 439112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.IWindowManager; 449112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.LayoutInflater; 459112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.MotionEvent; 469112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Surface; 479112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.View; 489112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.ViewGroup; 499112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.WindowManager; 509112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.FrameLayout; 519112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.ImageView; 529112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.TextView; 539112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.Toast; 549112ec3039dda4186c6f957981237c0691db2269Winson Chung 559112ec3039dda4186c6f957981237c0691db2269Winson Chungimport com.android.systemui.R; 569112ec3039dda4186c6f957981237c0691db2269Winson Chung 579112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.File; 589112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.FileOutputStream; 599112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.IOException; 609112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.OutputStream; 619112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.lang.Thread; 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; 719112ec3039dda4186c6f957981237c0691db2269Winson Chung int result; 729112ec3039dda4186c6f957981237c0691db2269Winson Chung} 739112ec3039dda4186c6f957981237c0691db2269Winson Chung 749112ec3039dda4186c6f957981237c0691db2269Winson Chung/** 759112ec3039dda4186c6f957981237c0691db2269Winson Chung * An AsyncTask that saves an image to the media store in the background. 769112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 779112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void, 789112ec3039dda4186c6f957981237c0691db2269Winson Chung SaveImageInBackgroundData> { 799112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String TAG = "SaveImageInBackgroundTask"; 809112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; 819112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String SCREENSHOT_FILE_PATH_TEMPLATE = "%s/%s/Screenshot_%s-%d.png"; 829112ec3039dda4186c6f957981237c0691db2269Winson Chung 839112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 849112ec3039dda4186c6f957981237c0691db2269Winson Chung protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) { 859112ec3039dda4186c6f957981237c0691db2269Winson Chung if (params.length != 1) return null; 869112ec3039dda4186c6f957981237c0691db2269Winson Chung 879112ec3039dda4186c6f957981237c0691db2269Winson Chung Context context = params[0].context; 889112ec3039dda4186c6f957981237c0691db2269Winson Chung Bitmap image = params[0].image; 899112ec3039dda4186c6f957981237c0691db2269Winson Chung 909112ec3039dda4186c6f957981237c0691db2269Winson Chung try{ 919112ec3039dda4186c6f957981237c0691db2269Winson Chung long currentTime = System.currentTimeMillis(); 929112ec3039dda4186c6f957981237c0691db2269Winson Chung String date = new SimpleDateFormat("MM-dd-yy-kk-mm-ss").format(new Date(currentTime)); 939112ec3039dda4186c6f957981237c0691db2269Winson Chung String imageDir = Environment.getExternalStoragePublicDirectory( 949112ec3039dda4186c6f957981237c0691db2269Winson Chung Environment.DIRECTORY_PICTURES).getAbsolutePath(); 959112ec3039dda4186c6f957981237c0691db2269Winson Chung String imageFilePath = String.format(SCREENSHOT_FILE_PATH_TEMPLATE, 969112ec3039dda4186c6f957981237c0691db2269Winson Chung imageDir, SCREENSHOTS_DIR_NAME, 979112ec3039dda4186c6f957981237c0691db2269Winson Chung date, currentTime % 1000); 989112ec3039dda4186c6f957981237c0691db2269Winson Chung 999112ec3039dda4186c6f957981237c0691db2269Winson Chung // Save the screenshot to the MediaStore 1009112ec3039dda4186c6f957981237c0691db2269Winson Chung ContentValues values = new ContentValues(); 1019112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.DATA, imageFilePath); 1029112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.TITLE, "Screenshot"); 1039112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "Screenshot"); 1049112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, currentTime); 1059112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_ADDED, currentTime); 1069112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, currentTime); 1079112ec3039dda4186c6f957981237c0691db2269Winson Chung values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png"); 1089112ec3039dda4186c6f957981237c0691db2269Winson Chung Uri uri = context.getContentResolver().insert( 1099112ec3039dda4186c6f957981237c0691db2269Winson Chung MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 1109112ec3039dda4186c6f957981237c0691db2269Winson Chung 1119112ec3039dda4186c6f957981237c0691db2269Winson Chung OutputStream out = context.getContentResolver().openOutputStream(uri); 1129112ec3039dda4186c6f957981237c0691db2269Winson Chung image.compress(Bitmap.CompressFormat.PNG, 100, out); 1139112ec3039dda4186c6f957981237c0691db2269Winson Chung out.flush(); 1149112ec3039dda4186c6f957981237c0691db2269Winson Chung out.close(); 1159112ec3039dda4186c6f957981237c0691db2269Winson Chung 1169112ec3039dda4186c6f957981237c0691db2269Winson Chung params[0].result = 0; 1179112ec3039dda4186c6f957981237c0691db2269Winson Chung }catch(IOException e){ 1189112ec3039dda4186c6f957981237c0691db2269Winson Chung params[0].result = 1; 1199112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1209112ec3039dda4186c6f957981237c0691db2269Winson Chung 1219112ec3039dda4186c6f957981237c0691db2269Winson Chung return params[0]; 1229112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 1239112ec3039dda4186c6f957981237c0691db2269Winson Chung 1249112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 1259112ec3039dda4186c6f957981237c0691db2269Winson Chung protected void onPostExecute(SaveImageInBackgroundData params) { 1269112ec3039dda4186c6f957981237c0691db2269Winson Chung if (params.result > 0) { 1279112ec3039dda4186c6f957981237c0691db2269Winson Chung // Show a message that we've failed to save the image to disk 1289112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.makeText(params.context, R.string.screenshot_failed_toast, 1299112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.LENGTH_SHORT).show(); 1309112ec3039dda4186c6f957981237c0691db2269Winson Chung } else { 1319112ec3039dda4186c6f957981237c0691db2269Winson Chung // Show a message that we've saved the screenshot to disk 1329112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.makeText(params.context, R.string.screenshot_saving_toast, 1339112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.LENGTH_SHORT).show(); 1349112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1359112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 1369112ec3039dda4186c6f957981237c0691db2269Winson Chung} 1379112ec3039dda4186c6f957981237c0691db2269Winson Chung 1389112ec3039dda4186c6f957981237c0691db2269Winson Chung/** 1399112ec3039dda4186c6f957981237c0691db2269Winson Chung * TODO: 1409112ec3039dda4186c6f957981237c0691db2269Winson Chung * - Performance when over gl surfaces? Ie. Gallery 1419112ec3039dda4186c6f957981237c0691db2269Winson Chung * - what do we say in the Toast? Which icon do we get if the user uses another 1429112ec3039dda4186c6f957981237c0691db2269Winson Chung * type of gallery? 1439112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 1449112ec3039dda4186c6f957981237c0691db2269Winson Chungclass GlobalScreenshot { 1459112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final String TAG = "GlobalScreenshot"; 1469112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int SCREENSHOT_FADE_IN_DURATION = 900; 1479112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int SCREENSHOT_FADE_OUT_DELAY = 1000; 1489112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int SCREENSHOT_FADE_OUT_DURATION = 450; 1499112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int TOAST_FADE_IN_DURATION = 500; 1509112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int TOAST_FADE_OUT_DELAY = 1000; 1519112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final int TOAST_FADE_OUT_DURATION = 500; 1529112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final float BACKGROUND_ALPHA = 0.65f; 1539112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final float SCREENSHOT_SCALE = 0.85f; 1549112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final float SCREENSHOT_MIN_SCALE = 0.7f; 1559112ec3039dda4186c6f957981237c0691db2269Winson Chung private static final float SCREENSHOT_ROTATION = -6.75f; // -12.5f; 1569112ec3039dda4186c6f957981237c0691db2269Winson Chung 1579112ec3039dda4186c6f957981237c0691db2269Winson Chung private Context mContext; 1589112ec3039dda4186c6f957981237c0691db2269Winson Chung private LayoutInflater mLayoutInflater; 1599112ec3039dda4186c6f957981237c0691db2269Winson Chung private IWindowManager mIWindowManager; 1609112ec3039dda4186c6f957981237c0691db2269Winson Chung private WindowManager mWindowManager; 1619112ec3039dda4186c6f957981237c0691db2269Winson Chung private WindowManager.LayoutParams mWindowLayoutParams; 1629112ec3039dda4186c6f957981237c0691db2269Winson Chung private Display mDisplay; 1639112ec3039dda4186c6f957981237c0691db2269Winson Chung private DisplayMetrics mDisplayMetrics; 1649112ec3039dda4186c6f957981237c0691db2269Winson Chung private Matrix mDisplayMatrix; 1659112ec3039dda4186c6f957981237c0691db2269Winson Chung 1669112ec3039dda4186c6f957981237c0691db2269Winson Chung private Bitmap mScreenBitmap; 1679112ec3039dda4186c6f957981237c0691db2269Winson Chung private View mScreenshotLayout; 1689112ec3039dda4186c6f957981237c0691db2269Winson Chung private ImageView mBackgroundView; 1699112ec3039dda4186c6f957981237c0691db2269Winson Chung private FrameLayout mScreenshotContainerView; 1709112ec3039dda4186c6f957981237c0691db2269Winson Chung private ImageView mScreenshotView; 1719112ec3039dda4186c6f957981237c0691db2269Winson Chung 1729112ec3039dda4186c6f957981237c0691db2269Winson Chung private AnimatorSet mScreenshotAnimation; 1739112ec3039dda4186c6f957981237c0691db2269Winson Chung 1749112ec3039dda4186c6f957981237c0691db2269Winson Chung // General use cubic interpolator 1759112ec3039dda4186c6f957981237c0691db2269Winson Chung final TimeInterpolator mCubicInterpolator = new TimeInterpolator() { 1769112ec3039dda4186c6f957981237c0691db2269Winson Chung public float getInterpolation(float t) { 1779112ec3039dda4186c6f957981237c0691db2269Winson Chung return t*t*t; 1789112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1799112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 1809112ec3039dda4186c6f957981237c0691db2269Winson Chung // The interpolator used to control the background alpha at the start of the animation 1819112ec3039dda4186c6f957981237c0691db2269Winson Chung final TimeInterpolator mBackgroundViewAlphaInterpolator = new TimeInterpolator() { 1829112ec3039dda4186c6f957981237c0691db2269Winson Chung public float getInterpolation(float t) { 1839112ec3039dda4186c6f957981237c0691db2269Winson Chung float tStep = 0.35f; 1849112ec3039dda4186c6f957981237c0691db2269Winson Chung if (t < tStep) { 1859112ec3039dda4186c6f957981237c0691db2269Winson Chung return t * (1f / tStep); 1869112ec3039dda4186c6f957981237c0691db2269Winson Chung } else { 1879112ec3039dda4186c6f957981237c0691db2269Winson Chung return 1f; 1889112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1899112ec3039dda4186c6f957981237c0691db2269Winson Chung } 1909112ec3039dda4186c6f957981237c0691db2269Winson Chung }; 1919112ec3039dda4186c6f957981237c0691db2269Winson Chung 1929112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 1939112ec3039dda4186c6f957981237c0691db2269Winson Chung * @param context everything needs a context :( 1949112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 1959112ec3039dda4186c6f957981237c0691db2269Winson Chung public GlobalScreenshot(Context context) { 1969112ec3039dda4186c6f957981237c0691db2269Winson Chung mContext = context; 1979112ec3039dda4186c6f957981237c0691db2269Winson Chung mLayoutInflater = (LayoutInflater) 1989112ec3039dda4186c6f957981237c0691db2269Winson Chung context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 1999112ec3039dda4186c6f957981237c0691db2269Winson Chung 2009112ec3039dda4186c6f957981237c0691db2269Winson Chung // Inflate the screenshot layout 2019112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMetrics = new DisplayMetrics(); 2029112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix = new Matrix(); 2039112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout = mLayoutInflater.inflate(R.layout.global_screenshot, null); 2049112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background); 2059112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView = (FrameLayout) mScreenshotLayout.findViewById(R.id.global_screenshot_container); 2069112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot); 2079112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.setFocusable(true); 2089112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() { 2099112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 2109112ec3039dda4186c6f957981237c0691db2269Winson Chung public boolean onTouch(View v, MotionEvent event) { 2119112ec3039dda4186c6f957981237c0691db2269Winson Chung // Intercept and ignore all touch events 2129112ec3039dda4186c6f957981237c0691db2269Winson Chung return true; 2139112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2149112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 2159112ec3039dda4186c6f957981237c0691db2269Winson Chung 2169112ec3039dda4186c6f957981237c0691db2269Winson Chung // Setup the window that we are going to use 2179112ec3039dda4186c6f957981237c0691db2269Winson Chung mIWindowManager = IWindowManager.Stub.asInterface( 2189112ec3039dda4186c6f957981237c0691db2269Winson Chung ServiceManager.getService(Context.WINDOW_SERVICE)); 2199112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowLayoutParams = new WindowManager.LayoutParams( 2209112ec3039dda4186c6f957981237c0691db2269Winson Chung ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, 2219112ec3039dda4186c6f957981237c0691db2269Winson Chung WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY, 2229112ec3039dda4186c6f957981237c0691db2269Winson Chung WindowManager.LayoutParams.FLAG_FULLSCREEN 2239112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED 2249112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED_SYSTEM 2259112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING 2269112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 2279112ec3039dda4186c6f957981237c0691db2269Winson Chung | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, 2289112ec3039dda4186c6f957981237c0691db2269Winson Chung PixelFormat.TRANSLUCENT); 2299112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowLayoutParams.token = new Binder(); 2309112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowLayoutParams.setTitle("ScreenshotAnimation"); 2319112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 2329112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplay = mWindowManager.getDefaultDisplay(); 2339112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2349112ec3039dda4186c6f957981237c0691db2269Winson Chung 2359112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 2369112ec3039dda4186c6f957981237c0691db2269Winson Chung * Creates a new worker thread and saves the screenshot to the media store. 2379112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 2389112ec3039dda4186c6f957981237c0691db2269Winson Chung private void saveScreenshotInWorkerThread() { 2399112ec3039dda4186c6f957981237c0691db2269Winson Chung SaveImageInBackgroundData data = new SaveImageInBackgroundData(); 2409112ec3039dda4186c6f957981237c0691db2269Winson Chung data.context = mContext; 2419112ec3039dda4186c6f957981237c0691db2269Winson Chung data.image = mScreenBitmap; 2429112ec3039dda4186c6f957981237c0691db2269Winson Chung new SaveImageInBackgroundTask().execute(data); 2439112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2449112ec3039dda4186c6f957981237c0691db2269Winson Chung 2459112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 2469112ec3039dda4186c6f957981237c0691db2269Winson Chung * @return the current display rotation in degrees 2479112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 2489112ec3039dda4186c6f957981237c0691db2269Winson Chung private float getDegreesForRotation(int value) { 2499112ec3039dda4186c6f957981237c0691db2269Winson Chung switch (value) { 2509112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_90: 2519112ec3039dda4186c6f957981237c0691db2269Winson Chung return 90f; 2529112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_180: 2539112ec3039dda4186c6f957981237c0691db2269Winson Chung return 180f; 2549112ec3039dda4186c6f957981237c0691db2269Winson Chung case Surface.ROTATION_270: 2559112ec3039dda4186c6f957981237c0691db2269Winson Chung return 270f; 2569112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2579112ec3039dda4186c6f957981237c0691db2269Winson Chung return 0f; 2589112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2599112ec3039dda4186c6f957981237c0691db2269Winson Chung 2609112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 2619112ec3039dda4186c6f957981237c0691db2269Winson Chung * Takes a screenshot of the current display and shows an animation. 2629112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 2639112ec3039dda4186c6f957981237c0691db2269Winson Chung void takeScreenshot() { 2649112ec3039dda4186c6f957981237c0691db2269Winson Chung // We need to orient the screenshot correctly (and the Surface api seems to take screenshots 2659112ec3039dda4186c6f957981237c0691db2269Winson Chung // only in the natural orientation of the device :!) 2669112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplay.getRealMetrics(mDisplayMetrics); 2679112ec3039dda4186c6f957981237c0691db2269Winson Chung float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; 2689112ec3039dda4186c6f957981237c0691db2269Winson Chung float degrees = getDegreesForRotation(mDisplay.getRotation()); 2699112ec3039dda4186c6f957981237c0691db2269Winson Chung boolean requiresRotation = (degrees > 0); 2709112ec3039dda4186c6f957981237c0691db2269Winson Chung if (requiresRotation) { 2719112ec3039dda4186c6f957981237c0691db2269Winson Chung // Get the dimensions of the device in its native orientation 2729112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.reset(); 2739112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.preRotate(-degrees); 2749112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMatrix.mapPoints(dims); 2759112ec3039dda4186c6f957981237c0691db2269Winson Chung dims[0] = Math.abs(dims[0]); 2769112ec3039dda4186c6f957981237c0691db2269Winson Chung dims[1] = Math.abs(dims[1]); 2779112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2789112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]); 2799112ec3039dda4186c6f957981237c0691db2269Winson Chung if (requiresRotation) { 2809112ec3039dda4186c6f957981237c0691db2269Winson Chung // Rotate the screenshot to the current orientation 2819112ec3039dda4186c6f957981237c0691db2269Winson Chung Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, 2829112ec3039dda4186c6f957981237c0691db2269Winson Chung mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); 2839112ec3039dda4186c6f957981237c0691db2269Winson Chung Canvas c = new Canvas(ss); 2849112ec3039dda4186c6f957981237c0691db2269Winson Chung c.translate(ss.getWidth() / 2, ss.getHeight() / 2); 2859112ec3039dda4186c6f957981237c0691db2269Winson Chung c.rotate(360f - degrees); 2869112ec3039dda4186c6f957981237c0691db2269Winson Chung c.translate(-dims[0] / 2, -dims[1] / 2); 2879112ec3039dda4186c6f957981237c0691db2269Winson Chung c.drawBitmap(mScreenBitmap, 0, 0, null); 2889112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenBitmap = ss; 2899112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2909112ec3039dda4186c6f957981237c0691db2269Winson Chung 2919112ec3039dda4186c6f957981237c0691db2269Winson Chung // If we couldn't take the screenshot, notify the user 2929112ec3039dda4186c6f957981237c0691db2269Winson Chung if (mScreenBitmap == null) { 2939112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.makeText(mContext, R.string.screenshot_failed_toast, 2949112ec3039dda4186c6f957981237c0691db2269Winson Chung Toast.LENGTH_SHORT).show(); 2959112ec3039dda4186c6f957981237c0691db2269Winson Chung return; 2969112ec3039dda4186c6f957981237c0691db2269Winson Chung } 2979112ec3039dda4186c6f957981237c0691db2269Winson Chung 2989112ec3039dda4186c6f957981237c0691db2269Winson Chung // Start the post-screenshot animation 2999112ec3039dda4186c6f957981237c0691db2269Winson Chung startAnimation(); 3009112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3019112ec3039dda4186c6f957981237c0691db2269Winson Chung 3029112ec3039dda4186c6f957981237c0691db2269Winson Chung 3039112ec3039dda4186c6f957981237c0691db2269Winson Chung /** 3049112ec3039dda4186c6f957981237c0691db2269Winson Chung * Starts the animation after taking the screenshot 3059112ec3039dda4186c6f957981237c0691db2269Winson Chung */ 3069112ec3039dda4186c6f957981237c0691db2269Winson Chung private void startAnimation() { 3079112ec3039dda4186c6f957981237c0691db2269Winson Chung // Add the view for the animation 3089112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotView.setImageBitmap(mScreenBitmap); 3099112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotLayout.requestFocus(); 3109112ec3039dda4186c6f957981237c0691db2269Winson Chung 3119112ec3039dda4186c6f957981237c0691db2269Winson Chung // Setup the animation with the screenshot just taken 3129112ec3039dda4186c6f957981237c0691db2269Winson Chung if (mScreenshotAnimation != null) { 3139112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.end(); 3149112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3159112ec3039dda4186c6f957981237c0691db2269Winson Chung 3169112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); 3179112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator screenshotFadeInAnim = createScreenshotFadeInAnimation(); 3189112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator screenshotFadeOutAnim = createScreenshotFadeOutAnimation(); 3199112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation = new AnimatorSet(); 3209112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.play(screenshotFadeInAnim).before(screenshotFadeOutAnim); 3219112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { 3229112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 3239112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationEnd(Animator animation) { 3249112ec3039dda4186c6f957981237c0691db2269Winson Chung // Save the screenshot once we have a bit of time now 3259112ec3039dda4186c6f957981237c0691db2269Winson Chung saveScreenshotInWorkerThread(); 3269112ec3039dda4186c6f957981237c0691db2269Winson Chung 3279112ec3039dda4186c6f957981237c0691db2269Winson Chung mWindowManager.removeView(mScreenshotLayout); 3289112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3299112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 3309112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotAnimation.start(); 3319112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3329112ec3039dda4186c6f957981237c0691db2269Winson Chung private ValueAnimator createScreenshotFadeInAnimation() { 3339112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 3349112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setInterpolator(mCubicInterpolator); 3359112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setDuration(SCREENSHOT_FADE_IN_DURATION); 3369112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addListener(new AnimatorListenerAdapter() { 3379112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 3389112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationStart(Animator animation) { 3399112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setVisibility(View.VISIBLE); 3409112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setVisibility(View.VISIBLE); 3419112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3429112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 3439112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addUpdateListener(new AnimatorUpdateListener() { 3449112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 3459112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationUpdate(ValueAnimator animation) { 3469112ec3039dda4186c6f957981237c0691db2269Winson Chung float t = ((Float) animation.getAnimatedValue()).floatValue(); 3479112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setAlpha(mBackgroundViewAlphaInterpolator.getInterpolation(t) * 3489112ec3039dda4186c6f957981237c0691db2269Winson Chung BACKGROUND_ALPHA); 3499112ec3039dda4186c6f957981237c0691db2269Winson Chung float scaleT = SCREENSHOT_SCALE + (1f - t) * SCREENSHOT_SCALE; 3509112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setAlpha(t*t*t*t); 3519112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleX(scaleT); 3529112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleY(scaleT); 3539112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setRotation(t * SCREENSHOT_ROTATION); 3549112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3559112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 3569112ec3039dda4186c6f957981237c0691db2269Winson Chung return anim; 3579112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3589112ec3039dda4186c6f957981237c0691db2269Winson Chung private ValueAnimator createScreenshotFadeOutAnimation() { 3599112ec3039dda4186c6f957981237c0691db2269Winson Chung ValueAnimator anim = ValueAnimator.ofFloat(1f, 0f); 3609112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setInterpolator(mCubicInterpolator); 3619112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setStartDelay(SCREENSHOT_FADE_OUT_DELAY); 3629112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.setDuration(SCREENSHOT_FADE_OUT_DURATION); 3639112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addListener(new AnimatorListenerAdapter() { 3649112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 3659112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationEnd(Animator animation) { 3669112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setVisibility(View.GONE); 3679112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setVisibility(View.GONE); 3689112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3699112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 3709112ec3039dda4186c6f957981237c0691db2269Winson Chung anim.addUpdateListener(new AnimatorUpdateListener() { 3719112ec3039dda4186c6f957981237c0691db2269Winson Chung @Override 3729112ec3039dda4186c6f957981237c0691db2269Winson Chung public void onAnimationUpdate(ValueAnimator animation) { 3739112ec3039dda4186c6f957981237c0691db2269Winson Chung float t = ((Float) animation.getAnimatedValue()).floatValue(); 3749112ec3039dda4186c6f957981237c0691db2269Winson Chung float scaleT = SCREENSHOT_MIN_SCALE + 3759112ec3039dda4186c6f957981237c0691db2269Winson Chung t*(SCREENSHOT_SCALE - SCREENSHOT_MIN_SCALE); 3769112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setAlpha(t); 3779112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleX(scaleT); 3789112ec3039dda4186c6f957981237c0691db2269Winson Chung mScreenshotContainerView.setScaleY(scaleT); 3799112ec3039dda4186c6f957981237c0691db2269Winson Chung mBackgroundView.setAlpha(t * t * BACKGROUND_ALPHA); 3809112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3819112ec3039dda4186c6f957981237c0691db2269Winson Chung }); 3829112ec3039dda4186c6f957981237c0691db2269Winson Chung return anim; 3839112ec3039dda4186c6f957981237c0691db2269Winson Chung } 3849112ec3039dda4186c6f957981237c0691db2269Winson Chung} 385