blob: 647615325ce315d92a7298dff569d30b7122ba96 [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;
Daniel Sandler08d05e32012-08-08 16:39:54 -040037import android.util.Slog;
Joe Onorato263700d2010-05-14 11:54:53 -070038import android.view.View;
39import android.widget.TextView;
40
41import java.text.SimpleDateFormat;
42import java.util.Calendar;
43import java.util.TimeZone;
44
Elliott Hughes4caba612013-01-14 15:48:27 -080045import libcore.icu.LocaleData;
46
Joe Onorato263700d2010-05-14 11:54:53 -070047import com.android.internal.R;
48
49/**
50 * This widget display an analogic clock with two hands for hours and
51 * minutes.
52 */
53public class Clock extends TextView {
Joe Onorato263700d2010-05-14 11:54:53 -070054 private boolean mAttached;
55 private Calendar mCalendar;
56 private String mClockFormatString;
57 private SimpleDateFormat mClockFormat;
58
Daniel Sandler87937db2010-05-27 13:44:11 -040059 private static final int AM_PM_STYLE_NORMAL = 0;
60 private static final int AM_PM_STYLE_SMALL = 1;
61 private static final int AM_PM_STYLE_GONE = 2;
62
63 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
64
Joe Onorato263700d2010-05-14 11:54:53 -070065 public Clock(Context context) {
66 this(context, null);
67 }
68
69 public Clock(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public Clock(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
75 }
76
77 @Override
78 protected void onAttachedToWindow() {
79 super.onAttachedToWindow();
80
81 if (!mAttached) {
82 mAttached = true;
83 IntentFilter filter = new IntentFilter();
84
85 filter.addAction(Intent.ACTION_TIME_TICK);
86 filter.addAction(Intent.ACTION_TIME_CHANGED);
87 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
88 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
89
Joe Onorato1a86dd12010-06-01 08:17:58 -070090 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070091 }
92
93 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
94 // in the main thread, therefore the receiver can't run before this method returns.
95
96 // The time zone may have changed while the receiver wasn't registered, so update the Time
97 mCalendar = Calendar.getInstance(TimeZone.getDefault());
98
99 // Make sure we update to the current time
100 updateClock();
101 }
102
103 @Override
104 protected void onDetachedFromWindow() {
105 super.onDetachedFromWindow();
106 if (mAttached) {
107 getContext().unregisterReceiver(mIntentReceiver);
108 mAttached = false;
109 }
110 }
111
112 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
113 @Override
114 public void onReceive(Context context, Intent intent) {
115 String action = intent.getAction();
116 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
117 String tz = intent.getStringExtra("time-zone");
118 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
119 if (mClockFormat != null) {
120 mClockFormat.setTimeZone(mCalendar.getTimeZone());
121 }
122 }
123 updateClock();
124 }
125 };
126
127 final void updateClock() {
128 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 }
Daniel Sandler87937db2010-05-27 13:44:11 -0400199
200 return result;
201
Joe Onorato263700d2010-05-14 11:54:53 -0700202 }
203}
204