OSDN Git Service

Fix Bluetooth device details crash on screen rotation
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / BatteryInfoLoader.java
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.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;
29
30 /**
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
33  * when not available.
34  */
35 public class BatteryInfoLoader extends AsyncLoader<BatteryInfo>{
36     BatteryStatsHelper mStatsHelper;
37
38     public BatteryInfoLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
39         super(context);
40         mStatsHelper = batteryStatsHelper;
41     }
42
43     @Override
44     protected void onDiscardResult(BatteryInfo result) {
45
46     }
47
48     @Override
49     public BatteryInfo loadInBackground() {
50         Context context = getContext();
51         PowerUsageFeatureProvider powerUsageFeatureProvider =
52                 FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
53
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;
60
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
64         Cursor cursor = null;
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);
69         }
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 */);
75         } else {
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 */);
81         }
82
83         return batteryInfo;
84     }
85 }