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; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame^] | 43 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 44 | import java.util.TimeZone; |
| 45 | |
| 46 | import com.android.internal.R; |
| 47 | |
| 48 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 49 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 50 | */ |
| 51 | public class Clock extends TextView { |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 52 | private boolean mAttached; |
| 53 | private Calendar mCalendar; |
| 54 | private String mClockFormatString; |
| 55 | private SimpleDateFormat mClockFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame^] | 56 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 57 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 58 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 59 | private static final int AM_PM_STYLE_SMALL = 1; |
| 60 | private static final int AM_PM_STYLE_GONE = 2; |
| 61 | |
| 62 | private static final int AM_PM_STYLE = AM_PM_STYLE_GONE; |
| 63 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 64 | public Clock(Context context) { |
| 65 | this(context, null); |
| 66 | } |
| 67 | |
| 68 | public Clock(Context context, AttributeSet attrs) { |
| 69 | this(context, attrs, 0); |
| 70 | } |
| 71 | |
| 72 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
| 73 | super(context, attrs, defStyle); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onAttachedToWindow() { |
| 78 | super.onAttachedToWindow(); |
| 79 | |
| 80 | if (!mAttached) { |
| 81 | mAttached = true; |
| 82 | IntentFilter filter = new IntentFilter(); |
| 83 | |
| 84 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 85 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 86 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 87 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 88 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 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 | } |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame^] | 122 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 123 | final Locale newLocale = getResources().getConfiguration().locale; |
| 124 | if (! newLocale.equals(mLocale)) { |
| 125 | mLocale = newLocale; |
| 126 | mClockFormatString = ""; // force refresh |
| 127 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 128 | } |
| 129 | updateClock(); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | final void updateClock() { |
| 134 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 135 | setText(getSmallTime()); |
| 136 | } |
| 137 | |
| 138 | private final CharSequence getSmallTime() { |
| 139 | Context context = getContext(); |
| 140 | boolean b24 = DateFormat.is24HourFormat(context); |
| 141 | int res; |
| 142 | |
| 143 | if (b24) { |
| 144 | res = R.string.twenty_four_hour_time_format; |
| 145 | } else { |
| 146 | res = R.string.twelve_hour_time_format; |
| 147 | } |
| 148 | |
| 149 | final char MAGIC1 = '\uEF00'; |
| 150 | final char MAGIC2 = '\uEF01'; |
| 151 | |
| 152 | SimpleDateFormat sdf; |
| 153 | String format = context.getString(res); |
| 154 | if (!format.equals(mClockFormatString)) { |
| 155 | /* |
| 156 | * Search for an unquoted "a" in the format string, so we can |
| 157 | * add dummy characters around it to let us find it again after |
| 158 | * formatting and change its size. |
| 159 | */ |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 160 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 161 | int a = -1; |
| 162 | boolean quoted = false; |
| 163 | for (int i = 0; i < format.length(); i++) { |
| 164 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 165 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 166 | if (c == '\'') { |
| 167 | quoted = !quoted; |
| 168 | } |
| 169 | if (!quoted && c == 'a') { |
| 170 | a = i; |
| 171 | break; |
| 172 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 175 | if (a >= 0) { |
| 176 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 177 | final int b = a; |
| 178 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 179 | a--; |
| 180 | } |
| 181 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 182 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 183 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 184 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 185 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 186 | mClockFormatString = format; |
| 187 | } else { |
| 188 | sdf = mClockFormat; |
| 189 | } |
| 190 | String result = sdf.format(mCalendar.getTime()); |
| 191 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 192 | if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { |
| 193 | int magic1 = result.indexOf(MAGIC1); |
| 194 | int magic2 = result.indexOf(MAGIC2); |
| 195 | if (magic1 >= 0 && magic2 > magic1) { |
| 196 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
| 197 | if (AM_PM_STYLE == AM_PM_STYLE_GONE) { |
| 198 | formatted.delete(magic1, magic2+1); |
| 199 | } else { |
| 200 | if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { |
| 201 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 202 | formatted.setSpan(style, magic1, magic2, |
| 203 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 204 | } |
| 205 | formatted.delete(magic2, magic2 + 1); |
| 206 | formatted.delete(magic1, magic1 + 1); |
| 207 | } |
| 208 | return formatted; |
| 209 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 210 | } |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 211 | |
| 212 | return result; |
| 213 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |