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