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