Home | History | Annotate | Download | only in tuner
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. 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 distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.systemui.tuner;
     16 
     17 import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
     18 
     19 import android.content.Context;
     20 import android.provider.Settings;
     21 import android.text.TextUtils;
     22 import android.util.ArraySet;
     23 import android.util.AttributeSet;
     24 
     25 import androidx.preference.DropDownPreference;
     26 
     27 import com.android.internal.logging.MetricsLogger;
     28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     29 import com.android.systemui.Dependency;
     30 import com.android.systemui.statusbar.phone.StatusBarIconController;
     31 
     32 public class BatteryPreference extends DropDownPreference implements TunerService.Tunable {
     33 
     34     private static final String PERCENT = "percent";
     35     private static final String DEFAULT = "default";
     36     private static final String DISABLED = "disabled";
     37 
     38     private final String mBattery;
     39     private boolean mBatteryEnabled;
     40     private boolean mHasPercentage;
     41     private ArraySet<String> mBlacklist;
     42     private boolean mHasSetValue;
     43 
     44     public BatteryPreference(Context context, AttributeSet attrs) {
     45         super(context, attrs);
     46         mBattery = context.getString(com.android.internal.R.string.status_bar_battery);
     47         setEntryValues(new CharSequence[] {PERCENT, DEFAULT, DISABLED });
     48     }
     49 
     50     @Override
     51     public void onAttached() {
     52         super.onAttached();
     53         mHasPercentage = Settings.System.getInt(getContext().getContentResolver(),
     54                 SHOW_BATTERY_PERCENT, 0) != 0;
     55         Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
     56     }
     57 
     58     @Override
     59     public void onDetached() {
     60         Dependency.get(TunerService.class).removeTunable(this);
     61         super.onDetached();
     62     }
     63 
     64     @Override
     65     public void onTuningChanged(String key, String newValue) {
     66         if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
     67             mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
     68             mBatteryEnabled = !mBlacklist.contains(mBattery);
     69         }
     70         if (!mHasSetValue) {
     71             // Because of the complicated tri-state it can end up looping and setting state back to
     72             // what the user didn't choose.  To avoid this, just set the state once and rely on the
     73             // preference to handle updates.
     74             mHasSetValue = true;
     75             if (mBatteryEnabled && mHasPercentage) {
     76                 setValue(PERCENT);
     77             } else if (mBatteryEnabled) {
     78                 setValue(DEFAULT);
     79             } else {
     80                 setValue(DISABLED);
     81             }
     82         }
     83     }
     84 
     85     @Override
     86     protected boolean persistString(String value) {
     87         final boolean v = PERCENT.equals(value);
     88         MetricsLogger.action(getContext(), MetricsEvent.TUNER_BATTERY_PERCENTAGE, v);
     89         Settings.System.putInt(getContext().getContentResolver(), SHOW_BATTERY_PERCENT, v ? 1 : 0);
     90         if (DISABLED.equals(value)) {
     91             mBlacklist.add(mBattery);
     92         } else {
     93             mBlacklist.remove(mBattery);
     94         }
     95         Dependency.get(TunerService.class).setValue(StatusBarIconController.ICON_BLACKLIST,
     96                 TextUtils.join(",", mBlacklist));
     97         return true;
     98     }
     99 }
    100