blob: 55a0bba6a61dbb10a6d6966daf5b60366368b50a [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;
Jorim Jaggi740beb52014-05-11 18:53:19 +020023import android.content.res.TypedArray;
John Spurlock3c875662013-08-31 15:07:25 -040024import android.os.Bundle;
Joe Onorato263700d2010-05-14 11:54:53 -070025import android.text.Spannable;
26import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040027import android.text.format.DateFormat;
28import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040029import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070030import android.util.AttributeSet;
Joe Onorato263700d2010-05-14 11:54:53 -070031import android.widget.TextView;
32
John Spurlock3c875662013-08-31 15:07:25 -040033import com.android.systemui.DemoMode;
Jorim Jaggi740beb52014-05-11 18:53:19 +020034import com.android.systemui.R;
John Spurlock3c875662013-08-31 15:07:25 -040035
Joe Onorato263700d2010-05-14 11:54:53 -070036import java.text.SimpleDateFormat;
37import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050038import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070039import java.util.TimeZone;
40
Elliott Hughes4caba612013-01-14 15:48:27 -080041import libcore.icu.LocaleData;
42
Joe Onorato263700d2010-05-14 11:54:53 -070043/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050044 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070045 */
John Spurlock3c875662013-08-31 15:07:25 -040046public class Clock extends TextView implements DemoMode {
Joe Onorato263700d2010-05-14 11:54:53 -070047 private boolean mAttached;
48 private Calendar mCalendar;
49 private String mClockFormatString;
50 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050051 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070052
Daniel Sandler87937db2010-05-27 13:44:11 -040053 private static final int AM_PM_STYLE_NORMAL = 0;
54 private static final int AM_PM_STYLE_SMALL = 1;
55 private static final int AM_PM_STYLE_GONE = 2;
56
Jorim Jaggi740beb52014-05-11 18:53:19 +020057 private final int mAmPmStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040058
Joe Onorato263700d2010-05-14 11:54:53 -070059 public Clock(Context context) {
60 this(context, null);
61 }
62
63 public Clock(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public Clock(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020069 TypedArray a = context.getTheme().obtainStyledAttributes(
70 attrs,
71 R.styleable.Clock,
72 0, 0);
73 try {
74 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
75 } finally {
76 a.recycle();
77 }
Joe Onorato263700d2010-05-14 11:54:53 -070078 }
79
80 @Override
81 protected void onAttachedToWindow() {
82 super.onAttachedToWindow();
83
84 if (!mAttached) {
85 mAttached = true;
86 IntentFilter filter = new IntentFilter();
87
88 filter.addAction(Intent.ACTION_TIME_TICK);
89 filter.addAction(Intent.ACTION_TIME_CHANGED);
90 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
91 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050092 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070093
Joe Onorato1a86dd12010-06-01 08:17:58 -070094 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070095 }
96
97 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
98 // in the main thread, therefore the receiver can't run before this method returns.
99
100 // The time zone may have changed while the receiver wasn't registered, so update the Time
101 mCalendar = Calendar.getInstance(TimeZone.getDefault());
102
103 // Make sure we update to the current time
104 updateClock();
105 }
106
107 @Override
108 protected void onDetachedFromWindow() {
109 super.onDetachedFromWindow();
110 if (mAttached) {
111 getContext().unregisterReceiver(mIntentReceiver);
112 mAttached = false;
113 }
114 }
115
116 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
117 @Override
118 public void onReceive(Context context, Intent intent) {
119 String action = intent.getAction();
120 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
121 String tz = intent.getStringExtra("time-zone");
122 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
123 if (mClockFormat != null) {
124 mClockFormat.setTimeZone(mCalendar.getTimeZone());
125 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500126 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
127 final Locale newLocale = getResources().getConfiguration().locale;
128 if (! newLocale.equals(mLocale)) {
129 mLocale = newLocale;
130 mClockFormatString = ""; // force refresh
131 }
Joe Onorato263700d2010-05-14 11:54:53 -0700132 }
133 updateClock();
134 }
135 };
136
137 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400138 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700139 mCalendar.setTimeInMillis(System.currentTimeMillis());
140 setText(getSmallTime());
141 }
142
143 private final CharSequence getSmallTime() {
144 Context context = getContext();
Elliott Hughes4caba612013-01-14 15:48:27 -0800145 boolean is24 = DateFormat.is24HourFormat(context);
146 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700147
148 final char MAGIC1 = '\uEF00';
149 final char MAGIC2 = '\uEF01';
150
151 SimpleDateFormat sdf;
Elliott Hughes4caba612013-01-14 15:48:27 -0800152 String format = is24 ? d.timeFormat24 : d.timeFormat12;
Joe Onorato263700d2010-05-14 11:54:53 -0700153 if (!format.equals(mClockFormatString)) {
154 /*
155 * Search for an unquoted "a" in the format string, so we can
156 * add dummy characters around it to let us find it again after
157 * formatting and change its size.
158 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200159 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400160 int a = -1;
161 boolean quoted = false;
162 for (int i = 0; i < format.length(); i++) {
163 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700164
Daniel Sandler87937db2010-05-27 13:44:11 -0400165 if (c == '\'') {
166 quoted = !quoted;
167 }
168 if (!quoted && c == 'a') {
169 a = i;
170 break;
171 }
Joe Onorato263700d2010-05-14 11:54:53 -0700172 }
173
Daniel Sandler87937db2010-05-27 13:44:11 -0400174 if (a >= 0) {
175 // Move a back so any whitespace before AM/PM is also in the alternate size.
176 final int b = a;
177 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
178 a--;
179 }
180 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700181 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400182 }
Joe Onorato263700d2010-05-14 11:54:53 -0700183 }
Joe Onorato263700d2010-05-14 11:54:53 -0700184 mClockFormat = sdf = new SimpleDateFormat(format);
185 mClockFormatString = format;
186 } else {
187 sdf = mClockFormat;
188 }
189 String result = sdf.format(mCalendar.getTime());
190
Jorim Jaggi740beb52014-05-11 18:53:19 +0200191 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400192 int magic1 = result.indexOf(MAGIC1);
193 int magic2 = result.indexOf(MAGIC2);
194 if (magic1 >= 0 && magic2 > magic1) {
195 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200196 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400197 formatted.delete(magic1, magic2+1);
198 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200199 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400200 CharacterStyle style = new RelativeSizeSpan(0.7f);
201 formatted.setSpan(style, magic1, magic2,
202 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
203 }
204 formatted.delete(magic2, magic2 + 1);
205 formatted.delete(magic1, magic1 + 1);
206 }
207 return formatted;
208 }
Joe Onorato263700d2010-05-14 11:54:53 -0700209 }
John Spurlock209bede2013-07-17 12:23:27 -0400210
Daniel Sandler87937db2010-05-27 13:44:11 -0400211 return result;
212
Joe Onorato263700d2010-05-14 11:54:53 -0700213 }
John Spurlock3c875662013-08-31 15:07:25 -0400214
215 private boolean mDemoMode;
216
217 @Override
218 public void dispatchDemoCommand(String command, Bundle args) {
219 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
220 mDemoMode = true;
221 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
222 mDemoMode = false;
223 updateClock();
224 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
225 String millis = args.getString("millis");
226 String hhmm = args.getString("hhmm");
227 if (millis != null) {
228 mCalendar.setTimeInMillis(Long.parseLong(millis));
229 } else if (hhmm != null && hhmm.length() == 4) {
230 int hh = Integer.parseInt(hhmm.substring(0, 2));
231 int mm = Integer.parseInt(hhmm.substring(2));
232 mCalendar.set(Calendar.HOUR, hh);
233 mCalendar.set(Calendar.MINUTE, mm);
234 }
235 setText(getSmallTime());
236 }
237 }
Joe Onorato263700d2010-05-14 11:54:53 -0700238}
239