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 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 25 | import android.os.Bundle; |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 26 | import android.os.UserHandle; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 27 | import android.text.Spannable; |
| 28 | import android.text.SpannableStringBuilder; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 29 | import android.text.format.DateFormat; |
| 30 | import android.text.style.CharacterStyle; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 31 | import android.text.style.RelativeSizeSpan; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 35 | import com.android.systemui.DemoMode; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 36 | import com.android.systemui.R; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 37 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 38 | import java.text.SimpleDateFormat; |
| 39 | import java.util.Calendar; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 40 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 41 | import java.util.TimeZone; |
| 42 | |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 43 | import libcore.icu.LocaleData; |
| 44 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 45 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 46 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 47 | */ |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 48 | public class Clock extends TextView implements DemoMode { |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 49 | private boolean mAttached; |
| 50 | private Calendar mCalendar; |
| 51 | private String mClockFormatString; |
| 52 | private SimpleDateFormat mClockFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 53 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 54 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 55 | 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 Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 59 | private final int mAmPmStyle; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 60 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 61 | 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 Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 71 | 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 Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 80 | } |
| 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 Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 94 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 95 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 96 | getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, |
| 97 | null, getHandler()); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 98 | } |
| 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 Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 129 | } 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 Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 135 | } |
| 136 | updateClock(); |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | final void updateClock() { |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 141 | if (mDemoMode) return; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 142 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 143 | setText(getSmallTime()); |
| 144 | } |
| 145 | |
| 146 | private final CharSequence getSmallTime() { |
| 147 | Context context = getContext(); |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 148 | boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser()); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 149 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 150 | |
| 151 | final char MAGIC1 = '\uEF00'; |
| 152 | final char MAGIC2 = '\uEF01'; |
| 153 | |
| 154 | SimpleDateFormat sdf; |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 155 | String format = is24 ? d.timeFormat24 : d.timeFormat12; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 156 | 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 Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 162 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 163 | int a = -1; |
| 164 | boolean quoted = false; |
| 165 | for (int i = 0; i < format.length(); i++) { |
| 166 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 167 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 168 | if (c == '\'') { |
| 169 | quoted = !quoted; |
| 170 | } |
| 171 | if (!quoted && c == 'a') { |
| 172 | a = i; |
| 173 | break; |
| 174 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 177 | 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 Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 184 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 185 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 186 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 187 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 188 | mClockFormatString = format; |
| 189 | } else { |
| 190 | sdf = mClockFormat; |
| 191 | } |
| 192 | String result = sdf.format(mCalendar.getTime()); |
| 193 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 194 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 195 | int magic1 = result.indexOf(MAGIC1); |
| 196 | int magic2 = result.indexOf(MAGIC2); |
| 197 | if (magic1 >= 0 && magic2 > magic1) { |
| 198 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 199 | if (mAmPmStyle == AM_PM_STYLE_GONE) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 200 | formatted.delete(magic1, magic2+1); |
| 201 | } else { |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 202 | if (mAmPmStyle == AM_PM_STYLE_SMALL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 203 | 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 Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 212 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 213 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 214 | return result; |
| 215 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 216 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 217 | |
| 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)); |
| 235 | mCalendar.set(Calendar.HOUR, hh); |
| 236 | mCalendar.set(Calendar.MINUTE, mm); |
| 237 | } |
| 238 | setText(getSmallTime()); |
| 239 | } |
| 240 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 241 | } |
| 242 | |