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 package com.android.settings.fuelgauge; 17 18 import android.content.Context; 19 import android.provider.Settings; 20 21 import androidx.preference.Preference; 22 import androidx.preference.TwoStatePreference; 23 24 import com.android.settings.core.BasePreferenceController; 25 import com.android.settings.overlay.FeatureFactory; 26 27 /** Controller to change and update the auto restriction toggle */ 28 public class AutoRestrictionPreferenceController extends BasePreferenceController 29 implements Preference.OnPreferenceChangeListener { 30 private static final String KEY_SMART_BATTERY = "auto_restriction"; 31 private static final int ON = 1; 32 private static final int OFF = 0; 33 private final PowerUsageFeatureProvider mPowerUsageFeatureProvider; 34 AutoRestrictionPreferenceController(Context context)35 public AutoRestrictionPreferenceController(Context context) { 36 super(context, KEY_SMART_BATTERY); 37 mPowerUsageFeatureProvider = 38 FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider(); 39 } 40 41 @Override getAvailabilityStatus()42 public int getAvailabilityStatus() { 43 return mPowerUsageFeatureProvider.isSmartBatterySupported() 44 ? UNSUPPORTED_ON_DEVICE 45 : AVAILABLE; 46 } 47 48 @Override updateState(Preference preference)49 public void updateState(Preference preference) { 50 super.updateState(preference); 51 final boolean smartBatteryOn = 52 Settings.Global.getInt( 53 mContext.getContentResolver(), 54 Settings.Global.APP_AUTO_RESTRICTION_ENABLED, 55 ON) 56 == ON; 57 ((TwoStatePreference) preference).setChecked(smartBatteryOn); 58 } 59 60 @Override onPreferenceChange(Preference preference, Object newValue)61 public boolean onPreferenceChange(Preference preference, Object newValue) { 62 final boolean smartBatteryOn = (Boolean) newValue; 63 Settings.Global.putInt( 64 mContext.getContentResolver(), 65 Settings.Global.APP_AUTO_RESTRICTION_ENABLED, 66 smartBatteryOn ? ON : OFF); 67 return true; 68 } 69 } 70