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.text.TextUtils;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.SwitchPreference;
26 
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.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
isPublicSlice()59     public boolean isPublicSlice() {
60         return true;
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         super.updateState(preference);
66         final boolean smartBatteryOn = Settings.Global.getInt(mContext.getContentResolver(),
67                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON) == ON;
68         ((SwitchPreference) preference).setChecked(smartBatteryOn);
69     }
70 
71     @Override
onPreferenceChange(Preference preference, Object newValue)72     public boolean onPreferenceChange(Preference preference, Object newValue) {
73         final boolean smartBatteryOn = (Boolean) newValue;
74         Settings.Global.putInt(mContext.getContentResolver(),
75                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, smartBatteryOn ? ON : OFF);
76         return true;
77     }
78 }
79