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