blob: 69872df2653a0b76ee30d6371b1b5c327db63b7f [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.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Canvas;
Daniel Sandler87937db2010-05-27 13:44:11 -040026import android.graphics.Typeface;
Joe Onorato263700d2010-05-14 11:54:53 -070027import android.graphics.drawable.Drawable;
Joe Onorato263700d2010-05-14 11:54:53 -070028import android.text.Spannable;
29import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040030import android.text.format.DateFormat;
31import android.text.style.CharacterStyle;
32import android.text.style.ForegroundColorSpan;
33import android.text.style.RelativeSizeSpan;
34import android.text.style.RelativeSizeSpan;
35import android.text.style.StyleSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070036import android.util.AttributeSet;
37import android.view.View;
38import android.widget.TextView;
39
40import java.text.SimpleDateFormat;
41import java.util.Calendar;
42import java.util.TimeZone;
43
44import com.android.internal.R;
45
46/**
47 * This widget display an analogic clock with two hands for hours and
48 * minutes.
49 */
50public class Clock extends TextView {
Joe Onorato263700d2010-05-14 11:54:53 -070051 private boolean mAttached;
52 private Calendar mCalendar;
53 private String mClockFormatString;
54 private SimpleDateFormat mClockFormat;
55
Daniel Sandler87937db2010-05-27 13:44:11 -040056 private static final int AM_PM_STYLE_NORMAL = 0;
57 private static final int AM_PM_STYLE_SMALL = 1;
58 private static final int AM_PM_STYLE_GONE = 2;
59
60 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
61
Joe Onorato263700d2010-05-14 11:54:53 -070062 public Clock(Context context) {
63 this(context, null);
64 }
65
66 public Clock(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
70 public Clock(Context context, AttributeSet attrs, int defStyle) {
71 super(context, attrs, defStyle);
72 }
73
74 @Override
75 protected void onAttachedToWindow() {
76 super.onAttachedToWindow();
77
78 if (!mAttached) {
79 mAttached = true;
80 IntentFilter filter = new IntentFilter();
81
82 filter.addAction(Intent.ACTION_TIME_TICK);
83 filter.addAction(Intent.ACTION_TIME_CHANGED);
84 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
85 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
86
Joe Onorato1a86dd12010-06-01 08:17:58 -070087 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070088 }
89
90 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
91 // in the main thread, therefore the receiver can't run before this method returns.
92
93 // The time zone may have changed while the receiver wasn't registered, so update the Time
94 mCalendar = Calendar.getInstance(TimeZone.getDefault());
95
96 // Make sure we update to the current time
97 updateClock();
98 }
99
100 @Override
101 protected void onDetachedFromWindow() {
102 super.onDetachedFromWindow();
103 if (mAttached) {
104 getContext().unregisterReceiver(mIntentReceiver);
105 mAttached = false;
106 }
107 }
108
109 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
110 @Override
111 public void onReceive(Context context, Intent intent) {
112 String action = intent.getAction();
113 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
114 String tz = intent.getStringExtra("time-zone");
115 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
116 if (mClockFormat != null) {
117 mClockFormat.setTimeZone(mCalendar.getTimeZone());
118 }
119 }
120 updateClock();
121 }
122 };
123
124 final void updateClock() {
125 mCalendar.setTimeInMillis(System.currentTimeMillis());
126 setText(getSmallTime());
127 }
128
129 private final CharSequence getSmallTime() {
130 Context context = getContext();
131 boolean b24 = DateFormat.is24HourFormat(context);
132 int res;
133
134 if (b24) {
135 res = R.string.twenty_four_hour_time_format;
136 } else {
137 res = R.string.twelve_hour_time_format;
138 }
139
140 final char MAGIC1 = '\uEF00';
141 final char MAGIC2 = '\uEF01';
142
143 SimpleDateFormat sdf;
144 String format = context.getString(res);
145 if (!format.equals(mClockFormatString)) {
146 /*
147 * Search for an unquoted "a" in the format string, so we can
148 * add dummy characters around it to let us find it again after
149 * formatting and change its size.
150 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400151 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
152 int a = -1;
153 boolean quoted = false;
154 for (int i = 0; i < format.length(); i++) {
155 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700156
Daniel Sandler87937db2010-05-27 13:44:11 -0400157 if (c == '\'') {
158 quoted = !quoted;
159 }
160 if (!quoted && c == 'a') {
161 a = i;
162 break;
163 }
Joe Onorato263700d2010-05-14 11:54:53 -0700164 }
165
Daniel Sandler87937db2010-05-27 13:44:11 -0400166 if (a >= 0) {
167 // Move a back so any whitespace before AM/PM is also in the alternate size.
168 final int b = a;
169 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
170 a--;
171 }
172 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700173 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400174 }
Joe Onorato263700d2010-05-14 11:54:53 -0700175 }
176
177 mClockFormat = sdf = new SimpleDateFormat(format);
178 mClockFormatString = format;
179 } else {
180 sdf = mClockFormat;
181 }
182 String result = sdf.format(mCalendar.getTime());
183
Daniel Sandler87937db2010-05-27 13:44:11 -0400184 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
185 int magic1 = result.indexOf(MAGIC1);
186 int magic2 = result.indexOf(MAGIC2);
187 if (magic1 >= 0 && magic2 > magic1) {
188 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
189 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
190 formatted.delete(magic1, magic2+1);
191 } else {
192 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
193 CharacterStyle style = new RelativeSizeSpan(0.7f);
194 formatted.setSpan(style, magic1, magic2,
195 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
196 }
197 formatted.delete(magic2, magic2 + 1);
198 formatted.delete(magic1, magic1 + 1);
199 }
200 return formatted;
201 }
Joe Onorato263700d2010-05-14 11:54:53 -0700202 }
Daniel Sandler87937db2010-05-27 13:44:11 -0400203
204 return result;
205
Joe Onorato263700d2010-05-14 11:54:53 -0700206 }
207}
208