Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.policy; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 18 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 19 | import android.app.StatusBarManager; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 25 | import android.graphics.Rect; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 26 | import android.os.Bundle; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame] | 28 | import android.os.Parcelable; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 29 | import android.os.SystemClock; |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 30 | import android.os.UserHandle; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 31 | import android.text.Spannable; |
| 32 | import android.text.SpannableStringBuilder; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 33 | import android.text.format.DateFormat; |
| 34 | import android.text.style.CharacterStyle; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 35 | import android.text.style.RelativeSizeSpan; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 37 | import android.view.Display; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 38 | import android.view.View; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 40 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 41 | import com.android.settingslib.Utils; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 42 | import com.android.systemui.DemoMode; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 43 | import com.android.systemui.Dependency; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 44 | import com.android.systemui.FontSizeUtils; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 45 | import com.android.systemui.R; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 46 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Beverly | 1be62f4 | 2018-12-19 17:17:48 -0500 | [diff] [blame] | 47 | import com.android.systemui.plugins.DarkIconDispatcher; |
| 48 | import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 49 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 52 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 53 | import com.android.systemui.tuner.TunerService; |
| 54 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 55 | |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 56 | import libcore.icu.LocaleData; |
| 57 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 58 | import java.text.SimpleDateFormat; |
| 59 | import java.util.Calendar; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 60 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 61 | import java.util.TimeZone; |
| 62 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 63 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 64 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 65 | */ |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 66 | public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks, |
| 67 | DarkReceiver, ConfigurationListener { |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 68 | |
| 69 | public static final String CLOCK_SECONDS = "clock_seconds"; |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame] | 70 | private static final String CLOCK_SUPER_PARCELABLE = "clock_super_parcelable"; |
| 71 | private static final String CURRENT_USER_ID = "current_user_id"; |
| 72 | private static final String VISIBLE_BY_POLICY = "visible_by_policy"; |
| 73 | private static final String VISIBLE_BY_USER = "visible_by_user"; |
| 74 | private static final String SHOW_SECONDS = "show_seconds"; |
| 75 | private static final String VISIBILITY = "visibility"; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 76 | |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 77 | private final CurrentUserTracker mCurrentUserTracker; |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 78 | private final CommandQueue mCommandQueue; |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 79 | private int mCurrentUserId; |
| 80 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 81 | private boolean mClockVisibleByPolicy = true; |
| 82 | private boolean mClockVisibleByUser = true; |
| 83 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 84 | private boolean mAttached; |
| 85 | private Calendar mCalendar; |
| 86 | private String mClockFormatString; |
| 87 | private SimpleDateFormat mClockFormat; |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 88 | private SimpleDateFormat mContentDescriptionFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 89 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 90 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 91 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 92 | private static final int AM_PM_STYLE_SMALL = 1; |
| 93 | private static final int AM_PM_STYLE_GONE = 2; |
| 94 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 95 | private final int mAmPmStyle; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 96 | private final boolean mShowDark; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 97 | private boolean mShowSeconds; |
| 98 | private Handler mSecondsHandler; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 99 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings |
| 102 | * for text. |
| 103 | */ |
| 104 | private boolean mUseWallpaperTextColor; |
| 105 | |
| 106 | /** |
| 107 | * Color to be set on this {@link TextView}, when wallpaperTextColor is <b>not</b> utilized. |
| 108 | */ |
| 109 | private int mNonAdaptedColor; |
| 110 | |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 111 | private final BroadcastDispatcher mBroadcastDispatcher; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 112 | |
| 113 | public Clock(Context context, AttributeSet attrs) { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 114 | this(context, attrs, 0); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 117 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 118 | super(context, attrs, defStyle); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 119 | mCommandQueue = Dependency.get(CommandQueue.class); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 120 | TypedArray a = context.getTheme().obtainStyledAttributes( |
| 121 | attrs, |
| 122 | R.styleable.Clock, |
| 123 | 0, 0); |
| 124 | try { |
| 125 | mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 126 | mShowDark = a.getBoolean(R.styleable.Clock_showDark, true); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 127 | mNonAdaptedColor = getCurrentTextColor(); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 128 | } finally { |
| 129 | a.recycle(); |
| 130 | } |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 131 | mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class); |
| 132 | mCurrentUserTracker = new CurrentUserTracker(mBroadcastDispatcher) { |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public void onUserSwitched(int newUserId) { |
| 135 | mCurrentUserId = newUserId; |
| 136 | } |
| 137 | }; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame] | 141 | public Parcelable onSaveInstanceState() { |
| 142 | Bundle bundle = new Bundle(); |
| 143 | bundle.putParcelable(CLOCK_SUPER_PARCELABLE, super.onSaveInstanceState()); |
| 144 | bundle.putInt(CURRENT_USER_ID, mCurrentUserId); |
| 145 | bundle.putBoolean(VISIBLE_BY_POLICY, mClockVisibleByPolicy); |
| 146 | bundle.putBoolean(VISIBLE_BY_USER, mClockVisibleByUser); |
| 147 | bundle.putBoolean(SHOW_SECONDS, mShowSeconds); |
| 148 | bundle.putInt(VISIBILITY, getVisibility()); |
| 149 | |
| 150 | return bundle; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onRestoreInstanceState(Parcelable state) { |
| 155 | if (state == null || !(state instanceof Bundle)) { |
| 156 | super.onRestoreInstanceState(state); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | Bundle bundle = (Bundle) state; |
| 161 | Parcelable superState = bundle.getParcelable(CLOCK_SUPER_PARCELABLE); |
| 162 | super.onRestoreInstanceState(superState); |
| 163 | if (bundle.containsKey(CURRENT_USER_ID)) { |
| 164 | mCurrentUserId = bundle.getInt(CURRENT_USER_ID); |
| 165 | } |
| 166 | mClockVisibleByPolicy = bundle.getBoolean(VISIBLE_BY_POLICY, true); |
| 167 | mClockVisibleByUser = bundle.getBoolean(VISIBLE_BY_USER, true); |
| 168 | mShowSeconds = bundle.getBoolean(SHOW_SECONDS, false); |
| 169 | if (bundle.containsKey(VISIBILITY)) { |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 170 | super.setVisibility(bundle.getInt(VISIBILITY)); |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 175 | protected void onAttachedToWindow() { |
| 176 | super.onAttachedToWindow(); |
| 177 | |
| 178 | if (!mAttached) { |
| 179 | mAttached = true; |
| 180 | IntentFilter filter = new IntentFilter(); |
| 181 | |
| 182 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 183 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 184 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 185 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 186 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 187 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 188 | mBroadcastDispatcher.registerReceiver(mIntentReceiver, filter, |
| 189 | Dependency.get(Dependency.TIME_TICK_HANDLER), UserHandle.ALL); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 190 | Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS, |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 191 | StatusBarIconController.ICON_BLACKLIST); |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 192 | mCommandQueue.addCallback(this); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 193 | if (mShowDark) { |
| 194 | Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this); |
| 195 | } |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 196 | mCurrentUserTracker.startTracking(); |
| 197 | mCurrentUserId = mCurrentUserTracker.getCurrentUserId(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // NOTE: It's safe to do these after registering the receiver since the receiver always runs |
| 201 | // in the main thread, therefore the receiver can't run before this method returns. |
| 202 | |
| 203 | // The time zone may have changed while the receiver wasn't registered, so update the Time |
| 204 | mCalendar = Calendar.getInstance(TimeZone.getDefault()); |
| 205 | |
| 206 | // Make sure we update to the current time |
| 207 | updateClock(); |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 208 | updateClockVisibility(); |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 209 | updateShowSeconds(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected void onDetachedFromWindow() { |
| 214 | super.onDetachedFromWindow(); |
| 215 | if (mAttached) { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 216 | mBroadcastDispatcher.unregisterReceiver(mIntentReceiver); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 217 | mAttached = false; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 218 | Dependency.get(TunerService.class).removeTunable(this); |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 219 | mCommandQueue.removeCallback(this); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 220 | if (mShowDark) { |
| 221 | Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this); |
| 222 | } |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 223 | mCurrentUserTracker.stopTracking(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 228 | @Override |
| 229 | public void onReceive(Context context, Intent intent) { |
| 230 | String action = intent.getAction(); |
| 231 | if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { |
| 232 | String tz = intent.getStringExtra("time-zone"); |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 233 | getHandler().post(() -> { |
| 234 | mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); |
| 235 | if (mClockFormat != null) { |
| 236 | mClockFormat.setTimeZone(mCalendar.getTimeZone()); |
| 237 | } |
| 238 | }); |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 239 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 240 | final Locale newLocale = getResources().getConfiguration().locale; |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 241 | getHandler().post(() -> { |
| 242 | if (!newLocale.equals(mLocale)) { |
| 243 | mLocale = newLocale; |
| 244 | mClockFormatString = ""; // force refresh |
| 245 | } |
| 246 | }); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 247 | } |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 248 | getHandler().post(() -> updateClock()); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 249 | } |
| 250 | }; |
| 251 | |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 252 | @Override |
| 253 | public void setVisibility(int visibility) { |
| 254 | if (visibility == View.VISIBLE && !shouldBeVisible()) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | super.setVisibility(visibility); |
| 259 | } |
| 260 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 261 | public void setClockVisibleByUser(boolean visible) { |
| 262 | mClockVisibleByUser = visible; |
| 263 | updateClockVisibility(); |
| 264 | } |
| 265 | |
| 266 | public void setClockVisibilityByPolicy(boolean visible) { |
| 267 | mClockVisibleByPolicy = visible; |
| 268 | updateClockVisibility(); |
| 269 | } |
| 270 | |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 271 | private boolean shouldBeVisible() { |
| 272 | return mClockVisibleByPolicy && mClockVisibleByUser; |
| 273 | } |
| 274 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 275 | private void updateClockVisibility() { |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 276 | boolean visible = shouldBeVisible(); |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 277 | int visibility = visible ? View.VISIBLE : View.GONE; |
Hector Dearman | ad4506a | 2018-11-09 18:11:07 +0000 | [diff] [blame] | 278 | super.setVisibility(visibility); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 281 | final void updateClock() { |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 282 | if (mDemoMode) return; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 283 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 284 | setText(getSmallTime()); |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 285 | setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 288 | @Override |
| 289 | public void onTuningChanged(String key, String newValue) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 290 | if (CLOCK_SECONDS.equals(key)) { |
Henrik Baard | a2c86e8 | 2019-03-14 12:23:04 +0100 | [diff] [blame] | 291 | mShowSeconds = TunerService.parseIntegerSwitch(newValue, false); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 292 | updateShowSeconds(); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 293 | } else { |
| 294 | setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue) |
| 295 | .contains("clock")); |
| 296 | updateClockVisibility(); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 297 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 300 | @Override |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 301 | public void disable(int displayId, int state1, int state2, boolean animate) { |
| 302 | if (displayId != getDisplay().getDisplayId()) { |
| 303 | return; |
| 304 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 305 | boolean clockVisibleByPolicy = (state1 & StatusBarManager.DISABLE_CLOCK) == 0; |
| 306 | if (clockVisibleByPolicy != mClockVisibleByPolicy) { |
| 307 | setClockVisibilityByPolicy(clockVisibleByPolicy); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 313 | mNonAdaptedColor = DarkIconDispatcher.getTint(area, this, tint); |
| 314 | if (!mUseWallpaperTextColor) { |
| 315 | setTextColor(mNonAdaptedColor); |
| 316 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void onDensityOrFontScaleChanged() { |
| 321 | FontSizeUtils.updateFontSize(this, R.dimen.status_bar_clock_size); |
| 322 | setPaddingRelative( |
| 323 | mContext.getResources().getDimensionPixelSize( |
| 324 | R.dimen.status_bar_clock_starting_padding), |
| 325 | 0, |
| 326 | mContext.getResources().getDimensionPixelSize( |
| 327 | R.dimen.status_bar_clock_end_padding), |
| 328 | 0); |
| 329 | } |
| 330 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Sets whether the clock uses the wallpaperTextColor. If we're not using it, we'll revert back |
| 333 | * to dark-mode-based/tinted colors. |
| 334 | * |
| 335 | * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for text color |
| 336 | */ |
| 337 | public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) { |
| 338 | if (shouldUseWallpaperTextColor == mUseWallpaperTextColor) { |
| 339 | return; |
| 340 | } |
| 341 | mUseWallpaperTextColor = shouldUseWallpaperTextColor; |
| 342 | |
| 343 | if (mUseWallpaperTextColor) { |
| 344 | setTextColor(Utils.getColorAttr(mContext, R.attr.wallpaperTextColor)); |
| 345 | } else { |
| 346 | setTextColor(mNonAdaptedColor); |
| 347 | } |
| 348 | } |
| 349 | |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 350 | private void updateShowSeconds() { |
| 351 | if (mShowSeconds) { |
| 352 | // Wait until we have a display to start trying to show seconds. |
| 353 | if (mSecondsHandler == null && getDisplay() != null) { |
| 354 | mSecondsHandler = new Handler(); |
| 355 | if (getDisplay().getState() == Display.STATE_ON) { |
| 356 | mSecondsHandler.postAtTime(mSecondTick, |
| 357 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 358 | } |
| 359 | IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); |
| 360 | filter.addAction(Intent.ACTION_SCREEN_ON); |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 361 | mBroadcastDispatcher.registerReceiver(mScreenReceiver, filter); |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 362 | } |
| 363 | } else { |
| 364 | if (mSecondsHandler != null) { |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 365 | mBroadcastDispatcher.unregisterReceiver(mScreenReceiver); |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 366 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 367 | mSecondsHandler = null; |
| 368 | updateClock(); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 373 | private final CharSequence getSmallTime() { |
| 374 | Context context = getContext(); |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 375 | boolean is24 = DateFormat.is24HourFormat(context, mCurrentUserId); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 376 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 377 | |
| 378 | final char MAGIC1 = '\uEF00'; |
| 379 | final char MAGIC2 = '\uEF01'; |
| 380 | |
| 381 | SimpleDateFormat sdf; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 382 | String format = mShowSeconds |
| 383 | ? is24 ? d.timeFormat_Hms : d.timeFormat_hms |
| 384 | : is24 ? d.timeFormat_Hm : d.timeFormat_hm; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 385 | if (!format.equals(mClockFormatString)) { |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 386 | mContentDescriptionFormat = new SimpleDateFormat(format); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 387 | /* |
| 388 | * Search for an unquoted "a" in the format string, so we can |
| 389 | * add dummy characters around it to let us find it again after |
| 390 | * formatting and change its size. |
| 391 | */ |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 392 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 393 | int a = -1; |
| 394 | boolean quoted = false; |
| 395 | for (int i = 0; i < format.length(); i++) { |
| 396 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 397 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 398 | if (c == '\'') { |
| 399 | quoted = !quoted; |
| 400 | } |
| 401 | if (!quoted && c == 'a') { |
| 402 | a = i; |
| 403 | break; |
| 404 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 407 | if (a >= 0) { |
| 408 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 409 | final int b = a; |
| 410 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 411 | a--; |
| 412 | } |
| 413 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 414 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 415 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 416 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 417 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 418 | mClockFormatString = format; |
| 419 | } else { |
| 420 | sdf = mClockFormat; |
| 421 | } |
| 422 | String result = sdf.format(mCalendar.getTime()); |
| 423 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 424 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 425 | int magic1 = result.indexOf(MAGIC1); |
| 426 | int magic2 = result.indexOf(MAGIC2); |
| 427 | if (magic1 >= 0 && magic2 > magic1) { |
| 428 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 429 | if (mAmPmStyle == AM_PM_STYLE_GONE) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 430 | formatted.delete(magic1, magic2+1); |
| 431 | } else { |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 432 | if (mAmPmStyle == AM_PM_STYLE_SMALL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 433 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 434 | formatted.setSpan(style, magic1, magic2, |
| 435 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 436 | } |
| 437 | formatted.delete(magic2, magic2 + 1); |
| 438 | formatted.delete(magic1, magic1 + 1); |
| 439 | } |
| 440 | return formatted; |
| 441 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 442 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 443 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 444 | return result; |
| 445 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 446 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 447 | |
| 448 | private boolean mDemoMode; |
| 449 | |
| 450 | @Override |
| 451 | public void dispatchDemoCommand(String command, Bundle args) { |
| 452 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
| 453 | mDemoMode = true; |
| 454 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
| 455 | mDemoMode = false; |
| 456 | updateClock(); |
| 457 | } else if (mDemoMode && command.equals(COMMAND_CLOCK)) { |
| 458 | String millis = args.getString("millis"); |
| 459 | String hhmm = args.getString("hhmm"); |
| 460 | if (millis != null) { |
| 461 | mCalendar.setTimeInMillis(Long.parseLong(millis)); |
| 462 | } else if (hhmm != null && hhmm.length() == 4) { |
| 463 | int hh = Integer.parseInt(hhmm.substring(0, 2)); |
| 464 | int mm = Integer.parseInt(hhmm.substring(2)); |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 465 | boolean is24 = DateFormat.is24HourFormat(getContext(), mCurrentUserId); |
Julia Reynolds | c11d838 | 2015-07-16 14:40:37 -0400 | [diff] [blame] | 466 | if (is24) { |
| 467 | mCalendar.set(Calendar.HOUR_OF_DAY, hh); |
| 468 | } else { |
| 469 | mCalendar.set(Calendar.HOUR, hh); |
| 470 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 471 | mCalendar.set(Calendar.MINUTE, mm); |
| 472 | } |
| 473 | setText(getSmallTime()); |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 474 | setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 475 | } |
| 476 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 477 | |
| 478 | private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 479 | @Override |
| 480 | public void onReceive(Context context, Intent intent) { |
| 481 | String action = intent.getAction(); |
| 482 | if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 483 | if (mSecondsHandler != null) { |
| 484 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 485 | } |
| 486 | } else if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 487 | if (mSecondsHandler != null) { |
| 488 | mSecondsHandler.postAtTime(mSecondTick, |
| 489 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | }; |
| 494 | |
| 495 | private final Runnable mSecondTick = new Runnable() { |
| 496 | @Override |
| 497 | public void run() { |
| 498 | if (mCalendar != null) { |
| 499 | updateClock(); |
| 500 | } |
| 501 | mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 502 | } |
| 503 | }; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 504 | } |
| 505 | |