blob: baeaaadf419fb30657570cccb89be89ce37dfd03 [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
Winsonc0d70582016-01-29 10:24:39 -080019import libcore.icu.LocaleData;
20
Selim Cinek9c4a7072014-11-21 17:44:34 +010021import android.app.ActivityManager;
Jason Monkaa573e92017-01-27 17:00:29 -050022import android.app.StatusBarManager;
Daniel Sandler87937db2010-05-27 13:44:11 -040023import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Jorim Jaggi740beb52014-05-11 18:53:19 +020027import android.content.res.TypedArray;
Jason Monkaa573e92017-01-27 17:00:29 -050028import android.graphics.Rect;
John Spurlock3c875662013-08-31 15:07:25 -040029import android.os.Bundle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040030import android.os.Handler;
31import android.os.SystemClock;
Selim Cinek9c4a7072014-11-21 17:44:34 +010032import android.os.UserHandle;
Joe Onorato263700d2010-05-14 11:54:53 -070033import android.text.Spannable;
34import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040035import android.text.format.DateFormat;
36import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040037import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070038import android.util.AttributeSet;
Jason Monkfe7c91b2015-08-13 15:42:28 -040039import android.view.Display;
Jason Monkaa573e92017-01-27 17:00:29 -050040import android.view.View;
Joe Onorato263700d2010-05-14 11:54:53 -070041import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080042
Rohan Shahcc3d1d82018-03-30 21:24:17 +000043import com.android.settingslib.Utils;
John Spurlock3c875662013-08-31 15:07:25 -040044import com.android.systemui.DemoMode;
Jason Monk9c7844c2017-01-18 15:21:53 -050045import com.android.systemui.Dependency;
Jason Monkaa573e92017-01-27 17:00:29 -050046import com.android.systemui.FontSizeUtils;
Jorim Jaggi740beb52014-05-11 18:53:19 +020047import com.android.systemui.R;
Jason Monkaa573e92017-01-27 17:00:29 -050048import com.android.systemui.SysUiServiceProvider;
49import com.android.systemui.statusbar.CommandQueue;
Jason Monk3e189872016-01-12 09:10:34 -050050import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkaa573e92017-01-27 17:00:29 -050051import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
52import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Jason Monkfe7c91b2015-08-13 15:42:28 -040053import com.android.systemui.tuner.TunerService;
54import com.android.systemui.tuner.TunerService.Tunable;
John Spurlock3c875662013-08-31 15:07:25 -040055
Joe Onorato263700d2010-05-14 11:54:53 -070056import java.text.SimpleDateFormat;
57import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050058import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070059import java.util.TimeZone;
60
Joe Onorato263700d2010-05-14 11:54:53 -070061/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050062 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070063 */
Jason Monkaa573e92017-01-27 17:00:29 -050064public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks,
65 DarkReceiver, ConfigurationListener {
Jason Monkfe7c91b2015-08-13 15:42:28 -040066
67 public static final String CLOCK_SECONDS = "clock_seconds";
68
Jason Monkaa573e92017-01-27 17:00:29 -050069 private boolean mClockVisibleByPolicy = true;
70 private boolean mClockVisibleByUser = true;
71
Joe Onorato263700d2010-05-14 11:54:53 -070072 private boolean mAttached;
73 private Calendar mCalendar;
74 private String mClockFormatString;
75 private SimpleDateFormat mClockFormat;
Adrian Roos70dcf832016-04-20 15:51:42 -070076 private SimpleDateFormat mContentDescriptionFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050077 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070078
Daniel Sandler87937db2010-05-27 13:44:11 -040079 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 Jaggi740beb52014-05-11 18:53:19 +020083 private final int mAmPmStyle;
Jason Monke5b770e2017-03-03 21:49:29 -050084 private final boolean mShowDark;
Jason Monkfe7c91b2015-08-13 15:42:28 -040085 private boolean mShowSeconds;
86 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040087
Rohan Shahcc3d1d82018-03-30 21:24:17 +000088 /**
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 Onorato263700d2010-05-14 11:54:53 -070099 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 Jaggi740beb52014-05-11 18:53:19 +0200109 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 Monke5b770e2017-03-03 21:49:29 -0500115 mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000116 mNonAdaptedColor = getCurrentTextColor();
Jorim Jaggi740beb52014-05-11 18:53:19 +0200117 } finally {
118 a.recycle();
119 }
Joe Onorato263700d2010-05-14 11:54:53 -0700120 }
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 Sandlerce4f5e52012-11-19 14:47:18 -0500134 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700135
Selim Cinek9c4a7072014-11-21 17:44:34 +0100136 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
Jason Monk9c7844c2017-01-18 15:21:53 -0500137 null, Dependency.get(Dependency.TIME_TICK_HANDLER));
Jason Monkde850bb2017-02-01 19:26:30 -0500138 Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS,
Jason Monk3e189872016-01-12 09:10:34 -0500139 StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500140 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500141 if (mShowDark) {
142 Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
143 }
Joe Onorato263700d2010-05-14 11:54:53 -0700144 }
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 Monkfe7c91b2015-08-13 15:42:28 -0400154 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700155 }
156
157 @Override
158 protected void onDetachedFromWindow() {
159 super.onDetachedFromWindow();
160 if (mAttached) {
161 getContext().unregisterReceiver(mIntentReceiver);
162 mAttached = false;
Jason Monkde850bb2017-02-01 19:26:30 -0500163 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500164 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class)
165 .removeCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500166 if (mShowDark) {
167 Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
168 }
Joe Onorato263700d2010-05-14 11:54:53 -0700169 }
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 Monkcd26af72017-01-11 14:32:58 -0500178 getHandler().post(() -> {
179 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
180 if (mClockFormat != null) {
181 mClockFormat.setTimeZone(mCalendar.getTimeZone());
182 }
183 });
Daniel Sandler43b23c62012-11-29 11:35:02 -0500184 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
185 final Locale newLocale = getResources().getConfiguration().locale;
Jason Monkcd26af72017-01-11 14:32:58 -0500186 getHandler().post(() -> {
187 if (!newLocale.equals(mLocale)) {
188 mLocale = newLocale;
189 mClockFormatString = ""; // force refresh
190 }
191 });
Joe Onorato263700d2010-05-14 11:54:53 -0700192 }
Jason Monkcd26af72017-01-11 14:32:58 -0500193 getHandler().post(() -> updateClock());
Joe Onorato263700d2010-05-14 11:54:53 -0700194 }
195 };
196
Jason Monkaa573e92017-01-27 17:00:29 -0500197 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 Monkf8c2f7b2017-09-06 09:22:29 -0400208 boolean visible = mClockVisibleByPolicy && mClockVisibleByUser;
209 Dependency.get(IconLogger.class).onIconVisibility("clock", visible);
210 int visibility = visible ? View.VISIBLE : View.GONE;
Jason Monkaa573e92017-01-27 17:00:29 -0500211 setVisibility(visibility);
212 }
213
Joe Onorato263700d2010-05-14 11:54:53 -0700214 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400215 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700216 mCalendar.setTimeInMillis(System.currentTimeMillis());
217 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700218 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
Joe Onorato263700d2010-05-14 11:54:53 -0700219 }
220
Jason Monkfe7c91b2015-08-13 15:42:28 -0400221 @Override
222 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500223 if (CLOCK_SECONDS.equals(key)) {
224 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
225 updateShowSeconds();
Jason Monkaa573e92017-01-27 17:00:29 -0500226 } else {
227 setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue)
228 .contains("clock"));
229 updateClockVisibility();
Jason Monk3e189872016-01-12 09:10:34 -0500230 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400231 }
232
Jason Monkaa573e92017-01-27 17:00:29 -0500233 @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 Shahcc3d1d82018-03-30 21:24:17 +0000243 mNonAdaptedColor = DarkIconDispatcher.getTint(area, this, tint);
244 if (!mUseWallpaperTextColor) {
245 setTextColor(mNonAdaptedColor);
246 }
Jason Monkaa573e92017-01-27 17:00:29 -0500247 }
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 Shahcc3d1d82018-03-30 21:24:17 +0000261 /**
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 Monkfe7c91b2015-08-13 15:42:28 -0400280 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 Onorato263700d2010-05-14 11:54:53 -0700303 private final CharSequence getSmallTime() {
304 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100305 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800306 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700307
308 final char MAGIC1 = '\uEF00';
309 final char MAGIC2 = '\uEF01';
310
311 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400312 String format = mShowSeconds
313 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
314 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700315 if (!format.equals(mClockFormatString)) {
Adrian Roos70dcf832016-04-20 15:51:42 -0700316 mContentDescriptionFormat = new SimpleDateFormat(format);
Joe Onorato263700d2010-05-14 11:54:53 -0700317 /*
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 Jaggi740beb52014-05-11 18:53:19 +0200322 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400323 int a = -1;
324 boolean quoted = false;
325 for (int i = 0; i < format.length(); i++) {
326 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700327
Daniel Sandler87937db2010-05-27 13:44:11 -0400328 if (c == '\'') {
329 quoted = !quoted;
330 }
331 if (!quoted && c == 'a') {
332 a = i;
333 break;
334 }
Joe Onorato263700d2010-05-14 11:54:53 -0700335 }
336
Daniel Sandler87937db2010-05-27 13:44:11 -0400337 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 Onorato263700d2010-05-14 11:54:53 -0700344 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400345 }
Joe Onorato263700d2010-05-14 11:54:53 -0700346 }
Joe Onorato263700d2010-05-14 11:54:53 -0700347 mClockFormat = sdf = new SimpleDateFormat(format);
348 mClockFormatString = format;
349 } else {
350 sdf = mClockFormat;
351 }
352 String result = sdf.format(mCalendar.getTime());
353
Jorim Jaggi740beb52014-05-11 18:53:19 +0200354 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400355 int magic1 = result.indexOf(MAGIC1);
356 int magic2 = result.indexOf(MAGIC2);
357 if (magic1 >= 0 && magic2 > magic1) {
358 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200359 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400360 formatted.delete(magic1, magic2+1);
361 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200362 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400363 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 Onorato263700d2010-05-14 11:54:53 -0700372 }
John Spurlock209bede2013-07-17 12:23:27 -0400373
Daniel Sandler87937db2010-05-27 13:44:11 -0400374 return result;
375
Joe Onorato263700d2010-05-14 11:54:53 -0700376 }
John Spurlock3c875662013-08-31 15:07:25 -0400377
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 Reynoldsc11d8382015-07-16 14:40:37 -0400395 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 Spurlock3c875662013-08-31 15:07:25 -0400402 mCalendar.set(Calendar.MINUTE, mm);
403 }
404 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700405 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
John Spurlock3c875662013-08-31 15:07:25 -0400406 }
407 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400408
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 Onorato263700d2010-05-14 11:54:53 -0700435}
436