blob: bff6cdad74bdf6831ceb061fa4b93ba1c27a14bd [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
Elliott Hughes4caba612013-01-14 15:48:27 -080046import libcore.icu.LocaleData;
47
Joe Onorato263700d2010-05-14 11:54:53 -070048import com.android.internal.R;
49
50/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050051 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070052 */
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;
Daniel Sandler43b23c62012-11-29 11:35:02 -050058 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070059
Daniel Sandler87937db2010-05-27 13:44:11 -040060 private static final int AM_PM_STYLE_NORMAL = 0;
61 private static final int AM_PM_STYLE_SMALL = 1;
62 private static final int AM_PM_STYLE_GONE = 2;
63
64 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
65
Joe Onorato263700d2010-05-14 11:54:53 -070066 public Clock(Context context) {
67 this(context, null);
68 }
69
70 public Clock(Context context, AttributeSet attrs) {
71 this(context, attrs, 0);
72 }
73
74 public Clock(Context context, AttributeSet attrs, int defStyle) {
75 super(context, attrs, defStyle);
76 }
77
78 @Override
79 protected void onAttachedToWindow() {
80 super.onAttachedToWindow();
81
82 if (!mAttached) {
83 mAttached = true;
84 IntentFilter filter = new IntentFilter();
85
86 filter.addAction(Intent.ACTION_TIME_TICK);
87 filter.addAction(Intent.ACTION_TIME_CHANGED);
88 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
89 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050090 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070091
Joe Onorato1a86dd12010-06-01 08:17:58 -070092 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070093 }
94
95 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
96 // in the main thread, therefore the receiver can't run before this method returns.
97
98 // The time zone may have changed while the receiver wasn't registered, so update the Time
99 mCalendar = Calendar.getInstance(TimeZone.getDefault());
100
101 // Make sure we update to the current time
102 updateClock();
103 }
104
105 @Override
106 protected void onDetachedFromWindow() {
107 super.onDetachedFromWindow();
108 if (mAttached) {
109 getContext().unregisterReceiver(mIntentReceiver);
110 mAttached = false;
111 }
112 }
113
114 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
115 @Override
116 public void onReceive(Context context, Intent intent) {
117 String action = intent.getAction();
118 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
119 String tz = intent.getStringExtra("time-zone");
120 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
121 if (mClockFormat != null) {
122 mClockFormat.setTimeZone(mCalendar.getTimeZone());
123 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500124 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
125 final Locale newLocale = getResources().getConfiguration().locale;
126 if (! newLocale.equals(mLocale)) {
127 mLocale = newLocale;
128 mClockFormatString = ""; // force refresh
129 }
Joe Onorato263700d2010-05-14 11:54:53 -0700130 }
131 updateClock();
132 }
133 };
134
135 final void updateClock() {
136 mCalendar.setTimeInMillis(System.currentTimeMillis());
137 setText(getSmallTime());
138 }
139
140 private final CharSequence getSmallTime() {
141 Context context = getContext();
Elliott Hughes4caba612013-01-14 15:48:27 -0800142 boolean is24 = DateFormat.is24HourFormat(context);
143 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700144
145 final char MAGIC1 = '\uEF00';
146 final char MAGIC2 = '\uEF01';
147
148 SimpleDateFormat sdf;
Elliott Hughes4caba612013-01-14 15:48:27 -0800149 String format = is24 ? d.timeFormat24 : d.timeFormat12;
Joe Onorato263700d2010-05-14 11:54:53 -0700150 if (!format.equals(mClockFormatString)) {
151 /*
152 * Search for an unquoted "a" in the format string, so we can
153 * add dummy characters around it to let us find it again after
154 * formatting and change its size.
155 */
Daniel Sandler87937db2010-05-27 13:44:11 -0400156 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
157 int a = -1;
158 boolean quoted = false;
159 for (int i = 0; i < format.length(); i++) {
160 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700161
Daniel Sandler87937db2010-05-27 13:44:11 -0400162 if (c == '\'') {
163 quoted = !quoted;
164 }
165 if (!quoted && c == 'a') {
166 a = i;
167 break;
168 }
Joe Onorato263700d2010-05-14 11:54:53 -0700169 }
170
Daniel Sandler87937db2010-05-27 13:44:11 -0400171 if (a >= 0) {
172 // Move a back so any whitespace before AM/PM is also in the alternate size.
173 final int b = a;
174 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
175 a--;
176 }
177 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700178 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400179 }
Joe Onorato263700d2010-05-14 11:54:53 -0700180 }
Joe Onorato263700d2010-05-14 11:54:53 -0700181 mClockFormat = sdf = new SimpleDateFormat(format);
182 mClockFormatString = format;
183 } else {
184 sdf = mClockFormat;
185 }
186 String result = sdf.format(mCalendar.getTime());
187
Daniel Sandler87937db2010-05-27 13:44:11 -0400188 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
189 int magic1 = result.indexOf(MAGIC1);
190 int magic2 = result.indexOf(MAGIC2);
191 if (magic1 >= 0 && magic2 > magic1) {
192 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
193 if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
194 formatted.delete(magic1, magic2+1);
195 } else {
196 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
197 CharacterStyle style = new RelativeSizeSpan(0.7f);
198 formatted.setSpan(style, magic1, magic2,
199 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
200 }
201 formatted.delete(magic2, magic2 + 1);
202 formatted.delete(magic1, magic1 + 1);
203 }
204 return formatted;
205 }
Joe Onorato263700d2010-05-14 11:54:53 -0700206 }
Daniel Sandler87937db2010-05-27 13:44:11 -0400207
208 return result;
209
Joe Onorato263700d2010-05-14 11:54:53 -0700210 }
211}
212