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.content.res.Resources; |
| 24 | import android.content.res.TypedArray; |
| 25 | import android.graphics.Canvas; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 26 | import android.graphics.Typeface; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 28 | import android.text.Spannable; |
| 29 | import android.text.SpannableStringBuilder; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 30 | import android.text.format.DateFormat; |
| 31 | import android.text.style.CharacterStyle; |
| 32 | import android.text.style.ForegroundColorSpan; |
| 33 | import android.text.style.RelativeSizeSpan; |
| 34 | import android.text.style.RelativeSizeSpan; |
| 35 | import android.text.style.StyleSpan; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 37 | import android.util.Slog; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 38 | import android.view.View; |
| 39 | import android.widget.TextView; |
| 40 | |
| 41 | import java.text.SimpleDateFormat; |
| 42 | import java.util.Calendar; |
| 43 | import java.util.TimeZone; |
| 44 | |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame^] | 45 | import libcore.icu.LocaleData; |
| 46 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 47 | import com.android.internal.R; |
| 48 | |
| 49 | /** |
| 50 | * This widget display an analogic clock with two hands for hours and |
| 51 | * minutes. |
| 52 | */ |
| 53 | public class Clock extends TextView { |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 54 | private boolean mAttached; |
| 55 | private Calendar mCalendar; |
| 56 | private String mClockFormatString; |
| 57 | private SimpleDateFormat mClockFormat; |
| 58 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 59 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 60 | private static final int AM_PM_STYLE_SMALL = 1; |
| 61 | private static final int AM_PM_STYLE_GONE = 2; |
| 62 | |
| 63 | private static final int AM_PM_STYLE = AM_PM_STYLE_GONE; |
| 64 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 65 | public Clock(Context context) { |
| 66 | this(context, null); |
| 67 | } |
| 68 | |
| 69 | public Clock(Context context, AttributeSet attrs) { |
| 70 | this(context, attrs, 0); |
| 71 | } |
| 72 | |
| 73 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onAttachedToWindow() { |
| 79 | super.onAttachedToWindow(); |
| 80 | |
| 81 | if (!mAttached) { |
| 82 | mAttached = true; |
| 83 | IntentFilter filter = new IntentFilter(); |
| 84 | |
| 85 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 86 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 87 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 88 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 89 | |
Joe Onorato | 1a86dd1 | 2010-06-01 08:17:58 -0700 | [diff] [blame] | 90 | getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 91 | } |
| 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 | } |
| 122 | } |
| 123 | updateClock(); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | final void updateClock() { |
| 128 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 129 | setText(getSmallTime()); |
| 130 | } |
| 131 | |
| 132 | private final CharSequence getSmallTime() { |
| 133 | Context context = getContext(); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame^] | 134 | boolean is24 = DateFormat.is24HourFormat(context); |
| 135 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 136 | |
| 137 | final char MAGIC1 = '\uEF00'; |
| 138 | final char MAGIC2 = '\uEF01'; |
| 139 | |
| 140 | SimpleDateFormat sdf; |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame^] | 141 | String format = is24 ? d.timeFormat24 : d.timeFormat12; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 142 | if (!format.equals(mClockFormatString)) { |
| 143 | /* |
| 144 | * Search for an unquoted "a" in the format string, so we can |
| 145 | * add dummy characters around it to let us find it again after |
| 146 | * formatting and change its size. |
| 147 | */ |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 148 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 149 | int a = -1; |
| 150 | boolean quoted = false; |
| 151 | for (int i = 0; i < format.length(); i++) { |
| 152 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 153 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 154 | if (c == '\'') { |
| 155 | quoted = !quoted; |
| 156 | } |
| 157 | if (!quoted && c == 'a') { |
| 158 | a = i; |
| 159 | break; |
| 160 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 163 | if (a >= 0) { |
| 164 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 165 | final int b = a; |
| 166 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 167 | a--; |
| 168 | } |
| 169 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 170 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 171 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 172 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 173 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 174 | mClockFormatString = format; |
| 175 | } else { |
| 176 | sdf = mClockFormat; |
| 177 | } |
| 178 | String result = sdf.format(mCalendar.getTime()); |
| 179 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 180 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 181 | int magic1 = result.indexOf(MAGIC1); |
| 182 | int magic2 = result.indexOf(MAGIC2); |
| 183 | if (magic1 >= 0 && magic2 > magic1) { |
| 184 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
| 185 | if (AM_PM_STYLE == AM_PM_STYLE_GONE) { |
| 186 | formatted.delete(magic1, magic2+1); |
| 187 | } else { |
| 188 | if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { |
| 189 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 190 | formatted.setSpan(style, magic1, magic2, |
| 191 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 192 | } |
| 193 | formatted.delete(magic2, magic2 + 1); |
| 194 | formatted.delete(magic1, magic1 + 1); |
| 195 | } |
| 196 | return formatted; |
| 197 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 198 | } |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 199 | |
| 200 | return result; |
| 201 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |