blob: 8ced1c9cc2c6c5e22415c608efe95d58dc7c2f3b [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;
John Spurlock3c875662013-08-31 15:07:25 -040023import android.os.Bundle;
Joe Onorato263700d2010-05-14 11:54:53 -070024import android.text.Spannable;
25import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040026import android.text.format.DateFormat;
27import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040028import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070029import android.util.AttributeSet;
Joe Onorato263700d2010-05-14 11:54:53 -070030import android.widget.TextView;
31
John Spurlock3c875662013-08-31 15:07:25 -040032import com.android.systemui.DemoMode;
33
Joe Onorato263700d2010-05-14 11:54:53 -070034import java.text.SimpleDateFormat;
35import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050036import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070037import java.util.TimeZone;
38
Elliott Hughes4caba612013-01-14 15:48:27 -080039import libcore.icu.LocaleData;
40
Joe Onorato263700d2010-05-14 11:54:53 -070041/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050042 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070043 */
John Spurlock3c875662013-08-31 15:07:25 -040044public class Clock extends TextView implements DemoMode {
Joe Onorato263700d2010-05-14 11:54:53 -070045 private boolean mAttached;
46 private Calendar mCalendar;
47 private String mClockFormatString;
48 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050049 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070050
Daniel Sandler87937db2010-05-27 13:44:11 -040051 private static final int AM_PM_STYLE_NORMAL = 0;
52 private static final int AM_PM_STYLE_SMALL = 1;
53 private static final int AM_PM_STYLE_GONE = 2;
54
55 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
56
Joe Onorato263700d2010-05-14 11:54:53 -070057 public Clock(Context context) {
58 this(context, null);
59 }
60
61 public Clock(Context context, AttributeSet attrs) {
62 this(context, attrs, 0);
63 }
64
65 public Clock(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67 }
68
69 @Override
70 protected void onAttachedToWindow() {
71 super.onAttachedToWindow();
72
73 if (!mAttached) {
74 mAttached = true;
75 IntentFilter filter = new IntentFilter();
76
77 filter.addAction(Intent.ACTION_TIME_TICK);
78 filter.addAction(Intent.ACTION_TIME_CHANGED);
79 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
80 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050081 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070082
Joe Onorato1a86dd12010-06-01 08:17:58 -070083 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070084 }
85
86 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
87 // in the main thread, therefore the receiver can't run before this method returns.
88
89 // The time zone may have changed while the receiver wasn't registered, so update the Time
90 mCalendar = Calendar.getInstance(TimeZone.getDefault());
91
92 // Make sure we update to the current time
93 updateClock();
94 }
95
96 @Override
97 protected void onDetachedFromWindow() {
98 super.onDetachedFromWindow();
99 if (mAttached) {
100 getContext().unregisterReceiver(mIntentReceiver);
101 mAttached = false;
102 }
103 }
104
105 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
106 @Override
107 public void onReceive(Context context, Intent intent) {
108 String action = intent.getAction();
109 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
110 String tz = intent.getStringExtra("time-zone");
111 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
112 if (mClockFormat != null) {
113 mClockFormat.setTimeZone(mCalendar.getTimeZone());
114 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500115 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
116 final Locale newLocale = getResources().getConfiguration().locale;
117 if (! newLocale.equals(mLocale)) {
118 mLocale = newLocale;
119 mClockFormatString = ""; // force refresh
120 }
Joe Onorato263700d2010-05-14 11:54:53 -0700121 }
122 updateClock();
123 }
124 };
125
126 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400127 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700128 mCalendar.setTimeInMillis(System.currentTimeMillis());
129 setText(getSmallTime());
130 }
131
132 private final CharSequence getSmallTime() {
133 Context context = getContext();
Elliott Hughes4caba612013-01-14 15:48:27 -0800134 boolean is24 = DateFormat.is24HourFormat(context);
135 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700136
137 final char MAGIC1 = '\uEF00';
138 final char MAGIC2 = '\uEF01';
139
140 SimpleDateFormat sdf;
Elliott Hughes4caba612013-01-14 15:48:27 -0800141 String format = is24 ? d.timeFormat24 : d.timeFormat12;
Joe Onorato263700d2010-05-14 11:54:53 -0700142 if (!format.equals(mClockFormatString)) {
143 /*
144 * Search for an unquoted "a" in the format string, so we can
145 * add dummy characters around it to let us find it again after
146 * formatting and change its size.
147 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400148 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
149 int a = -1;
150 boolean quoted = false;
151 for (int i = 0; i < format.length(); i++) {
152 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700153
Daniel Sandler87937db2010-05-27 13:44:11 -0400154 if (c == '\'') {
155 quoted = !quoted;
156 }
157 if (!quoted && c == 'a') {
158 a = i;
159 break;
160 }
Joe Onorato263700d2010-05-14 11:54:53 -0700161 }
162
Daniel Sandler87937db2010-05-27 13:44:11 -0400163 if (a >= 0) {
164 // Move a back so any whitespace before AM/PM is also in the alternate size.
165 final int b = a;
166 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
167 a--;
168 }
169 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700170 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400171 }
Joe Onorato263700d2010-05-14 11:54:53 -0700172 }
Joe Onorato263700d2010-05-14 11:54:53 -0700173 mClockFormat = sdf = new SimpleDateFormat(format);
174 mClockFormatString = format;
175 } else {
176 sdf = mClockFormat;
177 }
178 String result = sdf.format(mCalendar.getTime());
179
Daniel Sandler87937db2010-05-27 13:44:11 -0400180 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
181 int magic1 = result.indexOf(MAGIC1);
182 int magic2 = result.indexOf(MAGIC2);
183 if (magic1 >= 0 && magic2 > magic1) {
184 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
185 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
186 formatted.delete(magic1, magic2+1);
187 } else {
188 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
189 CharacterStyle style = new RelativeSizeSpan(0.7f);
190 formatted.setSpan(style, magic1, magic2,
191 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
192 }
193 formatted.delete(magic2, magic2 + 1);
194 formatted.delete(magic1, magic1 + 1);
195 }
196 return formatted;
197 }
Joe Onorato263700d2010-05-14 11:54:53 -0700198 }
John Spurlock209bede2013-07-17 12:23:27 -0400199
Daniel Sandler87937db2010-05-27 13:44:11 -0400200 return result;
201
Joe Onorato263700d2010-05-14 11:54:53 -0700202 }
John Spurlock3c875662013-08-31 15:07:25 -0400203
204 private boolean mDemoMode;
205
206 @Override
207 public void dispatchDemoCommand(String command, Bundle args) {
208 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
209 mDemoMode = true;
210 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
211 mDemoMode = false;
212 updateClock();
213 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
214 String millis = args.getString("millis");
215 String hhmm = args.getString("hhmm");
216 if (millis != null) {
217 mCalendar.setTimeInMillis(Long.parseLong(millis));
218 } else if (hhmm != null && hhmm.length() == 4) {
219 int hh = Integer.parseInt(hhmm.substring(0, 2));
220 int mm = Integer.parseInt(hhmm.substring(2));
221 mCalendar.set(Calendar.HOUR, hh);
222 mCalendar.set(Calendar.MINUTE, mm);
223 }
224 setText(getSmallTime());
225 }
226 }
Joe Onorato263700d2010-05-14 11:54:53 -0700227}
228