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 36 public ClockPreference(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 mClock = context.getString(com.android.internal.R.string.status_bar_clock); 39 setEntryValues(new CharSequence[] { SECONDS, DEFAULT, DISABLED }); 40 } 41 42 @Override 43 public void onAttached() { 44 super.onAttached(); 45 TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST, 46 Clock.CLOCK_SECONDS); 47 } 48 49 @Override 50 public void onDetached() { 51 TunerService.get(getContext()).removeTunable(this); 52 super.onDetached(); 53 } 54 55 @Override 56 public void onTuningChanged(String key, String newValue) { 57 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { 58 mBlacklist = StatusBarIconController.getIconBlacklist(newValue); 59 mClockEnabled = !mBlacklist.contains(mClock); 60 } else if (Clock.CLOCK_SECONDS.equals(key)) { 61 mHasSeconds = newValue != null && Integer.parseInt(newValue) != 0; 62 } 63 if (!mHasSetValue) { 64 // Because of the complicated tri-state it can end up looping and setting state back to 65 // what the user didn't choose. To avoid this, just set the state once and rely on the 66 // preference to handle updates. 67 mHasSetValue = true; 68 if (mClockEnabled && mHasSeconds) { 69 setValue(SECONDS); 70 } else if (mClockEnabled) { 71 setValue(DEFAULT); 72 } else { 73 setValue(DISABLED); 74 } 75 } 76 } 77 78 @Override 79 protected boolean persistString(String value) { 80 TunerService.get(getContext()).setValue(Clock.CLOCK_SECONDS, SECONDS.equals(value) ? 1 : 0); 81 if (DISABLED.equals(value)) { 82 mBlacklist.add(mClock); 83 } else { 84 mBlacklist.remove(mClock); 85 } 86 TunerService.get(getContext()).setValue(StatusBarIconController.ICON_BLACKLIST, 87 TextUtils.join(",", mBlacklist)); 88 return true; 89 } 90} 91