From fe13d2813a0cf30871fc999102bfe1ee8dabf9c0 Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Mon, 12 Feb 2018 13:56:41 -0800 Subject: [PATCH] Add method to update "state" column in anomaly db. This state could be "new", "handled" or "auto-handled". Bug: 72385333 Test: RunSettingsRoboTests Change-Id: I2e3a9d2d3457f125421467e72f5b734990273abe --- .../batterytip/BatteryDatabaseManager.java | 38 +++++++++++++-- .../fuelgauge/BatteryDatabaseManagerTest.java | 48 +++++++++++++++---- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryDatabaseManager.java b/src/com/android/settings/fuelgauge/batterytip/BatteryDatabaseManager.java index f87501ffba8..2019b9d7d64 100644 --- a/src/com/android/settings/fuelgauge/batterytip/BatteryDatabaseManager.java +++ b/src/com/android/settings/fuelgauge/batterytip/BatteryDatabaseManager.java @@ -16,6 +16,8 @@ package com.android.settings.fuelgauge.batterytip; +import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.AnomalyColumns + .ANOMALY_STATE; import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.AnomalyColumns .PACKAGE_NAME; import static com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper.AnomalyColumns @@ -28,8 +30,10 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.text.TextUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -55,22 +59,24 @@ public class BatteryDatabaseManager { values.put(PACKAGE_NAME, packageName); values.put(ANOMALY_TYPE, type); values.put(TIME_STAMP_MS, timestampMs); - + values.put(ANOMALY_STATE, AnomalyDatabaseHelper.State.NEW); db.insert(TABLE_ANOMALY, null, values); } } /** - * Query all the anomalies that happened after {@code timestampMs}. + * Query all the anomalies that happened after {@code timestampMsAfter} and with {@code state}. */ - public List queryAllAnomaliesAfter(long timestampMs) { + public List queryAllAnomalies(long timestampMsAfter, int state) { final List appInfos = new ArrayList<>(); try (SQLiteDatabase db = mDatabaseHelper.getReadableDatabase()) { final String[] projection = {PACKAGE_NAME, ANOMALY_TYPE}; final String orderBy = AnomalyDatabaseHelper.AnomalyColumns.TIME_STAMP_MS + " DESC"; - try (Cursor cursor = db.query(TABLE_ANOMALY, projection, TIME_STAMP_MS + " > ?", - new String[]{String.valueOf(timestampMs)}, null, null, orderBy)) { + 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)) { while (cursor.moveToNext()) { AppInfo appInfo = new AppInfo.Builder() .setPackageName(cursor.getString(cursor.getColumnIndex(PACKAGE_NAME))) @@ -90,4 +96,26 @@ public class BatteryDatabaseManager { new String[]{String.valueOf(timestampMs)}); } } + + /** + * Update the type of anomalies to {@code state} + * + * @param appInfos represents the anomalies + * @param state which state to update to + */ + public void updateAnomalies(List appInfos, int state) { + if (!appInfos.isEmpty()) { + final int size = appInfos.size(); + final String[] whereArgs = new String[size]; + for (int i = 0; i < size; i++) { + whereArgs[i] = appInfos.get(i).packageName; + } + try (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); + } + } + } } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryDatabaseManagerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryDatabaseManagerTest.java index ac8800e5c7a..92332f26822 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryDatabaseManagerTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryDatabaseManagerTest.java @@ -24,6 +24,7 @@ import android.content.Context; import android.text.format.DateUtils; import com.android.settings.TestConfig; +import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper; import com.android.settings.fuelgauge.batterytip.AppInfo; import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager; import com.android.settings.testutils.DatabaseTestUtils; @@ -37,6 +38,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import java.util.ArrayList; import java.util.List; @RunWith(RobolectricTestRunner.class) @@ -71,28 +73,54 @@ public class BatteryDatabaseManagerTest { mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD, TWO_DAYS_BEFORE); // In database, it contains two record - List totalAppInfos = mBatteryDatabaseManager.queryAllAnomaliesAfter(0); + List totalAppInfos = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */, + AnomalyDatabaseHelper.State.NEW); assertThat(totalAppInfos).hasSize(2); - verifyAppInfo(totalAppInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW); - verifyAppInfo(totalAppInfos.get(1), PACKAGE_NAME_OLD, TYPE_OLD); + assertAppInfo(totalAppInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW); + assertAppInfo(totalAppInfos.get(1), PACKAGE_NAME_OLD, TYPE_OLD); // Only one record shows up if we query by timestamp - List appInfos = mBatteryDatabaseManager.queryAllAnomaliesAfter(ONE_DAY_BEFORE); + List appInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE, + AnomalyDatabaseHelper.State.NEW); assertThat(appInfos).hasSize(1); - verifyAppInfo(appInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW); + assertAppInfo(appInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW); mBatteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(ONE_DAY_BEFORE); // The obsolete record is removed from database - List appInfos1 = mBatteryDatabaseManager.queryAllAnomaliesAfter(0); + List appInfos1 = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */, + AnomalyDatabaseHelper.State.NEW); assertThat(appInfos1).hasSize(1); - verifyAppInfo(appInfos1.get(0), PACKAGE_NAME_NEW, TYPE_NEW); - + assertAppInfo(appInfos1.get(0), PACKAGE_NAME_NEW, TYPE_NEW); } - private void verifyAppInfo(final AppInfo appInfo, String packageName, int type) { + @Test + public void testUpdateAnomalies_updateSuccessfully() { + mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW, NOW); + mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD, NOW); + final AppInfo appInfo = new AppInfo.Builder().setPackageName(PACKAGE_NAME_OLD).build(); + final List updateAppInfos = new ArrayList<>(); + updateAppInfos.add(appInfo); + + // Change state of PACKAGE_NAME_OLD to handled + mBatteryDatabaseManager.updateAnomalies(updateAppInfos, + AnomalyDatabaseHelper.State.HANDLED); + + // The state of PACKAGE_NAME_NEW is still new + List newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE, + AnomalyDatabaseHelper.State.NEW); + assertThat(newAppInfos).hasSize(1); + assertAppInfo(newAppInfos.get(0), PACKAGE_NAME_NEW, TYPE_NEW); + + // The state of PACKAGE_NAME_OLD is changed to handled + List handledAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE, + AnomalyDatabaseHelper.State.HANDLED); + assertThat(handledAppInfos).hasSize(1); + assertAppInfo(handledAppInfos.get(0), PACKAGE_NAME_OLD, TYPE_OLD); + } + + private void assertAppInfo(final AppInfo appInfo, String packageName, int type) { assertThat(appInfo.packageName).isEqualTo(packageName); assertThat(appInfo.anomalyType).isEqualTo(type); } - }