Bug 1243558 - Safely close Cursor in ScreenshotObserver. r=ahunt draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 22 Feb 2016 16:26:40 -0800
changeset 335139 3d3825dd1d31406e1e6c04ffd3e16d727eb2929c
parent 335067 3693e541f0ee18223dd00a1745a4cd09416722a9
child 335140 ec348aa8aeb26deed28169dbbbe35a8335a43c0c
child 335144 c5e44d32490584f7308b7886b263e68e5b95979e
push id11730
push usermichael.l.comella@gmail.com
push dateSat, 27 Feb 2016 02:11:43 +0000
reviewersahunt
bugs1243558
milestone47.0a1
Bug 1243558 - Safely close Cursor in ScreenshotObserver. r=ahunt MozReview-Commit-ID: L2R51jt34oS
mobile/android/base/java/org/mozilla/gecko/ScreenshotObserver.java
--- a/mobile/android/base/java/org/mozilla/gecko/ScreenshotObserver.java
+++ b/mobile/android/base/java/org/mozilla/gecko/ScreenshotObserver.java
@@ -101,18 +101,18 @@ public class ScreenshotObserver {
 
     public void onMediaChange(final Uri uri) {
         // Make sure we are on not on the main thread.
         final ContentResolver cr = context.getContentResolver();
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
                 // Find the most recent image added to the MediaStore and see if it's a screenshot.
+                final Cursor cursor = cr.query(uri, mediaProjections, null, null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC LIMIT 1");
                 try {
-                    Cursor cursor = cr.query(uri, mediaProjections, null, null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC LIMIT 1");
                     if (cursor == null) {
                         return;
                     }
 
                     while (cursor.moveToNext()) {
                         String data = cursor.getString(0);
                         Log.i(LOGTAG, "data: " + data);
                         String display = cursor.getString(1);
@@ -124,19 +124,22 @@ public class ScreenshotObserver {
                         Log.i(LOGTAG, "title: " + title);
                         if (album != null && album.toLowerCase().contains("screenshot")) {
                             if (listener != null) {
                                 listener.onScreenshotTaken(data, title);
                                 break;
                             }
                         }
                     }
-                    cursor.close();
                 } catch (Exception e) {
                     Log.e(LOGTAG, "Failure to process media change: ", e);
+                } finally {
+                    if (cursor != null) {
+                        cursor.close();
+                    }
                 }
             }
         });
     }
 
     private class MediaObserver extends ContentObserver {
         public MediaObserver() {
             super(null);