Fix Dialer memory leaks

Close the cursor properly to avoid memory leak.

Bug: 69641622
Change-Id: Ieb1bceb67d0d89ef77382050500d5cd5c10fba0e
Signed-off-by: DennySPB <dennyspb@gmail.com>
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
diff --git a/java/com/android/dialer/blocking/FilteredNumberAsyncQueryHandler.java b/java/com/android/dialer/blocking/FilteredNumberAsyncQueryHandler.java
index 08526ef..7323606 100644
--- a/java/com/android/dialer/blocking/FilteredNumberAsyncQueryHandler.java
+++ b/java/com/android/dialer/blocking/FilteredNumberAsyncQueryHandler.java
@@ -98,7 +98,13 @@
         new Listener() {
           @Override
           protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
-            listener.onHasBlockedNumbers(cursor != null && cursor.getCount() > 0);
+            try {
+              listener.onHasBlockedNumbers(cursor != null && cursor.getCount() > 0);
+            } finally {
+              if (cursor != null) {
+                cursor.close();
+              }
+            }
           }
         },
         FilteredNumberCompat.getContentUri(context, null),
@@ -164,23 +170,29 @@
              * example, both '16502530000' and '6502530000' can exist at the same time
              * and will be returned by this query.
              */
-            if (cursor == null || cursor.getCount() == 0) {
-              blockedNumberCache.put(number, BLOCKED_NUMBER_CACHE_NULL_ID);
-              listener.onCheckComplete(null);
-              return;
+            try {
+              if (cursor == null || cursor.getCount() == 0) {
+                blockedNumberCache.put(number, BLOCKED_NUMBER_CACHE_NULL_ID);
+                listener.onCheckComplete(null);
+                return;
+              }
+              cursor.moveToFirst();
+              // New filtering doesn't have a concept of type
+              if (!FilteredNumberCompat.useNewFiltering(context)
+                  && cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns.TYPE))
+                      != FilteredNumberTypes.BLOCKED_NUMBER) {
+                blockedNumberCache.put(number, BLOCKED_NUMBER_CACHE_NULL_ID);
+                listener.onCheckComplete(null);
+                return;
+              }
+              Integer blockedId = cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns._ID));
+              blockedNumberCache.put(number, blockedId);
+              listener.onCheckComplete(blockedId);
+            } finally {
+              if (cursor != null) {
+                cursor.close();
+              }
             }
-            cursor.moveToFirst();
-            // New filtering doesn't have a concept of type
-            if (!FilteredNumberCompat.useNewFiltering(context)
-                && cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns.TYPE))
-                    != FilteredNumberTypes.BLOCKED_NUMBER) {
-              blockedNumberCache.put(number, BLOCKED_NUMBER_CACHE_NULL_ID);
-              listener.onCheckComplete(null);
-              return;
-            }
-            Integer blockedId = cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns._ID));
-            blockedNumberCache.put(number, blockedId);
-            listener.onCheckComplete(blockedId);
           }
         },
         FilteredNumberCompat.getContentUri(context, null),
@@ -221,9 +233,9 @@
     if (TextUtils.isEmpty(formattedNumber)) {
       return null;
     }
-
-    try (Cursor cursor =
-        context
+    Cursor cursor = null;
+    try {
+        cursor = context
             .getContentResolver()
             .query(
                 FilteredNumberCompat.getContentUri(context, null),
@@ -234,7 +246,7 @@
                     }),
                 getIsBlockedNumberSelection(e164Number != null) + " = ?",
                 new String[] {formattedNumber},
-                null)) {
+                null);
       /*
        * In the frameworking blocking, numbers can be blocked in both e164 format
        * and not, resulting in multiple rows being returned for this query. For
@@ -252,6 +264,10 @@
     } catch (SecurityException e) {
       LogUtil.e("FilteredNumberAsyncQueryHandler.getBlockedIdSynchronous", null, e);
       return null;
+    }  finally {
+       if (cursor != null) {
+         cursor.close();
+       }
     }
   }
 
@@ -349,29 +365,35 @@
         new Listener() {
           @Override
           public void onQueryComplete(int token, Object cookie, Cursor cursor) {
-            int rowsReturned = cursor == null ? 0 : cursor.getCount();
-            if (rowsReturned != 1) {
-              throw new SQLiteDatabaseCorruptException(
-                  "Returned " + rowsReturned + " rows for uri " + uri + "where 1 expected.");
-            }
-            cursor.moveToFirst();
-            final ContentValues values = new ContentValues();
-            DatabaseUtils.cursorRowToContentValues(cursor, values);
-            values.remove(FilteredNumberCompat.getIdColumnName(context));
+            try {
+              int rowsReturned = cursor == null ? 0 : cursor.getCount();
+              if (rowsReturned != 1) {
+                throw new SQLiteDatabaseCorruptException(
+                    "Returned " + rowsReturned + " rows for uri " + uri + "where 1 expected.");
+              }
+              cursor.moveToFirst();
+              final ContentValues values = new ContentValues();
+              DatabaseUtils.cursorRowToContentValues(cursor, values);
+              values.remove(FilteredNumberCompat.getIdColumnName(context));
 
-            startDelete(
-                NO_TOKEN,
-                new Listener() {
-                  @Override
-                  public void onDeleteComplete(int token, Object cookie, int result) {
-                    if (listener != null) {
-                      listener.onUnblockComplete(result, values);
+              startDelete(
+                  NO_TOKEN,
+                  new Listener() {
+                    @Override
+                    public void onDeleteComplete(int token, Object cookie, int result) {
+                      if (listener != null) {
+                        listener.onUnblockComplete(result, values);
+                      }
                     }
-                  }
-                },
-                uri,
-                null,
-                null);
+                  },
+                  uri,
+                  null,
+                  null);
+            } finally {
+              if (cursor != null) {
+                cursor.close();
+              }
+            }
           }
         },
         uri,