blob: 32939641f95b6e48e0e1e488eb8e2a6ac414a4e5 [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;
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,
110 null, getHandler());
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");
142 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
143 if (mClockFormat != null) {
144 mClockFormat.setTimeZone(mCalendar.getTimeZone());
145 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500146 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
147 final Locale newLocale = getResources().getConfiguration().locale;
148 if (! newLocale.equals(mLocale)) {
149 mLocale = newLocale;
150 mClockFormatString = ""; // force refresh
151 }
Joe Onorato263700d2010-05-14 11:54:53 -0700152 }
153 updateClock();
154 }
155 };
156
157 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400158 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700159 mCalendar.setTimeInMillis(System.currentTimeMillis());
160 setText(getSmallTime());
161 }
162
Jason Monkfe7c91b2015-08-13 15:42:28 -0400163 @Override
164 public void onTuningChanged(String key, String newValue) {
Jason Monk3e189872016-01-12 09:10:34 -0500165 if (CLOCK_SECONDS.equals(key)) {
166 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
167 updateShowSeconds();
168 } else if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
169 ArraySet<String> list = StatusBarIconController.getIconBlacklist(newValue);
170 setVisibility(list.contains("clock") ? View.GONE : View.VISIBLE);
171 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400172 }
173
174 private void updateShowSeconds() {
175 if (mShowSeconds) {
176 // Wait until we have a display to start trying to show seconds.
177 if (mSecondsHandler == null && getDisplay() != null) {
178 mSecondsHandler = new Handler();
179 if (getDisplay().getState() == Display.STATE_ON) {
180 mSecondsHandler.postAtTime(mSecondTick,
181 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
182 }
183 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
184 filter.addAction(Intent.ACTION_SCREEN_ON);
185 mContext.registerReceiver(mScreenReceiver, filter);
186 }
187 } else {
188 if (mSecondsHandler != null) {
189 mContext.unregisterReceiver(mScreenReceiver);
190 mSecondsHandler.removeCallbacks(mSecondTick);
191 mSecondsHandler = null;
192 updateClock();
193 }
194 }
195 }
196
Joe Onorato263700d2010-05-14 11:54:53 -0700197 private final CharSequence getSmallTime() {
198 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100199 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800200 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700201
202 final char MAGIC1 = '\uEF00';
203 final char MAGIC2 = '\uEF01';
204
205 SimpleDateFormat sdf;
Jason Monkfe7c91b2015-08-13 15:42:28 -0400206 String format = mShowSeconds
207 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
208 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700209 if (!format.equals(mClockFormatString)) {
210 /*
211 * Search for an unquoted "a" in the format string, so we can
212 * add dummy characters around it to let us find it again after
213 * formatting and change its size.
214 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200215 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400216 int a = -1;
217 boolean quoted = false;
218 for (int i = 0; i < format.length(); i++) {
219 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700220
Daniel Sandler87937db2010-05-27 13:44:11 -0400221 if (c == '\'') {
222 quoted = !quoted;
223 }
224 if (!quoted && c == 'a') {
225 a = i;
226 break;
227 }
Joe Onorato263700d2010-05-14 11:54:53 -0700228 }
229
Daniel Sandler87937db2010-05-27 13:44:11 -0400230 if (a >= 0) {
231 // Move a back so any whitespace before AM/PM is also in the alternate size.
232 final int b = a;
233 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
234 a--;
235 }
236 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700237 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400238 }
Joe Onorato263700d2010-05-14 11:54:53 -0700239 }
Joe Onorato263700d2010-05-14 11:54:53 -0700240 mClockFormat = sdf = new SimpleDateFormat(format);
241 mClockFormatString = format;
242 } else {
243 sdf = mClockFormat;
244 }
245 String result = sdf.format(mCalendar.getTime());
246
Jorim Jaggi740beb52014-05-11 18:53:19 +0200247 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400248 int magic1 = result.indexOf(MAGIC1);
249 int magic2 = result.indexOf(MAGIC2);
250 if (magic1 >= 0 && magic2 > magic1) {
251 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200252 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400253 formatted.delete(magic1, magic2+1);
254 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200255 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400256 CharacterStyle style = new RelativeSizeSpan(0.7f);
257 formatted.setSpan(style, magic1, magic2,
258 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
259 }
260 formatted.delete(magic2, magic2 + 1);
261 formatted.delete(magic1, magic1 + 1);
262 }
263 return formatted;
264 }
Joe Onorato263700d2010-05-14 11:54:53 -0700265 }
John Spurlock209bede2013-07-17 12:23:27 -0400266
Daniel Sandler87937db2010-05-27 13:44:11 -0400267 return result;
268
Joe Onorato263700d2010-05-14 11:54:53 -0700269 }
John Spurlock3c875662013-08-31 15:07:25 -0400270
271 private boolean mDemoMode;
272
273 @Override
274 public void dispatchDemoCommand(String command, Bundle args) {
275 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
276 mDemoMode = true;
277 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
278 mDemoMode = false;
279 updateClock();
280 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
281 String millis = args.getString("millis");
282 String hhmm = args.getString("hhmm");
283 if (millis != null) {
284 mCalendar.setTimeInMillis(Long.parseLong(millis));
285 } else if (hhmm != null && hhmm.length() == 4) {
286 int hh = Integer.parseInt(hhmm.substring(0, 2));
287 int mm = Integer.parseInt(hhmm.substring(2));
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400288 boolean is24 = DateFormat.is24HourFormat(
289 getContext(), ActivityManager.getCurrentUser());
290 if (is24) {
291 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
292 } else {
293 mCalendar.set(Calendar.HOUR, hh);
294 }
John Spurlock3c875662013-08-31 15:07:25 -0400295 mCalendar.set(Calendar.MINUTE, mm);
296 }
297 setText(getSmallTime());
298 }
299 }
Jason Monkfe7c91b2015-08-13 15:42:28 -0400300
301 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
302 @Override
303 public void onReceive(Context context, Intent intent) {
304 String action = intent.getAction();
305 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
306 if (mSecondsHandler != null) {
307 mSecondsHandler.removeCallbacks(mSecondTick);
308 }
309 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
310 if (mSecondsHandler != null) {
311 mSecondsHandler.postAtTime(mSecondTick,
312 SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
313 }
314 }
315 }
316 };
317
318 private final Runnable mSecondTick = new Runnable() {
319 @Override
320 public void run() {
321 if (mCalendar != null) {
322 updateClock();
323 }
324 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
325 }
326 };
Joe Onorato263700d2010-05-14 11:54:53 -0700327}
328