blob: c9448084bbce9e7b485c969c032e937bbbf352e4 [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 Onorato79de0c52010-05-26 17:03:26 -040017package com.android.systemui.statusbar;
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;
28import android.os.Handler;
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;
33import android.text.style.ForegroundColorSpan;
34import android.text.style.RelativeSizeSpan;
35import android.text.style.RelativeSizeSpan;
36import android.text.style.StyleSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070037import android.util.AttributeSet;
38import android.view.View;
39import android.widget.TextView;
40
41import java.text.SimpleDateFormat;
42import java.util.Calendar;
43import java.util.TimeZone;
44
45import com.android.internal.R;
46
47/**
48 * This widget display an analogic clock with two hands for hours and
49 * minutes.
50 */
51public class Clock extends TextView {
52 private final Handler mHandler = new Handler();
53 private boolean mAttached;
54 private Calendar mCalendar;
55 private String mClockFormatString;
56 private SimpleDateFormat mClockFormat;
57
Daniel Sandler87937db2010-05-27 13:44:11 -040058 private static final int AM_PM_STYLE_NORMAL = 0;
59 private static final int AM_PM_STYLE_SMALL = 1;
60 private static final int AM_PM_STYLE_GONE = 2;
61
62 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
63
Joe Onorato263700d2010-05-14 11:54:53 -070064 public Clock(Context context) {
65 this(context, null);
66 }
67
68 public Clock(Context context, AttributeSet attrs) {
69 this(context, attrs, 0);
70 }
71
72 public Clock(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
74 }
75
76 @Override
77 protected void onAttachedToWindow() {
78 super.onAttachedToWindow();
79
80 if (!mAttached) {
81 mAttached = true;
82 IntentFilter filter = new IntentFilter();
83
84 filter.addAction(Intent.ACTION_TIME_TICK);
85 filter.addAction(Intent.ACTION_TIME_CHANGED);
86 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
87 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
88
89 getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
90 }
91
92 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
93 // in the main thread, therefore the receiver can't run before this method returns.
94
95 // The time zone may have changed while the receiver wasn't registered, so update the Time
96 mCalendar = Calendar.getInstance(TimeZone.getDefault());
97
98 // Make sure we update to the current time
99 updateClock();
100 }
101
102 @Override
103 protected void onDetachedFromWindow() {
104 super.onDetachedFromWindow();
105 if (mAttached) {
106 getContext().unregisterReceiver(mIntentReceiver);
107 mAttached = false;
108 }
109 }
110
111 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
112 @Override
113 public void onReceive(Context context, Intent intent) {
114 String action = intent.getAction();
115 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
116 String tz = intent.getStringExtra("time-zone");
117 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
118 if (mClockFormat != null) {
119 mClockFormat.setTimeZone(mCalendar.getTimeZone());
120 }
121 }
122 updateClock();
123 }
124 };
125
126 final void updateClock() {
127 mCalendar.setTimeInMillis(System.currentTimeMillis());
128 setText(getSmallTime());
129 }
130
131 private final CharSequence getSmallTime() {
132 Context context = getContext();
133 boolean b24 = DateFormat.is24HourFormat(context);
134 int res;
135
136 if (b24) {
137 res = R.string.twenty_four_hour_time_format;
138 } else {
139 res = R.string.twelve_hour_time_format;
140 }
141
142 final char MAGIC1 = '\uEF00';
143 final char MAGIC2 = '\uEF01';
144
145 SimpleDateFormat sdf;
146 String format = context.getString(res);
147 if (!format.equals(mClockFormatString)) {
148 /*
149 * Search for an unquoted "a" in the format string, so we can
150 * add dummy characters around it to let us find it again after
151 * formatting and change its size.
152 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400153 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
154 int a = -1;
155 boolean quoted = false;
156 for (int i = 0; i < format.length(); i++) {
157 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700158
Daniel Sandler87937db2010-05-27 13:44:11 -0400159 if (c == '\'') {
160 quoted = !quoted;
161 }
162 if (!quoted && c == 'a') {
163 a = i;
164 break;
165 }
Joe Onorato263700d2010-05-14 11:54:53 -0700166 }
167
Daniel Sandler87937db2010-05-27 13:44:11 -0400168 if (a >= 0) {
169 // Move a back so any whitespace before AM/PM is also in the alternate size.
170 final int b = a;
171 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
172 a--;
173 }
174 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700175 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400176 }
Joe Onorato263700d2010-05-14 11:54:53 -0700177 }
178
179 mClockFormat = sdf = new SimpleDateFormat(format);
180 mClockFormatString = format;
181 } else {
182 sdf = mClockFormat;
183 }
184 String result = sdf.format(mCalendar.getTime());
185
Daniel Sandler87937db2010-05-27 13:44:11 -0400186 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
187 int magic1 = result.indexOf(MAGIC1);
188 int magic2 = result.indexOf(MAGIC2);
189 if (magic1 >= 0 && magic2 > magic1) {
190 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
191 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
192 formatted.delete(magic1, magic2+1);
193 } else {
194 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
195 CharacterStyle style = new RelativeSizeSpan(0.7f);
196 formatted.setSpan(style, magic1, magic2,
197 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
198 }
199 formatted.delete(magic2, magic2 + 1);
200 formatted.delete(magic1, magic1 + 1);
201 }
202 return formatted;
203 }
Joe Onorato263700d2010-05-14 11:54:53 -0700204 }
Daniel Sandler87937db2010-05-27 13:44:11 -0400205
206 return result;
207
Joe Onorato263700d2010-05-14 11:54:53 -0700208 }
209}
210