From e559171fe1458ced8b7b0f8bb19c97b3982ab4c7 Mon Sep 17 00:00:00 2001 From: Winson Date: Thu, 18 Feb 2016 13:45:54 -0800 Subject: [PATCH] Fixing issue with persistent screenshot notification - Ensure that we dismiss notifications when cancelled - Removing unnecessary notification id and cancel extra - Fixing the strings when capturing a screenshot fails Bug: 27149651 Bug: 27177097 Change-Id: Ie2ea4318309fff4041acb04833216fdd5cf8838a --- packages/SystemUI/res/values/strings.xml | 4 +- .../systemui/screenshot/GlobalScreenshot.java | 177 ++++++++++----------- 2 files changed, 85 insertions(+), 96 deletions(-) diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 26c3f3ab3546..6f6e515605b3 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -186,7 +186,9 @@ Couldn\'t capture screenshot. - Can\'t take screenshot due to limited storage space, or it isn\'t allowed by the app or your organization. + Can\'t save screenshot due to limited storage space. + + Taking screenshots is not allowed by the app or your organization. USB file transfer options diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java b/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java index e6a291c49e73..a06700d88c15 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java @@ -75,9 +75,9 @@ class SaveImageInBackgroundData { Uri imageUri; Runnable finisher; int iconSize; - int result; int previewWidth; int previewheight; + int errorMsgResId; void clearImage() { image = null; @@ -94,13 +94,11 @@ class SaveImageInBackgroundData { */ class SaveImageInBackgroundTask extends AsyncTask { - private static final String TAG = "SaveImageInBackgroundTask"; private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)"; - private final int mNotificationId; private final NotificationManager mNotificationManager; private final Notification.Builder mNotificationBuilder, mPublicNotificationBuilder; private final File mScreenshotDir; @@ -119,7 +117,7 @@ class SaveImageInBackgroundTask extends AsyncTask 0) { + if (params.errorMsgResId != 0) { // Show a message that we've failed to save the image to disk - GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager); + GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager, + params.errorMsgResId); } else { // Show the final notification to indicate screenshot saved - Resources r = params.context.getResources(); + Context context = params.context; + Resources r = context.getResources(); // Create the intent to show the screenshot in gallery Intent launchIntent = new Intent(Intent.ACTION_VIEW); @@ -326,34 +319,41 @@ class SaveImageInBackgroundTask extends AsyncTask { } } -/** - * TODO: - * - Performance when over gl surfaces? Ie. Gallery - * - what do we say in the Toast? Which icon do we get if the user uses another - * type of gallery? - */ class GlobalScreenshot { - private static final String TAG = "GlobalScreenshot"; - - static final String CANCEL_ID = "android:cancel_id"; static final String SCREENSHOT_URI_ID = "android:screenshot_uri_id"; private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130; @@ -512,8 +503,8 @@ class GlobalScreenshot { if (mSaveInBgTask != null) { mSaveInBgTask.cancel(false); } - mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager, - R.id.notification_screenshot).execute(data); + mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager) + .execute(data); } /** @@ -553,7 +544,8 @@ class GlobalScreenshot { // Take the screenshot mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]); if (mScreenBitmap == null) { - notifyScreenshotError(mContext, mNotificationManager); + notifyScreenshotError(mContext, mNotificationManager, + R.string.screenshot_failed_to_capture_text); finisher.run(); return; } @@ -763,14 +755,15 @@ class GlobalScreenshot { return anim; } - static void notifyScreenshotError(Context context, NotificationManager nManager) { + static void notifyScreenshotError(Context context, NotificationManager nManager, int msgResId) { Resources r = context.getResources(); + String errorMsg = r.getString(msgResId); - // Clear all existing notification, compose the new notification and show it + // Repurpose the existing notification to notify the user of the error Notification.Builder b = new Notification.Builder(context) .setTicker(r.getString(R.string.screenshot_failed_title)) .setContentTitle(r.getString(R.string.screenshot_failed_title)) - .setContentText(r.getString(R.string.screenshot_failed_text)) + .setContentText(errorMsg) .setSmallIcon(R.drawable.stat_notify_image_error) .setWhen(System.currentTimeMillis()) .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen @@ -778,9 +771,9 @@ class GlobalScreenshot { .setAutoCancel(true) .setColor(context.getColor( com.android.internal.R.color.system_notification_accent_color)); - Notification n = - new Notification.BigTextStyle(b) - .bigText(r.getString(R.string.screenshot_failed_text)) + + Notification n = new Notification.BigTextStyle(b) + .bigText(errorMsg) .build(); nManager.notify(R.id.notification_screenshot, n); } @@ -791,15 +784,10 @@ class GlobalScreenshot { public static class TargetChosenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (!intent.hasExtra(CANCEL_ID)) { - return; - } - // Clear the notification final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - final int id = intent.getIntExtra(CANCEL_ID, 0); - nm.cancel(id); + nm.cancel(R.id.notification_screenshot); } } @@ -809,16 +797,15 @@ class GlobalScreenshot { public static class DeleteScreenshotReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (!intent.hasExtra(CANCEL_ID) || !intent.hasExtra(SCREENSHOT_URI_ID)) { + if (!intent.hasExtra(SCREENSHOT_URI_ID)) { return; } // Clear the notification final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - final int id = intent.getIntExtra(CANCEL_ID, 0); final Uri uri = Uri.parse(intent.getStringExtra(SCREENSHOT_URI_ID)); - nm.cancel(id); + nm.cancel(R.id.notification_screenshot); // And delete the image from the media store new DeleteImageInBackgroundTask(context).execute(uri); -- 2.11.0