Prevent crash with invalid cancelled params.

Bug: 27368990
Change-Id: I3eaa9e0391a4149da29bb5dc638ef152f5125bc6
This commit is contained in:
Winson
2016-03-10 12:52:27 -08:00
parent 95171050e9
commit c7aa65b8d5

View File

@@ -93,13 +93,13 @@ class SaveImageInBackgroundData {
/** /**
* An AsyncTask that saves an image to the media store in the background. * An AsyncTask that saves an image to the media store in the background.
*/ */
class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Void, class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
SaveImageInBackgroundData> {
private static final String SCREENSHOTS_DIR_NAME = "Screenshots"; private static final String SCREENSHOTS_DIR_NAME = "Screenshots";
private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png"; private static final String SCREENSHOT_FILE_NAME_TEMPLATE = "Screenshot_%s.png";
private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)"; private static final String SCREENSHOT_SHARE_SUBJECT_TEMPLATE = "Screenshot (%s)";
private final SaveImageInBackgroundData mParams;
private final NotificationManager mNotificationManager; private final NotificationManager mNotificationManager;
private final Notification.Builder mNotificationBuilder, mPublicNotificationBuilder; private final Notification.Builder mNotificationBuilder, mPublicNotificationBuilder;
private final File mScreenshotDir; private final File mScreenshotDir;
@@ -122,6 +122,7 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
Resources r = context.getResources(); Resources r = context.getResources();
// Prepare all the output metadata // Prepare all the output metadata
mParams = data;
mImageTime = System.currentTimeMillis(); mImageTime = System.currentTimeMillis();
String imageDate = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(mImageTime)); String imageDate = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(mImageTime));
mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate); mImageFileName = String.format(SCREENSHOT_FILE_NAME_TEMPLATE, imageDate);
@@ -210,17 +211,17 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
} }
@Override @Override
protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) { protected Void doInBackground(Void... params) {
if (isCancelled()) { if (isCancelled()) {
return params[0]; return null;
} }
// By default, AsyncTask sets the worker thread to have background thread priority, so bump // By default, AsyncTask sets the worker thread to have background thread priority, so bump
// it back up so that we save a little quicker. // it back up so that we save a little quicker.
Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
Context context = params[0].context; Context context = mParams.context;
Bitmap image = params[0].image; Bitmap image = mParams.image;
Resources r = context.getResources(); Resources r = context.getResources();
try { try {
@@ -284,14 +285,14 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
r.getString(com.android.internal.R.string.delete), deleteAction); r.getString(com.android.internal.R.string.delete), deleteAction);
mNotificationBuilder.addAction(deleteActionBuilder.build()); mNotificationBuilder.addAction(deleteActionBuilder.build());
params[0].imageUri = uri; mParams.imageUri = uri;
params[0].image = null; mParams.image = null;
params[0].errorMsgResId = 0; mParams.errorMsgResId = 0;
} catch (Exception e) { } catch (Exception e) {
// IOException/UnsupportedOperationException may be thrown if external storage is not // IOException/UnsupportedOperationException may be thrown if external storage is not
// mounted // mounted
params[0].clearImage(); mParams.clearImage();
params[0].errorMsgResId = R.string.screenshot_failed_to_save_text; mParams.errorMsgResId = R.string.screenshot_failed_to_save_text;
} }
// Recycle the bitmap data // Recycle the bitmap data
@@ -299,23 +300,23 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
image.recycle(); image.recycle();
} }
return params[0]; return null;
} }
@Override @Override
protected void onPostExecute(SaveImageInBackgroundData params) { protected void onPostExecute(Void params) {
if (params.errorMsgResId != 0) { if (mParams.errorMsgResId != 0) {
// Show a message that we've failed to save the image to disk // Show a message that we've failed to save the image to disk
GlobalScreenshot.notifyScreenshotError(params.context, mNotificationManager, GlobalScreenshot.notifyScreenshotError(mParams.context, mNotificationManager,
params.errorMsgResId); mParams.errorMsgResId);
} else { } else {
// Show the final notification to indicate screenshot saved // Show the final notification to indicate screenshot saved
Context context = params.context; Context context = mParams.context;
Resources r = context.getResources(); Resources r = context.getResources();
// Create the intent to show the screenshot in gallery // Create the intent to show the screenshot in gallery
Intent launchIntent = new Intent(Intent.ACTION_VIEW); Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setDataAndType(params.imageUri, "image/png"); launchIntent.setDataAndType(mParams.imageUri, "image/png");
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
@@ -324,7 +325,7 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
mPublicNotificationBuilder mPublicNotificationBuilder
.setContentTitle(r.getString(R.string.screenshot_saved_title)) .setContentTitle(r.getString(R.string.screenshot_saved_title))
.setContentText(r.getString(R.string.screenshot_saved_text)) .setContentText(r.getString(R.string.screenshot_saved_text))
.setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0)) .setContentIntent(PendingIntent.getActivity(mParams.context, 0, launchIntent, 0))
.setWhen(now) .setWhen(now)
.setAutoCancel(true) .setAutoCancel(true)
.setColor(context.getColor( .setColor(context.getColor(
@@ -332,7 +333,7 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
mNotificationBuilder mNotificationBuilder
.setContentTitle(r.getString(R.string.screenshot_saved_title)) .setContentTitle(r.getString(R.string.screenshot_saved_title))
.setContentText(r.getString(R.string.screenshot_saved_text)) .setContentText(r.getString(R.string.screenshot_saved_text))
.setContentIntent(PendingIntent.getActivity(params.context, 0, launchIntent, 0)) .setContentIntent(PendingIntent.getActivity(mParams.context, 0, launchIntent, 0))
.setWhen(now) .setWhen(now)
.setAutoCancel(true) .setAutoCancel(true)
.setColor(context.getColor( .setColor(context.getColor(
@@ -342,15 +343,18 @@ class SaveImageInBackgroundTask extends AsyncTask<SaveImageInBackgroundData, Voi
mNotificationManager.notify(R.id.notification_screenshot, mNotificationBuilder.build()); mNotificationManager.notify(R.id.notification_screenshot, mNotificationBuilder.build());
} }
params.finisher.run(); mParams.finisher.run();
params.clearContext(); mParams.clearContext();
} }
@Override @Override
protected void onCancelled(SaveImageInBackgroundData params) { protected void onCancelled(Void params) {
params.finisher.run(); // If we are cancelled while the task is running in the background, we may get null params.
params.clearImage(); // The finisher is expected to always be called back, so just use the baked-in params from
params.clearContext(); // the ctor in any case.
mParams.finisher.run();
mParams.clearImage();
mParams.clearContext();
// Cancel the posted notification // Cancel the posted notification
mNotificationManager.cancel(R.id.notification_screenshot); mNotificationManager.cancel(R.id.notification_screenshot);
@@ -419,7 +423,7 @@ class GlobalScreenshot {
private float mBgPadding; private float mBgPadding;
private float mBgPaddingScale; private float mBgPaddingScale;
private AsyncTask<SaveImageInBackgroundData, Void, SaveImageInBackgroundData> mSaveInBgTask; private AsyncTask<Void, Void, Void> mSaveInBgTask;
private MediaActionSound mCameraSound; private MediaActionSound mCameraSound;
@@ -510,7 +514,7 @@ class GlobalScreenshot {
mSaveInBgTask.cancel(false); mSaveInBgTask.cancel(false);
} }
mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager) mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager)
.execute(data); .execute();
} }
/** /**