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 17 package com.android.settings.fuelgauge.batterytip; 18 19 import android.content.Context; 20 21 import androidx.annotation.VisibleForTesting; 22 23 import com.android.internal.os.BatteryStatsHelper; 24 import com.android.settings.fuelgauge.BatteryInfo; 25 import com.android.settings.fuelgauge.BatteryUtils; 26 import com.android.settings.fuelgauge.batterytip.detectors.EarlyWarningDetector; 27 import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector; 28 import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector; 29 import com.android.settings.fuelgauge.batterytip.detectors.RestrictAppDetector; 30 import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector; 31 import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector; 32 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; 33 import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip; 34 import com.android.settings.fuelgauge.batterytip.tips.SummaryTip; 35 import com.android.settingslib.fuelgauge.EstimateKt; 36 import com.android.settingslib.utils.AsyncLoaderCompat; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 42 /** 43 * Loader to compute and return a battery tip list. It will always return a full length list even 44 * though some tips may have state {@code BaseBatteryTip.StateType.INVISIBLE}. 45 */ 46 public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> { 47 private static final String TAG = "BatteryTipLoader"; 48 49 private static final boolean USE_FAKE_DATA = false; 50 51 private BatteryStatsHelper mBatteryStatsHelper; 52 @VisibleForTesting 53 BatteryUtils mBatteryUtils; 54 BatteryTipLoader(Context context, BatteryStatsHelper batteryStatsHelper)55 public BatteryTipLoader(Context context, BatteryStatsHelper batteryStatsHelper) { 56 super(context); 57 mBatteryStatsHelper = batteryStatsHelper; 58 mBatteryUtils = BatteryUtils.getInstance(context); 59 } 60 61 @Override loadInBackground()62 public List<BatteryTip> loadInBackground() { 63 if (USE_FAKE_DATA) { 64 return getFakeData(); 65 } 66 final List<BatteryTip> tips = new ArrayList<>(); 67 final BatteryTipPolicy policy = new BatteryTipPolicy(getContext()); 68 final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG); 69 final Context context = getContext(); 70 71 tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect()); 72 tips.add(new HighUsageDetector(context, policy, mBatteryStatsHelper, 73 batteryInfo.discharging).detect()); 74 tips.add(new SmartBatteryDetector(policy, context.getContentResolver()).detect()); 75 tips.add(new EarlyWarningDetector(policy, context).detect()); 76 tips.add(new SummaryDetector(policy, batteryInfo.averageTimeToDischarge).detect()); 77 tips.add(new RestrictAppDetector(context, policy).detect()); 78 79 Collections.sort(tips); 80 return tips; 81 } 82 83 @Override onDiscardResult(List<BatteryTip> result)84 protected void onDiscardResult(List<BatteryTip> result) { 85 } 86 getFakeData()87 private List<BatteryTip> getFakeData() { 88 final List<BatteryTip> tips = new ArrayList<>(); 89 tips.add(new SummaryTip(BatteryTip.StateType.NEW, 90 EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN)); 91 tips.add(new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */, 92 "Fake data")); 93 94 return tips; 95 } 96 97 } 98