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.RadioButtonPreference; 28 29 public class UnrestrictedPreferenceController extends AbstractPreferenceController 30 implements PreferenceControllerMixin { 31 32 private static final String TAG = "UNRESTRICTED_PREF"; 33 34 @VisibleForTesting String KEY_UNRESTRICTED_PREF = "unrestricted_pref"; 35 @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils; 36 UnrestrictedPreferenceController(Context context, int uid, String packageName)37 public UnrestrictedPreferenceController(Context context, int uid, String packageName) { 38 super(context); 39 mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName); 40 } 41 42 @Override updateState(Preference preference)43 public void updateState(Preference preference) { 44 45 if (!mBatteryOptimizeUtils.isValidPackageName()) { 46 Log.d(TAG, "invalid package name, disable pref"); 47 preference.setEnabled(false); 48 return; 49 } else { 50 preference.setEnabled(true); 51 } 52 53 if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) { 54 Log.d(TAG, "is system or default app, unrestricted states only"); 55 ((RadioButtonPreference) preference).setChecked(true); 56 } else if (mBatteryOptimizeUtils.getAppOptimizationMode() 57 == BatteryOptimizeUtils.MODE_UNRESTRICTED) { 58 Log.d(TAG, "is unrestricted states"); 59 ((RadioButtonPreference) preference).setChecked(true); 60 } else { 61 ((RadioButtonPreference) preference).setChecked(false); 62 } 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 return true; 68 } 69 70 @Override getPreferenceKey()71 public String getPreferenceKey() { 72 return KEY_UNRESTRICTED_PREF; 73 } 74 75 @Override handlePreferenceTreeClick(Preference preference)76 public boolean handlePreferenceTreeClick(Preference preference) { 77 return getPreferenceKey().equals(preference.getKey()); 78 } 79 } 80