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