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