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