2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Make sure the cursor is always closed

This commit is contained in:
Albert Vaca
2018-10-28 12:17:52 +01:00
parent 555e3a3f03
commit be6b108e64

View File

@@ -83,16 +83,18 @@ class AppDatabase {
void setEnabled(String packageName, boolean isEnabled) { void setEnabled(String packageName, boolean isEnabled) {
String[] columns = new String[]{KEY_IS_ENABLED}; String[] columns = new String[]{KEY_IS_ENABLED};
Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null); Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null);
try {
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.put(KEY_IS_ENABLED, isEnabled ? "true" : "false"); cv.put(KEY_IS_ENABLED, isEnabled ? "true" : "false");
if (res.getCount() > 0) { if (res.getCount() > 0) {
ourDatabase.update(DATABASE_TABLE, cv, KEY_PACKAGE_NAME + "=?", new String[]{packageName}); ourDatabase.update(DATABASE_TABLE, cv, KEY_PACKAGE_NAME + "=?", new String[]{packageName});
} else { } else {
cv.put(KEY_PACKAGE_NAME, packageName); cv.put(KEY_PACKAGE_NAME, packageName);
ourDatabase.insert(DATABASE_TABLE, null, cv); ourDatabase.insert(DATABASE_TABLE, null, cv);
}
} finally {
res.close();
} }
res.close();
} }
void setAllEnabled(boolean enabled) { void setAllEnabled(boolean enabled) {
@@ -102,15 +104,18 @@ class AppDatabase {
boolean isEnabled(String packageName) { boolean isEnabled(String packageName) {
String[] columns = new String[]{KEY_IS_ENABLED}; String[] columns = new String[]{KEY_IS_ENABLED};
Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null); Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null);
boolean result; try {
if (res.getCount() > 0) { boolean result;
res.moveToFirst(); if (res.getCount() > 0) {
result = (res.getString(res.getColumnIndex(KEY_IS_ENABLED))).equals("true"); res.moveToFirst();
} else { result = (res.getString(res.getColumnIndex(KEY_IS_ENABLED))).equals("true");
result = getDefaultStatus(packageName); } else {
result = getDefaultStatus(packageName);
}
return result;
} finally {
res.close();
} }
res.close();
return result;
} }
private boolean getDefaultStatus(String packageName) { private boolean getDefaultStatus(String packageName) {