blob: 4c92d01eae4c9e3e0fe840d4f0b7ce5de0397b04 [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
John Spurlock3c875662013-08-31 15:07:25 -040043import com.android.systemui.DemoMode;
Jason Monk9c7844c2017-01-18 15:21:53 -050044import com.android.systemui.Dependency;
Jason Monkaa573e92017-01-27 17:00:29 -050045import com.android.systemui.FontSizeUtils;
Jorim Jaggi740beb52014-05-11 18:53:19 +020046import com.android.systemui.R;
Jason Monkaa573e92017-01-27 17:00:29 -050047import com.android.systemui.SysUiServiceProvider;
48import 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
Joe Onorato263700d2010-05-14 11:54:53 -070055import java.text.SimpleDateFormat;
56import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050057import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070058import java.util.TimeZone;
59
Joe Onorato263700d2010-05-14 11:54:53 -070060/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050061 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070062 */
Jason Monkaa573e92017-01-27 17:00:29 -050063public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks,
64 DarkReceiver, ConfigurationListener {
Jason Monkfe7c91b2015-08-13 15:42:28 -040065
66 public static final String CLOCK_SECONDS = "clock_seconds";
67
Jason Monkaa573e92017-01-27 17:00:29 -050068 private boolean mClockVisibleByPolicy = true;
69 private boolean mClockVisibleByUser = true;
70
Joe Onorato263700d2010-05-14 11:54:53 -070071 private boolean mAttached;
72 private Calendar mCalendar;
73 private String mClockFormatString;
74 private SimpleDateFormat mClockFormat;
Adrian Roos70dcf832016-04-20 15:51:42 -070075 private SimpleDateFormat mContentDescriptionFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050076 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070077
Daniel Sandler87937db2010-05-27 13:44:11 -040078 private static final int AM_PM_STYLE_NORMAL = 0;
79 private static final int AM_PM_STYLE_SMALL = 1;
80 private static final int AM_PM_STYLE_GONE = 2;
81
Jorim Jaggi740beb52014-05-11 18:53:19 +020082 private final int mAmPmStyle;
Jason Monke5b770e2017-03-03 21:49:29 -050083 private final boolean mShowDark;
Jason Monkfe7c91b2015-08-13 15:42:28 -040084 private boolean mShowSeconds;
85 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040086
Joe Onorato263700d2010-05-14 11:54:53 -070087 public Clock(Context context) {
88 this(context, null);
89 }
90
91 public Clock(Context context, AttributeSet attrs) {
92 this(context, attrs, 0);
93 }
94
95 public Clock(Context context, AttributeSet attrs, int defStyle) {
96 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020097 TypedArray a = context.getTheme().obtainStyledAttributes(
98 attrs,
99 R.styleable.Clock,
100 0, 0);
101 try {
102 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
Jason Monke5b770e2017-03-03 21:49:29 -0500103 mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200104 } finally {
105 a.recycle();
106 }
Joe Onorato263700d2010-05-14 11:54:53 -0700107 }
108
109 @Override
110 protected void onAttachedToWindow() {
111 super.onAttachedToWindow();
112
113 if (!mAttached) {
114 mAttached = true;
115 IntentFilter filter = new IntentFilter();
116
117 filter.addAction(Intent.ACTION_TIME_TICK);
118 filter.addAction(Intent.ACTION_TIME_CHANGED);
119 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
120 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500121 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700122
Selim Cinek9c4a7072014-11-21 17:44:34 +0100123 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
Jason Monk9c7844c2017-01-18 15:21:53 -0500124 null, Dependency.get(Dependency.TIME_TICK_HANDLER));
Jason Monkde850bb2017-02-01 19:26:30 -0500125 Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS,
Jason Monk3e189872016-01-12 09:10:34 -0500126 StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500127 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500128 if (mShowDark) {
129 Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
130 }
Joe Onorato263700d2010-05-14 11:54:53 -0700131 }
132
133 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
134 // in the main thread, therefore the receiver can't run before this method returns.
135
136 // The time zone may have changed while the receiver wasn't registered, so update the Time
137 mCalendar = Calendar.getInstance(TimeZone.getDefault());
138
139 // Make sure we update to the current time
140 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400141 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700142 }
143
144 @Override
145 protected void onDetachedFromWindow() {
146 super.onDetachedFromWindow();
147 if (mAttached) {
148 getContext().unregisterReceiver(mIntentReceiver);
149 mAttached = false;
Jason Monkde850bb2017-02-01 19:26:30 -0500150 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500151 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class)
152 .removeCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500153 if (mShowDark) {
154 Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
155 }
Joe Onorato263700d2010-05-14 11:54:53 -0700156 }
157 }
158
159 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
160 @Override
161 public void onReceive(Context context, Intent intent) {
162 String action = intent.getAction();
163 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
164 String tz = intent.getStringExtra("time-zone");
Jason Monkcd26af72017-01-11 14:32:58 -0500165 getHandler().post(() -> {
166 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
167 if (mClockFormat != null) {
168 mClockFormat.setTimeZone(mCalendar.getTimeZone());
169 }
170 });
Daniel Sandler43b23c62012-11-29 11:35:02 -0500171 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
172 final Locale newLocale = getResources().getConfiguration().locale;
Jason Monkcd26af72017-01-11 14:32:58 -0500173 getHandler().post(() -> {
174 if (!newLocale.equals(mLocale)) {
175 mLocale = newLocale;
176 mClockFormatString = ""; // force refresh
177 }
178 });
Joe Onorato263700d2010-05-14 11:54:53 -0700179 }
Jason Monkcd26af72017-01-11 14:32:58 -0500180 getHandler().post(() -> updateClock());
Joe Onorato263700d2010-05-14 11:54:53 -0700181 }
182 };
183
Jason Monkaa573e92017-01-27 17:00:29 -0500184 public void setClockVisibleByUser(boolean visible) {
185 mClockVisibleByUser = visible;
186 updateClockVisibility();
187 }
188
189 public void setClockVisibilityByPolicy(boolean visible) {
190 mClockVisibleByPolicy = visible;
191 updateClockVisibility();
192 }
193
194 private void updateClockVisibility() {
Jason Monkf8c2f7b2017-09-06 09:22:29 -0400195 boolean visible = mClockVisibleByPolicy && mClockVisibleByUser;
196 Dependency.get(IconLogger.class).onIconVisibility("clock", visible);
197 int visibility = visible ? View.VISIBLE : View.GONE;
Jason Monkaa573e92017-01-27 17:00:29 -0500198 setVisibility(visibility);
199 }
200
Joe Onorato263700d2010-05-14 11:54:53 -0700201 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400202 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700203 mCalendar.setTimeInMillis(System.currentTimeMillis());
204 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700205 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
Joe Onorato263700d2010-05-14 11:54:53 -0700206 }
207
Jason Monkfe7c91b2015-08-13 15:42:28 -0400208 @Override
209 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500210 if (CLOCK_SECONDS.equals(key)) {
211 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
212 updateShowSeconds();
Jason Monkaa573e92017-01-27 17:00:29 -0500213 } else {
214 setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue)
215 .contains("clock"));
216 updateClockVisibility();
Jason Monk3e189872016-01-12 09:10:34 -0500217 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400218 }
219
Jason Monkaa573e92017-01-27 17:00:29 -0500220 @Override
221 public void disable(int state1, int state2, boolean animate) {
222 boolean clockVisibleByPolicy = (state1 & StatusBarManager.DISABLE_CLOCK) == 0;
223 if (clockVisibleByPolicy != mClockVisibleByPolicy) {
224 setClockVisibilityByPolicy(clockVisibleByPolicy);
225 }
226 }
227
228 @Override
229 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
230 setTextColor(DarkIconDispatcher.getTint(area, this, tint));
231 }
232
233 @Override
234 public void onDensityOrFontScaleChanged() {
235 FontSizeUtils.updateFontSize(this, R.dimen.status_bar_clock_size);
236 setPaddingRelative(
237 mContext.getResources().getDimensionPixelSize(
238 R.dimen.status_bar_clock_starting_padding),
239 0,
240 mContext.getResources().getDimensionPixelSize(
241 R.dimen.status_bar_clock_end_padding),
242 0);
243 }
244
Jason Monkfe7c91b2015-08-13 15:42:28 -0400245 private void updateShowSeconds() {
246 if (mShowSeconds) {
247 // Wait until we have a display to start trying to show seconds.
248 if (mSecondsHandler == null && getDisplay() != null) {
249 mSecondsHandler = new Handler();
250 if (getDisplay().getState() == Display.STATE_ON) {
251 mSecondsHandler.postAtTime(mSecondTick,
252 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
253 }
254 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
255 filter.addAction(Intent.ACTION_SCREEN_ON);
256 mContext.registerReceiver(mScreenReceiver, filter);
257 }
258 } else {
259 if (mSecondsHandler != null) {
260 mContext.unregisterReceiver(mScreenReceiver);
261 mSecondsHandler.removeCallbacks(mSecondTick);
262 mSecondsHandler = null;
263 updateClock();
264 }
265 }
266 }
267
Joe Onorato263700d2010-05-14 11:54:53 -0700268 private final CharSequence getSmallTime() {
269 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100270 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800271 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700272
273 final char MAGIC1 = '\uEF00';
274 final char MAGIC2 = '\uEF01';
275
276 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400277 String format = mShowSeconds
278 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
279 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700280 if (!format.equals(mClockFormatString)) {
Adrian Roos70dcf832016-04-20 15:51:42 -0700281 mContentDescriptionFormat = new SimpleDateFormat(format);
Joe Onorato263700d2010-05-14 11:54:53 -0700282 /*
283 * Search for an unquoted "a" in the format string, so we can
284 * add dummy characters around it to let us find it again after
285 * formatting and change its size.
286 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200287 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400288 int a = -1;
289 boolean quoted = false;
290 for (int i = 0; i < format.length(); i++) {
291 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700292
Daniel Sandler87937db2010-05-27 13:44:11 -0400293 if (c == '\'') {
294 quoted = !quoted;
295 }
296 if (!quoted && c == 'a') {
297 a = i;
298 break;
299 }
Joe Onorato263700d2010-05-14 11:54:53 -0700300 }
301
Daniel Sandler87937db2010-05-27 13:44:11 -0400302 if (a >= 0) {
303 // Move a back so any whitespace before AM/PM is also in the alternate size.
304 final int b = a;
305 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
306 a--;
307 }
308 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700309 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400310 }
Joe Onorato263700d2010-05-14 11:54:53 -0700311 }
Joe Onorato263700d2010-05-14 11:54:53 -0700312 mClockFormat = sdf = new SimpleDateFormat(format);
313 mClockFormatString = format;
314 } else {
315 sdf = mClockFormat;
316 }
317 String result = sdf.format(mCalendar.getTime());
318
Jorim Jaggi740beb52014-05-11 18:53:19 +0200319 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400320 int magic1 = result.indexOf(MAGIC1);
321 int magic2 = result.indexOf(MAGIC2);
322 if (magic1 >= 0 && magic2 > magic1) {
323 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200324 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400325 formatted.delete(magic1, magic2+1);
326 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200327 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400328 CharacterStyle style = new RelativeSizeSpan(0.7f);
329 formatted.setSpan(style, magic1, magic2,
330 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
331 }
332 formatted.delete(magic2, magic2 + 1);
333 formatted.delete(magic1, magic1 + 1);
334 }
335 return formatted;
336 }
Joe Onorato263700d2010-05-14 11:54:53 -0700337 }
John Spurlock209bede2013-07-17 12:23:27 -0400338
Daniel Sandler87937db2010-05-27 13:44:11 -0400339 return result;
340
Joe Onorato263700d2010-05-14 11:54:53 -0700341 }
John Spurlock3c875662013-08-31 15:07:25 -0400342
343 private boolean mDemoMode;
344
345 @Override
346 public void dispatchDemoCommand(String command, Bundle args) {
347 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
348 mDemoMode = true;
349 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
350 mDemoMode = false;
351 updateClock();
352 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
353 String millis = args.getString("millis");
354 String hhmm = args.getString("hhmm");
355 if (millis != null) {
356 mCalendar.setTimeInMillis(Long.parseLong(millis));
357 } else if (hhmm != null && hhmm.length() == 4) {
358 int hh = Integer.parseInt(hhmm.substring(0, 2));
359 int mm = Integer.parseInt(hhmm.substring(2));
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400360 boolean is24 = DateFormat.is24HourFormat(
361 getContext(), ActivityManager.getCurrentUser());
362 if (is24) {
363 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
364 } else {
365 mCalendar.set(Calendar.HOUR, hh);
366 }
John Spurlock3c875662013-08-31 15:07:25 -0400367 mCalendar.set(Calendar.MINUTE, mm);
368 }
369 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700370 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
John Spurlock3c875662013-08-31 15:07:25 -0400371 }
372 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400373
374 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
375 @Override
376 public void onReceive(Context context, Intent intent) {
377 String action = intent.getAction();
378 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
379 if (mSecondsHandler != null) {
380 mSecondsHandler.removeCallbacks(mSecondTick);
381 }
382 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
383 if (mSecondsHandler != null) {
384 mSecondsHandler.postAtTime(mSecondTick,
385 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
386 }
387 }
388 }
389 };
390
391 private final Runnable mSecondTick = new Runnable() {
392 @Override
393 public void run() {
394 if (mCalendar != null) {
395 updateClock();
396 }
397 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
398 }
399 };
Joe Onorato263700d2010-05-14 11:54:53 -0700400}
401