1 /* 2 * Copyright (C) 2022 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; 18 19 import android.content.BroadcastReceiver; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.provider.Settings; 24 import android.util.Log; 25 26 import androidx.annotation.VisibleForTesting; 27 28 import com.android.settings.fuelgauge.batterysaver.BatterySaverScheduleRadioButtonsController; 29 import com.android.settingslib.fuelgauge.BatterySaverUtils; 30 31 import java.util.List; 32 33 /** Execute battery settings migration tasks in the device booting stage. */ 34 public final class BatterySettingsMigrateChecker extends BroadcastReceiver { 35 private static final String TAG = "BatterySettingsMigrateChecker"; 36 37 @VisibleForTesting 38 static BatteryOptimizeUtils sBatteryOptimizeUtils = null; 39 40 @Override onReceive(Context context, Intent intent)41 public void onReceive(Context context, Intent intent) { 42 Log.d(TAG, "onReceive: " + intent + " owner: " + BatteryBackupHelper.isOwner()); 43 if (intent != null 44 && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) 45 && BatteryBackupHelper.isOwner()) { 46 verifyConfiguration(context); 47 } 48 } 49 verifyConfiguration(Context context)50 static void verifyConfiguration(Context context) { 51 context = context.getApplicationContext(); 52 verifySaverConfiguration(context); 53 verifyOptimizationModes(context); 54 } 55 56 /** Avoid users set important apps into the unexpected battery optimize modes */ verifyOptimizationModes(Context context)57 static void verifyOptimizationModes(Context context) { 58 Log.d(TAG, "invoke verifyOptimizationModes()"); 59 verifyOptimizationModes(context, BatteryOptimizeUtils.getAllowList(context)); 60 } 61 62 @VisibleForTesting verifyOptimizationModes(Context context, List<String> allowList)63 static void verifyOptimizationModes(Context context, List<String> allowList) { 64 allowList.forEach(packageName -> { 65 final BatteryOptimizeUtils batteryOptimizeUtils = 66 BatteryBackupHelper.newBatteryOptimizeUtils(context, packageName, 67 /* testOptimizeUtils */ sBatteryOptimizeUtils); 68 if (batteryOptimizeUtils == null) { 69 return; 70 } 71 if (batteryOptimizeUtils.getAppOptimizationMode() != 72 BatteryOptimizeUtils.MODE_OPTIMIZED) { 73 Log.w(TAG, "Reset optimization mode for: " + packageName); 74 batteryOptimizeUtils.setAppUsageState(BatteryOptimizeUtils.MODE_OPTIMIZED, 75 BatteryOptimizeHistoricalLogEntry.Action.FORCE_RESET); 76 } 77 }); 78 } 79 verifySaverConfiguration(Context context)80 static void verifySaverConfiguration(Context context) { 81 Log.d(TAG, "invoke verifySaverConfiguration()"); 82 final ContentResolver resolver = context.getContentResolver(); 83 final int threshold = Settings.Global.getInt(resolver, 84 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 85 // Force refine the invalid scheduled battery level. 86 if (threshold < BatterySaverScheduleRadioButtonsController.TRIGGER_LEVEL_MIN 87 && threshold > 0) { 88 Settings.Global.putInt(resolver, Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 89 BatterySaverScheduleRadioButtonsController.TRIGGER_LEVEL_MIN); 90 Log.w(TAG, "Reset invalid scheduled battery level from: " + threshold); 91 } 92 // Force removing the 'schedule by routine' state. 93 BatterySaverUtils.revertScheduleToNoneIfNeeded(context); 94 } 95 } 96