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