Send fake data if there is no battey consumption entry list am: bdaee1431f
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/14140156 Change-Id: I185246d4516efe96c72bc0c4c853ccfb5d8be3aa
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,24 +88,28 @@ public final class ConvertUtils {
|
||||
int batteryHealth,
|
||||
long timestamp) {
|
||||
final ContentValues values = new ContentValues();
|
||||
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()));
|
||||
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("totalPower",
|
||||
Double.valueOf(batteryUsageStats.getConsumedPower()));
|
||||
values.put("consumePower", Double.valueOf(entry.getConsumedPower()));
|
||||
values.put("percentOfTotal", Double.valueOf(entry.percent));
|
||||
values.put("foregroundUsageTimeInMs",
|
||||
Long.valueOf(entry.getTimeInForegroundMs()));
|
||||
values.put("backgroundUsageTimeInMs",
|
||||
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("totalPower",
|
||||
Double.valueOf(batteryUsageStats.getConsumedPower()));
|
||||
values.put("consumePower", Double.valueOf(entry.getConsumedPower()));
|
||||
values.put("percentOfTotal", Double.valueOf(entry.percent));
|
||||
values.put("foregroundUsageTimeInMs",
|
||||
Long.valueOf(entry.getTimeInForegroundMs()));
|
||||
values.put("backgroundUsageTimeInMs",
|
||||
Long.valueOf(entry.getTimeInBackgroundMs()));
|
||||
values.put("drainType", getDrainType(entry.getBatteryConsumer()));
|
||||
values.put("consumerType", getConsumerType(entry.getBatteryConsumer()));
|
||||
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