blob: 66152fcd965e38d440cd5ef3d17b082aad30f0df [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;
Jason Monk3e189872016-01-12 09:10:34 -050036import android.util.ArraySet;
Joe Onorato263700d2010-05-14 11:54:53 -070037import android.util.AttributeSet;
Jason Monkfe7c91b2015-08-13 15:42:28 -040038import android.view.Display;
Jason Monk3e189872016-01-12 09:10:34 -050039import android.view.View;
Joe Onorato263700d2010-05-14 11:54:53 -070040import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080041
John Spurlock3c875662013-08-31 15:07:25 -040042import com.android.systemui.DemoMode;
Jorim Jaggi740beb52014-05-11 18:53:19 +020043import com.android.systemui.R;
Jason Monk3e189872016-01-12 09:10:34 -050044import com.android.systemui.statusbar.phone.StatusBarIconController;
Jason Monkfe7c91b2015-08-13 15:42:28 -040045import com.android.systemui.tuner.TunerService;
46import com.android.systemui.tuner.TunerService.Tunable;
John Spurlock3c875662013-08-31 15:07:25 -040047
Joe Onorato263700d2010-05-14 11:54:53 -070048import java.text.SimpleDateFormat;
49import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050050import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070051import java.util.TimeZone;
52
Joe Onorato263700d2010-05-14 11:54:53 -070053/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050054 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070055 */
Jason Monkfe7c91b2015-08-13 15:42:28 -040056public class Clock extends TextView implements DemoMode, Tunable {
57
58 public static final String CLOCK_SECONDS = "clock_seconds";
59
Joe Onorato263700d2010-05-14 11:54:53 -070060 private boolean mAttached;
61 private Calendar mCalendar;
62 private String mClockFormatString;
63 private SimpleDateFormat mClockFormat;
Adrian Roos70dcf832016-04-20 15:51:42 -070064 private SimpleDateFormat mContentDescriptionFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050065 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070066
Daniel Sandler87937db2010-05-27 13:44:11 -040067 private static final int AM_PM_STYLE_NORMAL = 0;
68 private static final int AM_PM_STYLE_SMALL = 1;
69 private static final int AM_PM_STYLE_GONE = 2;
70
Jorim Jaggi740beb52014-05-11 18:53:19 +020071 private final int mAmPmStyle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040072 private boolean mShowSeconds;
73 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040074
Joe Onorato263700d2010-05-14 11:54:53 -070075 public Clock(Context context) {
76 this(context, null);
77 }
78
79 public Clock(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 public Clock(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020085 TypedArray a = context.getTheme().obtainStyledAttributes(
86 attrs,
87 R.styleable.Clock,
88 0, 0);
89 try {
90 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
91 } finally {
92 a.recycle();
93 }
Joe Onorato263700d2010-05-14 11:54:53 -070094 }
95
96 @Override
97 protected void onAttachedToWindow() {
98 super.onAttachedToWindow();
99
100 if (!mAttached) {
101 mAttached = true;
102 IntentFilter filter = new IntentFilter();
103
104 filter.addAction(Intent.ACTION_TIME_TICK);
105 filter.addAction(Intent.ACTION_TIME_CHANGED);
106 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
107 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500108 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700109
Selim Cinek9c4a7072014-11-21 17:44:34 +0100110 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
111 null, getHandler());
Jason Monk3e189872016-01-12 09:10:34 -0500112 TunerService.get(getContext()).addTunable(this, CLOCK_SECONDS,
113 StatusBarIconController.ICON_BLACKLIST);
Joe Onorato263700d2010-05-14 11:54:53 -0700114 }
115
116 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
117 // in the main thread, therefore the receiver can't run before this method returns.
118
119 // The time zone may have changed while the receiver wasn't registered, so update the Time
120 mCalendar = Calendar.getInstance(TimeZone.getDefault());
121
122 // Make sure we update to the current time
123 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400124 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700125 }
126
127 @Override
128 protected void onDetachedFromWindow() {
129 super.onDetachedFromWindow();
130 if (mAttached) {
131 getContext().unregisterReceiver(mIntentReceiver);
132 mAttached = false;
Jason Monk3e189872016-01-12 09:10:34 -0500133 TunerService.get(getContext()).removeTunable(this);
Joe Onorato263700d2010-05-14 11:54:53 -0700134 }
135 }
136
137 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
138 @Override
139 public void onReceive(Context context, Intent intent) {
140 String action = intent.getAction();
141 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
142 String tz = intent.getStringExtra("time-zone");
143 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
144 if (mClockFormat != null) {
145 mClockFormat.setTimeZone(mCalendar.getTimeZone());
146 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500147 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
148 final Locale newLocale = getResources().getConfiguration().locale;
149 if (! newLocale.equals(mLocale)) {
150 mLocale = newLocale;
151 mClockFormatString = ""; // force refresh
152 }
Joe Onorato263700d2010-05-14 11:54:53 -0700153 }
154 updateClock();
155 }
156 };
157
158 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400159 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700160 mCalendar.setTimeInMillis(System.currentTimeMillis());
161 setText(getSmallTime());
Adrian Roos70dcf832016-04-20 15:51:42 -0700162 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
Joe Onorato263700d2010-05-14 11:54:53 -0700163 }
164
Jason Monkfe7c91b2015-08-13 15:42:28 -0400165 @Override
166 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500167 if (CLOCK_SECONDS.equals(key)) {
168 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
169 updateShowSeconds();
170 } else if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
171 ArraySet<String> list = StatusBarIconController.getIconBlacklist(newValue);
172 setVisibility(list.contains("clock") ? View.GONE : View.VISIBLE);
173 }
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