1 /* 2 * Copyright (C) 2024 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.Context; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settings.dashboard.DashboardFragment; 32 import com.android.settingslib.PrimarySwitchPreference; 33 34 /** Controller to update the manage battery usage preference in App Battery Usage page */ 35 public class BackgroundUsageAllowabilityPreferenceController extends BasePreferenceController 36 implements PreferenceControllerMixin { 37 38 @VisibleForTesting 39 static final String KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH = 40 "background_usage_allowability_switch"; 41 42 private final BatteryOptimizeUtils mBatteryOptimizeUtils; 43 private final DashboardFragment mDashboardFragment; 44 @Nullable @VisibleForTesting PrimarySwitchPreference mBackgroundUsageAllowabilityPreference; 45 BackgroundUsageAllowabilityPreferenceController( @onNull Context context, @NonNull DashboardFragment dashboardFragment, @NonNull String preferenceKey, @NonNull BatteryOptimizeUtils batteryOptimizeUtils)46 public BackgroundUsageAllowabilityPreferenceController( 47 @NonNull Context context, 48 @NonNull DashboardFragment dashboardFragment, 49 @NonNull String preferenceKey, 50 @NonNull BatteryOptimizeUtils batteryOptimizeUtils) { 51 super(context, preferenceKey); 52 mDashboardFragment = dashboardFragment; 53 mBatteryOptimizeUtils = batteryOptimizeUtils; 54 } 55 56 @Override getAvailabilityStatus()57 public int getAvailabilityStatus() { 58 return AVAILABLE; 59 } 60 61 @Override updateState(@onNull Preference preference)62 public void updateState(@NonNull Preference preference) { 63 updatePreferences(mBatteryOptimizeUtils.getAppOptimizationMode()); 64 } 65 66 @Override displayPreference(@onNull PreferenceScreen screen)67 public void displayPreference(@NonNull PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mBackgroundUsageAllowabilityPreference = 70 screen.findPreference(KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH); 71 initPreferences(); 72 } 73 74 @VisibleForTesting initPreferences()75 void initPreferences() { 76 if (mBackgroundUsageAllowabilityPreference == null) { 77 return; 78 } 79 final String stateString; 80 final String detailInfoString; 81 boolean isPreferenceEnabled = true; 82 if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) { 83 // Present "Optimized" only string if the package name is invalid. 84 stateString = mContext.getString(R.string.manager_battery_usage_optimized_only); 85 detailInfoString = 86 mContext.getString(R.string.manager_battery_usage_footer_limited, stateString); 87 isPreferenceEnabled = false; 88 } else if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) { 89 // Present "Unrestricted" only string if the package is system important apps. 90 stateString = mContext.getString(R.string.manager_battery_usage_unrestricted_only); 91 detailInfoString = 92 mContext.getString(R.string.manager_battery_usage_footer_limited, stateString); 93 isPreferenceEnabled = false; 94 } else { 95 // Present default string to normal app. 96 detailInfoString = 97 mContext.getString( 98 R.string.manager_battery_usage_allow_background_usage_summary); 99 } 100 mBackgroundUsageAllowabilityPreference.setEnabled(isPreferenceEnabled); 101 mBackgroundUsageAllowabilityPreference.setSwitchEnabled(isPreferenceEnabled); 102 mBackgroundUsageAllowabilityPreference.setSummary(detailInfoString); 103 if (isPreferenceEnabled) { 104 mBackgroundUsageAllowabilityPreference.setOnPreferenceClickListener( 105 preference -> { 106 PowerBackgroundUsageDetail.startPowerBackgroundUsageDetailPage( 107 mContext, mDashboardFragment.getArguments()); 108 return true; 109 }); 110 mBackgroundUsageAllowabilityPreference.setOnPreferenceChangeListener( 111 (preference, isAllowBackground) -> { 112 handleBatteryOptimizeModeUpdated( 113 (boolean) isAllowBackground 114 ? BatteryOptimizeUtils.MODE_OPTIMIZED 115 : BatteryOptimizeUtils.MODE_RESTRICTED); 116 return true; 117 }); 118 } 119 } 120 121 @VisibleForTesting handleBatteryOptimizeModeUpdated(int newBatteryOptimizeMode)122 void handleBatteryOptimizeModeUpdated(int newBatteryOptimizeMode) { 123 if (mBatteryOptimizeUtils.getAppOptimizationMode() == newBatteryOptimizeMode) { 124 Log.w(TAG, "ignore same mode for: " + mBatteryOptimizeUtils.getPackageName()); 125 return; 126 } 127 mBatteryOptimizeUtils.setAppUsageState( 128 newBatteryOptimizeMode, BatteryOptimizeHistoricalLogEntry.Action.APPLY); 129 updatePreferences(newBatteryOptimizeMode); 130 } 131 132 @VisibleForTesting updatePreferences(int optimizationMode)133 void updatePreferences(int optimizationMode) { 134 if (mBackgroundUsageAllowabilityPreference == null) { 135 return; 136 } 137 mBackgroundUsageAllowabilityPreference.setChecked( 138 optimizationMode != BatteryOptimizeUtils.MODE_RESTRICTED); 139 } 140 } 141