Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.policy; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 18 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 23 | import android.text.Spannable; |
| 24 | import android.text.SpannableStringBuilder; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 25 | import android.text.format.DateFormat; |
| 26 | import android.text.style.CharacterStyle; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 27 | import android.text.style.RelativeSizeSpan; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 29 | import android.widget.TextView; |
| 30 | |
| 31 | import java.text.SimpleDateFormat; |
| 32 | import java.util.Calendar; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 33 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 34 | import java.util.TimeZone; |
| 35 | |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 36 | import libcore.icu.LocaleData; |
| 37 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 38 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 39 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 40 | */ |
| 41 | public class Clock extends TextView { |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 42 | private boolean mAttached; |
| 43 | private Calendar mCalendar; |
| 44 | private String mClockFormatString; |
| 45 | private SimpleDateFormat mClockFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 46 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 47 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 48 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 49 | private static final int AM_PM_STYLE_SMALL = 1; |
| 50 | private static final int AM_PM_STYLE_GONE = 2; |
| 51 | |
| 52 | private static final int AM_PM_STYLE = AM_PM_STYLE_GONE; |
| 53 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 54 | public Clock(Context context) { |
| 55 | this(context, null); |
| 56 | } |
| 57 | |
| 58 | public Clock(Context context, AttributeSet attrs) { |
| 59 | this(context, attrs, 0); |
| 60 | } |
| 61 | |
| 62 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
| 63 | super(context, attrs, defStyle); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onAttachedToWindow() { |
| 68 | super.onAttachedToWindow(); |
| 69 | |
| 70 | if (!mAttached) { |
| 71 | mAttached = true; |
| 72 | IntentFilter filter = new IntentFilter(); |
| 73 | |
| 74 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 75 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 76 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 77 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 78 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 79 | |
Joe Onorato | 1a86dd1 | 2010-06-01 08:17:58 -0700 | [diff] [blame] | 80 | getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // NOTE: It's safe to do these after registering the receiver since the receiver always runs |
| 84 | // in the main thread, therefore the receiver can't run before this method returns. |
| 85 | |
| 86 | // The time zone may have changed while the receiver wasn't registered, so update the Time |
| 87 | mCalendar = Calendar.getInstance(TimeZone.getDefault()); |
| 88 | |
| 89 | // Make sure we update to the current time |
| 90 | updateClock(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected void onDetachedFromWindow() { |
| 95 | super.onDetachedFromWindow(); |
| 96 | if (mAttached) { |
| 97 | getContext().unregisterReceiver(mIntentReceiver); |
| 98 | mAttached = false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 103 | @Override |
| 104 | public void onReceive(Context context, Intent intent) { |
| 105 | String action = intent.getAction(); |
| 106 | if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { |
| 107 | String tz = intent.getStringExtra("time-zone"); |
| 108 | mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); |
| 109 | if (mClockFormat != null) { |
| 110 | mClockFormat.setTimeZone(mCalendar.getTimeZone()); |
| 111 | } |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 112 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 113 | final Locale newLocale = getResources().getConfiguration().locale; |
| 114 | if (! newLocale.equals(mLocale)) { |
| 115 | mLocale = newLocale; |
| 116 | mClockFormatString = ""; // force refresh |
| 117 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 118 | } |
| 119 | updateClock(); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | final void updateClock() { |
| 124 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 125 | setText(getSmallTime()); |
| 126 | } |
| 127 | |
| 128 | private final CharSequence getSmallTime() { |
| 129 | Context context = getContext(); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 130 | boolean is24 = DateFormat.is24HourFormat(context); |
| 131 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 132 | |
| 133 | final char MAGIC1 = '\uEF00'; |
| 134 | final char MAGIC2 = '\uEF01'; |
| 135 | |
| 136 | SimpleDateFormat sdf; |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 137 | String format = is24 ? d.timeFormat24 : d.timeFormat12; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 138 | if (!format.equals(mClockFormatString)) { |
| 139 | /* |
| 140 | * Search for an unquoted "a" in the format string, so we can |
| 141 | * add dummy characters around it to let us find it again after |
| 142 | * formatting and change its size. |
| 143 | */ |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 144 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 145 | int a = -1; |
| 146 | boolean quoted = false; |
| 147 | for (int i = 0; i < format.length(); i++) { |
| 148 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 149 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 150 | if (c == '\'') { |
| 151 | quoted = !quoted; |
| 152 | } |
| 153 | if (!quoted && c == 'a') { |
| 154 | a = i; |
| 155 | break; |
| 156 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 159 | if (a >= 0) { |
| 160 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 161 | final int b = a; |
| 162 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 163 | a--; |
| 164 | } |
| 165 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 166 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 167 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 168 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 169 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 170 | mClockFormatString = format; |
| 171 | } else { |
| 172 | sdf = mClockFormat; |
| 173 | } |
| 174 | String result = sdf.format(mCalendar.getTime()); |
| 175 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 176 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 177 | int magic1 = result.indexOf(MAGIC1); |
| 178 | int magic2 = result.indexOf(MAGIC2); |
| 179 | if (magic1 >= 0 && magic2 > magic1) { |
| 180 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
| 181 | if (AM_PM_STYLE == AM_PM_STYLE_GONE) { |
| 182 | formatted.delete(magic1, magic2+1); |
| 183 | } else { |
| 184 | if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { |
| 185 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 186 | formatted.setSpan(style, magic1, magic2, |
| 187 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 188 | } |
| 189 | formatted.delete(magic2, magic2 + 1); |
| 190 | formatted.delete(magic1, magic1 + 1); |
| 191 | } |
| 192 | return formatted; |
| 193 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 194 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 195 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 196 | return result; |
| 197 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |