blob: e41de47f8c1229441137fabee52ed088506fde5c [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;
Daniel Sandler43b23c62012-11-29 11:35:02 -050043import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070044import java.util.TimeZone;
45
46import com.android.internal.R;
47
48/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050049 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070050 */
51public class Clock extends TextView {
Joe Onorato263700d2010-05-14 11:54:53 -070052 private boolean mAttached;
53 private Calendar mCalendar;
54 private String mClockFormatString;
55 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050056 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070057
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);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050088 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070089
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 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500122 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
123 final Locale newLocale = getResources().getConfiguration().locale;
124 if (! newLocale.equals(mLocale)) {
125 mLocale = newLocale;
126 mClockFormatString = ""; // force refresh
127 }
Joe Onorato263700d2010-05-14 11:54:53 -0700128 }
129 updateClock();
130 }
131 };
132
133 final void updateClock() {
134 mCalendar.setTimeInMillis(System.currentTimeMillis());
135 setText(getSmallTime());
136 }
137
138 private final CharSequence getSmallTime() {
139 Context context = getContext();
140 boolean b24 = DateFormat.is24HourFormat(context);
141 int res;
142
143 if (b24) {
144 res = R.string.twenty_four_hour_time_format;
145 } else {
146 res = R.string.twelve_hour_time_format;
147 }
148
149 final char MAGIC1 = '\uEF00';
150 final char MAGIC2 = '\uEF01';
151
152 SimpleDateFormat sdf;
153 String format = context.getString(res);
154 if (!format.equals(mClockFormatString)) {
155 /*
156 * Search for an unquoted "a" in the format string, so we can
157 * add dummy characters around it to let us find it again after
158 * formatting and change its size.
159 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400160 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
161 int a = -1;
162 boolean quoted = false;
163 for (int i = 0; i < format.length(); i++) {
164 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700165
Daniel Sandler87937db2010-05-27 13:44:11 -0400166 if (c == '\'') {
167 quoted = !quoted;
168 }
169 if (!quoted && c == 'a') {
170 a = i;
171 break;
172 }
Joe Onorato263700d2010-05-14 11:54:53 -0700173 }
174
Daniel Sandler87937db2010-05-27 13:44:11 -0400175 if (a >= 0) {
176 // Move a back so any whitespace before AM/PM is also in the alternate size.
177 final int b = a;
178 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
179 a--;
180 }
181 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700182 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400183 }
Joe Onorato263700d2010-05-14 11:54:53 -0700184 }
Joe Onorato263700d2010-05-14 11:54:53 -0700185 mClockFormat = sdf = new SimpleDateFormat(format);
186 mClockFormatString = format;
187 } else {
188 sdf = mClockFormat;
189 }
190 String result = sdf.format(mCalendar.getTime());
191
Daniel Sandler87937db2010-05-27 13:44:11 -0400192 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
193 int magic1 = result.indexOf(MAGIC1);
194 int magic2 = result.indexOf(MAGIC2);
195 if (magic1 >= 0 && magic2 > magic1) {
196 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
197 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
198 formatted.delete(magic1, magic2+1);
199 } else {
200 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
201 CharacterStyle style = new RelativeSizeSpan(0.7f);
202 formatted.setSpan(style, magic1, magic2,
203 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
204 }
205 formatted.delete(magic2, magic2 + 1);
206 formatted.delete(magic1, magic1 + 1);
207 }
208 return formatted;
209 }
Joe Onorato263700d2010-05-14 11:54:53 -0700210 }
Daniel Sandler87937db2010-05-27 13:44:11 -0400211
212 return result;
213
Joe Onorato263700d2010-05-14 11:54:53 -0700214 }
215}
216