You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
6.6 KiB
245 lines
6.6 KiB
10 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
10 years ago
|
|
||
|
package codetoanalyze.java.infer;
|
||
|
|
||
|
import android.app.DownloadManager;
|
||
|
import android.content.ContentProviderClient;
|
||
|
import android.content.ContentResolver;
|
||
|
import android.content.Context;
|
||
|
import android.database.Cursor;
|
||
|
import android.database.CursorWrapper;
|
||
|
import android.database.sqlite.SQLiteDatabase;
|
||
|
import android.database.sqlite.SQLiteQueryBuilder;
|
||
|
import android.os.RemoteException;
|
||
|
import android.provider.MediaStore;
|
||
|
|
||
|
public class CursorLeaks {
|
||
|
|
||
|
public int cursorClosed(SQLiteDatabase sqLiteDatabase) {
|
||
6 years ago
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
try {
|
||
|
return cursor.getCount();
|
||
|
} finally {
|
||
|
cursor.close();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
public Object cursorClosedCheckNull(SQLiteDatabase sqLiteDatabase) {
|
||
6 years ago
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
8 years ago
|
Object value = null;
|
||
|
|
||
|
try {
|
||
|
if (cursor == null) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
value = cursor.getString(0);
|
||
|
} finally {
|
||
|
if (cursor != null) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
7 years ago
|
public Object cursorClosedCheckNullCheckClosed_FP(SQLiteDatabase sqLiteDatabase) {
|
||
6 years ago
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
7 years ago
|
Object value = null;
|
||
|
|
||
|
try {
|
||
|
if (cursor == null) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
value = cursor.getString(0);
|
||
|
} finally {
|
||
|
if (cursor != null && !cursor.isClosed()) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
10 years ago
|
public int cursorNotClosed(SQLiteDatabase sqLiteDatabase) {
|
||
6 years ago
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
return cursor.getCount();
|
||
10 years ago
|
}
|
||
|
|
||
|
Context mContext;
|
||
|
ContentResolver mContentResolver;
|
||
|
|
||
|
public int getImageCountHelperNotClosed(String customClause) {
|
||
|
String[] projection = {"COUNT(*)"};
|
||
|
|
||
|
String selectionClause = selectionClause = customClause;
|
||
|
|
||
6 years ago
|
Cursor cursor =
|
||
|
mContext.getContentResolver().query(null, projection, selectionClause, null, null);
|
||
10 years ago
|
|
||
|
if (cursor != null) {
|
||
|
int count = cursor.getInt(0);
|
||
|
// cursor.close();
|
||
|
return count;
|
||
|
} else {
|
||
6 years ago
|
return 0;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public int getImageCountHelperClosed(String customClause) {
|
||
|
String[] projection = {"COUNT(*)"};
|
||
|
|
||
|
String selectionClause = selectionClause = customClause;
|
||
|
|
||
6 years ago
|
Cursor cursor =
|
||
|
mContext.getContentResolver().query(null, projection, selectionClause, null, null);
|
||
10 years ago
|
|
||
|
if (cursor != null) {
|
||
|
int count = cursor.getInt(0);
|
||
|
cursor.close();
|
||
|
return count;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int getBucketCountNotClosed() {
|
||
6 years ago
|
Cursor cursor = MediaStore.Images.Media.query(mContentResolver, null, null, null, null, null);
|
||
10 years ago
|
if (cursor == null) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
int count = 0;
|
||
|
while (cursor.moveToNext()) {
|
||
|
count++;
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int getBucketCountClosed() {
|
||
6 years ago
|
Cursor cursor = MediaStore.Images.Media.query(mContentResolver, null, null, null, null, null);
|
||
10 years ago
|
if (cursor == null) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
try {
|
||
|
int count = 0;
|
||
|
while (cursor.moveToNext()) {
|
||
|
count++;
|
||
|
}
|
||
|
return count;
|
||
|
} finally {
|
||
|
cursor.close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void queryUVMLegacyDbNotClosed() {
|
||
|
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
|
||
|
builder.setTables("");
|
||
|
Cursor cursor = builder.query(null, null, "", null, null, null, null);
|
||
|
if (cursor != null) cursor.moveToFirst();
|
||
|
}
|
||
|
|
||
|
private void queryUVMLegacyDbClosed() {
|
||
|
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
|
||
|
builder.setTables("");
|
||
|
Cursor cursor = builder.query(null, null, "", null, null, null, null);
|
||
|
if (cursor != null) cursor.close();
|
||
|
}
|
||
|
|
||
|
public int completeDownloadClosed(DownloadManager downloadManager) {
|
||
|
DownloadManager.Query query = new DownloadManager.Query();
|
||
8 years ago
|
Cursor cursor = (Cursor) null;
|
||
10 years ago
|
try {
|
||
|
cursor = downloadManager.query(query);
|
||
|
if (cursor == null) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
return cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
|
||
|
}
|
||
|
} finally {
|
||
|
if (cursor != null) cursor.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int completeDownloadNotClosed(DownloadManager downloadManager) {
|
||
|
DownloadManager.Query query = new DownloadManager.Query();
|
||
|
Cursor cursor = null;
|
||
|
try {
|
||
|
cursor = downloadManager.query(query);
|
||
|
if (cursor == null) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
return cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
|
||
|
}
|
||
|
} finally {
|
||
6 years ago
|
// cursor.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
private void loadPrefsFromContentProviderClosed() {
|
||
6 years ago
|
ContentProviderClient contentProviderClient = mContentResolver.acquireContentProviderClient("");
|
||
10 years ago
|
if (contentProviderClient != null) {
|
||
|
Cursor cursor = null;
|
||
|
try {
|
||
|
try {
|
||
|
cursor = contentProviderClient.query(null, null, null, null, null);
|
||
|
} catch (RemoteException ex) {
|
||
|
}
|
||
|
} finally {
|
||
|
if (cursor != null) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void loadPrefsFromContentProviderNotClosed() {
|
||
6 years ago
|
ContentProviderClient contentProviderClient = mContentResolver.acquireContentProviderClient("");
|
||
10 years ago
|
if (contentProviderClient == null) return;
|
||
|
Cursor cursor = null;
|
||
|
try {
|
||
|
try {
|
||
|
cursor = contentProviderClient.query(null, null, null, null, null);
|
||
|
} catch (RemoteException ex) {
|
||
|
}
|
||
|
} finally {
|
||
|
if (cursor != null) {
|
||
6 years ago
|
// cursor.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class NamedCursor extends CursorWrapper {
|
||
|
private String mName;
|
||
|
|
||
|
NamedCursor(Cursor cursor, String name) {
|
||
|
super(cursor);
|
||
|
mName = name;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Cursor cursorWrapperReturned(SQLiteDatabase sqLiteDatabase) {
|
||
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
return new NamedCursor(cursor, "abc");
|
||
|
}
|
||
|
|
||
10 years ago
|
// TODO (#7474990): investigate why is Infer reporting a resource leak here
|
||
6 years ago
|
// public void cursorWrapperClosed(SQLiteDatabase sqLiteDatabase) {
|
||
|
// Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
// Cursor c = new NamedCursor(cursor, "abc");
|
||
|
// c.close();
|
||
|
// }
|
||
10 years ago
|
|
||
|
native NamedCursor createWrapper(Cursor cursor);
|
||
|
|
||
|
public NamedCursor cursorAttachedTheWrapper(SQLiteDatabase sqLiteDatabase) {
|
||
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
return createWrapper(cursor);
|
||
|
}
|
||
10 years ago
|
}
|