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.BatteryStats;
22 import android.os.SystemClock;
23 
24 import com.android.internal.os.BatteryStatsHelper;
25 import com.android.settings.overlay.FeatureFactory;
26 import com.android.settingslib.fuelgauge.Estimate;
27 import com.android.settingslib.fuelgauge.EstimateKt;
28 import com.android.settingslib.utils.AsyncLoaderCompat;
29 import com.android.settingslib.utils.PowerUtil;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> {
35     private BatteryStatsHelper mStatsHelper;
36 
DebugEstimatesLoader(Context context, BatteryStatsHelper statsHelper)37     public DebugEstimatesLoader(Context context, BatteryStatsHelper statsHelper) {
38         super(context);
39         mStatsHelper = statsHelper;
40     }
41 
42     @Override
onDiscardResult(List<BatteryInfo> result)43     protected void onDiscardResult(List<BatteryInfo> result) {
44 
45     }
46 
47     @Override
loadInBackground()48     public List<BatteryInfo> loadInBackground() {
49         Context context = getContext();
50         PowerUsageFeatureProvider powerUsageFeatureProvider =
51                 FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
52 
53         // get stuff we'll need for both BatteryInfo
54         final long elapsedRealtimeUs = PowerUtil.convertMsToUs(
55                 SystemClock.elapsedRealtime());
56         Intent batteryBroadcast = getContext().registerReceiver(null,
57                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
58         BatteryStats stats = mStatsHelper.getStats();
59 
60         BatteryInfo oldinfo = BatteryInfo.getBatteryInfoOld(getContext(), batteryBroadcast,
61                 stats, elapsedRealtimeUs, false);
62 
63         Estimate estimate = powerUsageFeatureProvider.getEnhancedBatteryPrediction(context);
64         if (estimate == null) {
65             estimate = new Estimate(0, false, EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
66         }
67         BatteryInfo newInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast, stats,
68                 estimate, elapsedRealtimeUs, false);
69 
70         List<BatteryInfo> infos = new ArrayList<>();
71         infos.add(oldinfo);
72         infos.add(newInfo);
73         return infos;
74     }
75 }
76