blob: 61986adc1d6582de928610e55c8dbd31d3613075 [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
Selim Cinek9c4a7072014-11-21 17:44:34 +010019import android.app.ActivityManager;
Daniel Sandler87937db2010-05-27 13:44:11 -040020import android.content.BroadcastReceiver;
Joe Onorato263700d2010-05-14 11:54:53 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Jorim Jaggi740beb52014-05-11 18:53:19 +020024import android.content.res.TypedArray;
John Spurlock3c875662013-08-31 15:07:25 -040025import android.os.Bundle;
Selim Cinek9c4a7072014-11-21 17:44:34 +010026import android.os.UserHandle;
Joe Onorato263700d2010-05-14 11:54:53 -070027import android.text.Spannable;
28import android.text.SpannableStringBuilder;
Daniel Sandler87937db2010-05-27 13:44:11 -040029import android.text.format.DateFormat;
30import android.text.style.CharacterStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040031import android.text.style.RelativeSizeSpan;
Joe Onorato263700d2010-05-14 11:54:53 -070032import android.util.AttributeSet;
Joe Onorato263700d2010-05-14 11:54:53 -070033import android.widget.TextView;
34
John Spurlock3c875662013-08-31 15:07:25 -040035import com.android.systemui.DemoMode;
Jorim Jaggi740beb52014-05-11 18:53:19 +020036import com.android.systemui.R;
John Spurlock3c875662013-08-31 15:07:25 -040037
Joe Onorato263700d2010-05-14 11:54:53 -070038import java.text.SimpleDateFormat;
39import java.util.Calendar;
Daniel Sandler43b23c62012-11-29 11:35:02 -050040import java.util.Locale;
Joe Onorato263700d2010-05-14 11:54:53 -070041import java.util.TimeZone;
42
Elliott Hughes4caba612013-01-14 15:48:27 -080043import libcore.icu.LocaleData;
44
Joe Onorato263700d2010-05-14 11:54:53 -070045/**
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050046 * Digital clock for the status bar.
Joe Onorato263700d2010-05-14 11:54:53 -070047 */
John Spurlock3c875662013-08-31 15:07:25 -040048public class Clock extends TextView implements DemoMode {
Joe Onorato263700d2010-05-14 11:54:53 -070049 private boolean mAttached;
50 private Calendar mCalendar;
51 private String mClockFormatString;
52 private SimpleDateFormat mClockFormat;
Daniel Sandler43b23c62012-11-29 11:35:02 -050053 private Locale mLocale;
Joe Onorato263700d2010-05-14 11:54:53 -070054
Daniel Sandler87937db2010-05-27 13:44:11 -040055 private static final int AM_PM_STYLE_NORMAL = 0;
56 private static final int AM_PM_STYLE_SMALL = 1;
57 private static final int AM_PM_STYLE_GONE = 2;
58
Jorim Jaggi740beb52014-05-11 18:53:19 +020059 private final int mAmPmStyle;
Daniel Sandler87937db2010-05-27 13:44:11 -040060
Joe Onorato263700d2010-05-14 11:54:53 -070061 public Clock(Context context) {
62 this(context, null);
63 }
64
65 public Clock(Context context, AttributeSet attrs) {
66 this(context, attrs, 0);
67 }
68
69 public Clock(Context context, AttributeSet attrs, int defStyle) {
70 super(context, attrs, defStyle);
Jorim Jaggi740beb52014-05-11 18:53:19 +020071 TypedArray a = context.getTheme().obtainStyledAttributes(
72 attrs,
73 R.styleable.Clock,
74 0, 0);
75 try {
76 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
77 } finally {
78 a.recycle();
79 }
Joe Onorato263700d2010-05-14 11:54:53 -070080 }
81
82 @Override
83 protected void onAttachedToWindow() {
84 super.onAttachedToWindow();
85
86 if (!mAttached) {
87 mAttached = true;
88 IntentFilter filter = new IntentFilter();
89
90 filter.addAction(Intent.ACTION_TIME_TICK);
91 filter.addAction(Intent.ACTION_TIME_CHANGED);
92 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
93 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlerce4f5e52012-11-19 14:47:18 -050094 filter.addAction(Intent.ACTION_USER_SWITCHED);
Joe Onorato263700d2010-05-14 11:54:53 -070095
Selim Cinek9c4a7072014-11-21 17:44:34 +010096 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
97 null, getHandler());
Joe Onorato263700d2010-05-14 11:54:53 -070098 }
99
100 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
101 // in the main thread, therefore the receiver can't run before this method returns.
102
103 // The time zone may have changed while the receiver wasn't registered, so update the Time
104 mCalendar = Calendar.getInstance(TimeZone.getDefault());
105
106 // Make sure we update to the current time
107 updateClock();
108 }
109
110 @Override
111 protected void onDetachedFromWindow() {
112 super.onDetachedFromWindow();
113 if (mAttached) {
114 getContext().unregisterReceiver(mIntentReceiver);
115 mAttached = false;
116 }
117 }
118
119 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
120 @Override
121 public void onReceive(Context context, Intent intent) {
122 String action = intent.getAction();
123 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
124 String tz = intent.getStringExtra("time-zone");
125 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
126 if (mClockFormat != null) {
127 mClockFormat.setTimeZone(mCalendar.getTimeZone());
128 }
Daniel Sandler43b23c62012-11-29 11:35:02 -0500129 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
130 final Locale newLocale = getResources().getConfiguration().locale;
131 if (! newLocale.equals(mLocale)) {
132 mLocale = newLocale;
133 mClockFormatString = ""; // force refresh
134 }
Joe Onorato263700d2010-05-14 11:54:53 -0700135 }
136 updateClock();
137 }
138 };
139
140 final void updateClock() {
John Spurlock3c875662013-08-31 15:07:25 -0400141 if (mDemoMode) return;
Joe Onorato263700d2010-05-14 11:54:53 -0700142 mCalendar.setTimeInMillis(System.currentTimeMillis());
143 setText(getSmallTime());
144 }
145
146 private final CharSequence getSmallTime() {
147 Context context = getContext();
Selim Cinek9c4a7072014-11-21 17:44:34 +0100148 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
Elliott Hughes4caba612013-01-14 15:48:27 -0800149 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
Joe Onorato263700d2010-05-14 11:54:53 -0700150
151 final char MAGIC1 = '\uEF00';
152 final char MAGIC2 = '\uEF01';
153
154 SimpleDateFormat sdf;
Elliott Hughesf7d5e0a2014-10-23 11:06:43 -0700155 String format = is24 ? d.timeFormat_Hm : d.timeFormat_hm;
Joe Onorato263700d2010-05-14 11:54:53 -0700156 if (!format.equals(mClockFormatString)) {
157 /*
158 * Search for an unquoted "a" in the format string, so we can
159 * add dummy characters around it to let us find it again after
160 * formatting and change its size.
161 */
Jorim Jaggi740beb52014-05-11 18:53:19 +0200162 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400163 int a = -1;
164 boolean quoted = false;
165 for (int i = 0; i < format.length(); i++) {
166 char c = format.charAt(i);
Joe Onorato263700d2010-05-14 11:54:53 -0700167
Daniel Sandler87937db2010-05-27 13:44:11 -0400168 if (c == '\'') {
169 quoted = !quoted;
170 }
171 if (!quoted && c == 'a') {
172 a = i;
173 break;
174 }
Joe Onorato263700d2010-05-14 11:54:53 -0700175 }
176
Daniel Sandler87937db2010-05-27 13:44:11 -0400177 if (a >= 0) {
178 // Move a back so any whitespace before AM/PM is also in the alternate size.
179 final int b = a;
180 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
181 a--;
182 }
183 format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
Joe Onorato263700d2010-05-14 11:54:53 -0700184 + "a" + MAGIC2 + format.substring(b + 1);
Daniel Sandler87937db2010-05-27 13:44:11 -0400185 }
Joe Onorato263700d2010-05-14 11:54:53 -0700186 }
Joe Onorato263700d2010-05-14 11:54:53 -0700187 mClockFormat = sdf = new SimpleDateFormat(format);
188 mClockFormatString = format;
189 } else {
190 sdf = mClockFormat;
191 }
192 String result = sdf.format(mCalendar.getTime());
193
Jorim Jaggi740beb52014-05-11 18:53:19 +0200194 if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400195 int magic1 = result.indexOf(MAGIC1);
196 int magic2 = result.indexOf(MAGIC2);
197 if (magic1 >= 0 && magic2 > magic1) {
198 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
Jorim Jaggi740beb52014-05-11 18:53:19 +0200199 if (mAmPmStyle == AM_PM_STYLE_GONE) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400200 formatted.delete(magic1, magic2+1);
201 } else {
Jorim Jaggi740beb52014-05-11 18:53:19 +0200202 if (mAmPmStyle == AM_PM_STYLE_SMALL) {
Daniel Sandler87937db2010-05-27 13:44:11 -0400203 CharacterStyle style = new RelativeSizeSpan(0.7f);
204 formatted.setSpan(style, magic1, magic2,
205 Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
206 }
207 formatted.delete(magic2, magic2 + 1);
208 formatted.delete(magic1, magic1 + 1);
209 }
210 return formatted;
211 }
Joe Onorato263700d2010-05-14 11:54:53 -0700212 }
John Spurlock209bede2013-07-17 12:23:27 -0400213
Daniel Sandler87937db2010-05-27 13:44:11 -0400214 return result;
215
Joe Onorato263700d2010-05-14 11:54:53 -0700216 }
John Spurlock3c875662013-08-31 15:07:25 -0400217
218 private boolean mDemoMode;
219
220 @Override
221 public void dispatchDemoCommand(String command, Bundle args) {
222 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
223 mDemoMode = true;
224 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
225 mDemoMode = false;
226 updateClock();
227 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
228 String millis = args.getString("millis");
229 String hhmm = args.getString("hhmm");
230 if (millis != null) {
231 mCalendar.setTimeInMillis(Long.parseLong(millis));
232 } else if (hhmm != null && hhmm.length() == 4) {
233 int hh = Integer.parseInt(hhmm.substring(0, 2));
234 int mm = Integer.parseInt(hhmm.substring(2));
Julia Reynoldsc11d8382015-07-16 14:40:37 -0400235 boolean is24 = DateFormat.is24HourFormat(
236 getContext(), ActivityManager.getCurrentUser());
237 if (is24) {
238 mCalendar.set(Calendar.HOUR_OF_DAY, hh);
239 } else {
240 mCalendar.set(Calendar.HOUR, hh);
241 }
John Spurlock3c875662013-08-31 15:07:25 -0400242 mCalendar.set(Calendar.MINUTE, mm);
243 }
244 setText(getSmallTime());
245 }
246 }
Joe Onorato263700d2010-05-14 11:54:53 -0700247}
248