1 /* 2 * Copyright (C) 2010 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 17 package android.widget; 18 19 import static android.text.format.DateUtils.DAY_IN_MILLIS; 20 import static android.text.format.DateUtils.HOUR_IN_MILLIS; 21 import static android.text.format.DateUtils.MINUTE_IN_MILLIS; 22 import static android.text.format.DateUtils.YEAR_IN_MILLIS; 23 24 import android.app.ActivityThread; 25 import android.compat.annotation.UnsupportedAppUsage; 26 import android.content.BroadcastReceiver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.IntentFilter; 30 import android.content.res.Configuration; 31 import android.content.res.TypedArray; 32 import android.database.ContentObserver; 33 import android.os.Build; 34 import android.os.Handler; 35 import android.util.AttributeSet; 36 import android.view.accessibility.AccessibilityNodeInfo; 37 import android.view.inspector.InspectableProperty; 38 import android.widget.RemoteViews.RemoteView; 39 40 import com.android.internal.R; 41 42 import java.text.DateFormat; 43 import java.time.Instant; 44 import java.time.LocalDate; 45 import java.time.LocalDateTime; 46 import java.time.LocalTime; 47 import java.time.ZoneId; 48 import java.time.temporal.JulianFields; 49 import java.util.ArrayList; 50 import java.util.Date; 51 52 // 53 // TODO 54 // - listen for the next threshold time to update the view. 55 // - listen for date format pref changed 56 // - put the AM/PM in a smaller font 57 // 58 59 /** 60 * Displays a given time in a convenient human-readable foramt. 61 * 62 * @hide 63 */ 64 @RemoteView 65 public class DateTimeView extends TextView { 66 private static final int SHOW_TIME = 0; 67 private static final int SHOW_MONTH_DAY_YEAR = 1; 68 69 private long mTimeMillis; 70 // The LocalDateTime equivalent of mTimeMillis but truncated to minute, i.e. no seconds / nanos. 71 private LocalDateTime mLocalTime; 72 73 int mLastDisplay = -1; 74 DateFormat mLastFormat; 75 76 private long mUpdateTimeMillis; 77 private static final ThreadLocal<ReceiverInfo> sReceiverInfo = new ThreadLocal<ReceiverInfo>(); 78 private String mNowText; 79 private boolean mShowRelativeTime; 80 DateTimeView(Context context)81 public DateTimeView(Context context) { 82 this(context, null); 83 } 84 85 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) DateTimeView(Context context, AttributeSet attrs)86 public DateTimeView(Context context, AttributeSet attrs) { 87 super(context, attrs); 88 final TypedArray a = context.obtainStyledAttributes(attrs, 89 com.android.internal.R.styleable.DateTimeView, 0, 90 0); 91 92 final int N = a.getIndexCount(); 93 for (int i = 0; i < N; i++) { 94 int attr = a.getIndex(i); 95 switch (attr) { 96 case R.styleable.DateTimeView_showRelative: 97 boolean relative = a.getBoolean(i, false); 98 setShowRelativeTime(relative); 99 break; 100 } 101 } 102 a.recycle(); 103 } 104 105 @Override onAttachedToWindow()106 protected void onAttachedToWindow() { 107 super.onAttachedToWindow(); 108 ReceiverInfo ri = sReceiverInfo.get(); 109 if (ri == null) { 110 ri = new ReceiverInfo(); 111 sReceiverInfo.set(ri); 112 } 113 ri.addView(this); 114 // The view may not be added to the view hierarchy immediately right after setTime() 115 // is called which means it won't get any update from intents before being added. 116 // In such case, the view might show the incorrect relative time after being added to the 117 // view hierarchy until the next update intent comes. 118 // So we update the time here if mShowRelativeTime is enabled to prevent this case. 119 if (mShowRelativeTime) { 120 update(); 121 } 122 } 123 124 @Override onDetachedFromWindow()125 protected void onDetachedFromWindow() { 126 super.onDetachedFromWindow(); 127 final ReceiverInfo ri = sReceiverInfo.get(); 128 if (ri != null) { 129 ri.removeView(this); 130 } 131 } 132 133 @android.view.RemotableViewMethod 134 @UnsupportedAppUsage setTime(long timeMillis)135 public void setTime(long timeMillis) { 136 mTimeMillis = timeMillis; 137 LocalDateTime dateTime = toLocalDateTime(timeMillis, ZoneId.systemDefault()); 138 mLocalTime = dateTime.withSecond(0); 139 update(); 140 } 141 142 @android.view.RemotableViewMethod setShowRelativeTime(boolean showRelativeTime)143 public void setShowRelativeTime(boolean showRelativeTime) { 144 mShowRelativeTime = showRelativeTime; 145 updateNowText(); 146 update(); 147 } 148 149 /** 150 * Returns whether this view shows relative time 151 * 152 * @return True if it shows relative time, false otherwise 153 */ 154 @InspectableProperty(name = "showReleative", hasAttributeId = false) isShowRelativeTime()155 public boolean isShowRelativeTime() { 156 return mShowRelativeTime; 157 } 158 159 @Override 160 @android.view.RemotableViewMethod setVisibility(@isibility int visibility)161 public void setVisibility(@Visibility int visibility) { 162 boolean gotVisible = visibility != GONE && getVisibility() == GONE; 163 super.setVisibility(visibility); 164 if (gotVisible) { 165 update(); 166 } 167 } 168 169 @UnsupportedAppUsage update()170 void update() { 171 if (mLocalTime == null || getVisibility() == GONE) { 172 return; 173 } 174 if (mShowRelativeTime) { 175 updateRelativeTime(); 176 return; 177 } 178 179 int display; 180 ZoneId zoneId = ZoneId.systemDefault(); 181 182 // localTime is the local time for mTimeMillis but at zero seconds past the minute. 183 LocalDateTime localTime = mLocalTime; 184 LocalDateTime localStartOfDay = 185 LocalDateTime.of(localTime.toLocalDate(), LocalTime.MIDNIGHT); 186 LocalDateTime localTomorrowStartOfDay = localStartOfDay.plusDays(1); 187 // now is current local time but at zero seconds past the minute. 188 LocalDateTime localNow = LocalDateTime.now(zoneId).withSecond(0); 189 190 long twelveHoursBefore = toEpochMillis(localTime.minusHours(12), zoneId); 191 long twelveHoursAfter = toEpochMillis(localTime.plusHours(12), zoneId); 192 long midnightBefore = toEpochMillis(localStartOfDay, zoneId); 193 long midnightAfter = toEpochMillis(localTomorrowStartOfDay, zoneId); 194 long time = toEpochMillis(localTime, zoneId); 195 long now = toEpochMillis(localNow, zoneId); 196 197 // Choose the display mode 198 choose_display: { 199 if ((now >= midnightBefore && now < midnightAfter) 200 || (now >= twelveHoursBefore && now < twelveHoursAfter)) { 201 display = SHOW_TIME; 202 break choose_display; 203 } 204 // Else, show month day and year. 205 display = SHOW_MONTH_DAY_YEAR; 206 break choose_display; 207 } 208 209 // Choose the format 210 DateFormat format; 211 if (display == mLastDisplay && mLastFormat != null) { 212 // use cached format 213 format = mLastFormat; 214 } else { 215 switch (display) { 216 case SHOW_TIME: 217 format = getTimeFormat(); 218 break; 219 case SHOW_MONTH_DAY_YEAR: 220 format = DateFormat.getDateInstance(DateFormat.SHORT); 221 break; 222 default: 223 throw new RuntimeException("unknown display value: " + display); 224 } 225 mLastFormat = format; 226 } 227 228 // Set the text 229 String text = format.format(new Date(time)); 230 setText(text); 231 232 // Schedule the next update 233 if (display == SHOW_TIME) { 234 // Currently showing the time, update at the later of twelve hours after or midnight. 235 mUpdateTimeMillis = twelveHoursAfter > midnightAfter ? twelveHoursAfter : midnightAfter; 236 } else { 237 // Currently showing the date 238 if (mTimeMillis < now) { 239 // If the time is in the past, don't schedule an update 240 mUpdateTimeMillis = 0; 241 } else { 242 // If hte time is in the future, schedule one at the earlier of twelve hours 243 // before or midnight before. 244 mUpdateTimeMillis = twelveHoursBefore < midnightBefore 245 ? twelveHoursBefore : midnightBefore; 246 } 247 } 248 } 249 250 private void updateRelativeTime() { 251 long now = System.currentTimeMillis(); 252 long duration = Math.abs(now - mTimeMillis); 253 int count; 254 long millisIncrease; 255 boolean past = (now >= mTimeMillis); 256 String result; 257 if (duration < MINUTE_IN_MILLIS) { 258 setText(mNowText); 259 mUpdateTimeMillis = mTimeMillis + MINUTE_IN_MILLIS + 1; 260 return; 261 } else if (duration < HOUR_IN_MILLIS) { 262 count = (int)(duration / MINUTE_IN_MILLIS); 263 result = String.format(getContext().getResources().getQuantityString(past 264 ? com.android.internal.R.plurals.duration_minutes_shortest 265 : com.android.internal.R.plurals.duration_minutes_shortest_future, 266 count), 267 count); 268 millisIncrease = MINUTE_IN_MILLIS; 269 } else if (duration < DAY_IN_MILLIS) { 270 count = (int)(duration / HOUR_IN_MILLIS); 271 result = String.format(getContext().getResources().getQuantityString(past 272 ? com.android.internal.R.plurals.duration_hours_shortest 273 : com.android.internal.R.plurals.duration_hours_shortest_future, 274 count), 275 count); 276 millisIncrease = HOUR_IN_MILLIS; 277 } else if (duration < YEAR_IN_MILLIS) { 278 // In weird cases it can become 0 because of daylight savings 279 LocalDateTime localDateTime = mLocalTime; 280 ZoneId zoneId = ZoneId.systemDefault(); 281 LocalDateTime localNow = toLocalDateTime(now, zoneId); 282 283 count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1); 284 result = String.format(getContext().getResources().getQuantityString(past 285 ? com.android.internal.R.plurals.duration_days_shortest 286 : com.android.internal.R.plurals.duration_days_shortest_future, 287 count), 288 count); 289 if (past || count != 1) { 290 mUpdateTimeMillis = computeNextMidnight(localNow, zoneId); 291 millisIncrease = -1; 292 } else { 293 millisIncrease = DAY_IN_MILLIS; 294 } 295 296 } else { 297 count = (int)(duration / YEAR_IN_MILLIS); 298 result = String.format(getContext().getResources().getQuantityString(past 299 ? com.android.internal.R.plurals.duration_years_shortest 300 : com.android.internal.R.plurals.duration_years_shortest_future, 301 count), 302 count); 303 millisIncrease = YEAR_IN_MILLIS; 304 } 305 if (millisIncrease != -1) { 306 if (past) { 307 mUpdateTimeMillis = mTimeMillis + millisIncrease * (count + 1) + 1; 308 } else { 309 mUpdateTimeMillis = mTimeMillis - millisIncrease * count + 1; 310 } 311 } 312 setText(result); 313 } 314 315 /** 316 * Returns the epoch millis for the next midnight in the specified timezone. 317 */ computeNextMidnight(LocalDateTime time, ZoneId zoneId)318 private static long computeNextMidnight(LocalDateTime time, ZoneId zoneId) { 319 // This ignores the chance of overflow: it should never happen. 320 LocalDate tomorrow = time.toLocalDate().plusDays(1); 321 LocalDateTime nextMidnight = LocalDateTime.of(tomorrow, LocalTime.MIDNIGHT); 322 return toEpochMillis(nextMidnight, zoneId); 323 } 324 325 @Override onConfigurationChanged(Configuration newConfig)326 protected void onConfigurationChanged(Configuration newConfig) { 327 super.onConfigurationChanged(newConfig); 328 updateNowText(); 329 update(); 330 } 331 updateNowText()332 private void updateNowText() { 333 if (!mShowRelativeTime) { 334 return; 335 } 336 mNowText = getContext().getResources().getString( 337 com.android.internal.R.string.now_string_shortest); 338 } 339 340 // Return the number of days between the two dates. dayDistance(LocalDateTime start, LocalDateTime end)341 private static int dayDistance(LocalDateTime start, LocalDateTime end) { 342 return (int) (end.getLong(JulianFields.JULIAN_DAY) 343 - start.getLong(JulianFields.JULIAN_DAY)); 344 } 345 getTimeFormat()346 private DateFormat getTimeFormat() { 347 return android.text.format.DateFormat.getTimeFormat(getContext()); 348 } 349 clearFormatAndUpdate()350 void clearFormatAndUpdate() { 351 mLastFormat = null; 352 update(); 353 } 354 355 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)356 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 357 super.onInitializeAccessibilityNodeInfoInternal(info); 358 if (mShowRelativeTime) { 359 // The short version of the time might not be completely understandable and for 360 // accessibility we rather have a longer version. 361 long now = System.currentTimeMillis(); 362 long duration = Math.abs(now - mTimeMillis); 363 int count; 364 boolean past = (now >= mTimeMillis); 365 String result; 366 if (duration < MINUTE_IN_MILLIS) { 367 result = mNowText; 368 } else if (duration < HOUR_IN_MILLIS) { 369 count = (int)(duration / MINUTE_IN_MILLIS); 370 result = String.format(getContext().getResources().getQuantityString(past 371 ? com.android.internal. 372 R.plurals.duration_minutes_relative 373 : com.android.internal. 374 R.plurals.duration_minutes_relative_future, 375 count), 376 count); 377 } else if (duration < DAY_IN_MILLIS) { 378 count = (int)(duration / HOUR_IN_MILLIS); 379 result = String.format(getContext().getResources().getQuantityString(past 380 ? com.android.internal. 381 R.plurals.duration_hours_relative 382 : com.android.internal. 383 R.plurals.duration_hours_relative_future, 384 count), 385 count); 386 } else if (duration < YEAR_IN_MILLIS) { 387 // In weird cases it can become 0 because of daylight savings 388 LocalDateTime localDateTime = mLocalTime; 389 ZoneId zoneId = ZoneId.systemDefault(); 390 LocalDateTime localNow = toLocalDateTime(now, zoneId); 391 392 count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1); 393 result = String.format(getContext().getResources().getQuantityString(past 394 ? com.android.internal. 395 R.plurals.duration_days_relative 396 : com.android.internal. 397 R.plurals.duration_days_relative_future, 398 count), 399 count); 400 401 } else { 402 count = (int)(duration / YEAR_IN_MILLIS); 403 result = String.format(getContext().getResources().getQuantityString(past 404 ? com.android.internal. 405 R.plurals.duration_years_relative 406 : com.android.internal. 407 R.plurals.duration_years_relative_future, 408 count), 409 count); 410 } 411 info.setText(result); 412 } 413 } 414 415 /** 416 * @hide 417 */ setReceiverHandler(Handler handler)418 public static void setReceiverHandler(Handler handler) { 419 ReceiverInfo ri = sReceiverInfo.get(); 420 if (ri == null) { 421 ri = new ReceiverInfo(); 422 sReceiverInfo.set(ri); 423 } 424 ri.setHandler(handler); 425 } 426 427 private static class ReceiverInfo { 428 private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>(); 429 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 430 @Override 431 public void onReceive(Context context, Intent intent) { 432 String action = intent.getAction(); 433 if (Intent.ACTION_TIME_TICK.equals(action)) { 434 if (System.currentTimeMillis() < getSoonestUpdateTime()) { 435 // The update() function takes a few milliseconds to run because of 436 // all of the time conversions it needs to do, so we can't do that 437 // every minute. 438 return; 439 } 440 } 441 // ACTION_TIME_CHANGED can also signal a change of 12/24 hr. format. 442 updateAll(); 443 } 444 }; 445 446 private final ContentObserver mObserver = new ContentObserver(new Handler()) { 447 @Override 448 public void onChange(boolean selfChange) { 449 updateAll(); 450 } 451 }; 452 453 private Handler mHandler = new Handler(); 454 addView(DateTimeView v)455 public void addView(DateTimeView v) { 456 synchronized (mAttachedViews) { 457 final boolean register = mAttachedViews.isEmpty(); 458 mAttachedViews.add(v); 459 if (register) { 460 register(getApplicationContextIfAvailable(v.getContext())); 461 } 462 } 463 } 464 removeView(DateTimeView v)465 public void removeView(DateTimeView v) { 466 synchronized (mAttachedViews) { 467 final boolean removed = mAttachedViews.remove(v); 468 // Only unregister once when we remove the last view in the list otherwise we risk 469 // trying to unregister a receiver that is no longer registered. 470 if (removed && mAttachedViews.isEmpty()) { 471 unregister(getApplicationContextIfAvailable(v.getContext())); 472 } 473 } 474 } 475 updateAll()476 void updateAll() { 477 synchronized (mAttachedViews) { 478 final int count = mAttachedViews.size(); 479 for (int i = 0; i < count; i++) { 480 DateTimeView view = mAttachedViews.get(i); 481 view.post(() -> view.clearFormatAndUpdate()); 482 } 483 } 484 } 485 getSoonestUpdateTime()486 long getSoonestUpdateTime() { 487 long result = Long.MAX_VALUE; 488 synchronized (mAttachedViews) { 489 final int count = mAttachedViews.size(); 490 for (int i = 0; i < count; i++) { 491 final long time = mAttachedViews.get(i).mUpdateTimeMillis; 492 if (time < result) { 493 result = time; 494 } 495 } 496 } 497 return result; 498 } 499 getApplicationContextIfAvailable(Context context)500 static final Context getApplicationContextIfAvailable(Context context) { 501 final Context ac = context.getApplicationContext(); 502 return ac != null ? ac : ActivityThread.currentApplication().getApplicationContext(); 503 } 504 register(Context context)505 void register(Context context) { 506 final IntentFilter filter = new IntentFilter(); 507 filter.addAction(Intent.ACTION_TIME_TICK); 508 filter.addAction(Intent.ACTION_TIME_CHANGED); 509 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 510 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 511 context.registerReceiver(mReceiver, filter, null, mHandler); 512 } 513 unregister(Context context)514 void unregister(Context context) { 515 context.unregisterReceiver(mReceiver); 516 } 517 setHandler(Handler handler)518 public void setHandler(Handler handler) { 519 mHandler = handler; 520 synchronized (mAttachedViews) { 521 if (!mAttachedViews.isEmpty()) { 522 unregister(mAttachedViews.get(0).getContext()); 523 register(mAttachedViews.get(0).getContext()); 524 } 525 } 526 } 527 } 528 toLocalDateTime(long timeMillis, ZoneId zoneId)529 private static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zoneId) { 530 // java.time types like LocalDateTime / Instant can support the full range of "long millis" 531 // with room to spare so we do not need to worry about overflow / underflow and the rsulting 532 // exceptions while the input to this class is a long. 533 Instant instant = Instant.ofEpochMilli(timeMillis); 534 return LocalDateTime.ofInstant(instant, zoneId); 535 } 536 toEpochMillis(LocalDateTime time, ZoneId zoneId)537 private static long toEpochMillis(LocalDateTime time, ZoneId zoneId) { 538 Instant instant = time.toInstant(zoneId.getRules().getOffset(time)); 539 return instant.toEpochMilli(); 540 } 541 } 542