Send fake data if there is no battey consumption entry list
Sends fake data into SettingsIntelligence if there is no data in the battery consumption list, otherwise we don't have battery level data to draw into the chart graph. We should draw the battery levels if it is full-charged without any BatteryEntry data. Bug: 184807417 Test: make SettingsRoboTests Test: make SettingsGoogleRoboTests Change-Id: I759e769256f4aa0ec152afff5c265ee3d04c03da
This commit is contained in:
@@ -50,7 +50,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
private static final int CHART_LEVEL_ARRAY_SIZE = 13;
|
||||
|
||||
@VisibleForTesting
|
||||
PreferenceGroup mAppListGroup;
|
||||
PreferenceGroup mAppListPrefGroup;
|
||||
|
||||
private Context mPrefContext;
|
||||
private BatteryChartView mBatteryChartView;
|
||||
@@ -90,7 +90,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPrefContext = screen.getContext();
|
||||
mAppListGroup = screen.findPreference(mPreferenceKey);
|
||||
mAppListPrefGroup = screen.findPreference(mPreferenceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -137,8 +137,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
if (entryList != null && !entryList.isEmpty()) {
|
||||
// All battery levels are the same in the same timestamp snapshot.
|
||||
mBatteryHistoryLevels[index] = entryList.get(0).mBatteryLevel;
|
||||
} else {
|
||||
Log.w(TAG, "abnormal entry list in the timestamp:" + timestamp);
|
||||
} else if (entryList != null && entryList.isEmpty()) {
|
||||
Log.e(TAG, "abnormal entry list in the timestamp:" +
|
||||
ConvertUtils.utcToLocalTime(timestamp));
|
||||
}
|
||||
}
|
||||
if (mBatteryChartView != null) {
|
||||
|
@@ -33,10 +33,11 @@ import java.util.TimeZone;
|
||||
/** A utility class to convert data into another types. */
|
||||
public final class ConvertUtils {
|
||||
private static final String TAG = "ConvertUtils";
|
||||
private static final SimpleDateFormat SIMPLE_DATE_FORMAT =
|
||||
new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH);
|
||||
|
||||
/** Invalid system battery consumer drain type. */
|
||||
public static final int INVALID_DRAIN_TYPE = -1;
|
||||
/** A fake package name to represent no BatteryEntry data. */
|
||||
public static final String FAKE_PACKAGE_NAME = "fake_package";
|
||||
|
||||
@IntDef(prefix = {"CONSUMER_TYPE"}, value = {
|
||||
CONSUMER_TYPE_UNKNOWN,
|
||||
@@ -52,6 +53,11 @@ public final class ConvertUtils {
|
||||
public static final int CONSUMER_TYPE_USER_BATTERY = 2;
|
||||
public static final int CONSUMER_TYPE_SYSTEM_BATTERY = 3;
|
||||
|
||||
private static String sZoneId;
|
||||
private static SimpleDateFormat sSimpleDateFormat;
|
||||
|
||||
private ConvertUtils() {}
|
||||
|
||||
/** Gets consumer type from {@link BatteryConsumer}. */
|
||||
@ConsumerType
|
||||
public static int getConsumerType(BatteryConsumer consumer) {
|
||||
@@ -82,14 +88,13 @@ public final class ConvertUtils {
|
||||
int batteryHealth,
|
||||
long timestamp) {
|
||||
final ContentValues values = new ContentValues();
|
||||
if (entry != null && batteryUsageStats != null) {
|
||||
values.put("uid", Long.valueOf(entry.getUid()));
|
||||
values.put("userId",
|
||||
Long.valueOf(UserHandle.getUserId(entry.getUid())));
|
||||
values.put("appLabel", entry.getLabel());
|
||||
values.put("packageName", entry.getDefaultPackageName());
|
||||
values.put("isHidden", Boolean.valueOf(entry.isHidden()));
|
||||
values.put("timestamp", Long.valueOf(timestamp));
|
||||
values.put("zoneId", TimeZone.getDefault().getID());
|
||||
values.put("totalPower",
|
||||
Double.valueOf(batteryUsageStats.getConsumedPower()));
|
||||
values.put("consumePower", Double.valueOf(entry.getConsumedPower()));
|
||||
@@ -100,6 +105,11 @@ public final class ConvertUtils {
|
||||
Long.valueOf(entry.getTimeInBackgroundMs()));
|
||||
values.put("drainType", getDrainType(entry.getBatteryConsumer()));
|
||||
values.put("consumerType", getConsumerType(entry.getBatteryConsumer()));
|
||||
} else {
|
||||
values.put("packageName", FAKE_PACKAGE_NAME);
|
||||
}
|
||||
values.put("timestamp", Long.valueOf(timestamp));
|
||||
values.put("zoneId", TimeZone.getDefault().getID());
|
||||
values.put("batteryLevel", Integer.valueOf(batteryLevel));
|
||||
values.put("batteryStatus", Integer.valueOf(batteryStatus));
|
||||
values.put("batteryHealth", Integer.valueOf(batteryHealth));
|
||||
@@ -108,8 +118,12 @@ public final class ConvertUtils {
|
||||
|
||||
/** Converts UTC timestamp to human readable local time string. */
|
||||
public static String utcToLocalTime(long timestamp) {
|
||||
return SIMPLE_DATE_FORMAT.format(new Date(timestamp));
|
||||
final String currentZoneId = TimeZone.getDefault().getID();
|
||||
if (!currentZoneId.equals(sZoneId) || sSimpleDateFormat == null) {
|
||||
sZoneId = currentZoneId;
|
||||
sSimpleDateFormat =
|
||||
new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH);
|
||||
}
|
||||
return sSimpleDateFormat.format(new Date(timestamp));
|
||||
}
|
||||
|
||||
private ConvertUtils() {}
|
||||
}
|
||||
|
@@ -111,6 +111,29 @@ public final class ConvertUtilsTest {
|
||||
.isEqualTo(BatteryManager.BATTERY_HEALTH_COLD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert_nullBatteryEntry_returnsExpectedContentValues() {
|
||||
final ContentValues values =
|
||||
ConvertUtils.convert(
|
||||
/*entry=*/ null,
|
||||
/*batteryUsageStats=*/ null,
|
||||
/*batteryLevel=*/ 12,
|
||||
/*batteryStatus=*/ BatteryManager.BATTERY_STATUS_FULL,
|
||||
/*batteryHealth=*/ BatteryManager.BATTERY_HEALTH_COLD,
|
||||
/*timestamp=*/ 10001L);
|
||||
|
||||
assertThat(values.getAsLong("timestamp")).isEqualTo(10001L);
|
||||
assertThat(values.getAsString("zoneId"))
|
||||
.isEqualTo(TimeZone.getDefault().getID());
|
||||
assertThat(values.getAsInteger("batteryLevel")).isEqualTo(12);
|
||||
assertThat(values.getAsInteger("batteryStatus"))
|
||||
.isEqualTo(BatteryManager.BATTERY_STATUS_FULL);
|
||||
assertThat(values.getAsInteger("batteryHealth"))
|
||||
.isEqualTo(BatteryManager.BATTERY_HEALTH_COLD);
|
||||
assertThat(values.getAsString("packageName"))
|
||||
.isEqualTo(ConvertUtils.FAKE_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDrainType_returnsExpetcedResult() {
|
||||
final int expectedType = 3;
|
||||
|
Reference in New Issue
Block a user