blob: 93fb14fc58c2d78e651287e35d9af221af4c1cf2 [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
Daniel Sandler87937db2010-05-27 13:44:11 -040019import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070020import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Joe Onorato263700d2010-05-14 11:54:53 -070023import android.text.Spannable;
24import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040025import android.text.format.DateFormat;
26import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040027import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070028import android.util.AttributeSet;
Joe Onorato263700d2010-05-14 11:54:53 -070029import android.widget.TextView;
30
31import java.text.SimpleDateFormat;
32import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050033import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070034import java.util.TimeZone;
35
Elliott Hughes4caba612013-01-14 15:48:27 -080036import libcore.icu.LocaleData;
37
Joe Onorato263700d2010-05-14 11:54:53 -070038/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050039 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070040 */
41public class Clock extends TextView {
Joe Onorato263700d2010-05-14 11:54:53 -070042 private boolean mAttached;
43 private Calendar mCalendar;
44 private String mClockFormatString;
45 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050046 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070047
Daniel Sandler87937db2010-05-27 13:44:11 -040048 private static final int AM_PM_STYLE_NORMAL = 0;
49 private static final int AM_PM_STYLE_SMALL = 1;
50 private static final int AM_PM_STYLE_GONE = 2;
51
52 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
53
Joe Onorato263700d2010-05-14 11:54:53 -070054 public Clock(Context context) {
55 this(context, null);
56 }
57
58 public Clock(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public Clock(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64 }
65
66 @Override
67 protected void onAttachedToWindow() {
68 super.onAttachedToWindow();
69
70 if (!mAttached) {
71 mAttached = true;
72 IntentFilter filter = new IntentFilter();
73
74 filter.addAction(Intent.ACTION_TIME_TICK);
75 filter.addAction(Intent.ACTION_TIME_CHANGED);
76 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
77 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050078 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070079
Joe Onorato1a86dd12010-06-01 08:17:58 -070080 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070081 }
82
83 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
84 // in the main thread, therefore the receiver can't run before this method returns.
85
86 // The time zone may have changed while the receiver wasn't registered, so update the Time
87 mCalendar = Calendar.getInstance(TimeZone.getDefault());
88
89 // Make sure we update to the current time
90 updateClock();
91 }
92
93 @Override
94 protected void onDetachedFromWindow() {
95 super.onDetachedFromWindow();
96 if (mAttached) {
97 getContext().unregisterReceiver(mIntentReceiver);
98 mAttached = false;
99 }
100 }
101
102 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
103 @Override
104 public void onReceive(Context context, Intent intent) {
105 String action = intent.getAction();
106 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
107 String tz = intent.getStringExtra("time-zone");
108 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
109 if (mClockFormat != null) {
110 mClockFormat.setTimeZone(mCalendar.getTimeZone());
111 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500112 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
113 final Locale newLocale = getResources().getConfiguration().locale;
114 if (! newLocale.equals(mLocale)) {
115 mLocale = newLocale;
116 mClockFormatString = ""; // force refresh
117 }
Joe Onorato263700d2010-05-14 11:54:53 -0700118 }
119 updateClock();
120 }
121 };
122
123 final void updateClock() {
124 mCalendar.setTimeInMillis(System.currentTimeMillis());
125 setText(getSmallTime());
126 }
127
128 private final CharSequence getSmallTime() {
129 Context context = getContext();
Elliott Hughes4caba612013-01-14 15:48:27 -0800130 boolean is24 = DateFormat.is24HourFormat(context);
131 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700132
133 final char MAGIC1 = '\uEF00';
134 final char MAGIC2 = '\uEF01';
135
136 SimpleDateFormat sdf;
Elliott Hughes4caba612013-01-14 15:48:27 -0800137 String format = is24 ? d.timeFormat24 : d.timeFormat12;
Joe Onorato263700d2010-05-14 11:54:53 -0700138 if (!format.equals(mClockFormatString)) {
139 /*
140 * Search for an unquoted "a" in the format string, so we can
141 * add dummy characters around it to let us find it again after
142 * formatting and change its size.
143 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400144 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
145 int a = -1;
146 boolean quoted = false;
147 for (int i = 0; i < format.length(); i++) {
148 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700149
Daniel Sandler87937db2010-05-27 13:44:11 -0400150 if (c == '\'') {
151 quoted = !quoted;
152 }
153 if (!quoted && c == 'a') {
154 a = i;
155 break;
156 }
Joe Onorato263700d2010-05-14 11:54:53 -0700157 }
158
Daniel Sandler87937db2010-05-27 13:44:11 -0400159 if (a >= 0) {
160 // Move a back so any whitespace before AM/PM is also in the alternate size.
161 final int b = a;
162 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
163 a--;
164 }
165 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700166 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400167 }
Joe Onorato263700d2010-05-14 11:54:53 -0700168 }
Joe Onorato263700d2010-05-14 11:54:53 -0700169 mClockFormat = sdf = new SimpleDateFormat(format);
170 mClockFormatString = format;
171 } else {
172 sdf = mClockFormat;
173 }
174 String result = sdf.format(mCalendar.getTime());
175
Daniel Sandler87937db2010-05-27 13:44:11 -0400176 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
177 int magic1 = result.indexOf(MAGIC1);
178 int magic2 = result.indexOf(MAGIC2);
179 if (magic1 >= 0 && magic2 > magic1) {
180 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
181 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
182 formatted.delete(magic1, magic2+1);
183 } else {
184 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
185 CharacterStyle style = new RelativeSizeSpan(0.7f);
186 formatted.setSpan(style, magic1, magic2,
187 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
188 }
189 formatted.delete(magic2, magic2 + 1);
190 formatted.delete(magic1, magic1 + 1);
191 }
192 return formatted;
193 }
Joe Onorato263700d2010-05-14 11:54:53 -0700194 }
John Spurlock209bede2013-07-17 12:23:27 -0400195
Daniel Sandler87937db2010-05-27 13:44:11 -0400196 return result;
197
Joe Onorato263700d2010-05-14 11:54:53 -0700198 }
199}
200