blob: 8517d9086fc7b2b48f15db1d6d3d3292846f7410 [file] [log] [blame]
Joe Onorato263700d2010-05-14 11:54:53 -07001/*
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 Onoratofd52b182010-11-10 18:00:52 -080017package com.android.systemui.statusbar.policy;
Joe Onorato263700d2010-05-14 11:54:53 -070018
Jason Monkaa573e92017-01-27 17:00:29 -050019import android.app.StatusBarManager;
Daniel Sandler87937db2010-05-27 13:44:11 -040020import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Jorim Jaggi740beb52014-05-11 18:53:19 +020024import android.content.res.TypedArray;
Jason Monkaa573e92017-01-27 17:00:29 -050025import android.graphics.Rect;
John Spurlock3c875662013-08-31 15:07:25 -040026import android.os.Bundle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040027import android.os.Handler;
felkachange6c03a02018-05-24 15:38:04 +080028import android.os.Parcelable;
Jason Monkfe7c91b2015-08-13 15:42:28 -040029import android.os.SystemClock;
Selim Cinek9c4a7072014-11-21 17:44:34 +010030import android.os.UserHandle;
Joe Onorato263700d2010-05-14 11:54:53 -070031import android.text.Spannable;
32import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040033import android.text.format.DateFormat;
34import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040035import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070036import android.util.AttributeSet;
Jason Monkfe7c91b2015-08-13 15:42:28 -040037import android.view.Display;
Jason Monkaa573e92017-01-27 17:00:29 -050038import android.view.View;
Joe Onorato263700d2010-05-14 11:54:53 -070039import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080040
Rohan Shahcc3d1d82018-03-30 21:24:17 +000041import com.android.settingslib.Utils;
John Spurlock3c875662013-08-31 15:07:25 -040042import com.android.systemui.DemoMode;
Jason Monk9c7844c2017-01-18 15:21:53 -050043import com.android.systemui.Dependency;
Jason Monkaa573e92017-01-27 17:00:29 -050044import com.android.systemui.FontSizeUtils;
Jorim Jaggi740beb52014-05-11 18:53:19 +020045import com.android.systemui.R;
Jason Monkaa573e92017-01-27 17:00:29 -050046import com.android.systemui.SysUiServiceProvider;
Lucas Dupin4eae8812018-04-09 22:44:17 -070047import com.android.systemui.settings.CurrentUserTracker;
Jason Monkaa573e92017-01-27 17:00:29 -050048import com.android.systemui.statusbar.CommandQueue;
Jason Monk3e189872016-01-12 09:10:34 -050049import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkaa573e92017-01-27 17:00:29 -050050import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
51import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Jason Monkfe7c91b2015-08-13 15:42:28 -040052import com.android.systemui.tuner.TunerService;
53import com.android.systemui.tuner.TunerService.Tunable;
John Spurlock3c875662013-08-31 15:07:25 -040054
Lucas Dupin4eae8812018-04-09 22:44:17 -070055import libcore.icu.LocaleData;
56
Joe Onorato263700d2010-05-14 11:54:53 -070057import java.text.SimpleDateFormat;
58import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050059import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070060import java.util.TimeZone;
61
Joe Onorato263700d2010-05-14 11:54:53 -070062/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050063 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070064 */
Jason Monkaa573e92017-01-27 17:00:29 -050065public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks,
66 DarkReceiver, ConfigurationListener {
Jason Monkfe7c91b2015-08-13 15:42:28 -040067
68 public static final String CLOCK_SECONDS = "clock_seconds";
felkachange6c03a02018-05-24 15:38:04 +080069 private static final String CLOCK_SUPER_PARCELABLE = "clock_super_parcelable";
70 private static final String CURRENT_USER_ID = "current_user_id";
71 private static final String VISIBLE_BY_POLICY = "visible_by_policy";
72 private static final String VISIBLE_BY_USER = "visible_by_user";
73 private static final String SHOW_SECONDS = "show_seconds";
74 private static final String VISIBILITY = "visibility";
Jason Monkfe7c91b2015-08-13 15:42:28 -040075
Lucas Dupin4eae8812018-04-09 22:44:17 -070076 private final CurrentUserTracker mCurrentUserTracker;
77 private int mCurrentUserId;
78
Jason Monkaa573e92017-01-27 17:00:29 -050079 private boolean mClockVisibleByPolicy = true;
80 private boolean mClockVisibleByUser = true;
81
Joe Onorato263700d2010-05-14 11:54:53 -070082 private boolean mAttached;
83 private Calendar mCalendar;
84 private String mClockFormatString;
85 private SimpleDateFormat mClockFormat;
Adrian Roos70dcf832016-04-20 15:51:42 -070086 private SimpleDateFormat mContentDescriptionFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050087 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070088
Daniel Sandler87937db2010-05-27 13:44:11 -040089 private static final int AM_PM_STYLE_NORMAL = 0;
90 private static final int AM_PM_STYLE_SMALL = 1;
91 private static final int AM_PM_STYLE_GONE = 2;
92
Jorim Jaggi740beb52014-05-11 18:53:19 +020093 private final int mAmPmStyle;
Jason Monke5b770e2017-03-03 21:49:29 -050094 private final boolean mShowDark;
Jason Monkfe7c91b2015-08-13 15:42:28 -040095 private boolean mShowSeconds;
96 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040097
Rohan Shahcc3d1d82018-03-30 21:24:17 +000098 /**
99 * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings
100 * for text.
101 */
102 private boolean mUseWallpaperTextColor;
103
104 /**
105 * Color to be set on this {@link TextView}, when wallpaperTextColor is <b>not</b> utilized.
106 */
107 private int mNonAdaptedColor;
108
Joe Onorato263700d2010-05-14 11:54:53 -0700109 public Clock(Context context) {
110 this(context, null);
111 }
112
113 public Clock(Context context, AttributeSet attrs) {
114 this(context, attrs, 0);
115 }
116
117 public Clock(Context context, AttributeSet attrs, int defStyle) {
118 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200119 TypedArray a = context.getTheme().obtainStyledAttributes(
120 attrs,
121 R.styleable.Clock,
122 0, 0);
123 try {
124 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
Jason Monke5b770e2017-03-03 21:49:29 -0500125 mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000126 mNonAdaptedColor = getCurrentTextColor();
Jorim Jaggi740beb52014-05-11 18:53:19 +0200127 } finally {
128 a.recycle();
129 }
Lucas Dupin4eae8812018-04-09 22:44:17 -0700130 mCurrentUserTracker = new CurrentUserTracker(context) {
131 @Override
132 public void onUserSwitched(int newUserId) {
133 mCurrentUserId = newUserId;
134 }
135 };
Joe Onorato263700d2010-05-14 11:54:53 -0700136 }
137
138 @Override
felkachange6c03a02018-05-24 15:38:04 +0800139 public Parcelable onSaveInstanceState() {
140 Bundle bundle = new Bundle();
141 bundle.putParcelable(CLOCK_SUPER_PARCELABLE, super.onSaveInstanceState());
142 bundle.putInt(CURRENT_USER_ID, mCurrentUserId);
143 bundle.putBoolean(VISIBLE_BY_POLICY, mClockVisibleByPolicy);
144 bundle.putBoolean(VISIBLE_BY_USER, mClockVisibleByUser);
145 bundle.putBoolean(SHOW_SECONDS, mShowSeconds);
146 bundle.putInt(VISIBILITY, getVisibility());
147
148 return bundle;
149 }
150
151 @Override
152 public void onRestoreInstanceState(Parcelable state) {
153 if (state == null || !(state instanceof Bundle)) {
154 super.onRestoreInstanceState(state);
155 return;
156 }
157
158 Bundle bundle = (Bundle) state;
159 Parcelable superState = bundle.getParcelable(CLOCK_SUPER_PARCELABLE);
160 super.onRestoreInstanceState(superState);
161 if (bundle.containsKey(CURRENT_USER_ID)) {
162 mCurrentUserId = bundle.getInt(CURRENT_USER_ID);
163 }
164 mClockVisibleByPolicy = bundle.getBoolean(VISIBLE_BY_POLICY, true);
165 mClockVisibleByUser = bundle.getBoolean(VISIBLE_BY_USER, true);
166 mShowSeconds = bundle.getBoolean(SHOW_SECONDS, false);
167 if (bundle.containsKey(VISIBILITY)) {
168 setVisibility(bundle.getInt(VISIBILITY));
169 }
170 }
171
172 @Override
Joe Onorato263700d2010-05-14 11:54:53 -0700173 protected void onAttachedToWindow() {
174 super.onAttachedToWindow();
175
176 if (!mAttached) {
177 mAttached = true;
178 IntentFilter filter = new IntentFilter();
179
180 filter.addAction(Intent.ACTION_TIME_TICK);
181 filter.addAction(Intent.ACTION_TIME_CHANGED);
182 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
183 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500184 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700185
Selim Cinek9c4a7072014-11-21 17:44:34 +0100186 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
Jason Monk9c7844c2017-01-18 15:21:53 -0500187 null, Dependency.get(Dependency.TIME_TICK_HANDLER));
Jason Monkde850bb2017-02-01 19:26:30 -0500188 Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS,
Jason Monk3e189872016-01-12 09:10:34 -0500189 StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500190 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500191 if (mShowDark) {
192 Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
193 }
Lucas Dupin4eae8812018-04-09 22:44:17 -0700194 mCurrentUserTracker.startTracking();
195 mCurrentUserId = mCurrentUserTracker.getCurrentUserId();
Joe Onorato263700d2010-05-14 11:54:53 -0700196 }
197
198 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
199 // in the main thread, therefore the receiver can't run before this method returns.
200
201 // The time zone may have changed while the receiver wasn't registered, so update the Time
202 mCalendar = Calendar.getInstance(TimeZone.getDefault());
203
204 // Make sure we update to the current time
205 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400206 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700207 }
208
209 @Override
210 protected void onDetachedFromWindow() {
211 super.onDetachedFromWindow();
212 if (mAttached) {
213 getContext().unregisterReceiver(mIntentReceiver);
214 mAttached = false;
Jason Monkde850bb2017-02-01 19:26:30 -0500215 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500216 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class)
217 .removeCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500218 if (mShowDark) {
219 Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
220 }
Lucas Dupin4eae8812018-04-09 22:44:17 -0700221 mCurrentUserTracker.stopTracking();
Joe Onorato263700d2010-05-14 11:54:53 -0700222 }
223 }
224
225 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
226 @Override
227 public void onReceive(Context context, Intent intent) {
228 String action = intent.getAction();
229 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
230 String tz = intent.getStringExtra("time-zone");
Jason Monkcd26af72017-01-11 14:32:58 -0500231 getHandler().post(() -> {
232 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
233 if (mClockFormat != null) {
234 mClockFormat.setTimeZone(mCalendar.getTimeZone());
235 }
236 });
Daniel Sandler43b23c62012-11-29 11:35:02 -0500237 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
238 final Locale newLocale = getResources().getConfiguration().locale;
Jason Monkcd26af72017-01-11 14:32:58 -0500239 getHandler().post(() -> {
240 if (!newLocale.equals(mLocale)) {
241 mLocale = newLocale;
242 mClockFormatString = ""; // force refresh
243 }
244 });
Joe Onorato263700d2010-05-14 11:54:53 -0700245 }
Jason Monkcd26af72017-01-11 14:32:58 -0500246 getHandler().post(() -> updateClock());
Joe Onorato263700d2010-05-14 11:54:53 -0700247 }
248 };
249
Jason Monkaa573e92017-01-27 17:00:29 -0500250 public void setClockVisibleByUser(boolean visible) {
251 mClockVisibleByUser = visible;
252 updateClockVisibility();
253 }
254
255 public void setClockVisibilityByPolicy(boolean visible) {
256 mClockVisibleByPolicy = visible;
257 updateClockVisibility();
258 }
259
260 private void updateClockVisibility() {
Jason Monkf8c2f7b2017-09-06 09:22:29 -0400261 boolean visible = mClockVisibleByPolicy && mClockVisibleByUser;
262 Dependency.get(IconLogger.class).onIconVisibility("clock", visible);
263 int visibility = visible ? View.VISIBLE : View.GONE;
Jason Monkaa573e92017-01-27 17:00:29 -0500264 setVisibility(visibility);
265 }
266
Joe Onorato263700d2010-05-14 11:54:53 -0700267 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400268 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700269 mCalendar.setTimeInMillis(System.currentTimeMillis());
270 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700271 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
Joe Onorato263700d2010-05-14 11:54:53 -0700272 }
273
Jason Monkfe7c91b2015-08-13 15:42:28 -0400274 @Override
275 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500276 if (CLOCK_SECONDS.equals(key)) {
277 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
278 updateShowSeconds();
Jason Monkaa573e92017-01-27 17:00:29 -0500279 } else {
280 setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue)
281 .contains("clock"));
282 updateClockVisibility();
Jason Monk3e189872016-01-12 09:10:34 -0500283 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400284 }
285
Jason Monkaa573e92017-01-27 17:00:29 -0500286 @Override
287 public void disable(int state1, int state2, boolean animate) {
288 boolean clockVisibleByPolicy = (state1 & StatusBarManager.DISABLE_CLOCK) == 0;
289 if (clockVisibleByPolicy != mClockVisibleByPolicy) {
290 setClockVisibilityByPolicy(clockVisibleByPolicy);
291 }
292 }
293
294 @Override
295 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000296 mNonAdaptedColor = DarkIconDispatcher.getTint(area, this, tint);
297 if (!mUseWallpaperTextColor) {
298 setTextColor(mNonAdaptedColor);
299 }
Jason Monkaa573e92017-01-27 17:00:29 -0500300 }
301
302 @Override
303 public void onDensityOrFontScaleChanged() {
304 FontSizeUtils.updateFontSize(this, R.dimen.status_bar_clock_size);
305 setPaddingRelative(
306 mContext.getResources().getDimensionPixelSize(
307 R.dimen.status_bar_clock_starting_padding),
308 0,
309 mContext.getResources().getDimensionPixelSize(
310 R.dimen.status_bar_clock_end_padding),
311 0);
312 }
313
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000314 /**
315 * Sets whether the clock uses the wallpaperTextColor. If we're not using it, we'll revert back
316 * to dark-mode-based/tinted colors.
317 *
318 * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for text color
319 */
320 public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) {
321 if (shouldUseWallpaperTextColor == mUseWallpaperTextColor) {
322 return;
323 }
324 mUseWallpaperTextColor = shouldUseWallpaperTextColor;
325
326 if (mUseWallpaperTextColor) {
327 setTextColor(Utils.getColorAttr(mContext, R.attr.wallpaperTextColor));
328 } else {
329 setTextColor(mNonAdaptedColor);
330 }
331 }
332
Jason Monkfe7c91b2015-08-13 15:42:28 -0400333 private void updateShowSeconds() {
334 if (mShowSeconds) {
335 // Wait until we have a display to start trying to show seconds.
336 if (mSecondsHandler == null && getDisplay() != null) {
337 mSecondsHandler = new Handler();
338 if (getDisplay().getState() == Display.STATE_ON) {
339 mSecondsHandler.postAtTime(mSecondTick,
340 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
341 }
342 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
343 filter.addAction(Intent.ACTION_SCREEN_ON);
344 mContext.registerReceiver(mScreenReceiver, filter);
345 }
346 } else {
347 if (mSecondsHandler != null) {
348 mContext.unregisterReceiver(mScreenReceiver);
349 mSecondsHandler.removeCallbacks(mSecondTick);
350 mSecondsHandler = null;
351 updateClock();
352 }
353 }
354 }
355
Joe Onorato263700d2010-05-14 11:54:53 -0700356 private final CharSequence getSmallTime() {
357 Context context = getContext();
Lucas Dupin4eae8812018-04-09 22:44:17 -0700358 boolean is24 = DateFormat.is24HourFormat(context, mCurrentUserId);
Elliott Hughes4caba612013-01-14 15:48:27 -0800359 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700360
361 final char MAGIC1 = '\uEF00';
362 final char MAGIC2 = '\uEF01';
363
364 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400365 String format = mShowSeconds
366 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
367 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700368 if (!format.equals(mClockFormatString)) {
Adrian Roos70dcf832016-04-20 15:51:42 -0700369 mContentDescriptionFormat = new SimpleDateFormat(format);
Joe Onorato263700d2010-05-14 11:54:53 -0700370 /*
371 * Search for an unquoted "a" in the format string, so we can
372 * add dummy characters around it to let us find it again after
373 * formatting and change its size.
374 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200375 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400376 int a = -1;
377 boolean quoted = false;
378 for (int i = 0; i < format.length(); i++) {
379 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700380
Daniel Sandler87937db2010-05-27 13:44:11 -0400381 if (c == '\'') {
382 quoted = !quoted;
383 }
384 if (!quoted && c == 'a') {
385 a = i;
386 break;
387 }
Joe Onorato263700d2010-05-14 11:54:53 -0700388 }
389
Daniel Sandler87937db2010-05-27 13:44:11 -0400390 if (a >= 0) {
391 // Move a back so any whitespace before AM/PM is also in the alternate size.
392 final int b = a;
393 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
394 a--;
395 }
396 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700397 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400398 }
Joe Onorato263700d2010-05-14 11:54:53 -0700399 }
Joe Onorato263700d2010-05-14 11:54:53 -0700400 mClockFormat = sdf = new SimpleDateFormat(format);
401 mClockFormatString = format;
402 } else {
403 sdf = mClockFormat;
404 }
405 String result = sdf.format(mCalendar.getTime());
406
Jorim Jaggi740beb52014-05-11 18:53:19 +0200407 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400408 int magic1 = result.indexOf(MAGIC1);
409 int magic2 = result.indexOf(MAGIC2);
410 if (magic1 >= 0 && magic2 > magic1) {
411 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200412 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400413 formatted.delete(magic1, magic2+1);
414 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200415 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400416 CharacterStyle style = new RelativeSizeSpan(0.7f);
417 formatted.setSpan(style, magic1, magic2,
418 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
419 }
420 formatted.delete(magic2, magic2 + 1);
421 formatted.delete(magic1, magic1 + 1);
422 }
423 return formatted;
424 }
Joe Onorato263700d2010-05-14 11:54:53 -0700425 }
John Spurlock209bede2013-07-17 12:23:27 -0400426
Daniel Sandler87937db2010-05-27 13:44:11 -0400427 return result;
428
Joe Onorato263700d2010-05-14 11:54:53 -0700429 }
John Spurlock3c875662013-08-31 15:07:25 -0400430
431 private boolean mDemoMode;
432
433 @Override
434 public void dispatchDemoCommand(String command, Bundle args) {
435 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
436 mDemoMode = true;
437 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
438 mDemoMode = false;
439 updateClock();
440 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
441 String millis = args.getString("millis");
442 String hhmm = args.getString("hhmm");
443 if (millis != null) {
444 mCalendar.setTimeInMillis(Long.parseLong(millis));
445 } else if (hhmm != null && hhmm.length() == 4) {
446 int hh = Integer.parseInt(hhmm.substring(0, 2));
447 int mm = Integer.parseInt(hhmm.substring(2));
Lucas Dupin4eae8812018-04-09 22:44:17 -0700448 boolean is24 = DateFormat.is24HourFormat(getContext(), mCurrentUserId);
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400449 if (is24) {
450 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
451 } else {
452 mCalendar.set(Calendar.HOUR, hh);
453 }
John Spurlock3c875662013-08-31 15:07:25 -0400454 mCalendar.set(Calendar.MINUTE, mm);
455 }
456 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700457 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
John Spurlock3c875662013-08-31 15:07:25 -0400458 }
459 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400460
461 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
462 @Override
463 public void onReceive(Context context, Intent intent) {
464 String action = intent.getAction();
465 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
466 if (mSecondsHandler != null) {
467 mSecondsHandler.removeCallbacks(mSecondTick);
468 }
469 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
470 if (mSecondsHandler != null) {
471 mSecondsHandler.postAtTime(mSecondTick,
472 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
473 }
474 }
475 }
476 };
477
478 private final Runnable mSecondTick = new Runnable() {
479 @Override
480 public void run() {
481 if (mCalendar != null) {
482 updateClock();
483 }
484 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
485 }
486 };
Joe Onorato263700d2010-05-14 11:54:53 -0700487}
488