GlobalScreenshot.java revision a63bb84bbe98e72871c2138ab3eb517d0f9a80ef
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;
419112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.ServiceManager;
429112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.provider.MediaStore;
439112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.util.DisplayMetrics;
449112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Display;
459112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.IWindowManager;
469112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.LayoutInflater;
479112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.MotionEvent;
489112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.Surface;
499112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.View;
509112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.ViewGroup;
519112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.view.WindowManager;
52a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.view.animation.AccelerateInterpolator;
53a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport android.view.animation.DecelerateInterpolator;
549112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.FrameLayout;
559112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.widget.ImageView;
569112ec3039dda4186c6f957981237c0691db2269Winson Chung
57a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chungimport com.android.server.wm.WindowManagerService;
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;
71fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    Runnable finisher;
72a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    int iconSize;
739112ec3039dda4186c6f957981237c0691db2269Winson Chung    int result;
749112ec3039dda4186c6f957981237c0691db2269Winson Chung}
759112ec3039dda4186c6f957981237c0691db2269Winson Chung
769112ec3039dda4186c6f957981237c0691db2269Winson Chung/**
779112ec3039dda4186c6f957981237c0691db2269Winson Chung * An AsyncTask that saves an image to the media store in the background.
789112ec3039dda4186c6f957981237c0691db2269Winson Chung */
799112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void,
809112ec3039dda4186c6f957981237c0691db2269Winson Chung        SaveImageInBackgroundData> {
819112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String TAG = "SaveImageInBackgroundTask";
829112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String SCREENSHOTS_DIR_NAME = "Screenshots";
83753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung    private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png";
84753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung    private static final String SCREENSHOT_FILE_PATH_TEMPLATE = "%s/%s/%s";
859112ec3039dda4186c6f957981237c0691db2269Winson Chung
86c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private int mNotificationId;
87c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private NotificationManager mNotificationManager;
88c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private Notification.Builder mNotificationBuilder;
89c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private Intent mLaunchIntent;
90c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageDir;
91c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageFileName;
92c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageFilePath;
93c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageDate;
94c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private long mImageTime;
95c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
96a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    // WORKAROUND: We want the same notification across screenshots that we update so that we don't
97a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    // spam a user's notification drawer.  However, we only show the ticker for the saving state
98a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    // and if the ticker text is the same as the previous notification, then it will not show. So
99a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    // for now, we just add and remove a space from the ticker text to trigger the animation when
100a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    // necessary.
101a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static boolean mTickerAddSpace;
102a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
103a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data,
104a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            NotificationManager nManager, int nId) {
105c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Resources r = context.getResources();
106c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
107c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Prepare all the output metadata
108c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageTime = System.currentTimeMillis();
109c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime));
110c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageDir = Environment.getExternalStoragePublicDirectory(
111c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                Environment.DIRECTORY_PICTURES).getAbsolutePath();
112c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, mImageDate);
113c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageFilePath = String.format(SCREENSHOT_FILE_PATH_TEMPLATE, mImageDir,
114c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                SCREENSHOTS_DIR_NAME, mImageFileName);
115c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
116a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        // Create the large notification icon
117a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        int imageWidth = data.image.getWidth();
118a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        int imageHeight = data.image.getHeight();
119a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        int iconWidth = data.iconSize;
120a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        int iconHeight = data.iconSize;
121a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        if (imageWidth > imageHeight) {
122a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            iconWidth = (int) (((float) iconHeight / imageHeight) * imageWidth);
123a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        } else {
124a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            iconHeight = (int) (((float) iconWidth / imageWidth) * imageHeight);
125a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        }
126a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        Bitmap rawIcon = Bitmap.createScaledBitmap(data.image, iconWidth, iconHeight, true);
127a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        Bitmap croppedIcon = Bitmap.createBitmap(rawIcon, (iconWidth - data.iconSize) / 2,
128a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                (iconHeight - data.iconSize) / 2, data.iconSize, data.iconSize);
129a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
130c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Show the intermediate notification
131c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent = new Intent(Intent.ACTION_VIEW);
132c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent.setDataAndType(Uri.fromFile(new File(mImageFilePath)), "image/png");
133c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
134a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mTickerAddSpace = !mTickerAddSpace;
135c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationId = nId;
136c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationBuilder = new Notification.Builder(context)
137a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            .setLargeIcon(croppedIcon)
138a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            .setTicker(r.getString(R.string.screenshot_saving_ticker)
139a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    + (mTickerAddSpace ? " " : ""))
140c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentTitle(r.getString(R.string.screenshot_saving_title))
141c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentText(r.getString(R.string.screenshot_saving_text))
142c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setSmallIcon(android.R.drawable.ic_menu_gallery)
143c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setWhen(System.currentTimeMillis());
144c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Notification n = mNotificationBuilder.getNotification();
145c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        n.flags |= Notification.FLAG_NO_CLEAR;
146c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
147c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager = nManager;
148c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager.notify(nId, n);
149c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    }
150c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
1519112ec3039dda4186c6f957981237c0691db2269Winson Chung    @Override
1529112ec3039dda4186c6f957981237c0691db2269Winson Chung    protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) {
1539112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (params.length != 1) return null;
1549112ec3039dda4186c6f957981237c0691db2269Winson Chung
1559112ec3039dda4186c6f957981237c0691db2269Winson Chung        Context context = params[0].context;
1569112ec3039dda4186c6f957981237c0691db2269Winson Chung        Bitmap image = params[0].image;
1579112ec3039dda4186c6f957981237c0691db2269Winson Chung
158d859fa399133da32705415e138c897f263ae99adWinson Chung        try {
1599112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Save the screenshot to the MediaStore
1609112ec3039dda4186c6f957981237c0691db2269Winson Chung            ContentValues values = new ContentValues();
1619837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            ContentResolver resolver = context.getContentResolver();
162c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
163c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
164c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
165c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
166c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_ADDED, mImageTime);
167c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, mImageTime);
1689112ec3039dda4186c6f957981237c0691db2269Winson Chung            values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
1699837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
1709112ec3039dda4186c6f957981237c0691db2269Winson Chung
1719837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            OutputStream out = resolver.openOutputStream(uri);
1729112ec3039dda4186c6f957981237c0691db2269Winson Chung            image.compress(Bitmap.CompressFormat.PNG, 100, out);
1739112ec3039dda4186c6f957981237c0691db2269Winson Chung            out.flush();
1749112ec3039dda4186c6f957981237c0691db2269Winson Chung            out.close();
1759112ec3039dda4186c6f957981237c0691db2269Winson Chung
1769837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            // update file size in the database
1779837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            values.clear();
178c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
1799837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            resolver.update(uri, values, null, null);
1809837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood
1819112ec3039dda4186c6f957981237c0691db2269Winson Chung            params[0].result = 0;
182d859fa399133da32705415e138c897f263ae99adWinson Chung        } catch (Exception e) {
183d859fa399133da32705415e138c897f263ae99adWinson Chung            // IOException/UnsupportedOperationException may be thrown if external storage is not
184d859fa399133da32705415e138c897f263ae99adWinson Chung            // mounted
1859112ec3039dda4186c6f957981237c0691db2269Winson Chung            params[0].result = 1;
1869112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
1879112ec3039dda4186c6f957981237c0691db2269Winson Chung
1889112ec3039dda4186c6f957981237c0691db2269Winson Chung        return params[0];
1899112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
1909112ec3039dda4186c6f957981237c0691db2269Winson Chung
1919112ec3039dda4186c6f957981237c0691db2269Winson Chung    @Override
1929112ec3039dda4186c6f957981237c0691db2269Winson Chung    protected void onPostExecute(SaveImageInBackgroundData params) {
1939112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (params.result > 0) {
1949112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Show a message that we've failed to save the image to disk
195c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager);
1969112ec3039dda4186c6f957981237c0691db2269Winson Chung        } else {
197c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            // Show the final notification to indicate screenshot saved
198c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            Resources r = params.context.getResources();
199c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
200c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            mNotificationBuilder
201c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentTitle(r.getString(R.string.screenshot_saved_title))
202c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentText(r.getString(R.string.screenshot_saved_text))
203c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentIntent(PendingIntent.getActivity(params.context, 0, mLaunchIntent, 0))
204c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setWhen(System.currentTimeMillis())
205c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setAutoCancel(true);
206c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
207c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            Notification n = mNotificationBuilder.getNotification();
208c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            n.flags &= ~Notification.FLAG_NO_CLEAR;
209c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            mNotificationManager.notify(mNotificationId, n);
2109112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
211fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn        params.finisher.run();
2129112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
2139112ec3039dda4186c6f957981237c0691db2269Winson Chung}
2149112ec3039dda4186c6f957981237c0691db2269Winson Chung
2159112ec3039dda4186c6f957981237c0691db2269Winson Chung/**
2169112ec3039dda4186c6f957981237c0691db2269Winson Chung * TODO:
2179112ec3039dda4186c6f957981237c0691db2269Winson Chung *   - Performance when over gl surfaces? Ie. Gallery
2189112ec3039dda4186c6f957981237c0691db2269Winson Chung *   - what do we say in the Toast? Which icon do we get if the user uses another
2199112ec3039dda4186c6f957981237c0691db2269Winson Chung *     type of gallery?
2209112ec3039dda4186c6f957981237c0691db2269Winson Chung */
2219112ec3039dda4186c6f957981237c0691db2269Winson Chungclass GlobalScreenshot {
2229112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String TAG = "GlobalScreenshot";
223c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final int SCREENSHOT_NOTIFICATION_ID = 789;
224a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final int SCREENSHOT_FADE_IN_DURATION = 250;
225a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final int SCREENSHOT_FADE_OUT_DELAY = 750;
226a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final int SCREENSHOT_FADE_OUT_DURATION = 500;
227a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final int SCREENSHOT_FAST_FADE_OUT_DURATION = 350;
2289112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final float BACKGROUND_ALPHA = 0.65f;
229c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final float SCREENSHOT_SCALE_FUDGE = 0.075f; // To account for the border padding
230a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final float SCREENSHOT_SCALE = 0.55f;
231a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final float SCREENSHOT_FADE_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.975f;
232a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private static final float SCREENSHOT_FADE_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.925f;
2339112ec3039dda4186c6f957981237c0691db2269Winson Chung
2349112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Context mContext;
2359112ec3039dda4186c6f957981237c0691db2269Winson Chung    private LayoutInflater mLayoutInflater;
2369112ec3039dda4186c6f957981237c0691db2269Winson Chung    private IWindowManager mIWindowManager;
2379112ec3039dda4186c6f957981237c0691db2269Winson Chung    private WindowManager mWindowManager;
2389112ec3039dda4186c6f957981237c0691db2269Winson Chung    private WindowManager.LayoutParams mWindowLayoutParams;
239c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private NotificationManager mNotificationManager;
2409112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Display mDisplay;
2419112ec3039dda4186c6f957981237c0691db2269Winson Chung    private DisplayMetrics mDisplayMetrics;
2429112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Matrix mDisplayMatrix;
2439112ec3039dda4186c6f957981237c0691db2269Winson Chung
2449112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Bitmap mScreenBitmap;
2459112ec3039dda4186c6f957981237c0691db2269Winson Chung    private View mScreenshotLayout;
2469112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ImageView mBackgroundView;
2479112ec3039dda4186c6f957981237c0691db2269Winson Chung    private FrameLayout mScreenshotContainerView;
2489112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ImageView mScreenshotView;
2499112ec3039dda4186c6f957981237c0691db2269Winson Chung
2509112ec3039dda4186c6f957981237c0691db2269Winson Chung    private AnimatorSet mScreenshotAnimation;
2519112ec3039dda4186c6f957981237c0691db2269Winson Chung
252a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private int mStatusBarIconSize;
253a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private int mNotificationIconSize;
254a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private float mDropOffsetX;
255a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private float mDropOffsetY;
256a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private float mBgPadding;
257a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private float mBgPaddingScale;
258a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
2599112ec3039dda4186c6f957981237c0691db2269Winson Chung
2609112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
2619112ec3039dda4186c6f957981237c0691db2269Winson Chung     * @param context everything needs a context :(
2629112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
2639112ec3039dda4186c6f957981237c0691db2269Winson Chung    public GlobalScreenshot(Context context) {
264a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        Resources r = context.getResources();
2659112ec3039dda4186c6f957981237c0691db2269Winson Chung        mContext = context;
2669112ec3039dda4186c6f957981237c0691db2269Winson Chung        mLayoutInflater = (LayoutInflater)
2679112ec3039dda4186c6f957981237c0691db2269Winson Chung                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2689112ec3039dda4186c6f957981237c0691db2269Winson Chung
2699112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Inflate the screenshot layout
2709112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplayMatrix = new Matrix();
2719112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout = mLayoutInflater.inflate(R.layout.global_screenshot, null);
2729112ec3039dda4186c6f957981237c0691db2269Winson Chung        mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background);
2739112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotContainerView = (FrameLayout) mScreenshotLayout.findViewById(R.id.global_screenshot_container);
2749112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot);
2759112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.setFocusable(true);
2769112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() {
2779112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
2789112ec3039dda4186c6f957981237c0691db2269Winson Chung            public boolean onTouch(View v, MotionEvent event) {
2799112ec3039dda4186c6f957981237c0691db2269Winson Chung                // Intercept and ignore all touch events
2809112ec3039dda4186c6f957981237c0691db2269Winson Chung                return true;
2819112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
2829112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
2839112ec3039dda4186c6f957981237c0691db2269Winson Chung
2849112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Setup the window that we are going to use
2859112ec3039dda4186c6f957981237c0691db2269Winson Chung        mIWindowManager = IWindowManager.Stub.asInterface(
2869112ec3039dda4186c6f957981237c0691db2269Winson Chung                ServiceManager.getService(Context.WINDOW_SERVICE));
2879112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowLayoutParams = new WindowManager.LayoutParams(
2889112ec3039dda4186c6f957981237c0691db2269Winson Chung                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
2899112ec3039dda4186c6f957981237c0691db2269Winson Chung                WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY,
2909112ec3039dda4186c6f957981237c0691db2269Winson Chung                WindowManager.LayoutParams.FLAG_FULLSCREEN
2919112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
2929112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
2939112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
2949112ec3039dda4186c6f957981237c0691db2269Winson Chung                PixelFormat.TRANSLUCENT);
2959112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowLayoutParams.setTitle("ScreenshotAnimation");
2969112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
297c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager =
298c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2999112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplay = mWindowManager.getDefaultDisplay();
300a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mDisplayMetrics = new DisplayMetrics();
301a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mDisplay.getRealMetrics(mDisplayMetrics);
302a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
303a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        // Get the various target sizes
304a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mStatusBarIconSize =
305a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            r.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_icon_size);
306a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mNotificationIconSize =
307a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
308a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mDropOffsetX = r.getDimensionPixelSize(R.dimen.global_screenshot_drop_offset_x);
309a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mDropOffsetY = r.getDimensionPixelSize(R.dimen.global_screenshot_drop_offset_y);
310a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
311a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        // Scale has to account for both sides of the bg
312a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding);
313a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mBgPaddingScale = (2f * mBgPadding) /  mDisplayMetrics.widthPixels;
3149112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3159112ec3039dda4186c6f957981237c0691db2269Winson Chung
3169112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3179112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Creates a new worker thread and saves the screenshot to the media store.
3189112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
319fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    private void saveScreenshotInWorkerThread(Runnable finisher) {
3209112ec3039dda4186c6f957981237c0691db2269Winson Chung        SaveImageInBackgroundData data = new SaveImageInBackgroundData();
3219112ec3039dda4186c6f957981237c0691db2269Winson Chung        data.context = mContext;
3229112ec3039dda4186c6f957981237c0691db2269Winson Chung        data.image = mScreenBitmap;
323a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        data.iconSize = mNotificationIconSize;
324fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn        data.finisher = finisher;
325a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        new SaveImageInBackgroundTask(mContext, data, mNotificationManager,
326a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                SCREENSHOT_NOTIFICATION_ID).execute(data);
3279112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3289112ec3039dda4186c6f957981237c0691db2269Winson Chung
3299112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3309112ec3039dda4186c6f957981237c0691db2269Winson Chung     * @return the current display rotation in degrees
3319112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
3329112ec3039dda4186c6f957981237c0691db2269Winson Chung    private float getDegreesForRotation(int value) {
3339112ec3039dda4186c6f957981237c0691db2269Winson Chung        switch (value) {
3349112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_90:
335a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            return 360f - 90f;
3369112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_180:
337a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            return 360f - 180f;
3389112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_270:
339a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            return 360f - 270f;
3409112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3419112ec3039dda4186c6f957981237c0691db2269Winson Chung        return 0f;
3429112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3439112ec3039dda4186c6f957981237c0691db2269Winson Chung
3449112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3459112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Takes a screenshot of the current display and shows an animation.
3469112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
347a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {
3489112ec3039dda4186c6f957981237c0691db2269Winson Chung        // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
3499112ec3039dda4186c6f957981237c0691db2269Winson Chung        // only in the natural orientation of the device :!)
3509112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplay.getRealMetrics(mDisplayMetrics);
3519112ec3039dda4186c6f957981237c0691db2269Winson Chung        float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};
3529112ec3039dda4186c6f957981237c0691db2269Winson Chung        float degrees = getDegreesForRotation(mDisplay.getRotation());
3539112ec3039dda4186c6f957981237c0691db2269Winson Chung        boolean requiresRotation = (degrees > 0);
3549112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (requiresRotation) {
3559112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Get the dimensions of the device in its native orientation
3569112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.reset();
3579112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.preRotate(-degrees);
3589112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.mapPoints(dims);
3599112ec3039dda4186c6f957981237c0691db2269Winson Chung            dims[0] = Math.abs(dims[0]);
3609112ec3039dda4186c6f957981237c0691db2269Winson Chung            dims[1] = Math.abs(dims[1]);
3619112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3629112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);
3639112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (requiresRotation) {
3649112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Rotate the screenshot to the current orientation
3659112ec3039dda4186c6f957981237c0691db2269Winson Chung            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
3669112ec3039dda4186c6f957981237c0691db2269Winson Chung                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
3679112ec3039dda4186c6f957981237c0691db2269Winson Chung            Canvas c = new Canvas(ss);
3689112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
369a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            c.rotate(degrees);
3709112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.translate(-dims[0] / 2, -dims[1] / 2);
3719112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.drawBitmap(mScreenBitmap, 0, 0, null);
3726311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn            c.setBitmap(null);
3739112ec3039dda4186c6f957981237c0691db2269Winson Chung            mScreenBitmap = ss;
3749112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3759112ec3039dda4186c6f957981237c0691db2269Winson Chung
3769112ec3039dda4186c6f957981237c0691db2269Winson Chung        // If we couldn't take the screenshot, notify the user
3779112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (mScreenBitmap == null) {
378c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            notifyScreenshotError(mContext, mNotificationManager);
379fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn            finisher.run();
3809112ec3039dda4186c6f957981237c0691db2269Winson Chung            return;
3819112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3829112ec3039dda4186c6f957981237c0691db2269Winson Chung
383a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        // Optimizations
384a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mScreenBitmap.setHasAlpha(false);
385a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mScreenBitmap.prepareToDraw();
386a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
3879112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Start the post-screenshot animation
388a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
389a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                statusBarVisible, navBarVisible);
3909112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3919112ec3039dda4186c6f957981237c0691db2269Winson Chung
3929112ec3039dda4186c6f957981237c0691db2269Winson Chung
3939112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3949112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Starts the animation after taking the screenshot
3959112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
396a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible,
397a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            boolean navBarVisible) {
3989112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Add the view for the animation
3999112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotView.setImageBitmap(mScreenBitmap);
4009112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.requestFocus();
4019112ec3039dda4186c6f957981237c0691db2269Winson Chung
4029112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Setup the animation with the screenshot just taken
4039112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (mScreenshotAnimation != null) {
4049112ec3039dda4186c6f957981237c0691db2269Winson Chung            mScreenshotAnimation.end();
4059112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
4069112ec3039dda4186c6f957981237c0691db2269Winson Chung
4079112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
4089112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator screenshotFadeInAnim = createScreenshotFadeInAnimation();
409a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        ValueAnimator screenshotFadeOutAnim = createScreenshotFadeOutAnimation(w, h,
410a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                statusBarVisible, navBarVisible);
4119112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation = new AnimatorSet();
4129112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation.play(screenshotFadeInAnim).before(screenshotFadeOutAnim);
4139112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation.addListener(new AnimatorListenerAdapter() {
4149112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4159112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationEnd(Animator animation) {
4169112ec3039dda4186c6f957981237c0691db2269Winson Chung                // Save the screenshot once we have a bit of time now
417fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn                saveScreenshotInWorkerThread(finisher);
4189112ec3039dda4186c6f957981237c0691db2269Winson Chung                mWindowManager.removeView(mScreenshotLayout);
4199112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4209112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
421a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        mScreenshotLayout.post(new Runnable() {
422a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            @Override
423a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            public void run() {
424a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
425a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.buildLayer();
426a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotAnimation.start();
427a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            }
428a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        });
4299112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
4309112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ValueAnimator createScreenshotFadeInAnimation() {
4319112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
432a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        anim.setInterpolator(new AccelerateInterpolator(1.5f));
4339112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.setDuration(SCREENSHOT_FADE_IN_DURATION);
4349112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addListener(new AnimatorListenerAdapter() {
4359112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4369112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationStart(Animator animation) {
437a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mBackgroundView.setAlpha(0f);
4389112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setVisibility(View.VISIBLE);
439a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setTranslationX(0f);
440c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                mScreenshotContainerView.setTranslationY(0f);
441a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setScaleX(SCREENSHOT_FADE_IN_MIN_SCALE);
442a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setScaleY(SCREENSHOT_FADE_IN_MIN_SCALE);
443a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setAlpha(0f);
4449112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setVisibility(View.VISIBLE);
4459112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4469112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
4479112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addUpdateListener(new AnimatorUpdateListener() {
4489112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4499112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationUpdate(ValueAnimator animation) {
4509112ec3039dda4186c6f957981237c0691db2269Winson Chung                float t = ((Float) animation.getAnimatedValue()).floatValue();
451a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                float scaleT = (SCREENSHOT_FADE_IN_MIN_SCALE)
452a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    + (float) t * (SCREENSHOT_SCALE - SCREENSHOT_FADE_IN_MIN_SCALE);
453a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mBackgroundView.setAlpha(t * BACKGROUND_ALPHA);
4549112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleX(scaleT);
4559112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleY(scaleT);
456a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setAlpha(t);
4579112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4589112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
4599112ec3039dda4186c6f957981237c0691db2269Winson Chung        return anim;
4609112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
461a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung    private ValueAnimator createScreenshotFadeOutAnimation(int w, int h, boolean statusBarVisible,
462a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            boolean navBarVisible) {
463a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
464a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        anim.setInterpolator(new DecelerateInterpolator(0.5f));
4659112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.setStartDelay(SCREENSHOT_FADE_OUT_DELAY);
4669112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addListener(new AnimatorListenerAdapter() {
4679112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4689112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationEnd(Animator animation) {
4699112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setVisibility(View.GONE);
4709112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setVisibility(View.GONE);
471a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                mScreenshotContainerView.setLayerType(View.LAYER_TYPE_NONE, null);
4729112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4739112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
474a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
475a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        if (!statusBarVisible || !navBarVisible) {
476a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            // There is no status bar/nav bar, so just fade the screenshot away in place
477a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            anim.setDuration(SCREENSHOT_FAST_FADE_OUT_DURATION);
478a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            anim.addUpdateListener(new AnimatorUpdateListener() {
479a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                @Override
480a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                public void onAnimationUpdate(ValueAnimator animation) {
481a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    float t = ((Float) animation.getAnimatedValue()).floatValue();
482a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    float scaleT = (SCREENSHOT_FADE_OUT_MIN_SCALE)
483a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                            + (float) (1f - t) * (SCREENSHOT_SCALE - SCREENSHOT_FADE_OUT_MIN_SCALE);
484a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA);
485a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setAlpha((1f - t) * BACKGROUND_ALPHA);
486a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setScaleX(scaleT);
487a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setScaleY(scaleT);
488a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                }
489a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            });
490a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        } else {
491a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            // Determine the bounds of how to scale
492a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            float halfScreenWidth = (w - 2f * mBgPadding) / 2f;
493a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            float halfScreenHeight = (h - 2f * mBgPadding) / 2f;
494a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            final RectF finalBounds = new RectF(mDropOffsetX, mDropOffsetY,
495a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mDropOffsetX + mStatusBarIconSize,
496a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mDropOffsetY + mStatusBarIconSize);
497a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            final PointF currentPos = new PointF(0f, 0f);
498a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            final PointF finalPos = new PointF(-halfScreenWidth + finalBounds.centerX(),
499a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    -halfScreenHeight + finalBounds.centerY());
500a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            final DecelerateInterpolator d = new DecelerateInterpolator(2f);
501a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            // Note: since the scale origin is in the center of the view, divide difference by 2
502a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            float tmpMinScale = 0f;
503a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            if (w > h) {
504a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                tmpMinScale = finalBounds.width() / (2f * w);
505a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            } else {
506a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                tmpMinScale = finalBounds.height() / (2f * h);
5079112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
508a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            final float minScale = tmpMinScale;
509a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung
510a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            // Animate the screenshot to the status bar
511a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            anim.setDuration(SCREENSHOT_FADE_OUT_DURATION);
512a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            anim.addUpdateListener(new AnimatorUpdateListener() {
513a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                @Override
514a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                public void onAnimationUpdate(ValueAnimator animation) {
515a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    float t = ((Float) animation.getAnimatedValue()).floatValue();
516a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    float scaleT = minScale
517a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                            + (float) (1f - t) * (SCREENSHOT_SCALE - minScale - mBgPaddingScale)
518a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                            + mBgPaddingScale;
519a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setAlpha(d.getInterpolation(1f - t));
520a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setTranslationX(d.getInterpolation(t) * finalPos.x);
521a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setTranslationY(d.getInterpolation(t) * finalPos.y);
522a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setScaleX(scaleT);
523a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mScreenshotContainerView.setScaleY(scaleT);
524a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                    mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA);
525a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung                }
526a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung            });
527a63bb84bbe98e72871c2138ab3eb517d0f9a80efWinson Chung        }
5289112ec3039dda4186c6f957981237c0691db2269Winson Chung        return anim;
5299112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
530c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
531c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    static void notifyScreenshotError(Context context, NotificationManager nManager) {
532c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Resources r = context.getResources();
533c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
534c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Clear all existing notification, compose the new notification and show it
535c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Notification n = new Notification.Builder(context)
536c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setTicker(r.getString(R.string.screenshot_failed_title))
537c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentTitle(r.getString(R.string.screenshot_failed_title))
538c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentText(r.getString(R.string.screenshot_failed_text))
539c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setSmallIcon(android.R.drawable.ic_menu_report_image)
540c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setWhen(System.currentTimeMillis())
541c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setAutoCancel(true)
542c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .getNotification();
543c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        nManager.notify(SCREENSHOT_NOTIFICATION_ID, n);
544c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    }
5459112ec3039dda4186c6f957981237c0691db2269Winson Chung}
546