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