blob: 9cc97492e22c306f7b5cf5985847ce00b990e81e [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;
Daniel Sandler87937db2010-05-27 13:44:11 -040022import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070023import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Jorim Jaggi740beb52014-05-11 18:53:19 +020026import android.content.res.TypedArray;
John Spurlock3c875662013-08-31 15:07:25 -040027import android.os.Bundle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040028import android.os.Handler;
29import 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;
Joe Onorato263700d2010-05-14 11:54:53 -070038import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080039
John Spurlock3c875662013-08-31 15:07:25 -040040import com.android.systemui.DemoMode;
Jason Monk9c7844c2017-01-18 15:21:53 -050041import com.android.systemui.Dependency;
Jorim Jaggi740beb52014-05-11 18:53:19 +020042import com.android.systemui.R;
Jason Monk3e189872016-01-12 09:10:34 -050043import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkfe7c91b2015-08-13 15:42:28 -040044import com.android.systemui.tuner.TunerService;
45import com.android.systemui.tuner.TunerService.Tunable;
John Spurlock3c875662013-08-31 15:07:25 -040046
Joe Onorato263700d2010-05-14 11:54:53 -070047import java.text.SimpleDateFormat;
48import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050049import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070050import java.util.TimeZone;
51
Joe Onorato263700d2010-05-14 11:54:53 -070052/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050053 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070054 */
Jason Monkfe7c91b2015-08-13 15:42:28 -040055public class Clock extends TextView implements DemoMode, Tunable {
56
57 public static final String CLOCK_SECONDS = "clock_seconds";
58
Joe Onorato263700d2010-05-14 11:54:53 -070059 private boolean mAttached;
60 private Calendar mCalendar;
61 private String mClockFormatString;
62 private SimpleDateFormat mClockFormat;
Adrian Roos70dcf832016-04-20 15:51:42 -070063 private SimpleDateFormat mContentDescriptionFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050064 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070065
Daniel Sandler87937db2010-05-27 13:44:11 -040066 private static final int AM_PM_STYLE_NORMAL = 0;
67 private static final int AM_PM_STYLE_SMALL = 1;
68 private static final int AM_PM_STYLE_GONE = 2;
69
Jorim Jaggi740beb52014-05-11 18:53:19 +020070 private final int mAmPmStyle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040071 private boolean mShowSeconds;
72 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040073
Joe Onorato263700d2010-05-14 11:54:53 -070074 public Clock(Context context) {
75 this(context, null);
76 }
77
78 public Clock(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public Clock(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020084 TypedArray a = context.getTheme().obtainStyledAttributes(
85 attrs,
86 R.styleable.Clock,
87 0, 0);
88 try {
89 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
90 } finally {
91 a.recycle();
92 }
Joe Onorato263700d2010-05-14 11:54:53 -070093 }
94
95 @Override
96 protected void onAttachedToWindow() {
97 super.onAttachedToWindow();
98
99 if (!mAttached) {
100 mAttached = true;
101 IntentFilter filter = new IntentFilter();
102
103 filter.addAction(Intent.ACTION_TIME_TICK);
104 filter.addAction(Intent.ACTION_TIME_CHANGED);
105 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
106 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500107 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700108
Selim Cinek9c4a7072014-11-21 17:44:34 +0100109 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
Jason Monk9c7844c2017-01-18 15:21:53 -0500110 null, Dependency.get(Dependency.TIME_TICK_HANDLER));
Jason Monk3e189872016-01-12 09:10:34 -0500111 TunerService.get(getContext()).addTunable(this, CLOCK_SECONDS,
112 StatusBarIconController.ICON_BLACKLIST);
Joe Onorato263700d2010-05-14 11:54:53 -0700113 }
114
115 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
116 // in the main thread, therefore the receiver can't run before this method returns.
117
118 // The time zone may have changed while the receiver wasn't registered, so update the Time
119 mCalendar = Calendar.getInstance(TimeZone.getDefault());
120
121 // Make sure we update to the current time
122 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400123 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700124 }
125
126 @Override
127 protected void onDetachedFromWindow() {
128 super.onDetachedFromWindow();
129 if (mAttached) {
130 getContext().unregisterReceiver(mIntentReceiver);
131 mAttached = false;
Jason Monk3e189872016-01-12 09:10:34 -0500132 TunerService.get(getContext()).removeTunable(this);
Joe Onorato263700d2010-05-14 11:54:53 -0700133 }
134 }
135
136 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
137 @Override
138 public void onReceive(Context context, Intent intent) {
139 String action = intent.getAction();
140 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
141 String tz = intent.getStringExtra("time-zone");
Jason Monkcd26af72017-01-11 14:32:58 -0500142 getHandler().post(() -> {
143 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
144 if (mClockFormat != null) {
145 mClockFormat.setTimeZone(mCalendar.getTimeZone());
146 }
147 });
Daniel Sandler43b23c62012-11-29 11:35:02 -0500148 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
149 final Locale newLocale = getResources().getConfiguration().locale;
Jason Monkcd26af72017-01-11 14:32:58 -0500150 getHandler().post(() -> {
151 if (!newLocale.equals(mLocale)) {
152 mLocale = newLocale;
153 mClockFormatString = ""; // force refresh
154 }
155 });
Joe Onorato263700d2010-05-14 11:54:53 -0700156 }
Jason Monkcd26af72017-01-11 14:32:58 -0500157 getHandler().post(() -> updateClock());
Joe Onorato263700d2010-05-14 11:54:53 -0700158 }
159 };
160
161 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400162 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700163 mCalendar.setTimeInMillis(System.currentTimeMillis());
164 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700165 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
Joe Onorato263700d2010-05-14 11:54:53 -0700166 }
167
Jason Monkfe7c91b2015-08-13 15:42:28 -0400168 @Override
169 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500170 if (CLOCK_SECONDS.equals(key)) {
171 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
172 updateShowSeconds();
Jason Monk3e189872016-01-12 09:10:34 -0500173 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400174 }
175
176 private void updateShowSeconds() {
177 if (mShowSeconds) {
178 // Wait until we have a display to start trying to show seconds.
179 if (mSecondsHandler == null && getDisplay() != null) {
180 mSecondsHandler = new Handler();
181 if (getDisplay().getState() == Display.STATE_ON) {
182 mSecondsHandler.postAtTime(mSecondTick,
183 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
184 }
185 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
186 filter.addAction(Intent.ACTION_SCREEN_ON);
187 mContext.registerReceiver(mScreenReceiver, filter);
188 }
189 } else {
190 if (mSecondsHandler != null) {
191 mContext.unregisterReceiver(mScreenReceiver);
192 mSecondsHandler.removeCallbacks(mSecondTick);
193 mSecondsHandler = null;
194 updateClock();
195 }
196 }
197 }
198
Joe Onorato263700d2010-05-14 11:54:53 -0700199 private final CharSequence getSmallTime() {
200 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100201 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800202 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700203
204 final char MAGIC1 = '\uEF00';
205 final char MAGIC2 = '\uEF01';
206
207 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400208 String format = mShowSeconds
209 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
210 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700211 if (!format.equals(mClockFormatString)) {
Adrian Roos70dcf832016-04-20 15:51:42 -0700212 mContentDescriptionFormat = new SimpleDateFormat(format);
Joe Onorato263700d2010-05-14 11:54:53 -0700213 /*
214 * Search for an unquoted "a" in the format string, so we can
215 * add dummy characters around it to let us find it again after
216 * formatting and change its size.
217 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200218 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400219 int a = -1;
220 boolean quoted = false;
221 for (int i = 0; i < format.length(); i++) {
222 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700223
Daniel Sandler87937db2010-05-27 13:44:11 -0400224 if (c == '\'') {
225 quoted = !quoted;
226 }
227 if (!quoted && c == 'a') {
228 a = i;
229 break;
230 }
Joe Onorato263700d2010-05-14 11:54:53 -0700231 }
232
Daniel Sandler87937db2010-05-27 13:44:11 -0400233 if (a >= 0) {
234 // Move a back so any whitespace before AM/PM is also in the alternate size.
235 final int b = a;
236 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
237 a--;
238 }
239 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700240 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400241 }
Joe Onorato263700d2010-05-14 11:54:53 -0700242 }
Joe Onorato263700d2010-05-14 11:54:53 -0700243 mClockFormat = sdf = new SimpleDateFormat(format);
244 mClockFormatString = format;
245 } else {
246 sdf = mClockFormat;
247 }
248 String result = sdf.format(mCalendar.getTime());
249
Jorim Jaggi740beb52014-05-11 18:53:19 +0200250 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400251 int magic1 = result.indexOf(MAGIC1);
252 int magic2 = result.indexOf(MAGIC2);
253 if (magic1 >= 0 && magic2 > magic1) {
254 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200255 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400256 formatted.delete(magic1, magic2+1);
257 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200258 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400259 CharacterStyle style = new RelativeSizeSpan(0.7f);
260 formatted.setSpan(style, magic1, magic2,
261 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
262 }
263 formatted.delete(magic2, magic2 + 1);
264 formatted.delete(magic1, magic1 + 1);
265 }
266 return formatted;
267 }
Joe Onorato263700d2010-05-14 11:54:53 -0700268 }
John Spurlock209bede2013-07-17 12:23:27 -0400269
Daniel Sandler87937db2010-05-27 13:44:11 -0400270 return result;
271
Joe Onorato263700d2010-05-14 11:54:53 -0700272 }
John Spurlock3c875662013-08-31 15:07:25 -0400273
274 private boolean mDemoMode;
275
276 @Override
277 public void dispatchDemoCommand(String command, Bundle args) {
278 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
279 mDemoMode = true;
280 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
281 mDemoMode = false;
282 updateClock();
283 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
284 String millis = args.getString("millis");
285 String hhmm = args.getString("hhmm");
286 if (millis != null) {
287 mCalendar.setTimeInMillis(Long.parseLong(millis));
288 } else if (hhmm != null && hhmm.length() == 4) {
289 int hh = Integer.parseInt(hhmm.substring(0, 2));
290 int mm = Integer.parseInt(hhmm.substring(2));
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400291 boolean is24 = DateFormat.is24HourFormat(
292 getContext(), ActivityManager.getCurrentUser());
293 if (is24) {
294 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
295 } else {
296 mCalendar.set(Calendar.HOUR, hh);
297 }
John Spurlock3c875662013-08-31 15:07:25 -0400298 mCalendar.set(Calendar.MINUTE, mm);
299 }
300 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700301 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
John Spurlock3c875662013-08-31 15:07:25 -0400302 }
303 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400304
305 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
306 @Override
307 public void onReceive(Context context, Intent intent) {
308 String action = intent.getAction();
309 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
310 if (mSecondsHandler != null) {
311 mSecondsHandler.removeCallbacks(mSecondTick);
312 }
313 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
314 if (mSecondsHandler != null) {
315 mSecondsHandler.postAtTime(mSecondTick,
316 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
317 }
318 }
319 }
320 };
321
322 private final Runnable mSecondTick = new Runnable() {
323 @Override
324 public void run() {
325 if (mCalendar != null) {
326 updateClock();
327 }
328 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
329 }
330 };
Joe Onorato263700d2010-05-14 11:54:53 -0700331}
332