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