1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18 package com.android.settings.fuelgauge; 19 20 import android.content.Context; 21 import android.provider.Settings; 22 import android.text.TextUtils; 23 24 import androidx.preference.Preference; 25 import androidx.preference.SwitchPreference; 26 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settings.overlay.FeatureFactory; 29 30 /** 31 * Controller to change and update the smart battery toggle 32 */ 33 public class SmartBatteryPreferenceController extends BasePreferenceController implements 34 Preference.OnPreferenceChangeListener { 35 36 private static final String KEY_SMART_BATTERY = "smart_battery"; 37 private static final int ON = 1; 38 private static final int OFF = 0; 39 private PowerUsageFeatureProvider mPowerUsageFeatureProvider; 40 SmartBatteryPreferenceController(Context context)41 public SmartBatteryPreferenceController(Context context) { 42 super(context, KEY_SMART_BATTERY); 43 mPowerUsageFeatureProvider = FeatureFactory.getFactory(context) 44 .getPowerUsageFeatureProvider(context); 45 } 46 47 @Override getAvailabilityStatus()48 public int getAvailabilityStatus() { 49 return mPowerUsageFeatureProvider.isSmartBatterySupported() 50 ? AVAILABLE 51 : UNSUPPORTED_ON_DEVICE; 52 } 53 54 @Override isSliceable()55 public boolean isSliceable() { 56 return TextUtils.equals(getPreferenceKey(), "smart_battery"); 57 } 58 59 @Override isPublicSlice()60 public boolean isPublicSlice() { 61 return true; 62 } 63 64 @Override updateState(Preference preference)65 public void updateState(Preference preference) { 66 super.updateState(preference); 67 final boolean smartBatteryOn = Settings.Global.getInt(mContext.getContentResolver(), 68 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON) == ON; 69 ((SwitchPreference) preference).setChecked(smartBatteryOn); 70 } 71 72 @Override onPreferenceChange(Preference preference, Object newValue)73 public boolean onPreferenceChange(Preference preference, Object newValue) { 74 final boolean smartBatteryOn = (Boolean) newValue; 75 Settings.Global.putInt(mContext.getContentResolver(), 76 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, smartBatteryOn ? ON : OFF); 77 return true; 78 } 79 } 80