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