1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.car.developeroptions.display;
17 
18 import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
19 
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.SwitchPreference;
25 
26 import com.android.internal.R;
27 import com.android.car.developeroptions.core.BasePreferenceController;
28 import com.android.car.developeroptions.core.PreferenceControllerMixin;
29 
30 /**
31  * A controller to manage the switch for showing battery percentage in the status bar.
32  */
33 
34 public class BatteryPercentagePreferenceController extends BasePreferenceController implements
35         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
36 
BatteryPercentagePreferenceController(Context context, String preferenceKey)37     public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
38         super(context, preferenceKey);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         return mContext.getResources().getBoolean(
44                 R.bool.config_battery_percentage_setting_available) ? AVAILABLE
45                 : UNSUPPORTED_ON_DEVICE;
46     }
47 
48     @Override
updateState(Preference preference)49     public void updateState(Preference preference) {
50         int setting = Settings.System.getInt(mContext.getContentResolver(),
51                 SHOW_BATTERY_PERCENT, 0);
52 
53         ((SwitchPreference) preference).setChecked(setting == 1);
54     }
55 
56     @Override
onPreferenceChange(Preference preference, Object newValue)57     public boolean onPreferenceChange(Preference preference, Object newValue) {
58         boolean showPercentage = (Boolean) newValue;
59         Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,
60                 showPercentage ? 1 : 0);
61         return true;
62     }
63 }
64