Home | History | Annotate | Download | only in display
      1 /*
      2  * Copyright (C) 2016 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.settings.display;
     17 
     18 import android.content.Context;
     19 import android.provider.Settings;
     20 import android.support.v7.preference.Preference;
     21 import android.support.v14.preference.SwitchPreference;
     22 
     23 import com.android.settings.core.PreferenceControllerMixin;
     24 import com.android.settingslib.core.AbstractPreferenceController;
     25 
     26 import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
     27 
     28 /**
     29  * A controller to manage the switch for showing battery percentage in the status bar.
     30  */
     31 
     32 public class BatteryPercentagePreferenceController extends AbstractPreferenceController implements
     33         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
     34 
     35     private static final String KEY_BATTERY_PERCENTAGE = "battery_percentage";
     36 
     37     public BatteryPercentagePreferenceController(Context context) {
     38         super(context);
     39     }
     40 
     41     @Override
     42     public boolean isAvailable() {
     43         return true;
     44     }
     45 
     46     @Override
     47     public String getPreferenceKey() {
     48         return KEY_BATTERY_PERCENTAGE;
     49     }
     50 
     51     @Override
     52     public void updateState(Preference preference) {
     53         int setting = Settings.System.getInt(mContext.getContentResolver(),
     54                 SHOW_BATTERY_PERCENT, 0);
     55 
     56         ((SwitchPreference) preference).setChecked(setting == 1);
     57     }
     58 
     59     @Override
     60     public boolean onPreferenceChange(Preference preference, Object newValue) {
     61         boolean showPercentage = (Boolean) newValue;
     62         Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,
     63                 showPercentage ? 1 : 0);
     64         return true;
     65     }
     66 }
     67