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:
@@ -20,9 +20,12 @@ import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Model class stores app info(e.g. package name, type..) that used in battery tip
|
||||
*/
|
||||
@@ -32,13 +35,13 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
* Anomaly type of the app
|
||||
* @see Anomaly.AnomalyType
|
||||
*/
|
||||
public final int anomalyType;
|
||||
public final ArraySet<Integer> anomalyTypes;
|
||||
public final long screenOnTimeMs;
|
||||
public final int uid;
|
||||
|
||||
private AppInfo(AppInfo.Builder builder) {
|
||||
packageName = builder.mPackageName;
|
||||
anomalyType = builder.mAnomalyType;
|
||||
anomalyTypes = builder.mAnomalyTypes;
|
||||
screenOnTimeMs = builder.mScreenOnTimeMs;
|
||||
uid = builder.mUid;
|
||||
}
|
||||
@@ -46,7 +49,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
@VisibleForTesting
|
||||
AppInfo(Parcel in) {
|
||||
packageName = in.readString();
|
||||
anomalyType = in.readInt();
|
||||
anomalyTypes = (ArraySet<Integer>) in.readArraySet(null /* loader */);
|
||||
screenOnTimeMs = in.readLong();
|
||||
uid = in.readInt();
|
||||
}
|
||||
@@ -64,14 +67,14 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(packageName);
|
||||
dest.writeInt(anomalyType);
|
||||
dest.writeArraySet(anomalyTypes);
|
||||
dest.writeLong(screenOnTimeMs);
|
||||
dest.writeInt(uid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "packageName=" + packageName + ",anomalyType=" + anomalyType + ",screenTime="
|
||||
return "packageName=" + packageName + ",anomalyTypes=" + anomalyTypes + ",screenTime="
|
||||
+ screenOnTimeMs;
|
||||
}
|
||||
|
||||
@@ -85,7 +88,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
}
|
||||
|
||||
AppInfo other = (AppInfo) obj;
|
||||
return anomalyType == other.anomalyType
|
||||
return Objects.equals(anomalyTypes, other.anomalyTypes)
|
||||
&& uid == other.uid
|
||||
&& screenOnTimeMs == other.screenOnTimeMs
|
||||
&& TextUtils.equals(packageName, other.packageName);
|
||||
@@ -102,13 +105,13 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
|
||||
};
|
||||
|
||||
public static final class Builder {
|
||||
private int mAnomalyType;
|
||||
private ArraySet<Integer> mAnomalyTypes = new ArraySet<>();
|
||||
private String mPackageName;
|
||||
private long mScreenOnTimeMs;
|
||||
private int mUid;
|
||||
|
||||
public Builder setAnomalyType(int type) {
|
||||
mAnomalyType = type;
|
||||
public Builder addAnomalyType(int type) {
|
||||
mAnomalyTypes.add(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.android.settings.fuelgauge.batterytip;
|
||||
|
||||
import android.app.Fragment;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.batterytip.actions.BatterySaverAction;
|
||||
|
Reference in New Issue
Block a user