Merge "Update dabase manager to store the uid" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-03-07 16:57:35 +00:00
committed by Android (Google) Code Review
5 changed files with 35 additions and 15 deletions

View File

@@ -128,14 +128,14 @@ public class AnomalyDetectionJobService extends JobService {
Log.e(TAG, "Excessive detected uid=" + uid);
batteryUtils.setForceAppStandby(uid, packageName,
AppOpsManager.MODE_IGNORED);
databaseManager.insertAnomaly(packageName, anomalyType,
databaseManager.insertAnomaly(uid, packageName, anomalyType,
smartBatteryOn
? AnomalyDatabaseHelper.State.AUTO_HANDLED
: AnomalyDatabaseHelper.State.NEW,
timeMs);
}
} else {
databaseManager.insertAnomaly(packageName, anomalyType,
databaseManager.insertAnomaly(uid, packageName, anomalyType,
AnomalyDatabaseHelper.State.NEW, timeMs);
}
} catch (NullPointerException | IndexOutOfBoundsException e) {

View File

@@ -33,11 +33,13 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
*/
public final int anomalyType;
public final long screenOnTimeMs;
public final int uid;
private AppInfo(AppInfo.Builder builder) {
packageName = builder.mPackageName;
anomalyType = builder.mAnomalyType;
screenOnTimeMs = builder.mScreenOnTimeMs;
uid = builder.mUid;
}
@VisibleForTesting
@@ -45,6 +47,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
packageName = in.readString();
anomalyType = in.readInt();
screenOnTimeMs = in.readLong();
uid = in.readInt();
}
@Override
@@ -62,6 +65,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
dest.writeString(packageName);
dest.writeInt(anomalyType);
dest.writeLong(screenOnTimeMs);
dest.writeInt(uid);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@@ -78,6 +82,7 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
private int mAnomalyType;
private String mPackageName;
private long mScreenOnTimeMs;
private int mUid;
public Builder setAnomalyType(int type) {
mAnomalyType = type;
@@ -94,6 +99,11 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
return this;
}
public Builder setUid(int uid) {
mUid = uid;
return this;
}
public AppInfo build() {
return new AppInfo(this);
}

View File

@@ -24,6 +24,7 @@ import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.An
.ANOMALY_TYPE;
import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.AnomalyColumns
.TIME_STAMP_MS;
import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.AnomalyColumns.UID;
import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.Tables.TABLE_ANOMALY;
import android.content.ContentValues;
@@ -65,10 +66,11 @@ public class BatteryDatabaseManager {
* @param anomalyState the state of the anomaly
* @param timestampMs the time when it is happened
*/
public synchronized void insertAnomaly(String packageName, int type, int anomalyState,
public synchronized void insertAnomaly(int uid, String packageName, int type, int anomalyState,
long timestampMs) {
try (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);
@@ -83,7 +85,7 @@ public class BatteryDatabaseManager {
public synchronized List<AppInfo> queryAllAnomalies(long timestampMsAfter, int state) {
final List<AppInfo> appInfos = new ArrayList<>();
try (SQLiteDatabase db = mDatabaseHelper.getReadableDatabase()) {
final String[] projection = {PACKAGE_NAME, ANOMALY_TYPE};
final String[] projection = {PACKAGE_NAME, ANOMALY_TYPE, UID};
final String orderBy = AnomalyDatabaseHelper.AnomalyColumns.TIME_STAMP_MS + " DESC";
try (Cursor cursor = db.query(TABLE_ANOMALY, projection,
@@ -94,6 +96,7 @@ public class BatteryDatabaseManager {
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);
}

View File

@@ -42,8 +42,10 @@ import java.util.List;
public class BatteryDatabaseManagerTest {
private static String PACKAGE_NAME_NEW = "com.android.app1";
private static int UID_NEW = 345;
private static int TYPE_NEW = 1;
private static String PACKAGE_NAME_OLD = "com.android.app2";
private static int UID_OLD = 543;
private static int TYPE_OLD = 2;
private static long NOW = System.currentTimeMillis();
private static long ONE_DAY_BEFORE = NOW - DateUtils.DAY_IN_MILLIS;
@@ -67,23 +69,23 @@ public class BatteryDatabaseManagerTest {
@Test
public void testAllFunctions() {
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW,
mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
AnomalyDatabaseHelper.State.NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD,
mBatteryDatabaseManager.insertAnomaly(UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD,
AnomalyDatabaseHelper.State.NEW, TWO_DAYS_BEFORE);
// In database, it contains two record
List<AppInfo> totalAppInfos = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
AnomalyDatabaseHelper.State.NEW);
assertThat(totalAppInfos).hasSize(2);
assertAppInfo(totalAppInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(totalAppInfos.get(1), PACKAGE_NAME_OLD, TYPE_OLD);
assertAppInfo(totalAppInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(totalAppInfos.get(1), UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD);
// Only one record shows up if we query by timestamp
List<AppInfo> appInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.NEW);
assertThat(appInfos).hasSize(1);
assertAppInfo(appInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(appInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
mBatteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(ONE_DAY_BEFORE);
@@ -91,14 +93,14 @@ public class BatteryDatabaseManagerTest {
List<AppInfo> appInfos1 = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
AnomalyDatabaseHelper.State.NEW);
assertThat(appInfos1).hasSize(1);
assertAppInfo(appInfos1.get(0), PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(appInfos1.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
}
@Test
public void testUpdateAnomalies_updateSuccessfully() {
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW,
mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
AnomalyDatabaseHelper.State.NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD,
mBatteryDatabaseManager.insertAnomaly(UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD,
AnomalyDatabaseHelper.State.NEW, NOW);
final AppInfo appInfo = new AppInfo.Builder().setPackageName(PACKAGE_NAME_OLD).build();
final List<AppInfo> updateAppInfos = new ArrayList<>();
@@ -112,17 +114,18 @@ public class BatteryDatabaseManagerTest {
List<AppInfo> newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.NEW);
assertThat(newAppInfos).hasSize(1);
assertAppInfo(newAppInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(newAppInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
// The state of PACKAGE_NAME_OLD is changed to handled
List<AppInfo> handledAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.HANDLED);
assertThat(handledAppInfos).hasSize(1);
assertAppInfo(handledAppInfos.get(0), PACKAGE_NAME_OLD, TYPE_OLD);
assertAppInfo(handledAppInfos.get(0), UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD);
}
private void assertAppInfo(final AppInfo appInfo, String packageName, int type) {
private void assertAppInfo(final AppInfo appInfo, int uid, String packageName, int type) {
assertThat(appInfo.packageName).isEqualTo(packageName);
assertThat(appInfo.anomalyType).isEqualTo(type);
assertThat(appInfo.uid).isEqualTo(uid);
}
}

View File

@@ -38,6 +38,7 @@ public class AppInfoTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final int ANOMALY_TYPE = Anomaly.AnomalyType.WAKE_LOCK;
private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS;
private static final int UID = 3452;
private AppInfo mAppInfo;
@@ -47,6 +48,7 @@ public class AppInfoTest {
.setPackageName(PACKAGE_NAME)
.setAnomalyType(ANOMALY_TYPE)
.setScreenOnTimeMs(SCREEN_TIME_MS)
.setUid(UID)
.build();
}
@@ -61,6 +63,7 @@ public class AppInfoTest {
assertThat(appInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(appInfo.anomalyType).isEqualTo(ANOMALY_TYPE);
assertThat(appInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(appInfo.uid).isEqualTo(UID);
}
@Test
@@ -84,5 +87,6 @@ public class AppInfoTest {
assertThat(mAppInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(mAppInfo.anomalyType).isEqualTo(ANOMALY_TYPE);
assertThat(mAppInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(mAppInfo.uid).isEqualTo(UID);
}
}