OSDN Git Service

Fix null pointer from PowerUsageFeatureProvider
authorSalvador Martinez <dehboxturtle@google.com>
Wed, 24 Jan 2018 18:24:04 +0000 (10:24 -0800)
committerandroid-build-team Robot <android-build-team-robot@google.com>
Thu, 25 Jan 2018 03:57:03 +0000 (03:57 +0000)
Some locations did not check if the returned value was null before
doing operations on them and could crash. This CL changes those
spots to take that into account.

Test: b/72463854 will add in follow up to unblock dogfood
Bug: 72350595
Change-Id: I0ace5c0ab4a8aa9fd5b09d41d6f986143246f059
(cherry picked from commit 250a79830cd065602b99ec3d611da694ee294ea8)

src/com/android/settings/fuelgauge/BatteryInfo.java
src/com/android/settings/fuelgauge/DebugEstimatesLoader.java

index c4c795b..6384130 100644 (file)
@@ -171,18 +171,20 @@ public class BatteryInfo {
                 if (discharging && provider != null
                         && provider.isEnhancedBatteryPredictionEnabled(context)) {
                     Estimate estimate = provider.getEnhancedBatteryPrediction(context);
-                    BatteryUtils.logRuntime(LOG_TAG, "time for enhanced BatteryInfo", startTime);
-                    return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
-                            elapsedRealtimeUs, shortString,
-                            BatteryUtils.convertMsToUs(estimate.estimateMillis),
-                            estimate.isBasedOnUsage);
-                } else {
-                    long prediction = discharging
-                            ? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0;
-                    BatteryUtils.logRuntime(LOG_TAG, "time for regular BatteryInfo", startTime);
-                    return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
-                            elapsedRealtimeUs, shortString, prediction, false);
+                    if(estimate != null) {
+                        BatteryUtils
+                                .logRuntime(LOG_TAG, "time for enhanced BatteryInfo", startTime);
+                        return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
+                                elapsedRealtimeUs, shortString,
+                                BatteryUtils.convertMsToUs(estimate.estimateMillis),
+                                estimate.isBasedOnUsage);
+                    }
                 }
+                long prediction = discharging
+                        ? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0;
+                BatteryUtils.logRuntime(LOG_TAG, "time for regular BatteryInfo", startTime);
+                return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
+                        elapsedRealtimeUs, shortString, prediction, false);
             }
 
             @Override
index a080e2b..e58ccd3 100644 (file)
@@ -55,6 +55,9 @@ public class DebugEstimatesLoader extends AsyncLoader<List<BatteryInfo>> {
                 stats, elapsedRealtimeUs, false);
 
         Estimate estimate = powerUsageFeatureProvider.getEnhancedBatteryPrediction(context);
+        if (estimate == null) {
+            estimate = new Estimate(0, false);
+        }
         BatteryInfo newInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast, stats,
                 elapsedRealtimeUs, false,
                 BatteryUtils.convertMsToUs(estimate.estimateMillis),