1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.fuelgauge; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.IntentFilter; 21 import android.os.BatteryStatsManager; 22 import android.os.BatteryUsageStats; 23 import android.os.SystemClock; 24 import android.util.Log; 25 26 import com.android.settings.overlay.FeatureFactory; 27 import com.android.settingslib.fuelgauge.Estimate; 28 import com.android.settingslib.fuelgauge.EstimateKt; 29 import com.android.settingslib.utils.AsyncLoaderCompat; 30 import com.android.settingslib.utils.PowerUtil; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> { 36 private static final String TAG = "DebugEstimatesLoader"; 37 DebugEstimatesLoader(Context context)38 public DebugEstimatesLoader(Context context) { 39 super(context); 40 } 41 42 @Override onDiscardResult(List<BatteryInfo> result)43 protected void onDiscardResult(List<BatteryInfo> result) {} 44 45 @Override loadInBackground()46 public List<BatteryInfo> loadInBackground() { 47 Context context = getContext(); 48 PowerUsageFeatureProvider powerUsageFeatureProvider = 49 FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider(); 50 51 // get stuff we'll need for both BatteryInfo 52 final long elapsedRealtimeUs = PowerUtil.convertMsToUs(SystemClock.elapsedRealtime()); 53 Intent batteryBroadcast = 54 getContext() 55 .registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 56 BatteryUsageStats batteryUsageStats; 57 try { 58 batteryUsageStats = 59 context.getSystemService(BatteryStatsManager.class).getBatteryUsageStats(); 60 } catch (RuntimeException e) { 61 Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e); 62 // Use default BatteryUsageStats. 63 batteryUsageStats = new BatteryUsageStats.Builder(new String[0]).build(); 64 } 65 BatteryInfo oldinfo = 66 BatteryInfo.getBatteryInfoOld( 67 getContext(), 68 batteryBroadcast, 69 batteryUsageStats, 70 elapsedRealtimeUs, 71 false); 72 73 Estimate estimate = powerUsageFeatureProvider.getEnhancedBatteryPrediction(context); 74 if (estimate == null) { 75 estimate = new Estimate(0, false, EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN); 76 } 77 BatteryInfo newInfo = 78 BatteryInfo.getBatteryInfo( 79 getContext(), 80 batteryBroadcast, 81 batteryUsageStats, 82 estimate, 83 elapsedRealtimeUs, 84 false); 85 86 List<BatteryInfo> infos = new ArrayList<>(); 87 infos.add(oldinfo); 88 infos.add(newInfo); 89 90 try { 91 batteryUsageStats.close(); 92 } catch (Exception e) { 93 Log.e(TAG, "BatteryUsageStats.close() failed", e); 94 } 95 return infos; 96 } 97 } 98