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.actions;
18 
19 import android.app.AppOpsManager;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.internal.util.CollectionUtils;
26 import com.android.settings.fuelgauge.BatteryUtils;
27 import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
28 import com.android.settings.fuelgauge.batterytip.AppInfo;
29 import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
30 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
31 
32 import java.util.List;
33 
34 /** Action to restrict the apps, then app is not allowed to run in the background. */
35 public class RestrictAppAction extends BatteryTipAction {
36     private RestrictAppTip mRestrictAppTip;
37     @VisibleForTesting BatteryDatabaseManager mBatteryDatabaseManager;
38     @VisibleForTesting BatteryUtils mBatteryUtils;
39 
RestrictAppAction(Context context, RestrictAppTip tip)40     public RestrictAppAction(Context context, RestrictAppTip tip) {
41         super(context);
42         mRestrictAppTip = tip;
43         mBatteryUtils = BatteryUtils.getInstance(context);
44         mBatteryDatabaseManager = BatteryDatabaseManager.getInstance(context);
45     }
46 
47     /** Handle the action when user clicks positive button */
48     @Override
handlePositiveAction(int metricsKey)49     public void handlePositiveAction(int metricsKey) {
50         final List<AppInfo> appInfos = mRestrictAppTip.getRestrictAppList();
51 
52         for (int i = 0, size = appInfos.size(); i < size; i++) {
53             final AppInfo appInfo = appInfos.get(i);
54             final String packageName = appInfo.packageName;
55             // Force app standby, then app can't run in the background
56             mBatteryUtils.setForceAppStandby(appInfo.uid, packageName, AppOpsManager.MODE_IGNORED);
57             if (CollectionUtils.isEmpty(appInfo.anomalyTypes)) {
58                 // Only log context if there is no anomaly type
59                 mMetricsFeatureProvider.action(
60                         SettingsEnums.PAGE_UNKNOWN,
61                         SettingsEnums.ACTION_TIP_RESTRICT_APP,
62                         metricsKey,
63                         packageName,
64                         0);
65             } else {
66                 for (int type : appInfo.anomalyTypes) {
67                     mMetricsFeatureProvider.action(
68                             SettingsEnums.PAGE_UNKNOWN,
69                             SettingsEnums.ACTION_TIP_RESTRICT_APP,
70                             metricsKey,
71                             packageName,
72                             type);
73                 }
74             }
75         }
76 
77         mBatteryDatabaseManager.updateAnomalies(appInfos, AnomalyDatabaseHelper.State.HANDLED);
78     }
79 }
80