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