Home | History | Annotate | Download | only in batterysaver
      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.batterysaver;
     17 
     18 import android.content.Context;
     19 import android.provider.Settings;
     20 import android.support.annotation.VisibleForTesting;
     21 import android.support.v7.preference.Preference;
     22 
     23 import com.android.settings.core.TogglePreferenceController;
     24 import com.android.settingslib.fuelgauge.BatterySaverUtils;
     25 
     26 /**
     27  * Controller that update whether to turn on battery saver automatically
     28  */
     29 public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
     30         Preference.OnPreferenceChangeListener {
     31 
     32     /**
     33      * Default value for {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL}.
     34      */
     35     static final int DEFAULT_TRIGGER_LEVEL = 0;
     36 
     37     /**
     38      * The default value to set to {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL} when the
     39      * user enables battery saver.
     40      */
     41     private final int mDefaultTriggerLevelForOn;
     42 
     43     @VisibleForTesting
     44     static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
     45 
     46     public AutoBatterySaverPreferenceController(Context context) {
     47         super(context, KEY_AUTO_BATTERY_SAVER);
     48         mDefaultTriggerLevelForOn = mContext.getResources().getInteger(
     49                 com.android.internal.R.integer.config_lowBatteryWarningLevel);
     50     }
     51 
     52     @Override
     53     public int getAvailabilityStatus() {
     54         return AVAILABLE;
     55     }
     56 
     57     @Override
     58     public boolean isChecked() {
     59         return Settings.Global.getInt(mContext.getContentResolver(),
     60                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, DEFAULT_TRIGGER_LEVEL) != 0;
     61     }
     62 
     63     @Override
     64     public boolean setChecked(boolean isChecked) {
     65         BatterySaverUtils.setAutoBatterySaverTriggerLevel(mContext,
     66                 isChecked ? mDefaultTriggerLevelForOn : 0);
     67         return true;
     68     }
     69 }
     70