blob: 896bd62f9969a29ab508074de25d5eca6d89f6a4 [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
Selim Cinek9c4a7072014-11-21 17:44:34 +010019import android.app.ActivityManager;
Daniel Sandler87937db2010-05-27 13:44:11 -040020import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Jorim Jaggi740beb52014-05-11 18:53:19 +020024import android.content.res.TypedArray;
John Spurlock3c875662013-08-31 15:07:25 -040025import android.os.Bundle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040026import android.os.Handler;
27import android.os.SystemClock;
Selim Cinek9c4a7072014-11-21 17:44:34 +010028import android.os.UserHandle;
Joe Onorato263700d2010-05-14 11:54:53 -070029import android.text.Spannable;
30import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040031import android.text.format.DateFormat;
32import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040033import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070034import android.util.AttributeSet;
Jason Monkfe7c91b2015-08-13 15:42:28 -040035import android.view.Display;
Joe Onorato263700d2010-05-14 11:54:53 -070036import android.widget.TextView;
37
John Spurlock3c875662013-08-31 15:07:25 -040038import com.android.systemui.DemoMode;
Jorim Jaggi740beb52014-05-11 18:53:19 +020039import com.android.systemui.R;
Jason Monkfe7c91b2015-08-13 15:42:28 -040040import com.android.systemui.tuner.TunerService;
41import com.android.systemui.tuner.TunerService.Tunable;
42
43import libcore.icu.LocaleData;
John Spurlock3c875662013-08-31 15:07:25 -040044
Joe Onorato263700d2010-05-14 11:54:53 -070045import java.text.SimpleDateFormat;
46import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050047import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070048import java.util.TimeZone;
49
Joe Onorato263700d2010-05-14 11:54:53 -070050/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050051 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070052 */
Jason Monkfe7c91b2015-08-13 15:42:28 -040053public class Clock extends TextView implements DemoMode, Tunable {
54
55 public static final String CLOCK_SECONDS = "clock_seconds";
56
Joe Onorato263700d2010-05-14 11:54:53 -070057 private boolean mAttached;
58 private Calendar mCalendar;
59 private String mClockFormatString;
60 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050061 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070062
Daniel Sandler87937db2010-05-27 13:44:11 -040063 private static final int AM_PM_STYLE_NORMAL = 0;
64 private static final int AM_PM_STYLE_SMALL = 1;
65 private static final int AM_PM_STYLE_GONE = 2;
66
Jorim Jaggi740beb52014-05-11 18:53:19 +020067 private final int mAmPmStyle;
Jason Monkfe7c91b2015-08-13 15:42:28 -040068 private boolean mShowSeconds;
69 private Handler mSecondsHandler;
Daniel Sandler87937db2010-05-27 13:44:11 -040070
Joe Onorato263700d2010-05-14 11:54:53 -070071 public Clock(Context context) {
72 this(context, null);
73 }
74
75 public Clock(Context context, AttributeSet attrs) {
76 this(context, attrs, 0);
77 }
78
79 public Clock(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020081 TypedArray a = context.getTheme().obtainStyledAttributes(
82 attrs,
83 R.styleable.Clock,
84 0, 0);
85 try {
86 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
87 } finally {
88 a.recycle();
89 }
Jason Monkfe7c91b2015-08-13 15:42:28 -040090 TunerService.get(context).addTunable(this, CLOCK_SECONDS);
Joe Onorato263700d2010-05-14 11:54:53 -070091 }
92
93 @Override
94 protected void onAttachedToWindow() {
95 super.onAttachedToWindow();
96
97 if (!mAttached) {
98 mAttached = true;
99 IntentFilter filter = new IntentFilter();
100
101 filter.addAction(Intent.ACTION_TIME_TICK);
102 filter.addAction(Intent.ACTION_TIME_CHANGED);
103 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
104 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -0500105 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -0700106
Selim Cinek9c4a7072014-11-21 17:44:34 +0100107 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
108 null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -0700109 }
110
111 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
112 // in the main thread, therefore the receiver can't run before this method returns.
113
114 // The time zone may have changed while the receiver wasn't registered, so update the Time
115 mCalendar = Calendar.getInstance(TimeZone.getDefault());
116
117 // Make sure we update to the current time
118 updateClock();
Jason Monkfe7c91b2015-08-13 15:42:28 -0400119 updateShowSeconds();
Joe Onorato263700d2010-05-14 11:54:53 -0700120 }
121
122 @Override
123 protected void onDetachedFromWindow() {
124 super.onDetachedFromWindow();
125 if (mAttached) {
126 getContext().unregisterReceiver(mIntentReceiver);
127 mAttached = false;
128 }
129 }
130
131 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
132 @Override
133 public void onReceive(Context context, Intent intent) {
134 String action = intent.getAction();
135 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
136 String tz = intent.getStringExtra("time-zone");
137 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
138 if (mClockFormat != null) {
139 mClockFormat.setTimeZone(mCalendar.getTimeZone());
140 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500141 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
142 final Locale newLocale = getResources().getConfiguration().locale;
143 if (! newLocale.equals(mLocale)) {
144 mLocale = newLocale;
145 mClockFormatString = ""; // force refresh
146 }
Joe Onorato263700d2010-05-14 11:54:53 -0700147 }
148 updateClock();
149 }
150 };
151
152 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400153 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700154 mCalendar.setTimeInMillis(System.currentTimeMillis());
155 setText(getSmallTime());
156 }
157
Jason Monkfe7c91b2015-08-13 15:42:28 -0400158 @Override
159 public void onTuningChanged(String key, String newValue) {
160 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
161 updateShowSeconds();
162 }
163
164 private void updateShowSeconds() {
165 if (mShowSeconds) {
166 // Wait until we have a display to start trying to show seconds.
167 if (mSecondsHandler == null && getDisplay() != null) {
168 mSecondsHandler = new Handler();
169 if (getDisplay().getState() == Display.STATE_ON) {
170 mSecondsHandler.postAtTime(mSecondTick,
171 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
172 }
173 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
174 filter.addAction(Intent.ACTION_SCREEN_ON);
175 mContext.registerReceiver(mScreenReceiver, filter);
176 }
177 } else {
178 if (mSecondsHandler != null) {
179 mContext.unregisterReceiver(mScreenReceiver);
180 mSecondsHandler.removeCallbacks(mSecondTick);
181 mSecondsHandler = null;
182 updateClock();
183 }
184 }
185 }
186
Joe Onorato263700d2010-05-14 11:54:53 -0700187 private final CharSequence getSmallTime() {
188 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100189 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800190 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700191
192 final char MAGIC1 = '\uEF00';
193 final char MAGIC2 = '\uEF01';
194
195 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400196 String format = mShowSeconds
197 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
198 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700199 if (!format.equals(mClockFormatString)) {
200 /*
201 * Search for an unquoted "a" in the format string, so we can
202 * add dummy characters around it to let us find it again after
203 * formatting and change its size.
204 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200205 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400206 int a = -1;
207 boolean quoted = false;
208 for (int i = 0; i < format.length(); i++) {
209 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700210
Daniel Sandler87937db2010-05-27 13:44:11 -0400211 if (c == '\'') {
212 quoted = !quoted;
213 }
214 if (!quoted && c == 'a') {
215 a = i;
216 break;
217 }
Joe Onorato263700d2010-05-14 11:54:53 -0700218 }
219
Daniel Sandler87937db2010-05-27 13:44:11 -0400220 if (a >= 0) {
221 // Move a back so any whitespace before AM/PM is also in the alternate size.
222 final int b = a;
223 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
224 a--;
225 }
226 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700227 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400228 }
Joe Onorato263700d2010-05-14 11:54:53 -0700229 }
Joe Onorato263700d2010-05-14 11:54:53 -0700230 mClockFormat = sdf = new SimpleDateFormat(format);
231 mClockFormatString = format;
232 } else {
233 sdf = mClockFormat;
234 }
235 String result = sdf.format(mCalendar.getTime());
236
Jorim Jaggi740beb52014-05-11 18:53:19 +0200237 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400238 int magic1 = result.indexOf(MAGIC1);
239 int magic2 = result.indexOf(MAGIC2);
240 if (magic1 >= 0 && magic2 > magic1) {
241 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200242 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400243 formatted.delete(magic1, magic2+1);
244 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200245 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400246 CharacterStyle style = new RelativeSizeSpan(0.7f);
247 formatted.setSpan(style, magic1, magic2,
248 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
249 }
250 formatted.delete(magic2, magic2 + 1);
251 formatted.delete(magic1, magic1 + 1);
252 }
253 return formatted;
254 }
Joe Onorato263700d2010-05-14 11:54:53 -0700255 }
John Spurlock209bede2013-07-17 12:23:27 -0400256
Daniel Sandler87937db2010-05-27 13:44:11 -0400257 return result;
258
Joe Onorato263700d2010-05-14 11:54:53 -0700259 }
John Spurlock3c875662013-08-31 15:07:25 -0400260
261 private boolean mDemoMode;
262
263 @Override
264 public void dispatchDemoCommand(String command, Bundle args) {
265 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
266 mDemoMode = true;
267 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
268 mDemoMode = false;
269 updateClock();
270 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
271 String millis = args.getString("millis");
272 String hhmm = args.getString("hhmm");
273 if (millis != null) {
274 mCalendar.setTimeInMillis(Long.parseLong(millis));
275 } else if (hhmm != null && hhmm.length() == 4) {
276 int hh = Integer.parseInt(hhmm.substring(0, 2));
277 int mm = Integer.parseInt(hhmm.substring(2));
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400278 boolean is24 = DateFormat.is24HourFormat(
279 getContext(), ActivityManager.getCurrentUser());
280 if (is24) {
281 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
282 } else {
283 mCalendar.set(Calendar.HOUR, hh);
284 }
John Spurlock3c875662013-08-31 15:07:25 -0400285 mCalendar.set(Calendar.MINUTE, mm);
286 }
287 setText(getSmallTime());
288 }
289 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400290
291 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
292 @Override
293 public void onReceive(Context context, Intent intent) {
294 String action = intent.getAction();
295 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
296 if (mSecondsHandler != null) {
297 mSecondsHandler.removeCallbacks(mSecondTick);
298 }
299 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
300 if (mSecondsHandler != null) {
301 mSecondsHandler.postAtTime(mSecondTick,
302 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
303 }
304 }
305 }
306 };
307
308 private final Runnable mSecondTick = new Runnable() {
309 @Override
310 public void run() {
311 if (mCalendar != null) {
312 updateClock();
313 }
314 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
315 }
316 };
Joe Onorato263700d2010-05-14 11:54:53 -0700317}
318