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