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.R; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.overlay.FeatureFactory; 30 31 /** 32 * Controller to change and update the smart battery toggle 33 */ 34 public class SmartBatteryPreferenceController extends BasePreferenceController implements 35 Preference.OnPreferenceChangeListener { 36 37 private static final String KEY_SMART_BATTERY = "smart_battery"; 38 private static final int ON = 1; 39 private static final int OFF = 0; 40 private PowerUsageFeatureProvider mPowerUsageFeatureProvider; 41 SmartBatteryPreferenceController(Context context)42 public SmartBatteryPreferenceController(Context context) { 43 super(context, KEY_SMART_BATTERY); 44 mPowerUsageFeatureProvider = FeatureFactory.getFactory(context) 45 .getPowerUsageFeatureProvider(context); 46 } 47 48 @Override getAvailabilityStatus()49 public int getAvailabilityStatus() { 50 return mPowerUsageFeatureProvider.isSmartBatterySupported() 51 ? AVAILABLE 52 : UNSUPPORTED_ON_DEVICE; 53 } 54 55 @Override isSliceable()56 public boolean isSliceable() { 57 return TextUtils.equals(getPreferenceKey(), "smart_battery"); 58 } 59 60 @Override isPublicSlice()61 public boolean isPublicSlice() { 62 return true; 63 } 64 65 @Override getSliceHighlightMenuRes()66 public int getSliceHighlightMenuRes() { 67 return R.string.menu_key_battery; 68 } 69 70 @Override updateState(Preference preference)71 public void updateState(Preference preference) { 72 super.updateState(preference); 73 final boolean smartBatteryOn = Settings.Global.getInt(mContext.getContentResolver(), 74 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON) == ON; 75 ((SwitchPreference) preference).setChecked(smartBatteryOn); 76 } 77 78 @Override onPreferenceChange(Preference preference, Object newValue)79 public boolean onPreferenceChange(Preference preference, Object newValue) { 80 final boolean smartBatteryOn = (Boolean) newValue; 81 Settings.Global.putInt(mContext.getContentResolver(), 82 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, smartBatteryOn ? ON : OFF); 83 return true; 84 } 85 } 86