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.TwoStatePreference;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.overlay.FeatureFactory;
30 
31 /** Controller to change and update the smart battery toggle */
32 public class SmartBatteryPreferenceController extends BasePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
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 final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
39 
SmartBatteryPreferenceController(Context context)40     public SmartBatteryPreferenceController(Context context) {
41         super(context, KEY_SMART_BATTERY);
42         mPowerUsageFeatureProvider =
43                 FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
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
getSliceHighlightMenuRes()64     public int getSliceHighlightMenuRes() {
65         return R.string.menu_key_battery;
66     }
67 
68     @Override
updateState(Preference preference)69     public void updateState(Preference preference) {
70         super.updateState(preference);
71         final boolean smartBatteryOn =
72                 Settings.Global.getInt(
73                                 mContext.getContentResolver(),
74                                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED,
75                                 ON)
76                         == ON;
77         ((TwoStatePreference) preference).setChecked(smartBatteryOn);
78     }
79 
80     @Override
onPreferenceChange(Preference preference, Object newValue)81     public boolean onPreferenceChange(Preference preference, Object newValue) {
82         final boolean smartBatteryOn = (Boolean) newValue;
83         Settings.Global.putInt(
84                 mContext.getContentResolver(),
85                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED,
86                 smartBatteryOn ? ON : OFF);
87         return true;
88     }
89 }
90