GlobalScreenshot.java revision 046fddff5beabd21b9e9e0c6ae24ba11ab444f0d
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.systemui.screenshot; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.AnimatorSet; 22import android.animation.ValueAnimator; 23import android.animation.ValueAnimator.AnimatorUpdateListener; 24import android.app.Notification; 25import android.app.Notification.BigPictureStyle; 26import android.app.NotificationManager; 27import android.app.PendingIntent; 28import android.content.ContentResolver; 29import android.content.ContentValues; 30import android.content.Context; 31import android.content.Intent; 32import android.content.res.Resources; 33import android.graphics.Bitmap; 34import android.graphics.Canvas; 35import android.graphics.ColorMatrix; 36import android.graphics.ColorMatrixColorFilter; 37import android.graphics.Matrix; 38import android.graphics.Paint; 39import android.graphics.PixelFormat; 40import android.graphics.PointF; 41import android.graphics.RectF; 42import android.media.MediaActionSound; 43import android.net.Uri; 44import android.os.AsyncTask; 45import android.os.Environment; 46import android.os.Process; 47import android.provider.MediaStore; 48import android.util.DisplayMetrics; 49import android.view.Display; 50import android.view.LayoutInflater; 51import android.view.MotionEvent; 52import android.view.Surface; 53import android.view.View; 54import android.view.ViewGroup; 55import android.view.WindowManager; 56import android.view.animation.Interpolator; 57import android.widget.ImageView; 58 59import com.android.systemui.R; 60 61import java.io.File; 62import java.io.OutputStream; 63import java.text.SimpleDateFormat; 64import java.util.Date; 65 66/** 67 * POD used in the AsyncTask which saves an image in the background. 68 */ 69class SaveImageInBackgroundData { 70 Context context; 71 Bitmap image; 72 Uri imageUri; 73 Runnable finisher; 74 int iconSize; 75 int result; 76} 77 78/** 79 * An AsyncTask that saves an image to the media store in the background. 80 */ 81class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void, 82 SaveImageInBackgroundData> { 83 private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; 84 private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; 85 private static final String SCREENSHOT_FILE_PATH_TEMPLATE = "%s/%s/%s"; 86 87 private int mNotificationId; 88 private NotificationManager mNotificationManager; 89 private Notification.Builder mNotificationBuilder; 90 private String mImageFileName; 91 private String mImageFilePath; 92 private long mImageTime; 93 private BigPictureStyle mNotificationStyle; 94 95 // WORKAROUND: We want the same notification across screenshots that we update so that we don't 96 // spam a user's notification drawer. However, we only show the ticker for the saving state 97 // and if the ticker text is the same as the previous notification, then it will not show. So 98 // for now, we just add and remove a space from the ticker text to trigger the animation when 99 // necessary. 100 private static boolean mTickerAddSpace; 101 102 SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data, 103 NotificationManager nManager, int nId) { 104 Resources r = context.getResources(); 105 106 // Prepare all the output metadata 107 mImageTime = System.currentTimeMillis(); 108 String imageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(mImageTime)); 109 String imageDir = Environment.getExternalStoragePublicDirectory( 110 Environment.DIRECTORY_PICTURES).getAbsolutePath(); 111 mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate); 112 mImageFilePath = String.format(SCREENSHOT_FILE_PATH_TEMPLATE, imageDir, 113 SCREENSHOTS_DIR_NAME, mImageFileName); 114 115 // Create the large notification icon 116 int imageWidth = data.image.getWidth(); 117 int imageHeight = data.image.getHeight(); 118 int iconSize = data.iconSize; 119 120 final int shortSide = imageWidth < imageHeight ? imageWidth : imageHeight; 121 Bitmap preview = Bitmap.createBitmap(shortSide, shortSide, data.image.getConfig()); 122 Canvas c = new Canvas(preview); 123 Paint paint = new Paint(); 124 ColorMatrix desat = new ColorMatrix(); 125 desat.setSaturation(0.25f); 126 paint.setColorFilter(new ColorMatrixColorFilter(desat)); 127 Matrix matrix = new Matrix(); 128 matrix.postTranslate((shortSide - imageWidth) / 2, 129 (shortSide - imageHeight) / 2); 130 c.drawBitmap(data.image, matrix, paint); 131 c.drawColor(0x40FFFFFF); 132 133 Bitmap croppedIcon = Bitmap.createScaledBitmap(preview, iconSize, iconSize, true); 134 135 // Show the intermediate notification 136 mTickerAddSpace = !mTickerAddSpace; 137 mNotificationId = nId; 138 mNotificationManager = nManager; 139 mNotificationBuilder = new Notification.Builder(context) 140 .setTicker(r.getString(R.string.screenshot_saving_ticker) 141 + (mTickerAddSpace ? " " : "")) 142 .setContentTitle(r.getString(R.string.screenshot_saving_title)) 143 .setContentText(r.getString(R.string.screenshot_saving_text)) 144 .setSmallIcon(R.drawable.stat_notify_image) 145 .setWhen(System.currentTimeMillis()); 146 147 mNotificationStyle = new Notification.BigPictureStyle() 148 .bigPicture(preview); 149 mNotificationBuilder.setStyle(mNotificationStyle); 150 151 Notification n = mNotificationBuilder.build(); 152 n.flags |= Notification.FLAG_NO_CLEAR; 153 mNotificationManager.notify(nId, n); 154 155 // On the tablet, the large icon makes the notification appear as if it is clickable (and 156 // on small devices, the large icon is not shown) so defer showing the large icon until 157 // we compose the final post-save notification below. 158 mNotificationBuilder.setLargeIcon(croppedIcon); 159 // But we still don't set it for the expanded view, allowing the smallIcon to show here. 160 mNotificationStyle.bigLargeIcon(null); 161 } 162 163 @Override 164 protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) { 165 if (params.length != 1) return null; 166 167 // By default, AsyncTask sets the worker thread to have background thread priority, so bump 168 // it back up so that we save a little quicker. 169 Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); 170 171 Context context = params[0].context; 172 Bitmap image = params[0].image; 173 Resources r = context.getResources(); 174 175 try { 176 // Save the screenshot to the MediaStore 177 ContentValues values = new ContentValues(); 178 ContentResolver resolver = context.getContentResolver(); 179 values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath); 180 values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName); 181 values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName); 182 values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime); 183 values.put(MediaStore.Images.ImageColumns.DATE_ADDED, mImageTime); 184 values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, mImageTime); 185 values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png"); 186 Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 187 188 Intent sharingIntent = new Intent(Intent.ACTION_SEND); 189 sharingIntent.setType("image/png"); 190 sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 191 192 Intent chooserIntent = Intent.createChooser(sharingIntent, null); 193 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLOSE_SYSTEM_DIALOGS 194 | Intent.FLAG_ACTIVITY_CLEAR_TASK 195 | Intent.FLAG_ACTIVITY_NEW_TASK); 196 197 mNotificationBuilder.addAction(R.drawable.ic_menu_share, 198 r.getString(com.android.internal.R.string.share), 199 PendingIntent.getActivity(context, 0, chooserIntent, 200 PendingIntent.FLAG_CANCEL_CURRENT)); 201 202 OutputStream out = resolver.openOutputStream(uri); 203 image.compress(Bitmap.CompressFormat.PNG, 100, out); 204 out.flush(); 205 out.close(); 206 207 // update file size in the database 208 values.clear(); 209 values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length()); 210 resolver.update(uri, values, null, null); 211 212 params[0].imageUri = uri; 213 params[0].result = 0; 214 } catch (Exception e) { 215 // IOException/UnsupportedOperationException may be thrown if external storage is not 216 // mounted 217 params[0].result = 1; 218 } 219 220 return params[0]; 221 } 222 223 @Override 224 protected void onPostExecute(SaveImageInBackgroundData params) { 225 if (params.result > 0) { 226 // Show a message that we've failed to save the image to disk 227 GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager); 228 } else { 229 // Show the final notification to indicate screenshot saved 230 Resources r = params.context.getResources(); 231 232 // Create the intent to show the screenshot in gallery 233 Intent launchIntent = new Intent(Intent.ACTION_VIEW); 234 launchIntent.setDataAndType(params.imageUri, "image/png"); 235 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 236 237 mNotificationBuilder 238 .setContentTitle(r.getString(R.string.screenshot_saved_title)) 239 .setContentText(r.getString(R.string.screenshot_saved_text)) 240 .setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0)) 241 .setWhen(System.currentTimeMillis()) 242 .setAutoCancel(true); 243 244 Notification n = mNotificationBuilder.build(); 245 n.flags &= ~Notification.FLAG_NO_CLEAR; 246 mNotificationManager.notify(mNotificationId, n); 247 } 248 params.finisher.run(); 249 } 250} 251 252/** 253 * TODO: 254 * - Performance when over gl surfaces? Ie. Gallery 255 * - what do we say in the Toast? Which icon do we get if the user uses another 256 * type of gallery? 257 */ 258class GlobalScreenshot { 259 private static final int SCREENSHOT_NOTIFICATION_ID = 789; 260 private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130; 261 private static final int SCREENSHOT_DROP_IN_DURATION = 430; 262 private static final int SCREENSHOT_DROP_OUT_DELAY = 500; 263 private static final int SCREENSHOT_DROP_OUT_DURATION = 430; 264 private static final int SCREENSHOT_DROP_OUT_SCALE_DURATION = 370; 265 private static final int SCREENSHOT_FAST_DROP_OUT_DURATION = 320; 266 private static final float BACKGROUND_ALPHA = 0.5f; 267 private static final float SCREENSHOT_SCALE = 1f; 268 private static final float SCREENSHOT_DROP_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.725f; 269 private static final float SCREENSHOT_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.45f; 270 private static final float SCREENSHOT_FAST_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.6f; 271 private static final float SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET = 0f; 272 273 private Context mContext; 274 private WindowManager mWindowManager; 275 private WindowManager.LayoutParams mWindowLayoutParams; 276 private NotificationManager mNotificationManager; 277 private Display mDisplay; 278 private DisplayMetrics mDisplayMetrics; 279 private Matrix mDisplayMatrix; 280 281 private Bitmap mScreenBitmap; 282 private View mScreenshotLayout; 283 private ImageView mBackgroundView; 284 private ImageView mScreenshotView; 285 private ImageView mScreenshotFlash; 286 287 private AnimatorSet mScreenshotAnimation; 288 289 private int mNotificationIconSize; 290 private float mBgPadding; 291 private float mBgPaddingScale; 292 293 private MediaActionSound mCameraSound; 294 295 296 /** 297 * @param context everything needs a context :( 298 */ 299 public GlobalScreenshot(Context context) { 300 Resources r = context.getResources(); 301 mContext = context; 302 LayoutInflater layoutInflater = (LayoutInflater) 303 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 304 305 // Inflate the screenshot layout 306 mDisplayMatrix = new Matrix(); 307 mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null); 308 mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background); 309 mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot); 310 mScreenshotFlash = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_flash); 311 mScreenshotLayout.setFocusable(true); 312 mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() { 313 @Override 314 public boolean onTouch(View v, MotionEvent event) { 315 // Intercept and ignore all touch events 316 return true; 317 } 318 }); 319 320 // Setup the window that we are going to use 321 mWindowLayoutParams = new WindowManager.LayoutParams( 322 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0, 323 WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY, 324 WindowManager.LayoutParams.FLAG_FULLSCREEN 325 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED 326 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 327 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, 328 PixelFormat.TRANSLUCENT); 329 mWindowLayoutParams.setTitle("ScreenshotAnimation"); 330 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 331 mNotificationManager = 332 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 333 mDisplay = mWindowManager.getDefaultDisplay(); 334 mDisplayMetrics = new DisplayMetrics(); 335 mDisplay.getRealMetrics(mDisplayMetrics); 336 337 // Get the various target sizes 338 mNotificationIconSize = 339 r.getDimensionPixelSize(android.R.dimen.notification_large_icon_height); 340 341 // Scale has to account for both sides of the bg 342 mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding); 343 mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels; 344 345 // Setup the Camera shutter sound 346 mCameraSound = new MediaActionSound(); 347 mCameraSound.load(MediaActionSound.SHUTTER_CLICK); 348 } 349 350 /** 351 * Creates a new worker thread and saves the screenshot to the media store. 352 */ 353 private void saveScreenshotInWorkerThread(Runnable finisher) { 354 SaveImageInBackgroundData data = new SaveImageInBackgroundData(); 355 data.context = mContext; 356 data.image = mScreenBitmap; 357 data.iconSize = mNotificationIconSize; 358 data.finisher = finisher; 359 new SaveImageInBackgroundTask(mContext, data, mNotificationManager, 360 SCREENSHOT_NOTIFICATION_ID).execute(data); 361 } 362 363 /** 364 * @return the current display rotation in degrees 365 */ 366 private float getDegreesForRotation(int value) { 367 switch (value) { 368 case Surface.ROTATION_90: 369 return 360f - 90f; 370 case Surface.ROTATION_180: 371 return 360f - 180f; 372 case Surface.ROTATION_270: 373 return 360f - 270f; 374 } 375 return 0f; 376 } 377 378 /** 379 * Takes a screenshot of the current display and shows an animation. 380 */ 381 void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { 382 // We need to orient the screenshot correctly (and the Surface api seems to take screenshots 383 // only in the natural orientation of the device :!) 384 mDisplay.getRealMetrics(mDisplayMetrics); 385 float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; 386 float degrees = getDegreesForRotation(mDisplay.getRotation()); 387 boolean requiresRotation = (degrees > 0); 388 if (requiresRotation) { 389 // Get the dimensions of the device in its native orientation 390 mDisplayMatrix.reset(); 391 mDisplayMatrix.preRotate(-degrees); 392 mDisplayMatrix.mapPoints(dims); 393 dims[0] = Math.abs(dims[0]); 394 dims[1] = Math.abs(dims[1]); 395 } 396 397 // Take the screenshot 398 mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]); 399 if (mScreenBitmap == null) { 400 notifyScreenshotError(mContext, mNotificationManager); 401 finisher.run(); 402 return; 403 } 404 405 if (requiresRotation) { 406 // Rotate the screenshot to the current orientation 407 Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, 408 mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); 409 Canvas c = new Canvas(ss); 410 c.translate(ss.getWidth() / 2, ss.getHeight() / 2); 411 c.rotate(degrees); 412 c.translate(-dims[0] / 2, -dims[1] / 2); 413 c.drawBitmap(mScreenBitmap, 0, 0, null); 414 c.setBitmap(null); 415 mScreenBitmap = ss; 416 } 417 418 // Optimizations 419 mScreenBitmap.setHasAlpha(false); 420 mScreenBitmap.prepareToDraw(); 421 422 // Start the post-screenshot animation 423 startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, 424 statusBarVisible, navBarVisible); 425 } 426 427 428 /** 429 * Starts the animation after taking the screenshot 430 */ 431 private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible, 432 boolean navBarVisible) { 433 // Add the view for the animation 434 mScreenshotView.setImageBitmap(mScreenBitmap); 435 mScreenshotLayout.requestFocus(); 436 437 // Setup the animation with the screenshot just taken 438 if (mScreenshotAnimation != null) { 439 mScreenshotAnimation.end(); 440 } 441 442 mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams); 443 ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation(); 444 ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h, 445 statusBarVisible, navBarVisible); 446 mScreenshotAnimation = new AnimatorSet(); 447 mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim); 448 mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { 449 @Override 450 public void onAnimationEnd(Animator animation) { 451 // Save the screenshot once we have a bit of time now 452 saveScreenshotInWorkerThread(finisher); 453 mWindowManager.removeView(mScreenshotLayout); 454 } 455 }); 456 mScreenshotLayout.post(new Runnable() { 457 @Override 458 public void run() { 459 // Play the shutter sound to notify that we've taken a screenshot 460 mCameraSound.play(MediaActionSound.SHUTTER_CLICK); 461 462 mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 463 mScreenshotView.buildLayer(); 464 mScreenshotAnimation.start(); 465 } 466 }); 467 } 468 private ValueAnimator createScreenshotDropInAnimation() { 469 final float flashPeakDurationPct = ((float) (SCREENSHOT_FLASH_TO_PEAK_DURATION) 470 / SCREENSHOT_DROP_IN_DURATION); 471 final float flashDurationPct = 2f * flashPeakDurationPct; 472 final Interpolator flashAlphaInterpolator = new Interpolator() { 473 @Override 474 public float getInterpolation(float x) { 475 // Flash the flash view in and out quickly 476 if (x <= flashDurationPct) { 477 return (float) Math.sin(Math.PI * (x / flashDurationPct)); 478 } 479 return 0; 480 } 481 }; 482 final Interpolator scaleInterpolator = new Interpolator() { 483 @Override 484 public float getInterpolation(float x) { 485 // We start scaling when the flash is at it's peak 486 if (x < flashPeakDurationPct) { 487 return 0; 488 } 489 return (x - flashDurationPct) / (1f - flashDurationPct); 490 } 491 }; 492 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 493 anim.setDuration(SCREENSHOT_DROP_IN_DURATION); 494 anim.addListener(new AnimatorListenerAdapter() { 495 @Override 496 public void onAnimationStart(Animator animation) { 497 mBackgroundView.setAlpha(0f); 498 mBackgroundView.setVisibility(View.VISIBLE); 499 mScreenshotView.setAlpha(0f); 500 mScreenshotView.setTranslationX(0f); 501 mScreenshotView.setTranslationY(0f); 502 mScreenshotView.setScaleX(SCREENSHOT_SCALE + mBgPaddingScale); 503 mScreenshotView.setScaleY(SCREENSHOT_SCALE + mBgPaddingScale); 504 mScreenshotView.setVisibility(View.VISIBLE); 505 mScreenshotFlash.setAlpha(0f); 506 mScreenshotFlash.setVisibility(View.VISIBLE); 507 } 508 @Override 509 public void onAnimationEnd(android.animation.Animator animation) { 510 mScreenshotFlash.setVisibility(View.GONE); 511 } 512 }); 513 anim.addUpdateListener(new AnimatorUpdateListener() { 514 @Override 515 public void onAnimationUpdate(ValueAnimator animation) { 516 float t = (Float) animation.getAnimatedValue(); 517 float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale) 518 - scaleInterpolator.getInterpolation(t) 519 * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE); 520 mBackgroundView.setAlpha(scaleInterpolator.getInterpolation(t) * BACKGROUND_ALPHA); 521 mScreenshotView.setAlpha(t); 522 mScreenshotView.setScaleX(scaleT); 523 mScreenshotView.setScaleY(scaleT); 524 mScreenshotFlash.setAlpha(flashAlphaInterpolator.getInterpolation(t)); 525 } 526 }); 527 return anim; 528 } 529 private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible, 530 boolean navBarVisible) { 531 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); 532 anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY); 533 anim.addListener(new AnimatorListenerAdapter() { 534 @Override 535 public void onAnimationEnd(Animator animation) { 536 mBackgroundView.setVisibility(View.GONE); 537 mScreenshotView.setVisibility(View.GONE); 538 mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null); 539 } 540 }); 541 542 if (!statusBarVisible || !navBarVisible) { 543 // There is no status bar/nav bar, so just fade the screenshot away in place 544 anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION); 545 anim.addUpdateListener(new AnimatorUpdateListener() { 546 @Override 547 public void onAnimationUpdate(ValueAnimator animation) { 548 float t = (Float) animation.getAnimatedValue(); 549 float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) 550 - t * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_FAST_DROP_OUT_MIN_SCALE); 551 mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); 552 mScreenshotView.setAlpha(1f - t); 553 mScreenshotView.setScaleX(scaleT); 554 mScreenshotView.setScaleY(scaleT); 555 } 556 }); 557 } else { 558 // In the case where there is a status bar, animate to the origin of the bar (top-left) 559 final float scaleDurationPct = (float) SCREENSHOT_DROP_OUT_SCALE_DURATION 560 / SCREENSHOT_DROP_OUT_DURATION; 561 final Interpolator scaleInterpolator = new Interpolator() { 562 @Override 563 public float getInterpolation(float x) { 564 if (x < scaleDurationPct) { 565 // Decelerate, and scale the input accordingly 566 return (float) (1f - Math.pow(1f - (x / scaleDurationPct), 2f)); 567 } 568 return 1f; 569 } 570 }; 571 572 // Determine the bounds of how to scale 573 float halfScreenWidth = (w - 2f * mBgPadding) / 2f; 574 float halfScreenHeight = (h - 2f * mBgPadding) / 2f; 575 final float offsetPct = SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET; 576 final PointF finalPos = new PointF( 577 -halfScreenWidth + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenWidth, 578 -halfScreenHeight + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenHeight); 579 580 // Animate the screenshot to the status bar 581 anim.setDuration(SCREENSHOT_DROP_OUT_DURATION); 582 anim.addUpdateListener(new AnimatorUpdateListener() { 583 @Override 584 public void onAnimationUpdate(ValueAnimator animation) { 585 float t = (Float) animation.getAnimatedValue(); 586 float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale) 587 - scaleInterpolator.getInterpolation(t) 588 * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE); 589 mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA); 590 mScreenshotView.setAlpha(1f - scaleInterpolator.getInterpolation(t)); 591 mScreenshotView.setScaleX(scaleT); 592 mScreenshotView.setScaleY(scaleT); 593 mScreenshotView.setTranslationX(t * finalPos.x); 594 mScreenshotView.setTranslationY(t * finalPos.y); 595 } 596 }); 597 } 598 return anim; 599 } 600 601 static void notifyScreenshotError(Context context, NotificationManager nManager) { 602 Resources r = context.getResources(); 603 604 // Clear all existing notification, compose the new notification and show it 605 Notification n = new Notification.Builder(context) 606 .setTicker(r.getString(R.string.screenshot_failed_title)) 607 .setContentTitle(r.getString(R.string.screenshot_failed_title)) 608 .setContentText(r.getString(R.string.screenshot_failed_text)) 609 .setSmallIcon(R.drawable.stat_notify_image_error) 610 .setWhen(System.currentTimeMillis()) 611 .setAutoCancel(true) 612 .getNotification(); 613 nManager.notify(SCREENSHOT_NOTIFICATION_ID, n); 614 } 615} 616