Merge "Don't close db for singleton db manager"
This commit is contained in:
@@ -59,7 +59,7 @@ public class BatteryDatabaseManager {
|
||||
mDatabaseHelper = AnomalyDatabaseHelper.getInstance(context);
|
||||
}
|
||||
|
||||
public static BatteryDatabaseManager getInstance(Context context) {
|
||||
public static synchronized BatteryDatabaseManager getInstance(Context context) {
|
||||
if (sSingleton == null) {
|
||||
sSingleton = new BatteryDatabaseManager(context);
|
||||
}
|
||||
@@ -84,23 +84,23 @@ public class BatteryDatabaseManager {
|
||||
public synchronized boolean insertAnomaly(int uid, String packageName, int type,
|
||||
int anomalyState,
|
||||
long timestampMs) {
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
||||
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(UID, uid);
|
||||
values.put(PACKAGE_NAME, packageName);
|
||||
values.put(ANOMALY_TYPE, type);
|
||||
values.put(ANOMALY_STATE, anomalyState);
|
||||
values.put(TIME_STAMP_MS, timestampMs);
|
||||
|
||||
return db.insertWithOnConflict(TABLE_ANOMALY, null, values, CONFLICT_IGNORE) != -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query all the anomalies that happened after {@code timestampMsAfter} and with {@code state}.
|
||||
*/
|
||||
public synchronized List<AppInfo> queryAllAnomalies(long timestampMsAfter, int state) {
|
||||
final List<AppInfo> appInfos = new ArrayList<>();
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getReadableDatabase()) {
|
||||
final SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
|
||||
final String[] projection = {PACKAGE_NAME, ANOMALY_TYPE, UID};
|
||||
final String orderBy = AnomalyDatabaseHelper.AnomalyColumns.TIME_STAMP_MS + " DESC";
|
||||
final Map<Integer, AppInfo.Builder> mAppInfoBuilders = new ArrayMap<>();
|
||||
@@ -127,17 +127,15 @@ public class BatteryDatabaseManager {
|
||||
for (Integer uid : mAppInfoBuilders.keySet()) {
|
||||
appInfos.add(mAppInfoBuilders.get(uid).build());
|
||||
}
|
||||
}
|
||||
|
||||
return appInfos;
|
||||
}
|
||||
|
||||
public synchronized void deleteAllAnomaliesBeforeTimeStamp(long timestampMs) {
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
||||
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||
db.delete(TABLE_ANOMALY, TIME_STAMP_MS + " < ?",
|
||||
new String[]{String.valueOf(timestampMs)});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the type of anomalies to {@code state}
|
||||
@@ -152,14 +150,14 @@ public class BatteryDatabaseManager {
|
||||
for (int i = 0; i < size; i++) {
|
||||
whereArgs[i] = appInfos.get(i).packageName;
|
||||
}
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
||||
|
||||
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(ANOMALY_STATE, state);
|
||||
db.update(TABLE_ANOMALY, values, PACKAGE_NAME + " IN (" + TextUtils.join(",",
|
||||
Collections.nCopies(appInfos.size(), "?")) + ")", whereArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query latest timestamps when an app has been performed action {@code type}
|
||||
@@ -170,7 +168,7 @@ public class BatteryDatabaseManager {
|
||||
public synchronized SparseLongArray queryActionTime(
|
||||
@AnomalyDatabaseHelper.ActionType int type) {
|
||||
final SparseLongArray timeStamps = new SparseLongArray();
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getReadableDatabase()) {
|
||||
final SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
|
||||
final String[] projection = {ActionColumns.UID, ActionColumns.TIME_STAMP_MS};
|
||||
final String selection = ActionColumns.ACTION_TYPE + " = ? ";
|
||||
final String[] selectionArgs = new String[]{String.valueOf(type)};
|
||||
@@ -186,7 +184,6 @@ public class BatteryDatabaseManager {
|
||||
timeStamps.append(uid, timeStamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return timeStamps;
|
||||
}
|
||||
@@ -196,22 +193,22 @@ public class BatteryDatabaseManager {
|
||||
*/
|
||||
public synchronized boolean insertAction(@AnomalyDatabaseHelper.ActionType int type,
|
||||
int uid, String packageName, long timestampMs) {
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
||||
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(ActionColumns.UID, uid);
|
||||
values.put(ActionColumns.PACKAGE_NAME, packageName);
|
||||
values.put(ActionColumns.ACTION_TYPE, type);
|
||||
values.put(ActionColumns.TIME_STAMP_MS, timestampMs);
|
||||
|
||||
return db.insertWithOnConflict(TABLE_ACTION, null, values, CONFLICT_REPLACE) != -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an action
|
||||
*/
|
||||
public synchronized boolean deleteAction(@AnomalyDatabaseHelper.ActionType int type,
|
||||
int uid, String packageName) {
|
||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
||||
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||
final String where =
|
||||
ActionColumns.ACTION_TYPE + " = ? AND " + ActionColumns.UID + " = ? AND "
|
||||
+ ActionColumns.PACKAGE_NAME + " = ? ";
|
||||
@@ -221,4 +218,3 @@ public class BatteryDatabaseManager {
|
||||
return db.delete(TABLE_ACTION, where, whereArgs) != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user