• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.PendingIntent;
21 import android.app.StatsManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.internal.util.CollectionUtils;
30 import com.android.settings.SettingsActivity;
31 import com.android.settings.core.InstrumentedPreferenceFragment;
32 import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
33 import com.android.settings.fuelgauge.batterytip.actions.OpenBatterySaverAction;
34 import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
35 import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
36 import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
37 import com.android.settings.fuelgauge.batterytip.actions.UnrestrictAppAction;
38 import com.android.settings.fuelgauge.batterytip.tips.AppLabelPredicate;
39 import com.android.settings.fuelgauge.batterytip.tips.AppRestrictionPredicate;
40 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
41 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
42 import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 /**
48  * Utility class for {@link BatteryTip}
49  */
50 public class BatteryTipUtils {
51     private static final int REQUEST_CODE = 0;
52 
53     /**
54      * Get a list of restricted apps with {@link AppOpsManager#OP_RUN_ANY_IN_BACKGROUND}
55      */
56     @NonNull
getRestrictedAppsList(AppOpsManager appOpsManager, UserManager userManager)57     public static List<AppInfo> getRestrictedAppsList(AppOpsManager appOpsManager,
58             UserManager userManager) {
59         final List<UserHandle> userHandles = userManager.getUserProfiles();
60         final List<AppOpsManager.PackageOps> packageOpsList = appOpsManager.getPackagesForOps(
61                 new int[]{AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
62         final List<AppInfo> appInfos = new ArrayList<>();
63 
64         for (int i = 0, size = CollectionUtils.size(packageOpsList); i < size; i++) {
65             final AppOpsManager.PackageOps packageOps = packageOpsList.get(i);
66             final List<AppOpsManager.OpEntry> entries = packageOps.getOps();
67             for (int j = 0, entriesSize = entries.size(); j < entriesSize; j++) {
68                 AppOpsManager.OpEntry entry = entries.get(j);
69                 if (entry.getOp() != AppOpsManager.OP_RUN_ANY_IN_BACKGROUND) {
70                     continue;
71                 }
72                 if (entry.getMode() != AppOpsManager.MODE_ALLOWED
73                         && userHandles.contains(
74                         new UserHandle(UserHandle.getUserId(packageOps.getUid())))) {
75                     appInfos.add(new AppInfo.Builder()
76                             .setPackageName(packageOps.getPackageName())
77                             .setUid(packageOps.getUid())
78                             .build());
79                 }
80             }
81         }
82 
83         return appInfos;
84     }
85 
86     /**
87      * Get a corresponding action based on {@code batteryTip}
88      * @param batteryTip used to detect which action to choose
89      * @param settingsActivity used to populate {@link BatteryTipAction}
90      * @param fragment used to populate {@link BatteryTipAction}
91      * @return an action for {@code batteryTip}
92      */
getActionForBatteryTip(BatteryTip batteryTip, SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment)93     public static BatteryTipAction getActionForBatteryTip(BatteryTip batteryTip,
94             SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment) {
95         switch (batteryTip.getType()) {
96             case BatteryTip.TipType.SMART_BATTERY_MANAGER:
97                 return new SmartBatteryAction(settingsActivity, fragment);
98             case BatteryTip.TipType.BATTERY_SAVER:
99             case BatteryTip.TipType.LOW_BATTERY:
100                 return new OpenBatterySaverAction(settingsActivity);
101             case BatteryTip.TipType.APP_RESTRICTION:
102                 if (batteryTip.getState() == BatteryTip.StateType.HANDLED) {
103                     return new OpenRestrictAppFragmentAction(fragment, (RestrictAppTip) batteryTip);
104                 } else {
105                     return new RestrictAppAction(settingsActivity, (RestrictAppTip) batteryTip);
106                 }
107             case BatteryTip.TipType.REMOVE_APP_RESTRICTION:
108                 return new UnrestrictAppAction(settingsActivity, (UnrestrictAppTip) batteryTip);
109             default:
110                 return null;
111         }
112     }
113 
114     /**
115      * Upload the {@link PendingIntent} to {@link StatsManager} for anomaly detection
116      * @throws StatsManager.StatsUnavailableException if failed to communicate with stats service
117      */
uploadAnomalyPendingIntent(Context context, StatsManager statsManager)118     public static void uploadAnomalyPendingIntent(Context context, StatsManager statsManager)
119             throws StatsManager.StatsUnavailableException {
120         final Intent extraIntent = new Intent(context, AnomalyDetectionReceiver.class);
121         final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE,
122                 extraIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
123         statsManager.setBroadcastSubscriber(pendingIntent,
124                 StatsManagerConfig.ANOMALY_CONFIG_KEY, StatsManagerConfig.SUBSCRIBER_ID);
125     }
126 
127     /**
128      * Detect and return anomaly apps after {@code timeAfterMs}
129      */
detectAnomalies(Context context, long timeAfterMs)130     public static List<AppInfo> detectAnomalies(Context context, long timeAfterMs) {
131         final List<AppInfo> highUsageApps = BatteryDatabaseManager.getInstance(context)
132                 .queryAllAnomalies(timeAfterMs, AnomalyDatabaseHelper.State.NEW);
133         // Remove it if it doesn't have label or been restricted
134         highUsageApps.removeIf(AppLabelPredicate.getInstance(context)
135                 .or(AppRestrictionPredicate.getInstance(context)));
136 
137         return highUsageApps;
138     }
139 }
140