Bug fixed. Cursor was not closed.

This commit is contained in:
Mikhail Chabanov 2015-01-29 17:23:12 +03:00
parent b0e1484b8f
commit 2a18a1dba0
3 changed files with 32 additions and 17 deletions

View File

@ -0,0 +1,11 @@
package com.shortcutBadger.impl;
import android.database.Cursor;
public class CloseHelper {
public static void close(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
}

View File

@ -26,20 +26,23 @@ public class SamsungHomeBadger extends ShortcutBadger {
protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
Uri mUri = Uri.parse(CONTENT_URI);
ContentResolver contentResolver = mContext.getContentResolver();
Cursor cursor = contentResolver.query(mUri, new String[]{"_id",}, "package=?", new String[]{getContextPackageName()}, null);
if (cursor.moveToNext()) {
int id = cursor.getInt(0);
ContentValues contentValues = new ContentValues();
contentValues.put("badgecount", badgeCount);
contentResolver.update(mUri, contentValues, "_id=?", new String[]{String.valueOf(id)});
} else {
ContentValues contentValues = new ContentValues();
contentValues.put("package", getContextPackageName());
contentValues.put("class", getEntryActivityName());
contentValues.put("badgecount", badgeCount);
contentResolver.insert(mUri, contentValues);
Cursor cursor = null;
try {
cursor = contentResolver.query(mUri, new String[]{"_id",}, "package=?", new String[]{getContextPackageName()}, null);
if (cursor.moveToNext()) {
int id = cursor.getInt(0);
ContentValues contentValues = new ContentValues();
contentValues.put("badgecount", badgeCount);
contentResolver.update(mUri, contentValues, "_id=?", new String[]{String.valueOf(id)});
} else {
ContentValues contentValues = new ContentValues();
contentValues.put("package", getContextPackageName());
contentValues.put("class", getEntryActivityName());
contentValues.put("badgecount", badgeCount);
contentResolver.insert(mUri, contentValues);
}
} finally {
CloseHelper.close(cursor);
}
}
}

View File

@ -30,11 +30,14 @@ public class hTCHomeBadger extends ShortcutBadger {
String appName = mContext.getResources().getText(mContext.getResources().getIdentifier("app_name",
"string", getContextPackageName())).toString();
Cursor cursor = null;
boolean supportNotifyCount = true;
try {
Cursor cursor = contentResolver.query(mUri, new String[]{"notifyCount"}, "title=?", new String[]{appName}, null);
cursor = contentResolver.query(mUri, new String[]{"notifyCount"}, "title=?", new String[]{appName}, null);
} catch (Throwable e) {
supportNotifyCount = false;
} finally {
CloseHelper.close(cursor);
}
if (supportNotifyCount) {
@ -51,7 +54,5 @@ public class hTCHomeBadger extends ShortcutBadger {
contentValues.put("icon", bytes);
contentResolver.update(mUri, contentValues, "title=?", new String[]{appName});
}
}
}