2 * Copyright (C) 2016 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com
.android
.systemui
.tuner
;
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
;
27 import static com
.android
.systemui
.BatteryMeterDrawable
.SHOW_PERCENT_SETTING
;
29 public class BatteryPreference
extends DropDownPreference
implements TunerService
.Tunable
{
31 private static final String PERCENT
= "percent";
32 private static final String DEFAULT
= "default";
33 private static final String DISABLED
= "disabled";
35 private final String mBattery
;
36 private boolean mBatteryEnabled
;
37 private boolean mHasPercentage
;
38 private ArraySet
<String
> mBlacklist
;
39 private boolean mHasSetValue
;
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
});
48 public void onAttached() {
50 mHasPercentage
= Settings
.System
.getInt(getContext().getContentResolver(),
51 SHOW_PERCENT_SETTING
, 0) != 0;
52 TunerService
.get(getContext()).addTunable(this, StatusBarIconController
.ICON_BLACKLIST
);
56 public void onDetached() {
57 TunerService
.get(getContext()).removeTunable(this);
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
);
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.
72 if (mBatteryEnabled
&& mHasPercentage
) {
74 } else if (mBatteryEnabled
) {
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
);
90 mBlacklist
.remove(mBattery
);
92 TunerService
.get(getContext()).setValue(StatusBarIconController
.ICON_BLACKLIST
,
93 TextUtils
.join(",", mBlacklist
));