Adopt new battery history map with interpolation method

Bug: 184807417
Test: make SettingsRoboTests
Change-Id: I5c45a443d0a72df352d4bb707412328ad009f6d4
This commit is contained in:
ykhung
2021-05-06 15:29:41 +08:00
parent 953b7e3bf5
commit f76ba1189a
9 changed files with 94 additions and 147 deletions

View File

@@ -53,8 +53,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy, implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
BatteryChartView.OnSelectListener, ExpandDividerPreference.OnExpandListener { BatteryChartView.OnSelectListener, ExpandDividerPreference.OnExpandListener {
private static final String TAG = "BatteryChartPreferenceController"; private static final String TAG = "BatteryChartPreferenceController";
private static final int CHART_KEY_ARRAY_SIZE = 25; /** Desired battery history size for timestamp slots. */
public static final int DESIRED_HISTORY_SIZE = 25;
private static final int CHART_LEVEL_ARRAY_SIZE = 13; private static final int CHART_LEVEL_ARRAY_SIZE = 13;
private static final int CHART_KEY_ARRAY_SIZE = DESIRED_HISTORY_SIZE;
private static final long VALID_USAGE_TIME_DURATION = DateUtils.HOUR_IN_MILLIS * 2; private static final long VALID_USAGE_TIME_DURATION = DateUtils.HOUR_IN_MILLIS * 2;
private static final long VALID_DIFF_DURATION = DateUtils.MINUTE_IN_MILLIS * 3; private static final long VALID_DIFF_DURATION = DateUtils.MINUTE_IN_MILLIS * 3;
@@ -176,12 +178,12 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
} }
void setBatteryHistoryMap( void setBatteryHistoryMap(
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) { final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap) {
mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap)); mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap));
} }
private void setBatteryHistoryMapInner( private void setBatteryHistoryMapInner(
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) { final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap) {
// Resets all battery history data relative variables. // Resets all battery history data relative variables.
if (batteryHistoryMap == null) { if (batteryHistoryMap == null) {
mBatteryIndexedMap = null; mBatteryIndexedMap = null;
@@ -189,31 +191,32 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mBatteryHistoryLevels = null; mBatteryHistoryLevels = null;
return; return;
} }
// Generates battery history keys. // Generates battery history timestamp slots.
final List<Long> batteryHistoryKeyList = final List<Long> batteryHistoryKeyList =
new ArrayList<Long>(batteryHistoryMap.keySet()); new ArrayList<>(batteryHistoryMap.keySet());
Collections.sort(batteryHistoryKeyList); Collections.sort(batteryHistoryKeyList);
validateSlotTimestamp(batteryHistoryKeyList);
mBatteryHistoryKeys = new long[CHART_KEY_ARRAY_SIZE]; mBatteryHistoryKeys = new long[CHART_KEY_ARRAY_SIZE];
final int listSize = batteryHistoryKeyList.size(); for (int index = 0; index < CHART_KEY_ARRAY_SIZE; index++) {
final int elementSize = Math.min(listSize, CHART_KEY_ARRAY_SIZE); mBatteryHistoryKeys[index] = batteryHistoryKeyList.get(index);
for (int index = 0; index < elementSize; index++) {
mBatteryHistoryKeys[CHART_KEY_ARRAY_SIZE - index - 1] =
batteryHistoryKeyList.get(listSize - index - 1);
} }
// Generates the battery history levels. // Generates the battery history levels for chart graph.
mBatteryHistoryLevels = new int[CHART_LEVEL_ARRAY_SIZE]; mBatteryHistoryLevels = new int[CHART_LEVEL_ARRAY_SIZE];
for (int index = 0; index < CHART_LEVEL_ARRAY_SIZE; index++) { for (int index = 0; index < CHART_LEVEL_ARRAY_SIZE; index++) {
final Long timestamp = Long.valueOf(mBatteryHistoryKeys[index * 2]); final long timestamp = mBatteryHistoryKeys[index * 2];
final List<BatteryHistEntry> entryList = batteryHistoryMap.get(timestamp); final Map<String, BatteryHistEntry> entryMap = batteryHistoryMap.get(timestamp);
if (entryList != null && !entryList.isEmpty()) { if (entryMap == null || entryMap.isEmpty()) {
// All battery levels are the same in the same timestamp snapshot. Log.e(TAG, "abnormal entry list in the timestamp:"
mBatteryHistoryLevels[index] = entryList.get(0).mBatteryLevel; + ConvertUtils.utcToLocalTime(timestamp));
} else if (entryList != null && entryList.isEmpty()) { continue;
Log.e(TAG, "abnormal entry list in the timestamp:" +
ConvertUtils.utcToLocalTime(timestamp));
} }
// Averages the battery level in each time slot to avoid corner conditions.
float batteryLevelCounter = 0;
for (BatteryHistEntry entry : entryMap.values()) {
batteryLevelCounter += entry.mBatteryLevel;
}
mBatteryHistoryLevels[index] =
Math.round(batteryLevelCounter / entryMap.size());
} }
// Generates indexed usage map for chart. // Generates indexed usage map for chart.
mBatteryIndexedMap = mBatteryIndexedMap =
@@ -532,25 +535,4 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
} }
return true; return true;
} }
@VisibleForTesting
static boolean validateSlotTimestamp(List<Long> batteryHistoryKeys) {
// Whether the nearest two slot time diff is valid or not?
final int size = batteryHistoryKeys.size();
for (int index = 0; index < size - 1; index++) {
final long currentTime = batteryHistoryKeys.get(index);
final long nextTime = batteryHistoryKeys.get(index + 1);
final long diffTime = Math.abs(
DateUtils.HOUR_IN_MILLIS - Math.abs(currentTime - nextTime));
if (currentTime == 0) {
continue;
} else if (diffTime > VALID_DIFF_DURATION) {
Log.e(TAG, String.format("validateSlotTimestamp() %s > %s",
ConvertUtils.utcToLocalTime(currentTime),
ConvertUtils.utcToLocalTime(nextTime)));
return false;
}
}
return true;
}
} }

View File

@@ -20,12 +20,11 @@ import android.content.Context;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.utils.AsyncLoaderCompat; import com.android.settingslib.utils.AsyncLoaderCompat;
import java.util.List;
import java.util.Map; import java.util.Map;
/** Loader that can be used to load battery history information. */ /** Loader that can be used to load battery history information. */
public class BatteryHistoryLoader public class BatteryHistoryLoader
extends AsyncLoaderCompat<Map<Long, List<BatteryHistEntry>>> { extends AsyncLoaderCompat<Map<Long, Map<String, BatteryHistEntry>>> {
private static final String TAG = "BatteryHistoryLoader"; private static final String TAG = "BatteryHistoryLoader";
private final Context mContext; private final Context mContext;
@@ -36,11 +35,11 @@ public class BatteryHistoryLoader
} }
@Override @Override
protected void onDiscardResult(Map<Long, List<BatteryHistEntry>> result) { protected void onDiscardResult(Map<Long, Map<String, BatteryHistEntry>> result) {
} }
@Override @Override
public Map<Long, List<BatteryHistEntry>> loadInBackground() { public Map<Long, Map<String, BatteryHistEntry>> loadInBackground() {
final PowerUsageFeatureProvider powerUsageFeatureProvider = final PowerUsageFeatureProvider powerUsageFeatureProvider =
FeatureFactory.getFactory(mContext).getPowerUsageFeatureProvider(mContext); FeatureFactory.getFactory(mContext).getPowerUsageFeatureProvider(mContext);
return powerUsageFeatureProvider.getBatteryHistory(mContext); return powerUsageFeatureProvider.getBatteryHistory(mContext);

View File

@@ -144,30 +144,9 @@ public final class ConvertUtils {
final Context context, final Context context,
final int timeSlotSize, final int timeSlotSize,
final long[] batteryHistoryKeys, final long[] batteryHistoryKeys,
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap, final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap,
final boolean purgeLowPercentageData) { final boolean purgeLowPercentageData) {
final Map<Integer, List<BatteryDiffEntry>> resultMap = new HashMap<>(); final Map<Integer, List<BatteryDiffEntry>> resultMap = new HashMap<>();
// Generates a temporary map to calculate diff usage data, which converts the inputted
// List<BatteryDiffEntry> into Map<String, BatteryHistEntry> with the key comes from
// the BatteryHistEntry.getKey() method.
final Map<Long, Map<String, BatteryHistEntry>> newBatteryHistoryMap = new HashMap<>();
for (int index = 0; index < batteryHistoryKeys.length; index++) {
final Long timestamp = Long.valueOf(batteryHistoryKeys[index]);
final List<BatteryHistEntry> entries = batteryHistoryMap.get(timestamp);
if (entries == null || entries.isEmpty()) {
continue;
}
final Map<String, BatteryHistEntry> slotBatteryHistDataMap = new HashMap<>();
for (BatteryHistEntry entry : entries) {
// Excludes auto-generated fake BatteryHistEntry data,
// which is used to record battery level and status purpose only.
if (!FAKE_PACKAGE_NAME.equals(entry.mPackageName)) {
slotBatteryHistDataMap.put(entry.getKey(), entry);
}
}
newBatteryHistoryMap.put(timestamp, slotBatteryHistDataMap);
}
// Each time slot usage diff data = // Each time slot usage diff data =
// Math.abs(timestamp[i+2] data - timestamp[i+1] data) + // Math.abs(timestamp[i+2] data - timestamp[i+1] data) +
// Math.abs(timestamp[i+1] data - timestamp[i] data); // Math.abs(timestamp[i+1] data - timestamp[i] data);
@@ -188,11 +167,11 @@ public final class ConvertUtils {
// Fetches BatteryHistEntry data from corresponding time slot. // Fetches BatteryHistEntry data from corresponding time slot.
final Map<String, BatteryHistEntry> currentBatteryHistMap = final Map<String, BatteryHistEntry> currentBatteryHistMap =
newBatteryHistoryMap.getOrDefault(currentTimestamp, EMPTY_BATTERY_MAP); batteryHistoryMap.getOrDefault(currentTimestamp, EMPTY_BATTERY_MAP);
final Map<String, BatteryHistEntry> nextBatteryHistMap = final Map<String, BatteryHistEntry> nextBatteryHistMap =
newBatteryHistoryMap.getOrDefault(nextTimestamp, EMPTY_BATTERY_MAP); batteryHistoryMap.getOrDefault(nextTimestamp, EMPTY_BATTERY_MAP);
final Map<String, BatteryHistEntry> nextTwoBatteryHistMap = final Map<String, BatteryHistEntry> nextTwoBatteryHistMap =
newBatteryHistoryMap.getOrDefault(nextTwoTimestamp, EMPTY_BATTERY_MAP); batteryHistoryMap.getOrDefault(nextTwoTimestamp, EMPTY_BATTERY_MAP);
// Collects all keys in these three time slot records as population. // Collects all keys in these three time slot records as population.
final Set<String> allBatteryHistEntryKeys = new HashSet<>(); final Set<String> allBatteryHistEntryKeys = new HashSet<>();

View File

@@ -52,7 +52,7 @@ public class PowerUsageAdvanced extends PowerUsageBase {
@VisibleForTesting @VisibleForTesting
BatteryHistoryPreference mHistPref; BatteryHistoryPreference mHistPref;
@VisibleForTesting @VisibleForTesting
Map<Long, List<BatteryHistEntry>> mBatteryHistoryMap; Map<Long, Map<String, BatteryHistEntry>> mBatteryHistoryMap;
@VisibleForTesting @VisibleForTesting
final BatteryHistoryLoaderCallbacks mBatteryHistoryLoaderCallbacks = final BatteryHistoryLoaderCallbacks mBatteryHistoryLoaderCallbacks =
new BatteryHistoryLoaderCallbacks(); new BatteryHistoryLoaderCallbacks();
@@ -210,25 +210,26 @@ public class PowerUsageAdvanced extends PowerUsageBase {
}; };
private class BatteryHistoryLoaderCallbacks private class BatteryHistoryLoaderCallbacks
implements LoaderManager.LoaderCallbacks<Map<Long, List<BatteryHistEntry>>> { implements LoaderManager.LoaderCallbacks<Map<Long, Map<String, BatteryHistEntry>>> {
private int mRefreshType; private int mRefreshType;
@Override @Override
@NonNull @NonNull
public Loader<Map<Long, List<BatteryHistEntry>>> onCreateLoader(int id, Bundle bundle) { public Loader<Map<Long, Map<String, BatteryHistEntry>>> onCreateLoader(
int id, Bundle bundle) {
mRefreshType = bundle.getInt(KEY_REFRESH_TYPE); mRefreshType = bundle.getInt(KEY_REFRESH_TYPE);
return new BatteryHistoryLoader(getContext()); return new BatteryHistoryLoader(getContext());
} }
@Override @Override
public void onLoadFinished(Loader<Map<Long, List<BatteryHistEntry>>> loader, public void onLoadFinished(Loader<Map<Long, Map<String, BatteryHistEntry>>> loader,
Map<Long, List<BatteryHistEntry>> batteryHistoryMap) { Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap) {
mBatteryHistoryMap = batteryHistoryMap; mBatteryHistoryMap = batteryHistoryMap;
PowerUsageAdvanced.this.onLoadFinished(mRefreshType); PowerUsageAdvanced.this.onLoadFinished(mRefreshType);
} }
@Override @Override
public void onLoaderReset(Loader<Map<Long, List<BatteryHistEntry>>> loader) { public void onLoaderReset(Loader<Map<Long, Map<String, BatteryHistEntry>>> loader) {
} }
} }

View File

@@ -23,7 +23,6 @@ import android.util.SparseIntArray;
import com.android.internal.os.BatterySipper; import com.android.internal.os.BatterySipper;
import com.android.settingslib.fuelgauge.Estimate; import com.android.settingslib.fuelgauge.Estimate;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@@ -136,5 +135,5 @@ public interface PowerUsageFeatureProvider {
/** /**
* Returns battery history data with corresponding timestamp key. * Returns battery history data with corresponding timestamp key.
*/ */
Map<Long, List<BatteryHistEntry>> getBatteryHistory(Context context); Map<Long, Map<String, BatteryHistEntry>> getBatteryHistory(Context context);
} }

View File

@@ -26,7 +26,6 @@ import com.android.internal.os.BatterySipper;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
import com.android.settingslib.fuelgauge.Estimate; import com.android.settingslib.fuelgauge.Estimate;
import java.util.List;
import java.util.Map; import java.util.Map;
public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider { public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider {
@@ -160,7 +159,7 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
} }
@Override @Override
public Map<Long, List<BatteryHistEntry>> getBatteryHistory(Context context) { public Map<Long, Map<String, BatteryHistEntry>> getBatteryHistory(Context context) {
return null; return null;
} }
} }

View File

@@ -62,6 +62,8 @@ import java.util.TimeZone;
public final class BatteryChartPreferenceControllerTest { public final class BatteryChartPreferenceControllerTest {
private static final String PREF_KEY = "pref_key"; private static final String PREF_KEY = "pref_key";
private static final String PREF_SUMMARY = "fake preference summary"; private static final String PREF_SUMMARY = "fake preference summary";
private static final int DESIRED_HISTORY_SIZE =
BatteryChartPreferenceController.DESIRED_HISTORY_SIZE;
@Mock private InstrumentedPreferenceFragment mFragment; @Mock private InstrumentedPreferenceFragment mFragment;
@Mock private SettingsActivity mSettingsActivity; @Mock private SettingsActivity mSettingsActivity;
@@ -98,7 +100,7 @@ public final class BatteryChartPreferenceControllerTest {
"fakeBatteryDiffEntryKey", "fakeBatteryDiffEntryKey",
new BatteryEntry.NameAndIcon("fakeName", /*icon=*/ null, /*iconId=*/ 1)); new BatteryEntry.NameAndIcon("fakeName", /*icon=*/ null, /*iconId=*/ 1));
mBatteryChartPreferenceController.setBatteryHistoryMap( mBatteryChartPreferenceController.setBatteryHistoryMap(
createBatteryHistoryMap(/*size=*/ 5)); createBatteryHistoryMap());
} }
@Test @Test
@@ -142,19 +144,19 @@ public final class BatteryChartPreferenceControllerTest {
@Test @Test
public void testSetBatteryHistoryMap_createExpectedKeysAndLevels() { public void testSetBatteryHistoryMap_createExpectedKeysAndLevels() {
mBatteryChartPreferenceController.setBatteryHistoryMap( mBatteryChartPreferenceController.setBatteryHistoryMap(
createBatteryHistoryMap(/*size=*/ 5)); createBatteryHistoryMap());
// Verifies the created battery keys array. // Verifies the created battery keys array.
for (int index = 0; index < 25; index++) { for (int index = 0; index < DESIRED_HISTORY_SIZE; index++) {
assertThat(mBatteryChartPreferenceController.mBatteryHistoryKeys[index]) assertThat(mBatteryChartPreferenceController.mBatteryHistoryKeys[index])
// These values is are calculated by hand from createBatteryHistoryMap(). // These values is are calculated by hand from createBatteryHistoryMap().
.isEqualTo(index < 20 ? 0 : (index - 20 + 1)); .isEqualTo(index + 1);
} }
// Verifies the created battery levels array. // Verifies the created battery levels array.
for (int index = 0; index < 13; index++) { for (int index = 0; index < 13; index++) {
assertThat(mBatteryChartPreferenceController.mBatteryHistoryLevels[index]) assertThat(mBatteryChartPreferenceController.mBatteryHistoryLevels[index])
// These values is are calculated by hand from createBatteryHistoryMap(). // These values is are calculated by hand from createBatteryHistoryMap().
.isEqualTo(index < 10 ? 0 : (100 - (index - 10) * 2)); .isEqualTo(100 - index * 2);
} }
assertThat(mBatteryChartPreferenceController.mBatteryIndexedMap).hasSize(13); assertThat(mBatteryChartPreferenceController.mBatteryIndexedMap).hasSize(13);
} }
@@ -162,10 +164,10 @@ public final class BatteryChartPreferenceControllerTest {
@Test @Test
public void testSetBatteryHistoryMap_largeSize_createExpectedKeysAndLevels() { public void testSetBatteryHistoryMap_largeSize_createExpectedKeysAndLevels() {
mBatteryChartPreferenceController.setBatteryHistoryMap( mBatteryChartPreferenceController.setBatteryHistoryMap(
createBatteryHistoryMap(/*size=*/ 25)); createBatteryHistoryMap());
// Verifies the created battery keys array. // Verifies the created battery keys array.
for (int index = 0; index < 25; index++) { for (int index = 0; index < DESIRED_HISTORY_SIZE; index++) {
assertThat(mBatteryChartPreferenceController.mBatteryHistoryKeys[index]) assertThat(mBatteryChartPreferenceController.mBatteryHistoryKeys[index])
// These values is are calculated by hand from createBatteryHistoryMap(). // These values is are calculated by hand from createBatteryHistoryMap().
.isEqualTo(index + 1); .isEqualTo(index + 1);
@@ -214,7 +216,7 @@ public final class BatteryChartPreferenceControllerTest {
mBatteryChartPreferenceController.mTrapezoidIndex = mBatteryChartPreferenceController.mTrapezoidIndex =
BatteryChartView.SELECTED_INDEX_INVALID; BatteryChartView.SELECTED_INDEX_INVALID;
mBatteryChartPreferenceController.setBatteryHistoryMap( mBatteryChartPreferenceController.setBatteryHistoryMap(
createBatteryHistoryMap(/*size=*/ 25)); createBatteryHistoryMap());
assertThat(mBatteryChartPreferenceController.mTrapezoidIndex) assertThat(mBatteryChartPreferenceController.mTrapezoidIndex)
.isEqualTo(BatteryChartView.SELECTED_INDEX_ALL); .isEqualTo(BatteryChartView.SELECTED_INDEX_ALL);
@@ -426,31 +428,6 @@ public final class BatteryChartPreferenceControllerTest {
.isFalse(); .isFalse();
} }
@Test
public void testValidateSlotTimestamp_emptyContent_returnTrue() {
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(
new ArrayList<Long>())).isTrue();
}
@Test
public void testValidateSlotTimestamp_returnExpectedResult() {
final ArrayList<Long> slotTimestampList = new ArrayList<Long>(
Arrays.asList(
Long.valueOf(0),
Long.valueOf(DateUtils.HOUR_IN_MILLIS),
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 2 + DateUtils.MINUTE_IN_MILLIS),
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 3 + DateUtils.MINUTE_IN_MILLIS * 2)));
// Verifies the testing data is correct before we added invalid data into it.
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
.isTrue();
// Insert invalid timestamp into the list.
slotTimestampList.add(
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 4 + DateUtils.MINUTE_IN_MILLIS * 6));
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
.isFalse();
}
@Test @Test
public void testOnExpand_expandedIsTrue_addSystemEntriesToPreferenceGroup() { public void testOnExpand_expandedIsTrue_addSystemEntriesToPreferenceGroup() {
doReturn(1).when(mAppListGroup).getPreferenceCount(); doReturn(1).when(mAppListGroup).getPreferenceCount();
@@ -582,13 +559,15 @@ public final class BatteryChartPreferenceControllerTest {
.setTimestamps(any()); .setTimestamps(any());
} }
private static Map<Long, List<BatteryHistEntry>> createBatteryHistoryMap(int size) { private static Map<Long, Map<String, BatteryHistEntry>> createBatteryHistoryMap() {
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>(); final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
for (int index = 0; index < size; index++) { for (int index = 0; index < DESIRED_HISTORY_SIZE; index++) {
final ContentValues values = new ContentValues(); final ContentValues values = new ContentValues();
values.put("batteryLevel", Integer.valueOf(100 - index)); values.put("batteryLevel", Integer.valueOf(100 - index));
final BatteryHistEntry entry = new BatteryHistEntry(values); final BatteryHistEntry entry = new BatteryHistEntry(values);
batteryHistoryMap.put(Long.valueOf(index + 1), Arrays.asList(entry)); final Map<String, BatteryHistEntry> entryMap = new HashMap<>();
entryMap.put("fake_entry_key" + index, entry);
batteryHistoryMap.put(Long.valueOf(index + 1), entryMap);
} }
return batteryHistoryMap; return batteryHistoryMap;
} }

View File

@@ -23,7 +23,6 @@ import android.content.Context;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.Before;
@@ -51,7 +50,7 @@ public final class BatteryHistoryLoaderTest {
@Test @Test
public void testLoadIBackground_returnsMapFromPowerFeatureProvider() { public void testLoadIBackground_returnsMapFromPowerFeatureProvider() {
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>(); final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
doReturn(batteryHistoryMap).when(mFeatureFactory.powerUsageFeatureProvider) doReturn(batteryHistoryMap).when(mFeatureFactory.powerUsageFeatureProvider)
.getBatteryHistory(mContext); .getBatteryHistory(mContext);

View File

@@ -145,35 +145,45 @@ public final class ConvertUtilsTest {
// Creates the fake testing data. // Creates the fake testing data.
final int timeSlotSize = 2; final int timeSlotSize = 2;
final long[] batteryHistoryKeys = new long[] {101L, 102L, 103L, 104L, 105L}; final long[] batteryHistoryKeys = new long[] {101L, 102L, 103L, 104L, 105L};
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>(); final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap =
new HashMap<>();
// Adds the index = 0 data.
Map<String, BatteryHistEntry> entryMap = new HashMap<>();
BatteryHistEntry entry = createBatteryHistEntry(
"package1", "label1", 5.0, 1L, 10L, 20L);
entryMap.put(entry.getKey(), entry);
batteryHistoryMap.put(Long.valueOf(batteryHistoryKeys[0]), entryMap);
// Adds the index = 1 data.
batteryHistoryMap.put( batteryHistoryMap.put(
Long.valueOf(batteryHistoryKeys[0]), Long.valueOf(batteryHistoryKeys[1]),
Arrays.asList( new HashMap<String, BatteryHistEntry>());
createBatteryHistEntry( // Adds the index = 2 data.
"package1", "label1", 5.0, 1L, 10L, 20L))); entryMap = new HashMap<>();
batteryHistoryMap.put( entry = createBatteryHistEntry(
Long.valueOf(batteryHistoryKeys[1]), new ArrayList<BatteryHistEntry>()); "package2", "label2", 10.0, 2L, 15L, 25L);
batteryHistoryMap.put( entryMap.put(entry.getKey(), entry);
Long.valueOf(batteryHistoryKeys[2]), batteryHistoryMap.put(Long.valueOf(batteryHistoryKeys[2]), entryMap);
Arrays.asList( // Adds the index = 3 data.
createBatteryHistEntry( entryMap = new HashMap<>();
"package2", "label2", 10.0, 2L, 15L, 25L))); entry = createBatteryHistEntry(
batteryHistoryMap.put( "package2", "label2", 15.0, 2L, 25L, 35L);
Long.valueOf(batteryHistoryKeys[3]), entryMap.put(entry.getKey(), entry);
Arrays.asList( entry = createBatteryHistEntry(
createBatteryHistEntry( "package3", "label3", 5.0, 3L, 5L, 5L);
"package2", "label2", 15.0, 2L, 25L, 35L), entryMap.put(entry.getKey(), entry);
createBatteryHistEntry( batteryHistoryMap.put(Long.valueOf(batteryHistoryKeys[3]), entryMap);
"package3", "label3", 5.0, 3L, 5L, 5L))); // Adds the index = 4 data.
batteryHistoryMap.put( entryMap = new HashMap<>();
Long.valueOf(batteryHistoryKeys[4]), entry = createBatteryHistEntry(
Arrays.asList( "package2", "label2", 30.0, 2L, 30L, 40L);
createBatteryHistEntry( entryMap.put(entry.getKey(), entry);
"package2", "label2", 30.0, 2L, 30L, 40L), entry = createBatteryHistEntry(
createBatteryHistEntry( "package2", "label2", 75.0, 4L, 40L, 50L);
"package2", "label2", 75.0, 4L, 40L, 50L), entryMap.put(entry.getKey(), entry);
createBatteryHistEntry( entry = createBatteryHistEntry(
"package3", "label3", 5.0, 3L, 5L, 5L))); "package3", "label3", 5.0, 3L, 5L, 5L);
entryMap.put(entry.getKey(), entry);
batteryHistoryMap.put(Long.valueOf(batteryHistoryKeys[4]), entryMap);
final Map<Integer, List<BatteryDiffEntry>> resultMap = final Map<Integer, List<BatteryDiffEntry>> resultMap =
ConvertUtils.getIndexedUsageMap( ConvertUtils.getIndexedUsageMap(