Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe1143b4 authored by Leon Scroggins's avatar Leon Scroggins
Browse files

Call close on Cursors in finally blocks.

Fix for http://b/issue?id=2511581

Also call delete instead of deleteHistoryWhere, since we are only
considering non bookmarks anyway.

Change-Id: I00c052db3d27d61592688e7a160e721d5f82b210
parent f0f7eb06
Loading
Loading
Loading
Loading
+67 −58
Original line number Diff line number Diff line
@@ -351,23 +351,27 @@ public class Browser {
     *  @hide pending API council approval
     */
    public static final String[] getVisitedHistory(ContentResolver cr) {
        Cursor c = null;
        String[] str = null;
        try {
            String[] projection = new String[] {
                "url"
            };
            Cursor c = cr.query(BOOKMARKS_URI, projection, "visits > 0", null,
            c = cr.query(BOOKMARKS_URI, projection, "visits > 0", null,
                    null);
            String[] str = new String[c.getCount()];
            str = new String[c.getCount()];
            int i = 0;
            while (c.moveToNext()) {
                str[i] = c.getString(0);
                i++;
            }
            c.close();
            return str;
        } catch (IllegalStateException e) {
            return new String[0];
            Log.e(LOGTAG, "getVisitedHistory", e);
            str = new String[0];
        } finally {
            if (c != null) c.close();
        }
        return str;
    }

    /**
@@ -382,9 +386,10 @@ public class Browser {
     * @param cr The ContentResolver used to access the database.
     */
    public static final void truncateHistory(ContentResolver cr) {
        Cursor c = null;
        try {
            // Select non-bookmark history, ordered by date
            Cursor c = cr.query(
            c = cr.query(
                    BOOKMARKS_URI,
                    TRUNCATE_HISTORY_PROJECTION,
                    "bookmark = 0",
@@ -396,16 +401,16 @@ public class Browser {
                for (int i = 0; i < TRUNCATE_N_OLDEST; i++) {
                    // Log.v(LOGTAG, "truncate history " +
                    // c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX));
                    deleteHistoryWhere(
                            cr, "_id = " +
                            c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX));
                    cr.delete(BOOKMARKS_URI, "_id = " +
                            c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX),
                            null);
                    if (!c.moveToNext()) break;
                }
            }
            c.close();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "truncateHistory", e);
            return;
        } finally {
            if (c != null) c.close();
        }
    }

@@ -416,8 +421,10 @@ public class Browser {
     * @return boolean  True if the history can be cleared.
     */
    public static final boolean canClearHistory(ContentResolver cr) {
        Cursor c = null;
        boolean ret = false;
        try {
            Cursor c = cr.query(
            c = cr.query(
                BOOKMARKS_URI,
                new String [] { BookmarkColumns._ID, 
                                BookmarkColumns.BOOKMARK,
@@ -426,12 +433,13 @@ public class Browser {
                null,
                null
                );
            boolean ret = c.moveToFirst();
            c.close();
            return ret;
            ret = c.moveToFirst();
        } catch (IllegalStateException e) {
            return false;
            Log.e(LOGTAG, "canClearHistory", e);
        } finally {
            if (c != null) c.close();
        }
        return ret;
    }

    /**
@@ -455,17 +463,14 @@ public class Browser {
     */
    private static final void deleteHistoryWhere(ContentResolver cr,
            String whereClause) {
        Cursor c = null;
        try {
            Cursor c = cr.query(BOOKMARKS_URI,
            c = cr.query(BOOKMARKS_URI,
                HISTORY_PROJECTION,
                whereClause,
                null,
                null);
            if (!c.moveToFirst()) {
                c.close();
                return;
            }

            if (c.moveToFirst()) {
                final WebIconDatabase iconDb = WebIconDatabase.getInstance();
                /* Delete favicons, and revert bookmarks which have been visited
                 * to simply bookmarks.
@@ -489,7 +494,6 @@ public class Browser {
                        iconDb.releaseIconForPageUrl(url);
                    }
                } while (c.moveToNext());
            c.close();

                if (!firstTime) {
                    ContentValues map = new ContentValues();
@@ -504,8 +508,12 @@ public class Browser {
                    deleteWhereClause += " AND " + whereClause;
                }
                cr.delete(BOOKMARKS_URI, deleteWhereClause, null);
            }
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "deleteHistoryWhere", e);
            return;
        } finally {
            if (c != null) c.close();
        }
    }

@@ -560,8 +568,9 @@ public class Browser {
     */
    public static final void addSearchUrl(ContentResolver cr, String search) {
        long now = new Date().getTime();
        Cursor c = null;
        try {
            Cursor c = cr.query(
            c = cr.query(
                SEARCHES_URI,
                SEARCHES_PROJECTION,
                SEARCHES_WHERE_CLAUSE,
@@ -576,10 +585,10 @@ public class Browser {
            } else {
                cr.insert(SEARCHES_URI, map);
            }
            c.close();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "addSearchUrl", e);
            return;
        } finally {
            if (c != null) c.close();
        }
    }
    /**