2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.fuelgauge;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.IntentFilter;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.os.BatteryManager;
24 import android.os.BatteryStats;
25 import android.os.SystemClock;
26 import com.android.internal.os.BatteryStatsHelper;
27 import com.android.settings.overlay.FeatureFactory;
28 import com.android.settings.utils.AsyncLoader;
31 * Loader that can be used by classes to load BatteryInfo in a background thread. This loader will
32 * automatically grab enhanced battery estimates if available or fall back to the system estimate
35 public class BatteryInfoLoader extends AsyncLoader<BatteryInfo>{
36 BatteryStatsHelper mStatsHelper;
38 public BatteryInfoLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
40 mStatsHelper = batteryStatsHelper;
44 protected void onDiscardResult(BatteryInfo result) {
49 public BatteryInfo loadInBackground() {
50 Context context = getContext();
51 PowerUsageFeatureProvider powerUsageFeatureProvider =
52 FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
54 // Stuff we always need to get BatteryInfo
55 BatteryUtils batteryUtils = BatteryUtils.getInstance(context);
56 Intent batteryBroadcast = getContext().registerReceiver(null,
57 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
58 final long elapsedRealtimeUs = batteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
59 BatteryInfo batteryInfo;
61 // 0 means we are discharging, anything else means charging
62 boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
63 // Get enhanced prediction if available and discharging, otherwise use the old code
65 if (discharging && powerUsageFeatureProvider != null &&
66 powerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
67 final Uri queryUri = powerUsageFeatureProvider.getEnhancedBatteryPredictionUri();
68 cursor = context.getContentResolver().query(queryUri, null, null, null, null);
70 if (cursor != null && cursor.moveToFirst()) {
71 long enhancedEstimate = powerUsageFeatureProvider.getTimeRemainingEstimate(cursor);
72 batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
73 mStatsHelper.getStats(), elapsedRealtimeUs, false /* shortString */,
74 batteryUtils.convertMsToUs(enhancedEstimate), true /* basedOnUsage */);
76 BatteryStats stats = mStatsHelper.getStats();
77 batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
78 elapsedRealtimeUs, false /* shortString */,
79 discharging ? 0 : stats.computeBatteryTimeRemaining(elapsedRealtimeUs),
80 false /* basedOnUsage */);