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.os.BatteryUsageStats;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 import com.android.settings.fuelgauge.BatteryInfo;
25 import com.android.settings.fuelgauge.BatteryUtils;
26 import com.android.settings.fuelgauge.batterytip.detectors.BatteryDefenderDetector;
27 import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
28 import com.android.settings.fuelgauge.batterytip.detectors.IncompatibleChargerDetector;
29 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.utils.AsyncLoaderCompat;
32 
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 
37 /**
38  * Loader to compute and return a battery tip list. It will always return a full length list even
39  * though some tips may have state {@code BaseBatteryTip.StateType.INVISIBLE}.
40  */
41 public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
42     private static final String TAG = "BatteryTipLoader";
43 
44     private BatteryUsageStats mBatteryUsageStats;
45 
46     @VisibleForTesting BatteryUtils mBatteryUtils;
47 
BatteryTipLoader(Context context, BatteryUsageStats batteryUsageStats)48     public BatteryTipLoader(Context context, BatteryUsageStats batteryUsageStats) {
49         super(context);
50         mBatteryUsageStats = batteryUsageStats;
51         mBatteryUtils = BatteryUtils.getInstance(context);
52     }
53 
54     @Override
loadInBackground()55     public List<BatteryTip> loadInBackground() {
56         final List<BatteryTip> tips = new ArrayList<>();
57         final BatteryTipPolicy batteryTipPolicy = new BatteryTipPolicy(getContext());
58         final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
59         final Context context = getContext().getApplicationContext();
60 
61         tips.add(
62                 new HighUsageDetector(context, batteryTipPolicy, mBatteryUsageStats, batteryInfo)
63                         .detect());
64         tips.add(new BatteryDefenderDetector(batteryInfo, context).detect());
65         tips.add(new IncompatibleChargerDetector(context).detect());
66         FeatureFactory.getFeatureFactory()
67                 .getBatterySettingsFeatureProvider()
68                 .addBatteryTipDetector(context, tips, batteryInfo, batteryTipPolicy);
69         Collections.sort(tips);
70         return tips;
71     }
72 
73     @Override
onDiscardResult(List<BatteryTip> result)74     protected void onDiscardResult(List<BatteryTip> result) {}
75 }
76