From 43d4fefb36f1c14fdf2ac6e5ae3bff108b09df52 Mon Sep 17 00:00:00 2001 From: Alex Kulesza Date: Wed, 5 Jul 2017 17:43:11 -0400 Subject: [PATCH] Make utility methods static. BatteryUtils.convertUsToMs and .convertMsToUs should be static, and now they are. Bug: 63347148 Test: make RunSettingsRoboTests Change-Id: If652e2d3e1260df9a933805d7da670fbb26b2c25 --- src/com/android/settings/fuelgauge/BatteryInfo.java | 12 +++++------- .../settings/fuelgauge/BatteryInfoLoader.java | 9 ++++----- src/com/android/settings/fuelgauge/BatteryUtils.java | 4 ++-- .../settings/fuelgauge/DebugEstimatesLoader.java | 5 ++--- .../settings/fuelgauge/PowerUsageSummary.java | 5 ++--- .../android/settings/fuelgauge/BatteryInfoTest.java | 2 +- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/com/android/settings/fuelgauge/BatteryInfo.java b/src/com/android/settings/fuelgauge/BatteryInfo.java index b0f2f8458f4..f85ce41c14e 100644 --- a/src/com/android/settings/fuelgauge/BatteryInfo.java +++ b/src/com/android/settings/fuelgauge/BatteryInfo.java @@ -136,13 +136,11 @@ public class BatteryInfo { final long startTime = System.currentTimeMillis(); PowerUsageFeatureProvider provider = FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context); - final BatteryUtils batteryUtils = BatteryUtils.getInstance(context); final long elapsedRealtimeUs = - batteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); + BatteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); Intent batteryBroadcast = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - BatteryUtils utils = BatteryUtils.getInstance(context); // 0 means we are discharging, anything else means charging boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; @@ -152,7 +150,8 @@ public class BatteryInfo { final long prediction = provider.getEnhancedBatteryPrediction(context); BatteryUtils.logRuntime(LOG_TAG, "time for enhanced BatteryInfo", startTime); return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, - elapsedRealtimeUs, shortString, utils.convertMsToUs(prediction), true); + elapsedRealtimeUs, shortString, BatteryUtils.convertMsToUs(prediction), + true); } else { long prediction = discharging ? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0; @@ -187,14 +186,13 @@ public class BatteryInfo { info.batteryPercentString = Utils.formatPercentage(info.batteryLevel); info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0; final Resources resources = context.getResources(); - final BatteryUtils batteryUtils = BatteryUtils.getInstance(context); info.statusLabel = Utils.getBatteryStatus(resources, batteryBroadcast); if (!info.mCharging) { if (drainTimeUs > 0) { info.remainingTimeUs = drainTimeUs; CharSequence timeString = Utils.formatElapsedTime(context, - batteryUtils.convertUsToMs(drainTimeUs), false /* withSeconds */); + BatteryUtils.convertUsToMs(drainTimeUs), false /* withSeconds */); info.remainingLabel = TextUtils.expandTemplate(context.getText(shortString ? (basedOnUsage ? R.string.power_remaining_duration_only_short_enhanced : @@ -221,7 +219,7 @@ public class BatteryInfo { if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) { info.remainingTimeUs = chargeTime; CharSequence timeString = Utils.formatElapsedTime(context, - batteryUtils.convertUsToMs(chargeTime), false /* withSeconds */); + BatteryUtils.convertUsToMs(chargeTime), false /* withSeconds */); int resId = shortString ? R.string.power_charging_duration_short : R.string.power_charging_duration; info.remainingLabel = TextUtils.expandTemplate(context.getText( diff --git a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java index efaca3031ed..d6fbd656e96 100644 --- a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java +++ b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java @@ -55,10 +55,9 @@ public class BatteryInfoLoader extends AsyncLoader{ FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context); // Stuff we always need to get BatteryInfo - BatteryUtils batteryUtils = BatteryUtils.getInstance(context); Intent batteryBroadcast = getContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - final long elapsedRealtimeUs = batteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); + final long elapsedRealtimeUs = BatteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); BatteryInfo batteryInfo; // 0 means we are discharging, anything else means charging @@ -71,19 +70,19 @@ public class BatteryInfoLoader extends AsyncLoader{ cursor = context.getContentResolver().query(queryUri, null, null, null, null); } BatteryStats stats = mStatsHelper.getStats(); - batteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader post query", startTime); + BatteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader post query", startTime); if (cursor != null && cursor.moveToFirst()) { long enhancedEstimate = powerUsageFeatureProvider.getTimeRemainingEstimate(cursor); batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, elapsedRealtimeUs, false /* shortString */, - batteryUtils.convertMsToUs(enhancedEstimate), true /* basedOnUsage */); + BatteryUtils.convertMsToUs(enhancedEstimate), true /* basedOnUsage */); } else { batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, elapsedRealtimeUs, false /* shortString */, discharging ? 0 : stats.computeBatteryTimeRemaining(elapsedRealtimeUs), false /* basedOnUsage */); } - batteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader.loadInBackground", startTime); + BatteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader.loadInBackground", startTime); return batteryInfo; } } diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java index b6a7b02958d..48da88682e7 100644 --- a/src/com/android/settings/fuelgauge/BatteryUtils.java +++ b/src/com/android/settings/fuelgauge/BatteryUtils.java @@ -320,11 +320,11 @@ public class BatteryUtils { } } - public long convertUsToMs(long timeUs) { + public static long convertUsToMs(long timeUs) { return timeUs / 1000; } - public long convertMsToUs(long timeMs) { + public static long convertMsToUs(long timeMs) { return timeMs * 1000; } diff --git a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java index 08bd148ee88..5f4758a40b7 100644 --- a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java +++ b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java @@ -46,8 +46,7 @@ public class DebugEstimatesLoader extends AsyncLoader> { FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context); // get stuff we'll need for both BatteryInfo - BatteryUtils batteryUtils = BatteryUtils.getInstance(context); - final long elapsedRealtimeUs = batteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); + final long elapsedRealtimeUs = BatteryUtils.convertMsToUs(SystemClock.elapsedRealtime()); Intent batteryBroadcast = getContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); BatteryStats stats = mStatsHelper.getStats(); @@ -55,7 +54,7 @@ public class DebugEstimatesLoader extends AsyncLoader> { BatteryInfo oldinfo = BatteryInfo.getBatteryInfoOld(getContext(), batteryBroadcast, stats, elapsedRealtimeUs, false); - final long timeRemainingEnhanced = batteryUtils.convertMsToUs( + final long timeRemainingEnhanced = BatteryUtils.convertMsToUs( powerUsageFeatureProvider.getEnhancedBatteryPrediction(getContext())); BatteryInfo newinfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast, stats, elapsedRealtimeUs, false, timeRemainingEnhanced, true); diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java index dc52e1acf96..3ad014297cb 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java +++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java @@ -24,7 +24,6 @@ import android.content.Loader; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.BatteryStats; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -206,12 +205,12 @@ public class PowerUsageSummary extends PowerUsageBase implements // be unplugged for a period of time before being willing ot make an estimate. summary1.setText(mPowerFeatureProvider.getOldEstimateDebugString( Formatter.formatShortElapsedTime(getContext(), - mBatteryUtils.convertUsToMs(oldInfo.remainingTimeUs)))); + BatteryUtils.convertUsToMs(oldInfo.remainingTimeUs)))); // for this one we can just set the string directly summary2.setText(mPowerFeatureProvider.getEnhancedEstimateDebugString( Formatter.formatShortElapsedTime(getContext(), - mBatteryUtils.convertUsToMs(newInfo.remainingTimeUs)))); + BatteryUtils.convertUsToMs(newInfo.remainingTimeUs)))); batteryView.setBatteryLevel(oldInfo.batteryLevel); batteryView.setCharging(!oldInfo.discharging); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoTest.java index 715e4f070b0..bb0421bc5b6 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoTest.java @@ -160,7 +160,7 @@ public class BatteryInfoTest { .computeChargeTimeRemaining(anyLong()); BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast, mBatteryStats, SystemClock.elapsedRealtime() * 1000, false, 1000, false); - assertThat(info.remainingTimeUs = TEST_CHARGE_TIME_REMAINING); + assertThat(info.remainingTimeUs).isEqualTo(TEST_CHARGE_TIME_REMAINING); assertThat(info.remainingLabel.toString()) .isEqualTo(TEST_CHARGE_TIME_REMAINING_STRINGIFIED); }