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