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 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame^] | 19 | import libcore.icu.LocaleData; |
| 20 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 27 | import android.os.Bundle; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.SystemClock; |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 30 | import android.os.UserHandle; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 31 | import android.text.Spannable; |
| 32 | import android.text.SpannableStringBuilder; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 33 | import android.text.format.DateFormat; |
| 34 | import android.text.style.CharacterStyle; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 35 | import android.text.style.RelativeSizeSpan; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 36 | import android.util.ArraySet; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 38 | import android.view.Display; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 39 | import android.view.View; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 40 | import android.widget.TextView; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame^] | 41 | |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 42 | import com.android.systemui.DemoMode; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 43 | import com.android.systemui.R; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 45 | import com.android.systemui.tuner.TunerService; |
| 46 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 47 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 48 | import java.text.SimpleDateFormat; |
| 49 | import java.util.Calendar; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 50 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 51 | import java.util.TimeZone; |
| 52 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 53 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 54 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 55 | */ |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 56 | public class Clock extends TextView implements DemoMode, Tunable { |
| 57 | |
| 58 | public static final String CLOCK_SECONDS = "clock_seconds"; |
| 59 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 60 | private boolean mAttached; |
| 61 | private Calendar mCalendar; |
| 62 | private String mClockFormatString; |
| 63 | private SimpleDateFormat mClockFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 64 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 65 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 66 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 67 | private static final int AM_PM_STYLE_SMALL = 1; |
| 68 | private static final int AM_PM_STYLE_GONE = 2; |
| 69 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 70 | private final int mAmPmStyle; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 71 | private boolean mShowSeconds; |
| 72 | private Handler mSecondsHandler; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 73 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 74 | public Clock(Context context) { |
| 75 | this(context, null); |
| 76 | } |
| 77 | |
| 78 | public Clock(Context context, AttributeSet attrs) { |
| 79 | this(context, attrs, 0); |
| 80 | } |
| 81 | |
| 82 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
| 83 | super(context, attrs, defStyle); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 84 | TypedArray a = context.getTheme().obtainStyledAttributes( |
| 85 | attrs, |
| 86 | R.styleable.Clock, |
| 87 | 0, 0); |
| 88 | try { |
| 89 | mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE); |
| 90 | } finally { |
| 91 | a.recycle(); |
| 92 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected void onAttachedToWindow() { |
| 97 | super.onAttachedToWindow(); |
| 98 | |
| 99 | if (!mAttached) { |
| 100 | mAttached = true; |
| 101 | IntentFilter filter = new IntentFilter(); |
| 102 | |
| 103 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 104 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 105 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 106 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 107 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 108 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 109 | getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, |
| 110 | null, getHandler()); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 111 | TunerService.get(getContext()).addTunable(this, CLOCK_SECONDS, |
| 112 | StatusBarIconController.ICON_BLACKLIST); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // NOTE: It's safe to do these after registering the receiver since the receiver always runs |
| 116 | // in the main thread, therefore the receiver can't run before this method returns. |
| 117 | |
| 118 | // The time zone may have changed while the receiver wasn't registered, so update the Time |
| 119 | mCalendar = Calendar.getInstance(TimeZone.getDefault()); |
| 120 | |
| 121 | // Make sure we update to the current time |
| 122 | updateClock(); |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 123 | updateShowSeconds(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void onDetachedFromWindow() { |
| 128 | super.onDetachedFromWindow(); |
| 129 | if (mAttached) { |
| 130 | getContext().unregisterReceiver(mIntentReceiver); |
| 131 | mAttached = false; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 132 | TunerService.get(getContext()).removeTunable(this); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 137 | @Override |
| 138 | public void onReceive(Context context, Intent intent) { |
| 139 | String action = intent.getAction(); |
| 140 | if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { |
| 141 | String tz = intent.getStringExtra("time-zone"); |
| 142 | mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); |
| 143 | if (mClockFormat != null) { |
| 144 | mClockFormat.setTimeZone(mCalendar.getTimeZone()); |
| 145 | } |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 146 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 147 | final Locale newLocale = getResources().getConfiguration().locale; |
| 148 | if (! newLocale.equals(mLocale)) { |
| 149 | mLocale = newLocale; |
| 150 | mClockFormatString = ""; // force refresh |
| 151 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 152 | } |
| 153 | updateClock(); |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | final void updateClock() { |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 158 | if (mDemoMode) return; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 159 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 160 | setText(getSmallTime()); |
| 161 | } |
| 162 | |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 163 | @Override |
| 164 | public void onTuningChanged(String key, String newValue) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 165 | if (CLOCK_SECONDS.equals(key)) { |
| 166 | mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0; |
| 167 | updateShowSeconds(); |
| 168 | } else if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { |
| 169 | ArraySet<String> list = StatusBarIconController.getIconBlacklist(newValue); |
| 170 | setVisibility(list.contains("clock") ? View.GONE : View.VISIBLE); |
| 171 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private void updateShowSeconds() { |
| 175 | if (mShowSeconds) { |
| 176 | // Wait until we have a display to start trying to show seconds. |
| 177 | if (mSecondsHandler == null && getDisplay() != null) { |
| 178 | mSecondsHandler = new Handler(); |
| 179 | if (getDisplay().getState() == Display.STATE_ON) { |
| 180 | mSecondsHandler.postAtTime(mSecondTick, |
| 181 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 182 | } |
| 183 | IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); |
| 184 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 185 | mContext.registerReceiver(mScreenReceiver, filter); |
| 186 | } |
| 187 | } else { |
| 188 | if (mSecondsHandler != null) { |
| 189 | mContext.unregisterReceiver(mScreenReceiver); |
| 190 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 191 | mSecondsHandler = null; |
| 192 | updateClock(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 197 | private final CharSequence getSmallTime() { |
| 198 | Context context = getContext(); |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 199 | boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser()); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 200 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 201 | |
| 202 | final char MAGIC1 = '\uEF00'; |
| 203 | final char MAGIC2 = '\uEF01'; |
| 204 | |
| 205 | SimpleDateFormat sdf; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 206 | String format = mShowSeconds |
| 207 | ? is24 ? d.timeFormat_Hms : d.timeFormat_hms |
| 208 | : is24 ? d.timeFormat_Hm : d.timeFormat_hm; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 209 | if (!format.equals(mClockFormatString)) { |
| 210 | /* |
| 211 | * Search for an unquoted "a" in the format string, so we can |
| 212 | * add dummy characters around it to let us find it again after |
| 213 | * formatting and change its size. |
| 214 | */ |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 215 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 216 | int a = -1; |
| 217 | boolean quoted = false; |
| 218 | for (int i = 0; i < format.length(); i++) { |
| 219 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 220 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 221 | if (c == '\'') { |
| 222 | quoted = !quoted; |
| 223 | } |
| 224 | if (!quoted && c == 'a') { |
| 225 | a = i; |
| 226 | break; |
| 227 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 230 | if (a >= 0) { |
| 231 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 232 | final int b = a; |
| 233 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 234 | a--; |
| 235 | } |
| 236 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 237 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 238 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 239 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 240 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 241 | mClockFormatString = format; |
| 242 | } else { |
| 243 | sdf = mClockFormat; |
| 244 | } |
| 245 | String result = sdf.format(mCalendar.getTime()); |
| 246 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 247 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 248 | int magic1 = result.indexOf(MAGIC1); |
| 249 | int magic2 = result.indexOf(MAGIC2); |
| 250 | if (magic1 >= 0 && magic2 > magic1) { |
| 251 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 252 | if (mAmPmStyle == AM_PM_STYLE_GONE) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 253 | formatted.delete(magic1, magic2+1); |
| 254 | } else { |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 255 | if (mAmPmStyle == AM_PM_STYLE_SMALL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 256 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 257 | formatted.setSpan(style, magic1, magic2, |
| 258 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 259 | } |
| 260 | formatted.delete(magic2, magic2 + 1); |
| 261 | formatted.delete(magic1, magic1 + 1); |
| 262 | } |
| 263 | return formatted; |
| 264 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 265 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 266 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 267 | return result; |
| 268 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 269 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 270 | |
| 271 | private boolean mDemoMode; |
| 272 | |
| 273 | @Override |
| 274 | public void dispatchDemoCommand(String command, Bundle args) { |
| 275 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
| 276 | mDemoMode = true; |
| 277 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
| 278 | mDemoMode = false; |
| 279 | updateClock(); |
| 280 | } else if (mDemoMode && command.equals(COMMAND_CLOCK)) { |
| 281 | String millis = args.getString("millis"); |
| 282 | String hhmm = args.getString("hhmm"); |
| 283 | if (millis != null) { |
| 284 | mCalendar.setTimeInMillis(Long.parseLong(millis)); |
| 285 | } else if (hhmm != null && hhmm.length() == 4) { |
| 286 | int hh = Integer.parseInt(hhmm.substring(0, 2)); |
| 287 | int mm = Integer.parseInt(hhmm.substring(2)); |
Julia Reynolds | c11d838 | 2015-07-16 14:40:37 -0400 | [diff] [blame] | 288 | boolean is24 = DateFormat.is24HourFormat( |
| 289 | getContext(), ActivityManager.getCurrentUser()); |
| 290 | if (is24) { |
| 291 | mCalendar.set(Calendar.HOUR_OF_DAY, hh); |
| 292 | } else { |
| 293 | mCalendar.set(Calendar.HOUR, hh); |
| 294 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 295 | mCalendar.set(Calendar.MINUTE, mm); |
| 296 | } |
| 297 | setText(getSmallTime()); |
| 298 | } |
| 299 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 300 | |
| 301 | private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 302 | @Override |
| 303 | public void onReceive(Context context, Intent intent) { |
| 304 | String action = intent.getAction(); |
| 305 | if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 306 | if (mSecondsHandler != null) { |
| 307 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 308 | } |
| 309 | } else if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 310 | if (mSecondsHandler != null) { |
| 311 | mSecondsHandler.postAtTime(mSecondTick, |
| 312 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | private final Runnable mSecondTick = new Runnable() { |
| 319 | @Override |
| 320 | public void run() { |
| 321 | if (mCalendar != null) { |
| 322 | updateClock(); |
| 323 | } |
| 324 | mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 325 | } |
| 326 | }; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 327 | } |
| 328 | |