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 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 19 | import android.app.StatusBarManager; |
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; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 25 | import android.graphics.Rect; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 26 | import android.os.Bundle; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame^] | 28 | import android.os.Parcelable; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 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; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 37 | import android.view.Display; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 38 | import android.view.View; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 40 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 41 | import com.android.settingslib.Utils; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 42 | import com.android.systemui.DemoMode; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 43 | import com.android.systemui.Dependency; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 44 | import com.android.systemui.FontSizeUtils; |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 45 | import com.android.systemui.R; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 46 | import com.android.systemui.SysUiServiceProvider; |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 47 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 48 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
| 51 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 52 | import com.android.systemui.tuner.TunerService; |
| 53 | import com.android.systemui.tuner.TunerService.Tunable; |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 54 | |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 55 | import libcore.icu.LocaleData; |
| 56 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 57 | import java.text.SimpleDateFormat; |
| 58 | import java.util.Calendar; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 59 | import java.util.Locale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 60 | import java.util.TimeZone; |
| 61 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 62 | /** |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 63 | * Digital clock for the status bar. |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 64 | */ |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 65 | public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks, |
| 66 | DarkReceiver, ConfigurationListener { |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 67 | |
| 68 | public static final String CLOCK_SECONDS = "clock_seconds"; |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame^] | 69 | private static final String CLOCK_SUPER_PARCELABLE = "clock_super_parcelable"; |
| 70 | private static final String CURRENT_USER_ID = "current_user_id"; |
| 71 | private static final String VISIBLE_BY_POLICY = "visible_by_policy"; |
| 72 | private static final String VISIBLE_BY_USER = "visible_by_user"; |
| 73 | private static final String SHOW_SECONDS = "show_seconds"; |
| 74 | private static final String VISIBILITY = "visibility"; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 75 | |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 76 | private final CurrentUserTracker mCurrentUserTracker; |
| 77 | private int mCurrentUserId; |
| 78 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 79 | private boolean mClockVisibleByPolicy = true; |
| 80 | private boolean mClockVisibleByUser = true; |
| 81 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 82 | private boolean mAttached; |
| 83 | private Calendar mCalendar; |
| 84 | private String mClockFormatString; |
| 85 | private SimpleDateFormat mClockFormat; |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 86 | private SimpleDateFormat mContentDescriptionFormat; |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 87 | private Locale mLocale; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 88 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 89 | private static final int AM_PM_STYLE_NORMAL = 0; |
| 90 | private static final int AM_PM_STYLE_SMALL = 1; |
| 91 | private static final int AM_PM_STYLE_GONE = 2; |
| 92 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 93 | private final int mAmPmStyle; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 94 | private final boolean mShowDark; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 95 | private boolean mShowSeconds; |
| 96 | private Handler mSecondsHandler; |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 97 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings |
| 100 | * for text. |
| 101 | */ |
| 102 | private boolean mUseWallpaperTextColor; |
| 103 | |
| 104 | /** |
| 105 | * Color to be set on this {@link TextView}, when wallpaperTextColor is <b>not</b> utilized. |
| 106 | */ |
| 107 | private int mNonAdaptedColor; |
| 108 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 109 | public Clock(Context context) { |
| 110 | this(context, null); |
| 111 | } |
| 112 | |
| 113 | public Clock(Context context, AttributeSet attrs) { |
| 114 | this(context, attrs, 0); |
| 115 | } |
| 116 | |
| 117 | public Clock(Context context, AttributeSet attrs, int defStyle) { |
| 118 | super(context, attrs, defStyle); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 119 | TypedArray a = context.getTheme().obtainStyledAttributes( |
| 120 | attrs, |
| 121 | R.styleable.Clock, |
| 122 | 0, 0); |
| 123 | try { |
| 124 | mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 125 | mShowDark = a.getBoolean(R.styleable.Clock_showDark, true); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 126 | mNonAdaptedColor = getCurrentTextColor(); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 127 | } finally { |
| 128 | a.recycle(); |
| 129 | } |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 130 | mCurrentUserTracker = new CurrentUserTracker(context) { |
| 131 | @Override |
| 132 | public void onUserSwitched(int newUserId) { |
| 133 | mCurrentUserId = newUserId; |
| 134 | } |
| 135 | }; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
felkachang | e6c03a0 | 2018-05-24 15:38:04 +0800 | [diff] [blame^] | 139 | public Parcelable onSaveInstanceState() { |
| 140 | Bundle bundle = new Bundle(); |
| 141 | bundle.putParcelable(CLOCK_SUPER_PARCELABLE, super.onSaveInstanceState()); |
| 142 | bundle.putInt(CURRENT_USER_ID, mCurrentUserId); |
| 143 | bundle.putBoolean(VISIBLE_BY_POLICY, mClockVisibleByPolicy); |
| 144 | bundle.putBoolean(VISIBLE_BY_USER, mClockVisibleByUser); |
| 145 | bundle.putBoolean(SHOW_SECONDS, mShowSeconds); |
| 146 | bundle.putInt(VISIBILITY, getVisibility()); |
| 147 | |
| 148 | return bundle; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onRestoreInstanceState(Parcelable state) { |
| 153 | if (state == null || !(state instanceof Bundle)) { |
| 154 | super.onRestoreInstanceState(state); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | Bundle bundle = (Bundle) state; |
| 159 | Parcelable superState = bundle.getParcelable(CLOCK_SUPER_PARCELABLE); |
| 160 | super.onRestoreInstanceState(superState); |
| 161 | if (bundle.containsKey(CURRENT_USER_ID)) { |
| 162 | mCurrentUserId = bundle.getInt(CURRENT_USER_ID); |
| 163 | } |
| 164 | mClockVisibleByPolicy = bundle.getBoolean(VISIBLE_BY_POLICY, true); |
| 165 | mClockVisibleByUser = bundle.getBoolean(VISIBLE_BY_USER, true); |
| 166 | mShowSeconds = bundle.getBoolean(SHOW_SECONDS, false); |
| 167 | if (bundle.containsKey(VISIBILITY)) { |
| 168 | setVisibility(bundle.getInt(VISIBILITY)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 173 | protected void onAttachedToWindow() { |
| 174 | super.onAttachedToWindow(); |
| 175 | |
| 176 | if (!mAttached) { |
| 177 | mAttached = true; |
| 178 | IntentFilter filter = new IntentFilter(); |
| 179 | |
| 180 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 181 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 182 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 183 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | ce4f5e5 | 2012-11-19 14:47:18 -0500 | [diff] [blame] | 184 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 185 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 186 | getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 187 | null, Dependency.get(Dependency.TIME_TICK_HANDLER)); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 188 | Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS, |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 189 | StatusBarIconController.ICON_BLACKLIST); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 190 | SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 191 | if (mShowDark) { |
| 192 | Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this); |
| 193 | } |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 194 | mCurrentUserTracker.startTracking(); |
| 195 | mCurrentUserId = mCurrentUserTracker.getCurrentUserId(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // NOTE: It's safe to do these after registering the receiver since the receiver always runs |
| 199 | // in the main thread, therefore the receiver can't run before this method returns. |
| 200 | |
| 201 | // The time zone may have changed while the receiver wasn't registered, so update the Time |
| 202 | mCalendar = Calendar.getInstance(TimeZone.getDefault()); |
| 203 | |
| 204 | // Make sure we update to the current time |
| 205 | updateClock(); |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 206 | updateShowSeconds(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | protected void onDetachedFromWindow() { |
| 211 | super.onDetachedFromWindow(); |
| 212 | if (mAttached) { |
| 213 | getContext().unregisterReceiver(mIntentReceiver); |
| 214 | mAttached = false; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 215 | Dependency.get(TunerService.class).removeTunable(this); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 216 | SysUiServiceProvider.getComponent(getContext(), CommandQueue.class) |
| 217 | .removeCallbacks(this); |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 218 | if (mShowDark) { |
| 219 | Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this); |
| 220 | } |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 221 | mCurrentUserTracker.stopTracking(); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 226 | @Override |
| 227 | public void onReceive(Context context, Intent intent) { |
| 228 | String action = intent.getAction(); |
| 229 | if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { |
| 230 | String tz = intent.getStringExtra("time-zone"); |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 231 | getHandler().post(() -> { |
| 232 | mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); |
| 233 | if (mClockFormat != null) { |
| 234 | mClockFormat.setTimeZone(mCalendar.getTimeZone()); |
| 235 | } |
| 236 | }); |
Daniel Sandler | 43b23c6 | 2012-11-29 11:35:02 -0500 | [diff] [blame] | 237 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 238 | final Locale newLocale = getResources().getConfiguration().locale; |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 239 | getHandler().post(() -> { |
| 240 | if (!newLocale.equals(mLocale)) { |
| 241 | mLocale = newLocale; |
| 242 | mClockFormatString = ""; // force refresh |
| 243 | } |
| 244 | }); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 245 | } |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 246 | getHandler().post(() -> updateClock()); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 247 | } |
| 248 | }; |
| 249 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 250 | public void setClockVisibleByUser(boolean visible) { |
| 251 | mClockVisibleByUser = visible; |
| 252 | updateClockVisibility(); |
| 253 | } |
| 254 | |
| 255 | public void setClockVisibilityByPolicy(boolean visible) { |
| 256 | mClockVisibleByPolicy = visible; |
| 257 | updateClockVisibility(); |
| 258 | } |
| 259 | |
| 260 | private void updateClockVisibility() { |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 261 | boolean visible = mClockVisibleByPolicy && mClockVisibleByUser; |
| 262 | Dependency.get(IconLogger.class).onIconVisibility("clock", visible); |
| 263 | int visibility = visible ? View.VISIBLE : View.GONE; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 264 | setVisibility(visibility); |
| 265 | } |
| 266 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 267 | final void updateClock() { |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 268 | if (mDemoMode) return; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 269 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
| 270 | setText(getSmallTime()); |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 271 | setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 274 | @Override |
| 275 | public void onTuningChanged(String key, String newValue) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 276 | if (CLOCK_SECONDS.equals(key)) { |
| 277 | mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0; |
| 278 | updateShowSeconds(); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 279 | } else { |
| 280 | setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue) |
| 281 | .contains("clock")); |
| 282 | updateClockVisibility(); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 283 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 286 | @Override |
| 287 | public void disable(int state1, int state2, boolean animate) { |
| 288 | boolean clockVisibleByPolicy = (state1 & StatusBarManager.DISABLE_CLOCK) == 0; |
| 289 | if (clockVisibleByPolicy != mClockVisibleByPolicy) { |
| 290 | setClockVisibilityByPolicy(clockVisibleByPolicy); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 296 | mNonAdaptedColor = DarkIconDispatcher.getTint(area, this, tint); |
| 297 | if (!mUseWallpaperTextColor) { |
| 298 | setTextColor(mNonAdaptedColor); |
| 299 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void onDensityOrFontScaleChanged() { |
| 304 | FontSizeUtils.updateFontSize(this, R.dimen.status_bar_clock_size); |
| 305 | setPaddingRelative( |
| 306 | mContext.getResources().getDimensionPixelSize( |
| 307 | R.dimen.status_bar_clock_starting_padding), |
| 308 | 0, |
| 309 | mContext.getResources().getDimensionPixelSize( |
| 310 | R.dimen.status_bar_clock_end_padding), |
| 311 | 0); |
| 312 | } |
| 313 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 314 | /** |
| 315 | * Sets whether the clock uses the wallpaperTextColor. If we're not using it, we'll revert back |
| 316 | * to dark-mode-based/tinted colors. |
| 317 | * |
| 318 | * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for text color |
| 319 | */ |
| 320 | public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) { |
| 321 | if (shouldUseWallpaperTextColor == mUseWallpaperTextColor) { |
| 322 | return; |
| 323 | } |
| 324 | mUseWallpaperTextColor = shouldUseWallpaperTextColor; |
| 325 | |
| 326 | if (mUseWallpaperTextColor) { |
| 327 | setTextColor(Utils.getColorAttr(mContext, R.attr.wallpaperTextColor)); |
| 328 | } else { |
| 329 | setTextColor(mNonAdaptedColor); |
| 330 | } |
| 331 | } |
| 332 | |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 333 | private void updateShowSeconds() { |
| 334 | if (mShowSeconds) { |
| 335 | // Wait until we have a display to start trying to show seconds. |
| 336 | if (mSecondsHandler == null && getDisplay() != null) { |
| 337 | mSecondsHandler = new Handler(); |
| 338 | if (getDisplay().getState() == Display.STATE_ON) { |
| 339 | mSecondsHandler.postAtTime(mSecondTick, |
| 340 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 341 | } |
| 342 | IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); |
| 343 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 344 | mContext.registerReceiver(mScreenReceiver, filter); |
| 345 | } |
| 346 | } else { |
| 347 | if (mSecondsHandler != null) { |
| 348 | mContext.unregisterReceiver(mScreenReceiver); |
| 349 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 350 | mSecondsHandler = null; |
| 351 | updateClock(); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 356 | private final CharSequence getSmallTime() { |
| 357 | Context context = getContext(); |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 358 | boolean is24 = DateFormat.is24HourFormat(context, mCurrentUserId); |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 359 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 360 | |
| 361 | final char MAGIC1 = '\uEF00'; |
| 362 | final char MAGIC2 = '\uEF01'; |
| 363 | |
| 364 | SimpleDateFormat sdf; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 365 | String format = mShowSeconds |
| 366 | ? is24 ? d.timeFormat_Hms : d.timeFormat_hms |
| 367 | : is24 ? d.timeFormat_Hm : d.timeFormat_hm; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 368 | if (!format.equals(mClockFormatString)) { |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 369 | mContentDescriptionFormat = new SimpleDateFormat(format); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 370 | /* |
| 371 | * Search for an unquoted "a" in the format string, so we can |
| 372 | * add dummy characters around it to let us find it again after |
| 373 | * formatting and change its size. |
| 374 | */ |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 375 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 376 | int a = -1; |
| 377 | boolean quoted = false; |
| 378 | for (int i = 0; i < format.length(); i++) { |
| 379 | char c = format.charAt(i); |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 380 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 381 | if (c == '\'') { |
| 382 | quoted = !quoted; |
| 383 | } |
| 384 | if (!quoted && c == 'a') { |
| 385 | a = i; |
| 386 | break; |
| 387 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 390 | if (a >= 0) { |
| 391 | // Move a back so any whitespace before AM/PM is also in the alternate size. |
| 392 | final int b = a; |
| 393 | while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { |
| 394 | a--; |
| 395 | } |
| 396 | format = format.substring(0, a) + MAGIC1 + format.substring(a, b) |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 397 | + "a" + MAGIC2 + format.substring(b + 1); |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 398 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 399 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 400 | mClockFormat = sdf = new SimpleDateFormat(format); |
| 401 | mClockFormatString = format; |
| 402 | } else { |
| 403 | sdf = mClockFormat; |
| 404 | } |
| 405 | String result = sdf.format(mCalendar.getTime()); |
| 406 | |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 407 | if (mAmPmStyle != AM_PM_STYLE_NORMAL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 408 | int magic1 = result.indexOf(MAGIC1); |
| 409 | int magic2 = result.indexOf(MAGIC2); |
| 410 | if (magic1 >= 0 && magic2 > magic1) { |
| 411 | SpannableStringBuilder formatted = new SpannableStringBuilder(result); |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 412 | if (mAmPmStyle == AM_PM_STYLE_GONE) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 413 | formatted.delete(magic1, magic2+1); |
| 414 | } else { |
Jorim Jaggi | 740beb5 | 2014-05-11 18:53:19 +0200 | [diff] [blame] | 415 | if (mAmPmStyle == AM_PM_STYLE_SMALL) { |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 416 | CharacterStyle style = new RelativeSizeSpan(0.7f); |
| 417 | formatted.setSpan(style, magic1, magic2, |
| 418 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 419 | } |
| 420 | formatted.delete(magic2, magic2 + 1); |
| 421 | formatted.delete(magic1, magic1 + 1); |
| 422 | } |
| 423 | return formatted; |
| 424 | } |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 425 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 426 | |
Daniel Sandler | 87937db | 2010-05-27 13:44:11 -0400 | [diff] [blame] | 427 | return result; |
| 428 | |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 429 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 430 | |
| 431 | private boolean mDemoMode; |
| 432 | |
| 433 | @Override |
| 434 | public void dispatchDemoCommand(String command, Bundle args) { |
| 435 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
| 436 | mDemoMode = true; |
| 437 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
| 438 | mDemoMode = false; |
| 439 | updateClock(); |
| 440 | } else if (mDemoMode && command.equals(COMMAND_CLOCK)) { |
| 441 | String millis = args.getString("millis"); |
| 442 | String hhmm = args.getString("hhmm"); |
| 443 | if (millis != null) { |
| 444 | mCalendar.setTimeInMillis(Long.parseLong(millis)); |
| 445 | } else if (hhmm != null && hhmm.length() == 4) { |
| 446 | int hh = Integer.parseInt(hhmm.substring(0, 2)); |
| 447 | int mm = Integer.parseInt(hhmm.substring(2)); |
Lucas Dupin | 4eae881 | 2018-04-09 22:44:17 -0700 | [diff] [blame] | 448 | boolean is24 = DateFormat.is24HourFormat(getContext(), mCurrentUserId); |
Julia Reynolds | c11d838 | 2015-07-16 14:40:37 -0400 | [diff] [blame] | 449 | if (is24) { |
| 450 | mCalendar.set(Calendar.HOUR_OF_DAY, hh); |
| 451 | } else { |
| 452 | mCalendar.set(Calendar.HOUR, hh); |
| 453 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 454 | mCalendar.set(Calendar.MINUTE, mm); |
| 455 | } |
| 456 | setText(getSmallTime()); |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 457 | setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 458 | } |
| 459 | } |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 460 | |
| 461 | private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 462 | @Override |
| 463 | public void onReceive(Context context, Intent intent) { |
| 464 | String action = intent.getAction(); |
| 465 | if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 466 | if (mSecondsHandler != null) { |
| 467 | mSecondsHandler.removeCallbacks(mSecondTick); |
| 468 | } |
| 469 | } else if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 470 | if (mSecondsHandler != null) { |
| 471 | mSecondsHandler.postAtTime(mSecondTick, |
| 472 | SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | private final Runnable mSecondTick = new Runnable() { |
| 479 | @Override |
| 480 | public void run() { |
| 481 | if (mCalendar != null) { |
| 482 | updateClock(); |
| 483 | } |
| 484 | mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); |
| 485 | } |
| 486 | }; |
Joe Onorato | 263700d | 2010-05-14 11:54:53 -0700 | [diff] [blame] | 487 | } |
| 488 | |