Use CloseGuard for cursor leak detection

Protect Cursors with CloseGuard and report implicit cleanup.

By default, the leak detection code will print a one-line warning
message about failing to call the cleanup function. To report more
details like the original stacktrace and SQLite query statement, enable
the correspoinding VmPolicy flags as the example below, please.

/*
 * public void onCreate() {
 *     if (DEVELOPER_MODE) {
 *         StrictMode.setVmPolicy(new VmPolicy.Builder()
 *                 .detectLeakedSqlLiteObjects()  // for SqlLiteCursor
 *                 .detectLeakedClosableObjects() // for any Cursor
 *                 .penaltyLog()
 *                 .build());
 *     }
 *     super.onCreate();
 * }
 */

By enabling detectLeakedSqlLiteObjecs, the original SQLiteCursor query
statement is reported when close() or its equivalent cleanup function
is not called before finalize().

By enabling detectLeakedClosableObjects, the new CloseGuard report will
be provided, along with the original stack trace captured in open() or
its equivalent.

The former has better performance as it doesn't capture an original
stack trace during open() stage. Only enable the latter if performance
impact is not an issue. Both of them can be enabled at the same time.

Bug: 168639120
Test: manually test with an example SQLite Android application, with and
without calling cursor.close() after a db.rawQuery().

Change-Id: Ibe9fcdc8119c2e4651df1983e7ccd793f29e8e9d
Signed-off-by: Li Li <dualli@google.com>
2 files changed