GlobalScreenshot.java revision c57ccf01fe24ce508404c99b449e9097e6d8b270
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.TimeInterpolator;
239112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator;
249112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.animation.ValueAnimator.AnimatorUpdateListener;
25c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.Notification;
26c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.NotificationManager;
27c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.app.PendingIntent;
289837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwoodimport android.content.ContentResolver;
299112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.ContentValues;
309112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.content.Context;
31c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.content.Intent;
32c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chungimport android.content.res.Resources;
339112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Bitmap;
349112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Canvas;
359112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.Matrix;
369112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.graphics.PixelFormat;
379112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.net.Uri;
389112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.AsyncTask;
399112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.Environment;
409112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.os.ServiceManager;
419112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.provider.MediaStore;
429112ec3039dda4186c6f957981237c0691db2269Winson Chungimport android.util.DisplayMetrics;
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 Chung
549112ec3039dda4186c6f957981237c0691db2269Winson Chungimport com.android.systemui.R;
559112ec3039dda4186c6f957981237c0691db2269Winson Chung
569112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.File;
579112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.io.OutputStream;
589112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.text.SimpleDateFormat;
599112ec3039dda4186c6f957981237c0691db2269Winson Chungimport java.util.Date;
609112ec3039dda4186c6f957981237c0691db2269Winson Chung
619112ec3039dda4186c6f957981237c0691db2269Winson Chung/**
629112ec3039dda4186c6f957981237c0691db2269Winson Chung * POD used in the AsyncTask which saves an image in the background.
639112ec3039dda4186c6f957981237c0691db2269Winson Chung */
649112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundData {
659112ec3039dda4186c6f957981237c0691db2269Winson Chung    Context context;
669112ec3039dda4186c6f957981237c0691db2269Winson Chung    Bitmap image;
67fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    Runnable finisher;
689112ec3039dda4186c6f957981237c0691db2269Winson Chung    int result;
699112ec3039dda4186c6f957981237c0691db2269Winson Chung}
709112ec3039dda4186c6f957981237c0691db2269Winson Chung
719112ec3039dda4186c6f957981237c0691db2269Winson Chung/**
729112ec3039dda4186c6f957981237c0691db2269Winson Chung * An AsyncTask that saves an image to the media store in the background.
739112ec3039dda4186c6f957981237c0691db2269Winson Chung */
749112ec3039dda4186c6f957981237c0691db2269Winson Chungclass SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void,
759112ec3039dda4186c6f957981237c0691db2269Winson Chung        SaveImageInBackgroundData> {
769112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String TAG = "SaveImageInBackgroundTask";
779112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String SCREENSHOTS_DIR_NAME = "Screenshots";
78753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung    private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png";
79753e40b1472563987489bd5b187ced4c1b608b0dWinson Chung    private static final String SCREENSHOT_FILE_PATH_TEMPLATE = "%s/%s/%s";
809112ec3039dda4186c6f957981237c0691db2269Winson Chung
81c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private int mNotificationId;
82c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private NotificationManager mNotificationManager;
83c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private Notification.Builder mNotificationBuilder;
84c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private Intent mLaunchIntent;
85c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageDir;
86c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageFileName;
87c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageFilePath;
88c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private String mImageDate;
89c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private long mImageTime;
90c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
91c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    SaveImageInBackgroundTask(Context context, NotificationManager nManager, int nId) {
92c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Resources r = context.getResources();
93c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
94c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Prepare all the output metadata
95c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageTime = System.currentTimeMillis();
96c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime));
97c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageDir = Environment.getExternalStoragePublicDirectory(
98c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                Environment.DIRECTORY_PICTURES).getAbsolutePath();
99c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, mImageDate);
100c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mImageFilePath = String.format(SCREENSHOT_FILE_PATH_TEMPLATE, mImageDir,
101c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                SCREENSHOTS_DIR_NAME, mImageFileName);
102c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
103c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Show the intermediate notification
104c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent = new Intent(Intent.ACTION_VIEW);
105c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent.setDataAndType(Uri.fromFile(new File(mImageFilePath)), "image/png");
106c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
107c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationId = nId;
108c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationBuilder = new Notification.Builder(context)
109c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setTicker(r.getString(R.string.screenshot_saving_ticker))
110c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentTitle(r.getString(R.string.screenshot_saving_title))
111c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentText(r.getString(R.string.screenshot_saving_text))
112c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setSmallIcon(android.R.drawable.ic_menu_gallery)
113c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setWhen(System.currentTimeMillis());
114c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Notification n = mNotificationBuilder.getNotification();
115c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        n.flags |= Notification.FLAG_NO_CLEAR;
116c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
117c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager = nManager;
118c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager.notify(nId, n);
119c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    }
120c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
1219112ec3039dda4186c6f957981237c0691db2269Winson Chung    @Override
1229112ec3039dda4186c6f957981237c0691db2269Winson Chung    protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) {
1239112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (params.length != 1) return null;
1249112ec3039dda4186c6f957981237c0691db2269Winson Chung
1259112ec3039dda4186c6f957981237c0691db2269Winson Chung        Context context = params[0].context;
1269112ec3039dda4186c6f957981237c0691db2269Winson Chung        Bitmap image = params[0].image;
1279112ec3039dda4186c6f957981237c0691db2269Winson Chung
128d859fa399133da32705415e138c897f263ae99adWinson Chung        try {
1299112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Save the screenshot to the MediaStore
1309112ec3039dda4186c6f957981237c0691db2269Winson Chung            ContentValues values = new ContentValues();
1319837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            ContentResolver resolver = context.getContentResolver();
132c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
133c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
134c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
135c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
136c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_ADDED, mImageTime);
137c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, mImageTime);
1389112ec3039dda4186c6f957981237c0691db2269Winson Chung            values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
1399837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
1409112ec3039dda4186c6f957981237c0691db2269Winson Chung
1419837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            OutputStream out = resolver.openOutputStream(uri);
1429112ec3039dda4186c6f957981237c0691db2269Winson Chung            image.compress(Bitmap.CompressFormat.PNG, 100, out);
1439112ec3039dda4186c6f957981237c0691db2269Winson Chung            out.flush();
1449112ec3039dda4186c6f957981237c0691db2269Winson Chung            out.close();
1459112ec3039dda4186c6f957981237c0691db2269Winson Chung
1469837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            // update file size in the database
1479837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            values.clear();
148c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
1499837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood            resolver.update(uri, values, null, null);
1509837734b640ca3a1b95812612fc01c517fb02eb4Mike Lockwood
1519112ec3039dda4186c6f957981237c0691db2269Winson Chung            params[0].result = 0;
152d859fa399133da32705415e138c897f263ae99adWinson Chung        } catch (Exception e) {
153d859fa399133da32705415e138c897f263ae99adWinson Chung            // IOException/UnsupportedOperationException may be thrown if external storage is not
154d859fa399133da32705415e138c897f263ae99adWinson Chung            // mounted
1559112ec3039dda4186c6f957981237c0691db2269Winson Chung            params[0].result = 1;
1569112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
1579112ec3039dda4186c6f957981237c0691db2269Winson Chung
1589112ec3039dda4186c6f957981237c0691db2269Winson Chung        return params[0];
1599112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
1609112ec3039dda4186c6f957981237c0691db2269Winson Chung
1619112ec3039dda4186c6f957981237c0691db2269Winson Chung    @Override
1629112ec3039dda4186c6f957981237c0691db2269Winson Chung    protected void onPostExecute(SaveImageInBackgroundData params) {
1639112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (params.result > 0) {
1649112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Show a message that we've failed to save the image to disk
165c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager);
1669112ec3039dda4186c6f957981237c0691db2269Winson Chung        } else {
167c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            // Show the final notification to indicate screenshot saved
168c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            Resources r = params.context.getResources();
169c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
170c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            mNotificationBuilder
171c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setTicker(r.getString(R.string.screenshot_saved_title))
172c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentTitle(r.getString(R.string.screenshot_saved_title))
173c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentText(r.getString(R.string.screenshot_saved_text))
174c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setContentIntent(PendingIntent.getActivity(params.context, 0, mLaunchIntent, 0))
175c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setWhen(System.currentTimeMillis())
176c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .setAutoCancel(true);
177c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
178c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            Notification n = mNotificationBuilder.getNotification();
179c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            n.flags &= ~Notification.FLAG_NO_CLEAR;
180c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            mNotificationManager.notify(mNotificationId, n);
1819112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
182fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn        params.finisher.run();
1839112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
1849112ec3039dda4186c6f957981237c0691db2269Winson Chung}
1859112ec3039dda4186c6f957981237c0691db2269Winson Chung
1869112ec3039dda4186c6f957981237c0691db2269Winson Chung/**
1879112ec3039dda4186c6f957981237c0691db2269Winson Chung * TODO:
1889112ec3039dda4186c6f957981237c0691db2269Winson Chung *   - Performance when over gl surfaces? Ie. Gallery
1899112ec3039dda4186c6f957981237c0691db2269Winson Chung *   - what do we say in the Toast? Which icon do we get if the user uses another
1909112ec3039dda4186c6f957981237c0691db2269Winson Chung *     type of gallery?
1919112ec3039dda4186c6f957981237c0691db2269Winson Chung */
1929112ec3039dda4186c6f957981237c0691db2269Winson Chungclass GlobalScreenshot {
1939112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final String TAG = "GlobalScreenshot";
194c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final int SCREENSHOT_NOTIFICATION_ID = 789;
195c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final int SCREENSHOT_FADE_IN_DURATION = 500;
1969112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final int SCREENSHOT_FADE_OUT_DELAY = 1000;
197c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final int SCREENSHOT_FADE_OUT_DURATION = 300;
1989112ec3039dda4186c6f957981237c0691db2269Winson Chung    private static final float BACKGROUND_ALPHA = 0.65f;
199c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final float SCREENSHOT_SCALE_FUDGE = 0.075f; // To account for the border padding
200c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final float SCREENSHOT_SCALE = 0.8f;
201c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private static final float SCREENSHOT_MIN_SCALE = 0.775f;
2029112ec3039dda4186c6f957981237c0691db2269Winson Chung
2039112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Context mContext;
2049112ec3039dda4186c6f957981237c0691db2269Winson Chung    private LayoutInflater mLayoutInflater;
2059112ec3039dda4186c6f957981237c0691db2269Winson Chung    private IWindowManager mIWindowManager;
2069112ec3039dda4186c6f957981237c0691db2269Winson Chung    private WindowManager mWindowManager;
2079112ec3039dda4186c6f957981237c0691db2269Winson Chung    private WindowManager.LayoutParams mWindowLayoutParams;
208c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    private NotificationManager mNotificationManager;
2099112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Display mDisplay;
2109112ec3039dda4186c6f957981237c0691db2269Winson Chung    private DisplayMetrics mDisplayMetrics;
2119112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Matrix mDisplayMatrix;
2129112ec3039dda4186c6f957981237c0691db2269Winson Chung
2139112ec3039dda4186c6f957981237c0691db2269Winson Chung    private Bitmap mScreenBitmap;
2149112ec3039dda4186c6f957981237c0691db2269Winson Chung    private View mScreenshotLayout;
2159112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ImageView mBackgroundView;
2169112ec3039dda4186c6f957981237c0691db2269Winson Chung    private FrameLayout mScreenshotContainerView;
2179112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ImageView mScreenshotView;
2189112ec3039dda4186c6f957981237c0691db2269Winson Chung
2199112ec3039dda4186c6f957981237c0691db2269Winson Chung    private AnimatorSet mScreenshotAnimation;
2209112ec3039dda4186c6f957981237c0691db2269Winson Chung
221c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    // Fade interpolators
222c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    final TimeInterpolator mFadeInInterpolator = new TimeInterpolator() {
223c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        public float getInterpolation(float t) {
224c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            return (float) Math.pow(t, 1.5f);
225c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        }
226c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    };
227c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    final TimeInterpolator mFadeOutInterpolator = new TimeInterpolator() {
2289112ec3039dda4186c6f957981237c0691db2269Winson Chung        public float getInterpolation(float t) {
229c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            return (float) t;
2309112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
2319112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
2329112ec3039dda4186c6f957981237c0691db2269Winson Chung    // The interpolator used to control the background alpha at the start of the animation
2339112ec3039dda4186c6f957981237c0691db2269Winson Chung    final TimeInterpolator mBackgroundViewAlphaInterpolator = new TimeInterpolator() {
2349112ec3039dda4186c6f957981237c0691db2269Winson Chung        public float getInterpolation(float t) {
2359112ec3039dda4186c6f957981237c0691db2269Winson Chung            float tStep = 0.35f;
2369112ec3039dda4186c6f957981237c0691db2269Winson Chung            if (t < tStep) {
2379112ec3039dda4186c6f957981237c0691db2269Winson Chung                return t * (1f / tStep);
2389112ec3039dda4186c6f957981237c0691db2269Winson Chung            } else {
2399112ec3039dda4186c6f957981237c0691db2269Winson Chung                return 1f;
2409112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
2419112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
2429112ec3039dda4186c6f957981237c0691db2269Winson Chung    };
2439112ec3039dda4186c6f957981237c0691db2269Winson Chung
2449112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
2459112ec3039dda4186c6f957981237c0691db2269Winson Chung     * @param context everything needs a context :(
2469112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
2479112ec3039dda4186c6f957981237c0691db2269Winson Chung    public GlobalScreenshot(Context context) {
2489112ec3039dda4186c6f957981237c0691db2269Winson Chung        mContext = context;
2499112ec3039dda4186c6f957981237c0691db2269Winson Chung        mLayoutInflater = (LayoutInflater)
2509112ec3039dda4186c6f957981237c0691db2269Winson Chung                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2519112ec3039dda4186c6f957981237c0691db2269Winson Chung
2529112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Inflate the screenshot layout
2539112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplayMetrics = new DisplayMetrics();
2549112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplayMatrix = new Matrix();
2559112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout = mLayoutInflater.inflate(R.layout.global_screenshot, null);
2569112ec3039dda4186c6f957981237c0691db2269Winson Chung        mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background);
2579112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotContainerView = (FrameLayout) mScreenshotLayout.findViewById(R.id.global_screenshot_container);
2589112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot);
2599112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.setFocusable(true);
2609112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() {
2619112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
2629112ec3039dda4186c6f957981237c0691db2269Winson Chung            public boolean onTouch(View v, MotionEvent event) {
2639112ec3039dda4186c6f957981237c0691db2269Winson Chung                // Intercept and ignore all touch events
2649112ec3039dda4186c6f957981237c0691db2269Winson Chung                return true;
2659112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
2669112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
2679112ec3039dda4186c6f957981237c0691db2269Winson Chung
2689112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Setup the window that we are going to use
2699112ec3039dda4186c6f957981237c0691db2269Winson Chung        mIWindowManager = IWindowManager.Stub.asInterface(
2709112ec3039dda4186c6f957981237c0691db2269Winson Chung                ServiceManager.getService(Context.WINDOW_SERVICE));
2719112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowLayoutParams = new WindowManager.LayoutParams(
2729112ec3039dda4186c6f957981237c0691db2269Winson Chung                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
2739112ec3039dda4186c6f957981237c0691db2269Winson Chung                WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY,
2749112ec3039dda4186c6f957981237c0691db2269Winson Chung                WindowManager.LayoutParams.FLAG_FULLSCREEN
2759112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
2769112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
2779112ec3039dda4186c6f957981237c0691db2269Winson Chung                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
2789112ec3039dda4186c6f957981237c0691db2269Winson Chung                PixelFormat.TRANSLUCENT);
2799112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowLayoutParams.setTitle("ScreenshotAnimation");
2809112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
281c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        mNotificationManager =
282c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2839112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplay = mWindowManager.getDefaultDisplay();
2849112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
2859112ec3039dda4186c6f957981237c0691db2269Winson Chung
2869112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
2879112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Creates a new worker thread and saves the screenshot to the media store.
2889112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
289fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    private void saveScreenshotInWorkerThread(Runnable finisher) {
2909112ec3039dda4186c6f957981237c0691db2269Winson Chung        SaveImageInBackgroundData data = new SaveImageInBackgroundData();
2919112ec3039dda4186c6f957981237c0691db2269Winson Chung        data.context = mContext;
2929112ec3039dda4186c6f957981237c0691db2269Winson Chung        data.image = mScreenBitmap;
293fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn        data.finisher = finisher;
294c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        new SaveImageInBackgroundTask(mContext, mNotificationManager, SCREENSHOT_NOTIFICATION_ID)
295c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                .execute(data);
2969112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
2979112ec3039dda4186c6f957981237c0691db2269Winson Chung
2989112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
2999112ec3039dda4186c6f957981237c0691db2269Winson Chung     * @return the current display rotation in degrees
3009112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
3019112ec3039dda4186c6f957981237c0691db2269Winson Chung    private float getDegreesForRotation(int value) {
3029112ec3039dda4186c6f957981237c0691db2269Winson Chung        switch (value) {
3039112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_90:
3049112ec3039dda4186c6f957981237c0691db2269Winson Chung            return 90f;
3059112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_180:
3069112ec3039dda4186c6f957981237c0691db2269Winson Chung            return 180f;
3079112ec3039dda4186c6f957981237c0691db2269Winson Chung        case Surface.ROTATION_270:
3089112ec3039dda4186c6f957981237c0691db2269Winson Chung            return 270f;
3099112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3109112ec3039dda4186c6f957981237c0691db2269Winson Chung        return 0f;
3119112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3129112ec3039dda4186c6f957981237c0691db2269Winson Chung
3139112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3149112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Takes a screenshot of the current display and shows an animation.
3159112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
316fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    void takeScreenshot(Runnable finisher) {
3179112ec3039dda4186c6f957981237c0691db2269Winson Chung        // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
3189112ec3039dda4186c6f957981237c0691db2269Winson Chung        // only in the natural orientation of the device :!)
3199112ec3039dda4186c6f957981237c0691db2269Winson Chung        mDisplay.getRealMetrics(mDisplayMetrics);
3209112ec3039dda4186c6f957981237c0691db2269Winson Chung        float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};
3219112ec3039dda4186c6f957981237c0691db2269Winson Chung        float degrees = getDegreesForRotation(mDisplay.getRotation());
3229112ec3039dda4186c6f957981237c0691db2269Winson Chung        boolean requiresRotation = (degrees > 0);
3239112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (requiresRotation) {
3249112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Get the dimensions of the device in its native orientation
3259112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.reset();
3269112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.preRotate(-degrees);
3279112ec3039dda4186c6f957981237c0691db2269Winson Chung            mDisplayMatrix.mapPoints(dims);
3289112ec3039dda4186c6f957981237c0691db2269Winson Chung            dims[0] = Math.abs(dims[0]);
3299112ec3039dda4186c6f957981237c0691db2269Winson Chung            dims[1] = Math.abs(dims[1]);
3309112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3319112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);
3329112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (requiresRotation) {
3339112ec3039dda4186c6f957981237c0691db2269Winson Chung            // Rotate the screenshot to the current orientation
3349112ec3039dda4186c6f957981237c0691db2269Winson Chung            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
3359112ec3039dda4186c6f957981237c0691db2269Winson Chung                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
3369112ec3039dda4186c6f957981237c0691db2269Winson Chung            Canvas c = new Canvas(ss);
3379112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
3389112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.rotate(360f - degrees);
3399112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.translate(-dims[0] / 2, -dims[1] / 2);
3409112ec3039dda4186c6f957981237c0691db2269Winson Chung            c.drawBitmap(mScreenBitmap, 0, 0, null);
3416311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn            c.setBitmap(null);
3429112ec3039dda4186c6f957981237c0691db2269Winson Chung            mScreenBitmap = ss;
3439112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3449112ec3039dda4186c6f957981237c0691db2269Winson Chung
3459112ec3039dda4186c6f957981237c0691db2269Winson Chung        // If we couldn't take the screenshot, notify the user
3469112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (mScreenBitmap == null) {
347c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            notifyScreenshotError(mContext, mNotificationManager);
348fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn            finisher.run();
3499112ec3039dda4186c6f957981237c0691db2269Winson Chung            return;
3509112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3519112ec3039dda4186c6f957981237c0691db2269Winson Chung
3529112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Start the post-screenshot animation
353fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn        startAnimation(finisher);
3549112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3559112ec3039dda4186c6f957981237c0691db2269Winson Chung
3569112ec3039dda4186c6f957981237c0691db2269Winson Chung
3579112ec3039dda4186c6f957981237c0691db2269Winson Chung    /**
3589112ec3039dda4186c6f957981237c0691db2269Winson Chung     * Starts the animation after taking the screenshot
3599112ec3039dda4186c6f957981237c0691db2269Winson Chung     */
360fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn    private void startAnimation(final Runnable finisher) {
3619112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Add the view for the animation
3629112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotView.setImageBitmap(mScreenBitmap);
3639112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotLayout.requestFocus();
3649112ec3039dda4186c6f957981237c0691db2269Winson Chung
3659112ec3039dda4186c6f957981237c0691db2269Winson Chung        // Setup the animation with the screenshot just taken
3669112ec3039dda4186c6f957981237c0691db2269Winson Chung        if (mScreenshotAnimation != null) {
3679112ec3039dda4186c6f957981237c0691db2269Winson Chung            mScreenshotAnimation.end();
3689112ec3039dda4186c6f957981237c0691db2269Winson Chung        }
3699112ec3039dda4186c6f957981237c0691db2269Winson Chung
3709112ec3039dda4186c6f957981237c0691db2269Winson Chung        mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
3719112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator screenshotFadeInAnim = createScreenshotFadeInAnimation();
3729112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator screenshotFadeOutAnim = createScreenshotFadeOutAnimation();
3739112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation = new AnimatorSet();
3749112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation.play(screenshotFadeInAnim).before(screenshotFadeOutAnim);
3759112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation.addListener(new AnimatorListenerAdapter() {
3769112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
3779112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationEnd(Animator animation) {
3789112ec3039dda4186c6f957981237c0691db2269Winson Chung                // Save the screenshot once we have a bit of time now
379fc8fa638617efb5695a1f89ea75375faebbe2a40Dianne Hackborn                saveScreenshotInWorkerThread(finisher);
3809112ec3039dda4186c6f957981237c0691db2269Winson Chung                mWindowManager.removeView(mScreenshotLayout);
3819112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
3829112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
3839112ec3039dda4186c6f957981237c0691db2269Winson Chung        mScreenshotAnimation.start();
3849112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
3859112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ValueAnimator createScreenshotFadeInAnimation() {
3869112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
387c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        anim.setInterpolator(mFadeInInterpolator);
3889112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.setDuration(SCREENSHOT_FADE_IN_DURATION);
3899112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addListener(new AnimatorListenerAdapter() {
3909112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
3919112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationStart(Animator animation) {
3929112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setVisibility(View.VISIBLE);
393c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                mScreenshotContainerView.setTranslationY(0f);
3949112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setVisibility(View.VISIBLE);
3959112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
3969112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
3979112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addUpdateListener(new AnimatorUpdateListener() {
3989112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
3999112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationUpdate(ValueAnimator animation) {
4009112ec3039dda4186c6f957981237c0691db2269Winson Chung                float t = ((Float) animation.getAnimatedValue()).floatValue();
4019112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setAlpha(mBackgroundViewAlphaInterpolator.getInterpolation(t) *
4029112ec3039dda4186c6f957981237c0691db2269Winson Chung                        BACKGROUND_ALPHA);
403c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                float scaleT = SCREENSHOT_SCALE
404c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                        + (1f - t) * (1f - SCREENSHOT_SCALE)
405c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                        + SCREENSHOT_SCALE_FUDGE;
4069112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setAlpha(t*t*t*t);
4079112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleX(scaleT);
4089112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleY(scaleT);
4099112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4109112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
4119112ec3039dda4186c6f957981237c0691db2269Winson Chung        return anim;
4129112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
4139112ec3039dda4186c6f957981237c0691db2269Winson Chung    private ValueAnimator createScreenshotFadeOutAnimation() {
4149112ec3039dda4186c6f957981237c0691db2269Winson Chung        ValueAnimator anim = ValueAnimator.ofFloat(1f, 0f);
415c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        anim.setInterpolator(mFadeOutInterpolator);
4169112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.setStartDelay(SCREENSHOT_FADE_OUT_DELAY);
4179112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.setDuration(SCREENSHOT_FADE_OUT_DURATION);
4189112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addListener(new AnimatorListenerAdapter() {
4199112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4209112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationEnd(Animator animation) {
4219112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setVisibility(View.GONE);
4229112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setVisibility(View.GONE);
4239112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4249112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
4259112ec3039dda4186c6f957981237c0691db2269Winson Chung        anim.addUpdateListener(new AnimatorUpdateListener() {
4269112ec3039dda4186c6f957981237c0691db2269Winson Chung            @Override
4279112ec3039dda4186c6f957981237c0691db2269Winson Chung            public void onAnimationUpdate(ValueAnimator animation) {
4289112ec3039dda4186c6f957981237c0691db2269Winson Chung                float t = ((Float) animation.getAnimatedValue()).floatValue();
429c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                float scaleT = SCREENSHOT_MIN_SCALE
430c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                        + t * (SCREENSHOT_SCALE - SCREENSHOT_MIN_SCALE)
431c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung                        + SCREENSHOT_SCALE_FUDGE;
4329112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setAlpha(t);
4339112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleX(scaleT);
4349112ec3039dda4186c6f957981237c0691db2269Winson Chung                mScreenshotContainerView.setScaleY(scaleT);
4359112ec3039dda4186c6f957981237c0691db2269Winson Chung                mBackgroundView.setAlpha(t * t * BACKGROUND_ALPHA);
4369112ec3039dda4186c6f957981237c0691db2269Winson Chung            }
4379112ec3039dda4186c6f957981237c0691db2269Winson Chung        });
4389112ec3039dda4186c6f957981237c0691db2269Winson Chung        return anim;
4399112ec3039dda4186c6f957981237c0691db2269Winson Chung    }
440c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
441c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    static void notifyScreenshotError(Context context, NotificationManager nManager) {
442c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Resources r = context.getResources();
443c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung
444c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        // Clear all existing notification, compose the new notification and show it
445c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        Notification n = new Notification.Builder(context)
446c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setTicker(r.getString(R.string.screenshot_failed_title))
447c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentTitle(r.getString(R.string.screenshot_failed_title))
448c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setContentText(r.getString(R.string.screenshot_failed_text))
449c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setSmallIcon(android.R.drawable.ic_menu_report_image)
450c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setWhen(System.currentTimeMillis())
451c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .setAutoCancel(true)
452c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung            .getNotification();
453c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung        nManager.notify(SCREENSHOT_NOTIFICATION_ID, n);
454c57ccf01fe24ce508404c99b449e9097e6d8b270Winson Chung    }
4559112ec3039dda4186c6f957981237c0691db2269Winson Chung}
456