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.SmartBatteryDetector; 30 import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector; 31 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; 32 import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip; 33 import com.android.settings.fuelgauge.batterytip.tips.SummaryTip; 34 import com.android.settingslib.fuelgauge.EstimateKt; 35 import com.android.settingslib.utils.AsyncLoaderCompat; 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 AsyncLoaderCompat<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 BatteryTipLoader(Context context, BatteryStatsHelper batteryStatsHelper)54 public BatteryTipLoader(Context context, BatteryStatsHelper batteryStatsHelper) { 55 super(context); 56 mBatteryStatsHelper = batteryStatsHelper; 57 mBatteryUtils = BatteryUtils.getInstance(context); 58 } 59 60 @Override loadInBackground()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 // Disable this feature now since it introduces false positive cases. We will try to improve 77 // it in the future. 78 // tips.add(new RestrictAppDetector(context, policy).detect()); 79 80 Collections.sort(tips); 81 return tips; 82 } 83 84 @Override onDiscardResult(List<BatteryTip> result)85 protected void onDiscardResult(List<BatteryTip> result) { 86 } 87 getFakeData()88 private List<BatteryTip> getFakeData() { 89 final List<BatteryTip> tips = new ArrayList<>(); 90 tips.add(new SummaryTip(BatteryTip.StateType.NEW, 91 EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN)); 92 tips.add(new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */, 93 "Fake data")); 94 95 return tips; 96 } 97 98 } 99