blob: f0af77de6a22d37ad9af6c50ba3f504d2daff90e [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;
Jason Monkaa573e92017-01-27 17:00:29 -050027import android.content.res.Configuration;
Jorim Jaggi740beb52014-05-11 18:53:19 +020028import android.content.res.TypedArray;
Jason Monkaa573e92017-01-27 17:00:29 -050029import android.graphics.Rect;
John Spurlock3c875662013-08-31 15:07:25 -040030import android.os.Bundle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040031import android.os.Handler;
32import android.os.SystemClock;
Selim Cinek9c4a7072014-11-21 17:44:34 +010033import android.os.UserHandle;
Joe Onorato263700d2010-05-14 11:54:53 -070034import android.text.Spannable;
35import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040036import android.text.format.DateFormat;
37import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040038import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070039import android.util.AttributeSet;
Jason Monkfe7c91b2015-08-13 15:42:28 -040040import android.view.Display;
Jason Monkaa573e92017-01-27 17:00:29 -050041import android.view.View;
Joe Onorato263700d2010-05-14 11:54:53 -070042import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080043
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
Joe Onorato263700d2010-05-14 11:54:53 -070088 public Clock(Context context) {
89 this(context, null);
90 }
91
92 public Clock(Context context, AttributeSet attrs) {
93 this(context, attrs, 0);
94 }
95
96 public Clock(Context context, AttributeSet attrs, int defStyle) {
97 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020098 TypedArray a = context.getTheme().obtainStyledAttributes(
99 attrs,
100 R.styleable.Clock,
101 0, 0);
102 try {
103 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
Jason Monke5b770e2017-03-03 21:49:29 -0500104 mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200105 } finally {
106 a.recycle();
107 }
Joe Onorato263700d2010-05-14 11:54:53 -0700108 }
109
110 @Override
111 protected void onAttachedToWindow() {
112 super.onAttachedToWindow();
113
114 if (!mAttached) {
115 mAttached = true;
116 IntentFilter filter = new IntentFilter();
117
118 filter.addAction(Intent.ACTION_TIME_TICK);
119 filter.addAction(Intent.ACTION_TIME_CHANGED);
120 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
121 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500122 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700123
Selim Cinek9c4a7072014-11-21 17:44:34 +0100124 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
Jason Monk9c7844c2017-01-18 15:21:53 -0500125 null, Dependency.get(Dependency.TIME_TICK_HANDLER));
Jason Monkde850bb2017-02-01 19:26:30 -0500126 Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS,
Jason Monk3e189872016-01-12 09:10:34 -0500127 StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500128 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500129 if (mShowDark) {
130 Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
131 }
Joe Onorato263700d2010-05-14 11:54:53 -0700132 }
133
134 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
135 // in the main thread, therefore the receiver can't run before this method returns.
136
137 // The time zone may have changed while the receiver wasn't registered, so update the Time
138 mCalendar = Calendar.getInstance(TimeZone.getDefault());
139
140 // Make sure we update to the current time
141 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400142 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700143 }
144
145 @Override
146 protected void onDetachedFromWindow() {
147 super.onDetachedFromWindow();
148 if (mAttached) {
149 getContext().unregisterReceiver(mIntentReceiver);
150 mAttached = false;
Jason Monkde850bb2017-02-01 19:26:30 -0500151 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500152 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class)
153 .removeCallbacks(this);
Jason Monke5b770e2017-03-03 21:49:29 -0500154 if (mShowDark) {
155 Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
156 }
Joe Onorato263700d2010-05-14 11:54:53 -0700157 }
158 }
159
160 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
161 @Override
162 public void onReceive(Context context, Intent intent) {
163 String action = intent.getAction();
164 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
165 String tz = intent.getStringExtra("time-zone");
Jason Monkcd26af72017-01-11 14:32:58 -0500166 getHandler().post(() -> {
167 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
168 if (mClockFormat != null) {
169 mClockFormat.setTimeZone(mCalendar.getTimeZone());
170 }
171 });
Daniel Sandler43b23c62012-11-29 11:35:02 -0500172 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
173 final Locale newLocale = getResources().getConfiguration().locale;
Jason Monkcd26af72017-01-11 14:32:58 -0500174 getHandler().post(() -> {
175 if (!newLocale.equals(mLocale)) {
176 mLocale = newLocale;
177 mClockFormatString = ""; // force refresh
178 }
179 });
Joe Onorato263700d2010-05-14 11:54:53 -0700180 }
Jason Monkcd26af72017-01-11 14:32:58 -0500181 getHandler().post(() -> updateClock());
Joe Onorato263700d2010-05-14 11:54:53 -0700182 }
183 };
184
Jason Monkaa573e92017-01-27 17:00:29 -0500185 public void setClockVisibleByUser(boolean visible) {
186 mClockVisibleByUser = visible;
187 updateClockVisibility();
188 }
189
190 public void setClockVisibilityByPolicy(boolean visible) {
191 mClockVisibleByPolicy = visible;
192 updateClockVisibility();
193 }
194
195 private void updateClockVisibility() {
196 int visibility = (mClockVisibleByPolicy && mClockVisibleByUser)
197 ? View.VISIBLE : View.GONE;
198 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