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