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 android.content.Context; 18 import android.provider.Settings; 19 import android.support.v7.preference.DropDownPreference; 20 import android.text.TextUtils; 21 import android.util.ArraySet; 22 import android.util.AttributeSet; 23 import com.android.internal.logging.MetricsLogger; 24 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 25 import com.android.systemui.Dependency; 26 import com.android.systemui.statusbar.phone.StatusBarIconController; 27 28 import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; 29 30 public class BatteryPreference extends DropDownPreference implements TunerService.Tunable { 31 32 private static final String PERCENT = "percent"; 33 private static final String DEFAULT = "default"; 34 private static final String DISABLED = "disabled"; 35 36 private final String mBattery; 37 private boolean mBatteryEnabled; 38 private boolean mHasPercentage; 39 private ArraySet<String> mBlacklist; 40 private boolean mHasSetValue; 41 42 public BatteryPreference(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 mBattery = context.getString(com.android.internal.R.string.status_bar_battery); 45 setEntryValues(new CharSequence[] {PERCENT, DEFAULT, DISABLED }); 46 } 47 48 @Override 49 public void onAttached() { 50 super.onAttached(); 51 mHasPercentage = Settings.System.getInt(getContext().getContentResolver(), 52 SHOW_BATTERY_PERCENT, 0) != 0; 53 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST); 54 } 55 56 @Override 57 public void onDetached() { 58 Dependency.get(TunerService.class).removeTunable(this); 59 super.onDetached(); 60 } 61 62 @Override 63 public void onTuningChanged(String key, String newValue) { 64 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { 65 mBlacklist = StatusBarIconController.getIconBlacklist(newValue); 66 mBatteryEnabled = !mBlacklist.contains(mBattery); 67 } 68 if (!mHasSetValue) { 69 // Because of the complicated tri-state it can end up looping and setting state back to 70 // what the user didn't choose. To avoid this, just set the state once and rely on the 71 // preference to handle updates. 72 mHasSetValue = true; 73 if (mBatteryEnabled && mHasPercentage) { 74 setValue(PERCENT); 75 } else if (mBatteryEnabled) { 76 setValue(DEFAULT); 77 } else { 78 setValue(DISABLED); 79 } 80 } 81 } 82 83 @Override 84 protected boolean persistString(String value) { 85 final boolean v = PERCENT.equals(value); 86 MetricsLogger.action(getContext(), MetricsEvent.TUNER_BATTERY_PERCENTAGE, v); 87 Settings.System.putInt(getContext().getContentResolver(), SHOW_BATTERY_PERCENT, v ? 1 : 0); 88 if (DISABLED.equals(value)) { 89 mBlacklist.add(mBattery); 90 } else { 91 mBlacklist.remove(mBattery); 92 } 93 Dependency.get(TunerService.class).setValue(StatusBarIconController.ICON_BLACKLIST, 94 TextUtils.join(",", mBlacklist)); 95 return true; 96 } 97 } 98