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