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 package com.android.systemui.tuner;
15 
16 import android.content.Context;
17 import android.text.TextUtils;
18 import android.util.ArraySet;
19 import android.util.AttributeSet;
20 
21 import androidx.preference.DropDownPreference;
22 
23 import com.android.systemui.Dependency;
24 import com.android.systemui.statusbar.phone.StatusBarIconController;
25 import com.android.systemui.statusbar.policy.Clock;
26 
27 public class ClockPreference extends DropDownPreference implements TunerService.Tunable {
28 
29     private static final String SECONDS = "seconds";
30     private static final String DEFAULT = "default";
31     private static final String DISABLED = "disabled";
32 
33     private final String mClock;
34     private boolean mClockEnabled;
35     private boolean mHasSeconds;
36     private ArraySet<String> mBlacklist;
37     private boolean mHasSetValue;
38     private boolean mReceivedSeconds;
39     private boolean mReceivedClock;
40 
ClockPreference(Context context, AttributeSet attrs)41     public ClockPreference(Context context, AttributeSet attrs) {
42         super(context, attrs);
43         mClock = context.getString(com.android.internal.R.string.status_bar_clock);
44         setEntryValues(new CharSequence[] { SECONDS, DEFAULT, DISABLED });
45     }
46 
47     @Override
onAttached()48     public void onAttached() {
49         super.onAttached();
50         Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST,
51                 Clock.CLOCK_SECONDS);
52     }
53 
54     @Override
onDetached()55     public void onDetached() {
56         Dependency.get(TunerService.class).removeTunable(this);
57         super.onDetached();
58     }
59 
60     @Override
onTuningChanged(String key, String newValue)61     public void onTuningChanged(String key, String newValue) {
62         if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
63             mReceivedClock = true;
64             mBlacklist = StatusBarIconController.getIconBlacklist(getContext(), newValue);
65             mClockEnabled = !mBlacklist.contains(mClock);
66         } else if (Clock.CLOCK_SECONDS.equals(key)) {
67             mReceivedSeconds = true;
68             mHasSeconds = newValue != null && Integer.parseInt(newValue) != 0;
69         }
70         if (!mHasSetValue && mReceivedClock && mReceivedSeconds) {
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 (mClockEnabled && mHasSeconds) {
76                 setValue(SECONDS);
77             } else if (mClockEnabled) {
78                 setValue(DEFAULT);
79             } else {
80                 setValue(DISABLED);
81             }
82         }
83     }
84 
85     @Override
persistString(String value)86     protected boolean persistString(String value) {
87         Dependency.get(TunerService.class).setValue(Clock.CLOCK_SECONDS, SECONDS.equals(value) ? 1
88                 : 0);
89         if (DISABLED.equals(value)) {
90             mBlacklist.add(mClock);
91         } else {
92             mBlacklist.remove(mClock);
93         }
94         Dependency.get(TunerService.class).setValue(StatusBarIconController.ICON_BLACKLIST,
95                 TextUtils.join(",", mBlacklist));
96         return true;
97     }
98 }
99