1 /*
2  * Copyright (C) 2018 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.app.AppOpsManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.internal.util.CollectionUtils;
28 import com.android.settings.SettingsActivity;
29 import com.android.settings.core.InstrumentedPreferenceFragment;
30 import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
31 import com.android.settings.fuelgauge.batterytip.actions.OpenBatterySaverAction;
32 import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
33 import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
34 import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
35 import com.android.settings.fuelgauge.batterytip.actions.UnrestrictAppAction;
36 import com.android.settings.fuelgauge.batterytip.tips.AppLabelPredicate;
37 import com.android.settings.fuelgauge.batterytip.tips.AppRestrictionPredicate;
38 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
39 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
40 import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
41 
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 /** Utility class for {@link BatteryTip} */
46 public class BatteryTipUtils {
47     private static final int REQUEST_CODE = 0;
48 
49     /** Get a list of restricted apps with {@link AppOpsManager#OP_RUN_ANY_IN_BACKGROUND} */
50     @NonNull
getRestrictedAppsList( AppOpsManager appOpsManager, UserManager userManager)51     public static List<AppInfo> getRestrictedAppsList(
52             AppOpsManager appOpsManager, UserManager userManager) {
53         final List<UserHandle> userHandles = userManager.getUserProfiles();
54         final List<AppOpsManager.PackageOps> packageOpsList =
55                 appOpsManager.getPackagesForOps(new int[] {AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
56         final List<AppInfo> appInfos = new ArrayList<>();
57 
58         for (int i = 0, size = CollectionUtils.size(packageOpsList); i < size; i++) {
59             final AppOpsManager.PackageOps packageOps = packageOpsList.get(i);
60             final List<AppOpsManager.OpEntry> entries = packageOps.getOps();
61             for (int j = 0, entriesSize = entries.size(); j < entriesSize; j++) {
62                 AppOpsManager.OpEntry entry = entries.get(j);
63                 if (entry.getOp() != AppOpsManager.OP_RUN_ANY_IN_BACKGROUND) {
64                     continue;
65                 }
66                 if (entry.getMode() != AppOpsManager.MODE_ALLOWED
67                         && userHandles.contains(
68                                 new UserHandle(UserHandle.getUserId(packageOps.getUid())))) {
69                     appInfos.add(
70                             new AppInfo.Builder()
71                                     .setPackageName(packageOps.getPackageName())
72                                     .setUid(packageOps.getUid())
73                                     .build());
74                 }
75             }
76         }
77 
78         return appInfos;
79     }
80 
81     /**
82      * Get a corresponding action based on {@code batteryTip}
83      *
84      * @param batteryTip used to detect which action to choose
85      * @param settingsActivity used to populate {@link BatteryTipAction}
86      * @param fragment used to populate {@link BatteryTipAction}
87      * @return an action for {@code batteryTip}
88      */
getActionForBatteryTip( BatteryTip batteryTip, SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment)89     public static BatteryTipAction getActionForBatteryTip(
90             BatteryTip batteryTip,
91             SettingsActivity settingsActivity,
92             InstrumentedPreferenceFragment fragment) {
93         switch (batteryTip.getType()) {
94             case BatteryTip.TipType.SMART_BATTERY_MANAGER:
95                 return new SmartBatteryAction(settingsActivity, fragment);
96             case BatteryTip.TipType.BATTERY_SAVER:
97             case BatteryTip.TipType.LOW_BATTERY:
98                 return new OpenBatterySaverAction(settingsActivity);
99             case BatteryTip.TipType.APP_RESTRICTION:
100                 if (batteryTip.getState() == BatteryTip.StateType.HANDLED) {
101                     return new OpenRestrictAppFragmentAction(fragment, (RestrictAppTip) batteryTip);
102                 } else {
103                     return new RestrictAppAction(settingsActivity, (RestrictAppTip) batteryTip);
104                 }
105             case BatteryTip.TipType.REMOVE_APP_RESTRICTION:
106                 return new UnrestrictAppAction(settingsActivity, (UnrestrictAppTip) batteryTip);
107             default:
108                 return null;
109         }
110     }
111 
112    /** Detect and return anomaly apps after {@code timeAfterMs} */
detectAnomalies(Context context, long timeAfterMs)113     public static List<AppInfo> detectAnomalies(Context context, long timeAfterMs) {
114         return new ArrayList<>();
115     }
116 }
117