Change anomalyType to ArraySet in AppInfo

After this cl, in AppInfo we could store mutilple anomalyTypes, so
AppInfo list will only contain one instance for each uid(however
still keep all the anomaly data)

In this way we could remove the duplicate items in app dialog.

Bug: 74335346
Test: RunSettingsRoboTests
Change-Id: I2ef7c218df2a956eea66aa6bdf03f5ddd19948e3
This commit is contained in:
Lei Yu
2018-03-07 17:17:29 -08:00
parent ec9606addb
commit 7caecd36f7
7 changed files with 87 additions and 46 deletions

View File

@@ -32,10 +32,12 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.ArrayMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Database manager for battery data. Now it only contains anomaly data stored in {@link AppInfo}.
@@ -88,20 +90,30 @@ public class BatteryDatabaseManager {
try (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<>();
final String selection = TIME_STAMP_MS + " > ? AND " + ANOMALY_STATE + " = ? ";
final String[] selectionArgs = new String[]{String.valueOf(timestampMsAfter),
String.valueOf(state)};
try (Cursor cursor = db.query(TABLE_ANOMALY, projection,
TIME_STAMP_MS + " > ? AND " + ANOMALY_STATE + " = ? ",
new String[]{String.valueOf(timestampMsAfter), String.valueOf(state)}, null,
null, orderBy)) {
try (Cursor cursor = db.query(TABLE_ANOMALY, projection, selection, selectionArgs,
null /* groupBy */, null /* having */, orderBy)) {
while (cursor.moveToNext()) {
AppInfo appInfo = new AppInfo.Builder()
.setPackageName(cursor.getString(cursor.getColumnIndex(PACKAGE_NAME)))
.setAnomalyType(cursor.getInt(cursor.getColumnIndex(ANOMALY_TYPE)))
.setUid(cursor.getInt(cursor.getColumnIndex(UID)))
.build();
appInfos.add(appInfo);
final int uid = cursor.getInt(cursor.getColumnIndex(UID));
if (!mAppInfoBuilders.containsKey(uid)) {
final AppInfo.Builder builder = new AppInfo.Builder()
.setUid(uid)
.setPackageName(
cursor.getString(cursor.getColumnIndex(PACKAGE_NAME)));
mAppInfoBuilders.put(uid, builder);
}
mAppInfoBuilders.get(uid).addAnomalyType(
cursor.getInt(cursor.getColumnIndex(ANOMALY_TYPE)));
}
}
for (Integer uid : mAppInfoBuilders.keySet()) {
appInfos.add(mAppInfoBuilders.get(uid).build());
}
}
return appInfos;