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);
|
mDatabaseHelper = AnomalyDatabaseHelper.getInstance(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BatteryDatabaseManager getInstance(Context context) {
|
public static synchronized BatteryDatabaseManager getInstance(Context context) {
|
||||||
if (sSingleton == null) {
|
if (sSingleton == null) {
|
||||||
sSingleton = new BatteryDatabaseManager(context);
|
sSingleton = new BatteryDatabaseManager(context);
|
||||||
}
|
}
|
||||||
@@ -84,15 +84,15 @@ public class BatteryDatabaseManager {
|
|||||||
public synchronized boolean insertAnomaly(int uid, String packageName, int type,
|
public synchronized boolean insertAnomaly(int uid, String packageName, int type,
|
||||||
int anomalyState,
|
int anomalyState,
|
||||||
long timestampMs) {
|
long timestampMs) {
|
||||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(UID, uid);
|
values.put(UID, uid);
|
||||||
values.put(PACKAGE_NAME, packageName);
|
values.put(PACKAGE_NAME, packageName);
|
||||||
values.put(ANOMALY_TYPE, type);
|
values.put(ANOMALY_TYPE, type);
|
||||||
values.put(ANOMALY_STATE, anomalyState);
|
values.put(ANOMALY_STATE, anomalyState);
|
||||||
values.put(TIME_STAMP_MS, timestampMs);
|
values.put(TIME_STAMP_MS, timestampMs);
|
||||||
return db.insertWithOnConflict(TABLE_ANOMALY, null, values, CONFLICT_IGNORE) != -1;
|
|
||||||
}
|
return db.insertWithOnConflict(TABLE_ANOMALY, null, values, CONFLICT_IGNORE) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -100,43 +100,41 @@ public class BatteryDatabaseManager {
|
|||||||
*/
|
*/
|
||||||
public synchronized List<AppInfo> queryAllAnomalies(long timestampMsAfter, int state) {
|
public synchronized List<AppInfo> queryAllAnomalies(long timestampMsAfter, int state) {
|
||||||
final List<AppInfo> appInfos = new ArrayList<>();
|
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[] projection = {PACKAGE_NAME, ANOMALY_TYPE, UID};
|
||||||
final String orderBy = AnomalyDatabaseHelper.AnomalyColumns.TIME_STAMP_MS + " DESC";
|
final String orderBy = AnomalyDatabaseHelper.AnomalyColumns.TIME_STAMP_MS + " DESC";
|
||||||
final Map<Integer, AppInfo.Builder> mAppInfoBuilders = new ArrayMap<>();
|
final Map<Integer, AppInfo.Builder> mAppInfoBuilders = new ArrayMap<>();
|
||||||
final String selection = TIME_STAMP_MS + " > ? AND " + ANOMALY_STATE + " = ? ";
|
final String selection = TIME_STAMP_MS + " > ? AND " + ANOMALY_STATE + " = ? ";
|
||||||
final String[] selectionArgs = new String[]{String.valueOf(timestampMsAfter),
|
final String[] selectionArgs = new String[]{String.valueOf(timestampMsAfter),
|
||||||
String.valueOf(state)};
|
String.valueOf(state)};
|
||||||
|
|
||||||
try (Cursor cursor = db.query(TABLE_ANOMALY, projection, selection, selectionArgs,
|
try (Cursor cursor = db.query(TABLE_ANOMALY, projection, selection, selectionArgs,
|
||||||
null /* groupBy */, null /* having */, orderBy)) {
|
null /* groupBy */, null /* having */, orderBy)) {
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
final int uid = cursor.getInt(cursor.getColumnIndex(UID));
|
final int uid = cursor.getInt(cursor.getColumnIndex(UID));
|
||||||
if (!mAppInfoBuilders.containsKey(uid)) {
|
if (!mAppInfoBuilders.containsKey(uid)) {
|
||||||
final AppInfo.Builder builder = new AppInfo.Builder()
|
final AppInfo.Builder builder = new AppInfo.Builder()
|
||||||
.setUid(uid)
|
.setUid(uid)
|
||||||
.setPackageName(
|
.setPackageName(
|
||||||
cursor.getString(cursor.getColumnIndex(PACKAGE_NAME)));
|
cursor.getString(cursor.getColumnIndex(PACKAGE_NAME)));
|
||||||
mAppInfoBuilders.put(uid, builder);
|
mAppInfoBuilders.put(uid, builder);
|
||||||
}
|
|
||||||
mAppInfoBuilders.get(uid).addAnomalyType(
|
|
||||||
cursor.getInt(cursor.getColumnIndex(ANOMALY_TYPE)));
|
|
||||||
}
|
}
|
||||||
|
mAppInfoBuilders.get(uid).addAnomalyType(
|
||||||
|
cursor.getInt(cursor.getColumnIndex(ANOMALY_TYPE)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (Integer uid : mAppInfoBuilders.keySet()) {
|
for (Integer uid : mAppInfoBuilders.keySet()) {
|
||||||
appInfos.add(mAppInfoBuilders.get(uid).build());
|
appInfos.add(mAppInfoBuilders.get(uid).build());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return appInfos;
|
return appInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void deleteAllAnomaliesBeforeTimeStamp(long timestampMs) {
|
public synchronized void deleteAllAnomaliesBeforeTimeStamp(long timestampMs) {
|
||||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||||
db.delete(TABLE_ANOMALY, TIME_STAMP_MS + " < ?",
|
db.delete(TABLE_ANOMALY, TIME_STAMP_MS + " < ?",
|
||||||
new String[]{String.valueOf(timestampMs)});
|
new String[]{String.valueOf(timestampMs)});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,12 +150,12 @@ public class BatteryDatabaseManager {
|
|||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
whereArgs[i] = appInfos.get(i).packageName;
|
whereArgs[i] = appInfos.get(i).packageName;
|
||||||
}
|
}
|
||||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
|
||||||
final ContentValues values = new ContentValues();
|
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||||
values.put(ANOMALY_STATE, state);
|
final ContentValues values = new ContentValues();
|
||||||
db.update(TABLE_ANOMALY, values, PACKAGE_NAME + " IN (" + TextUtils.join(",",
|
values.put(ANOMALY_STATE, state);
|
||||||
Collections.nCopies(appInfos.size(), "?")) + ")", whereArgs);
|
db.update(TABLE_ANOMALY, values, PACKAGE_NAME + " IN (" + TextUtils.join(",",
|
||||||
}
|
Collections.nCopies(appInfos.size(), "?")) + ")", whereArgs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,21 +168,20 @@ public class BatteryDatabaseManager {
|
|||||||
public synchronized SparseLongArray queryActionTime(
|
public synchronized SparseLongArray queryActionTime(
|
||||||
@AnomalyDatabaseHelper.ActionType int type) {
|
@AnomalyDatabaseHelper.ActionType int type) {
|
||||||
final SparseLongArray timeStamps = new SparseLongArray();
|
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[] projection = {ActionColumns.UID, ActionColumns.TIME_STAMP_MS};
|
||||||
final String selection = ActionColumns.ACTION_TYPE + " = ? ";
|
final String selection = ActionColumns.ACTION_TYPE + " = ? ";
|
||||||
final String[] selectionArgs = new String[]{String.valueOf(type)};
|
final String[] selectionArgs = new String[]{String.valueOf(type)};
|
||||||
|
|
||||||
try (Cursor cursor = db.query(TABLE_ACTION, projection, selection, selectionArgs,
|
try (Cursor cursor = db.query(TABLE_ACTION, projection, selection, selectionArgs,
|
||||||
null /* groupBy */, null /* having */, null /* orderBy */)) {
|
null /* groupBy */, null /* having */, null /* orderBy */)) {
|
||||||
final int uidIndex = cursor.getColumnIndex(ActionColumns.UID);
|
final int uidIndex = cursor.getColumnIndex(ActionColumns.UID);
|
||||||
final int timestampIndex = cursor.getColumnIndex(ActionColumns.TIME_STAMP_MS);
|
final int timestampIndex = cursor.getColumnIndex(ActionColumns.TIME_STAMP_MS);
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
final int uid = cursor.getInt(uidIndex);
|
final int uid = cursor.getInt(uidIndex);
|
||||||
final long timeStamp = cursor.getLong(timestampIndex);
|
final long timeStamp = cursor.getLong(timestampIndex);
|
||||||
timeStamps.append(uid, timeStamp);
|
timeStamps.append(uid, timeStamp);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,14 +193,14 @@ public class BatteryDatabaseManager {
|
|||||||
*/
|
*/
|
||||||
public synchronized boolean insertAction(@AnomalyDatabaseHelper.ActionType int type,
|
public synchronized boolean insertAction(@AnomalyDatabaseHelper.ActionType int type,
|
||||||
int uid, String packageName, long timestampMs) {
|
int uid, String packageName, long timestampMs) {
|
||||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||||
final ContentValues values = new ContentValues();
|
final ContentValues values = new ContentValues();
|
||||||
values.put(ActionColumns.UID, uid);
|
values.put(ActionColumns.UID, uid);
|
||||||
values.put(ActionColumns.PACKAGE_NAME, packageName);
|
values.put(ActionColumns.PACKAGE_NAME, packageName);
|
||||||
values.put(ActionColumns.ACTION_TYPE, type);
|
values.put(ActionColumns.ACTION_TYPE, type);
|
||||||
values.put(ActionColumns.TIME_STAMP_MS, timestampMs);
|
values.put(ActionColumns.TIME_STAMP_MS, timestampMs);
|
||||||
return db.insertWithOnConflict(TABLE_ACTION, null, values, CONFLICT_REPLACE) != -1;
|
|
||||||
}
|
return db.insertWithOnConflict(TABLE_ACTION, null, values, CONFLICT_REPLACE) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -211,14 +208,13 @@ public class BatteryDatabaseManager {
|
|||||||
*/
|
*/
|
||||||
public synchronized boolean deleteAction(@AnomalyDatabaseHelper.ActionType int type,
|
public synchronized boolean deleteAction(@AnomalyDatabaseHelper.ActionType int type,
|
||||||
int uid, String packageName) {
|
int uid, String packageName) {
|
||||||
try (SQLiteDatabase db = mDatabaseHelper.getWritableDatabase()) {
|
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
|
||||||
final String where =
|
final String where =
|
||||||
ActionColumns.ACTION_TYPE + " = ? AND " + ActionColumns.UID + " = ? AND "
|
ActionColumns.ACTION_TYPE + " = ? AND " + ActionColumns.UID + " = ? AND "
|
||||||
+ ActionColumns.PACKAGE_NAME + " = ? ";
|
+ ActionColumns.PACKAGE_NAME + " = ? ";
|
||||||
final String[] whereArgs = new String[]{String.valueOf(type), String.valueOf(uid),
|
final String[] whereArgs = new String[]{String.valueOf(type), String.valueOf(uid),
|
||||||
String.valueOf(packageName)};
|
String.valueOf(packageName)};
|
||||||
|
|
||||||
return db.delete(TABLE_ACTION, where, whereArgs) != 0;
|
return db.delete(TABLE_ACTION, where, whereArgs) != 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user