OSDN Git Service

Catching security exception when activity destroyed
authorSelim Cinek <cinek@google.com>
Tue, 4 Mar 2014 20:29:40 +0000 (21:29 +0100)
committerSelim Cinek <cinek@google.com>
Wed, 5 Mar 2014 13:56:42 +0000 (14:56 +0100)
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

WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java

index 561c4bb..bbbd910 100644 (file)
@@ -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;
             }