1 /* 2 * Copyright (C) 2021 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.VisibleForTesting; 23 import androidx.preference.Preference; 24 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settingslib.core.AbstractPreferenceController; 27 import com.android.settingslib.widget.SelectorWithWidgetPreference; 28 29 public class OptimizedPreferenceController extends AbstractPreferenceController 30 implements PreferenceControllerMixin { 31 32 private static final String TAG = "OPTIMIZED_PREF"; 33 34 @VisibleForTesting String KEY_OPTIMIZED_PREF = "optimized_pref"; 35 @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils; 36 OptimizedPreferenceController(Context context, int uid, String packageName)37 public OptimizedPreferenceController(Context context, int uid, String packageName) { 38 super(context); 39 mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName); 40 } 41 42 @Override isAvailable()43 public boolean isAvailable() { 44 return true; 45 } 46 47 @Override updateState(Preference preference)48 public void updateState(Preference preference) { 49 if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) { 50 Log.d(TAG, "disable preference for " + mBatteryOptimizeUtils.getPackageName()); 51 preference.setEnabled(true); 52 ((SelectorWithWidgetPreference) preference).setChecked(true); 53 return; 54 } 55 56 if (mBatteryOptimizeUtils.getAppOptimizationMode() 57 == BatteryOptimizeUtils.MODE_OPTIMIZED) { 58 Log.d(TAG, "is optimized states"); 59 ((SelectorWithWidgetPreference) preference).setChecked(true); 60 } else { 61 ((SelectorWithWidgetPreference) preference).setChecked(false); 62 if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) { 63 Log.d(TAG, "is system or default app, disable pref"); 64 preference.setEnabled(false); 65 } 66 } 67 } 68 69 @Override getPreferenceKey()70 public String getPreferenceKey() { 71 return KEY_OPTIMIZED_PREF; 72 } 73 74 @Override handlePreferenceTreeClick(Preference preference)75 public boolean handlePreferenceTreeClick(Preference preference) { 76 return getPreferenceKey().equals(preference.getKey()); 77 } 78 } 79