Catching security exception when activity destroyed

Fixed a bug where an asynctask tried to load an image
even after the activity was destroyed leading to a
security exception since the permission could not be granted anymore.

Bug: 12760267
Change-Id: I76ee2c0ab268b0ab4bde606706046caf0e39f1e9
This commit is contained in:
Selim Cinek
2014-03-04 21:29:40 +01:00
parent 10acea596e
commit 459875d09e
@@ -141,7 +141,21 @@ public class WallpaperCropActivity extends Activity {
final AsyncTask<Void, Void, Void> loadBitmapTask = new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void...args) {
if (!isCancelled()) {
bitmapSource.loadInBackground();
try {
bitmapSource.loadInBackground();
} catch (SecurityException securityException) {
if (isDestroyed()) {
// Temporarily granted permissions are revoked when the activity
// finishes, potentially resulting in a SecurityException here.
// Even though {@link #isDestroyed} might also return true in different
// situations where the configuration changes, we are fine with
// catching these cases here as well.
cancel(false);
} else {
// otherwise it had a different cause and we throw it further
throw securityException;
}
}
}
return null;
}