| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.text.TextUtils; |
| 36 |
import android.util.AttributeSet; |
| 37 |
import android.util.PluralsMessageFormatter; |
| 38 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 39 |
import android.view.inspector.InspectableProperty; |
| 40 |
import android.widget.RemoteViews.RemoteView; |
| 41 |
|
| 42 |
import com.android.internal.R; |
| 43 |
|
| 44 |
import java.text.DateFormat; |
| 45 |
import java.time.Instant; |
| 46 |
import java.time.LocalDate; |
| 47 |
import java.time.LocalDateTime; |
| 48 |
import java.time.LocalTime; |
| 49 |
import java.time.ZoneId; |
| 50 |
import java.time.temporal.JulianFields; |
| 51 |
import java.util.ArrayList; |
| 52 |
import java.util.Date; |
| 53 |
import java.util.HashMap; |
| 54 |
import java.util.Map; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@RemoteView |
| 69 |
public class DateTimeView extends TextView { |
| 70 |
private static final int SHOW_TIME = 0; |
| 71 |
private static final int SHOW_MONTH_DAY_YEAR = 1; |
| 72 |
|
| 73 |
private long mTimeMillis; |
| 74 |
|
| 75 |
private LocalDateTime mLocalTime; |
| 76 |
|
| 77 |
int mLastDisplay = -1; |
| 78 |
DateFormat mLastFormat; |
| 79 |
|
| 80 |
private long mUpdateTimeMillis; |
| 81 |
private static final ThreadLocal<ReceiverInfo> sReceiverInfo = new ThreadLocal<ReceiverInfo>(); |
| 82 |
private String mNowText; |
| 83 |
private boolean mShowRelativeTime; |
| 84 |
|
| 85 |
public DateTimeView(Context context) { |
| 86 |
this(context, null); |
| 87 |
} |
| 88 |
|
| 89 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 90 |
public DateTimeView(Context context, AttributeSet attrs) { |
| 91 |
super(context, attrs); |
| 92 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 93 |
com.android.internal.R.styleable.DateTimeView, 0, |
| 94 |
0); |
| 95 |
|
| 96 |
final int N = a.getIndexCount(); |
| 97 |
for (int i = 0; i < N; i++) { |
| 98 |
int attr = a.getIndex(i); |
| 99 |
switch (attr) { |
| 100 |
case R.styleable.DateTimeView_showRelative: |
| 101 |
boolean relative = a.getBoolean(i, false); |
| 102 |
setShowRelativeTime(relative); |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
a.recycle(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
protected void onAttachedToWindow() { |
| 111 |
super.onAttachedToWindow(); |
| 112 |
ReceiverInfo ri = sReceiverInfo.get(); |
| 113 |
if (ri == null) { |
| 114 |
ri = new ReceiverInfo(); |
| 115 |
sReceiverInfo.set(ri); |
| 116 |
} |
| 117 |
ri.addView(this); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if (mShowRelativeTime) { |
| 124 |
update(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
protected void onDetachedFromWindow() { |
| 130 |
super.onDetachedFromWindow(); |
| 131 |
final ReceiverInfo ri = sReceiverInfo.get(); |
| 132 |
if (ri != null) { |
| 133 |
ri.removeView(this); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
@android.view.RemotableViewMethod |
| 138 |
@UnsupportedAppUsage |
| 139 |
public void setTime(long timeMillis) { |
| 140 |
mTimeMillis = timeMillis; |
| 141 |
LocalDateTime dateTime = toLocalDateTime(timeMillis, ZoneId.systemDefault()); |
| 142 |
mLocalTime = dateTime.withSecond(0); |
| 143 |
update(); |
| 144 |
} |
| 145 |
|
| 146 |
@android.view.RemotableViewMethod |
| 147 |
public void setShowRelativeTime(boolean showRelativeTime) { |
| 148 |
mShowRelativeTime = showRelativeTime; |
| 149 |
updateNowText(); |
| 150 |
update(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@InspectableProperty(name = "showReleative", hasAttributeId = false) |
| 159 |
public boolean isShowRelativeTime() { |
| 160 |
return mShowRelativeTime; |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
@android.view.RemotableViewMethod |
| 165 |
public void setVisibility(@Visibility int visibility) { |
| 166 |
boolean gotVisible = visibility != GONE && getVisibility() == GONE; |
| 167 |
super.setVisibility(visibility); |
| 168 |
if (gotVisible) { |
| 169 |
update(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@UnsupportedAppUsage |
| 174 |
void update() { |
| 175 |
if (mLocalTime == null || getVisibility() == GONE) { |
| 176 |
return; |
| 177 |
} |
| 178 |
if (mShowRelativeTime) { |
| 179 |
updateRelativeTime(); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
int display; |
| 184 |
ZoneId zoneId = ZoneId.systemDefault(); |
| 185 |
|
| 186 |
|
| 187 |
LocalDateTime localTime = mLocalTime; |
| 188 |
LocalDateTime localStartOfDay = |
| 189 |
LocalDateTime.of(localTime.toLocalDate(), LocalTime.MIDNIGHT); |
| 190 |
LocalDateTime localTomorrowStartOfDay = localStartOfDay.plusDays(1); |
| 191 |
|
| 192 |
LocalDateTime localNow = LocalDateTime.now(zoneId).withSecond(0); |
| 193 |
|
| 194 |
long twelveHoursBefore = toEpochMillis(localTime.minusHours(12), zoneId); |
| 195 |
long twelveHoursAfter = toEpochMillis(localTime.plusHours(12), zoneId); |
| 196 |
long midnightBefore = toEpochMillis(localStartOfDay, zoneId); |
| 197 |
long midnightAfter = toEpochMillis(localTomorrowStartOfDay, zoneId); |
| 198 |
long time = toEpochMillis(localTime, zoneId); |
| 199 |
long now = toEpochMillis(localNow, zoneId); |
| 200 |
|
| 201 |
|
| 202 |
choose_display: { |
| 203 |
if ((now >= midnightBefore && now < midnightAfter) |
| 204 |
|| (now >= twelveHoursBefore && now < twelveHoursAfter)) { |
| 205 |
display = SHOW_TIME; |
| 206 |
break choose_display; |
| 207 |
} |
| 208 |
|
| 209 |
display = SHOW_MONTH_DAY_YEAR; |
| 210 |
break choose_display; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
DateFormat format; |
| 215 |
if (display == mLastDisplay && mLastFormat != null) { |
| 216 |
|
| 217 |
format = mLastFormat; |
| 218 |
} else { |
| 219 |
switch (display) { |
| 220 |
case SHOW_TIME: |
| 221 |
format = getTimeFormat(); |
| 222 |
break; |
| 223 |
case SHOW_MONTH_DAY_YEAR: |
| 224 |
format = DateFormat.getDateInstance(DateFormat.SHORT); |
| 225 |
break; |
| 226 |
default: |
| 227 |
throw new RuntimeException("unknown display value: " + display); |
| 228 |
} |
| 229 |
mLastFormat = format; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
String text = format.format(new Date(time)); |
| 234 |
maybeSetText(text); |
| 235 |
|
| 236 |
|
| 237 |
if (display == SHOW_TIME) { |
| 238 |
|
| 239 |
mUpdateTimeMillis = twelveHoursAfter > midnightAfter ? twelveHoursAfter : midnightAfter; |
| 240 |
} else { |
| 241 |
|
| 242 |
if (mTimeMillis < now) { |
| 243 |
|
| 244 |
mUpdateTimeMillis = 0; |
| 245 |
} else { |
| 246 |
|
| 247 |
|
| 248 |
mUpdateTimeMillis = twelveHoursBefore < midnightBefore |
| 249 |
? twelveHoursBefore : midnightBefore; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
private void updateRelativeTime() { |
| 255 |
long now = System.currentTimeMillis(); |
| 256 |
long duration = Math.abs(now - mTimeMillis); |
| 257 |
int count; |
| 258 |
long millisIncrease; |
| 259 |
boolean past = (now >= mTimeMillis); |
| 260 |
String result; |
| 261 |
if (duration < MINUTE_IN_MILLIS) { |
| 262 |
maybeSetText(mNowText); |
| 263 |
mUpdateTimeMillis = mTimeMillis + MINUTE_IN_MILLIS + 1; |
| 264 |
return; |
| 265 |
} else if (duration < HOUR_IN_MILLIS) { |
| 266 |
count = (int)(duration / MINUTE_IN_MILLIS); |
| 267 |
result = getContext().getResources().getString(past |
| 268 |
? com.android.internal.R.string.duration_minutes_shortest |
| 269 |
: com.android.internal.R.string.duration_minutes_shortest_future, |
| 270 |
count); |
| 271 |
millisIncrease = MINUTE_IN_MILLIS; |
| 272 |
} else if (duration < DAY_IN_MILLIS) { |
| 273 |
count = (int)(duration / HOUR_IN_MILLIS); |
| 274 |
result = getContext().getResources().getString(past |
| 275 |
? com.android.internal.R.string.duration_hours_shortest |
| 276 |
: com.android.internal.R.string.duration_hours_shortest_future, |
| 277 |
count); |
| 278 |
millisIncrease = HOUR_IN_MILLIS; |
| 279 |
} else if (duration < YEAR_IN_MILLIS) { |
| 280 |
|
| 281 |
LocalDateTime localDateTime = mLocalTime; |
| 282 |
ZoneId zoneId = ZoneId.systemDefault(); |
| 283 |
LocalDateTime localNow = toLocalDateTime(now, zoneId); |
| 284 |
|
| 285 |
count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1); |
| 286 |
result = getContext().getResources().getString(past |
| 287 |
? com.android.internal.R.string.duration_days_shortest |
| 288 |
: com.android.internal.R.string.duration_days_shortest_future, |
| 289 |
count); |
| 290 |
if (past || count != 1) { |
| 291 |
mUpdateTimeMillis = computeNextMidnight(localNow, zoneId); |
| 292 |
millisIncrease = -1; |
| 293 |
} else { |
| 294 |
millisIncrease = DAY_IN_MILLIS; |
| 295 |
} |
| 296 |
|
| 297 |
} else { |
| 298 |
count = (int)(duration / YEAR_IN_MILLIS); |
| 299 |
result = getContext().getResources().getString(past |
| 300 |
? com.android.internal.R.string.duration_years_shortest |
| 301 |
: com.android.internal.R.string.duration_years_shortest_future, |
| 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 |
maybeSetText(result); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private void maybeSetText(String text) { |
| 320 |
if (TextUtils.equals(getText(), text)) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
setText(text); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private static long computeNextMidnight(LocalDateTime time, ZoneId zoneId) { |
| 331 |
|
| 332 |
LocalDate tomorrow = time.toLocalDate().plusDays(1); |
| 333 |
LocalDateTime nextMidnight = LocalDateTime.of(tomorrow, LocalTime.MIDNIGHT); |
| 334 |
return toEpochMillis(nextMidnight, zoneId); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
protected void onConfigurationChanged(Configuration newConfig) { |
| 339 |
super.onConfigurationChanged(newConfig); |
| 340 |
updateNowText(); |
| 341 |
update(); |
| 342 |
} |
| 343 |
|
| 344 |
private void updateNowText() { |
| 345 |
if (!mShowRelativeTime) { |
| 346 |
return; |
| 347 |
} |
| 348 |
mNowText = getContext().getResources().getString( |
| 349 |
com.android.internal.R.string.now_string_shortest); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
private static int dayDistance(LocalDateTime start, LocalDateTime end) { |
| 354 |
return (int) (end.getLong(JulianFields.JULIAN_DAY) |
| 355 |
- start.getLong(JulianFields.JULIAN_DAY)); |
| 356 |
} |
| 357 |
|
| 358 |
private DateFormat getTimeFormat() { |
| 359 |
return android.text.format.DateFormat.getTimeFormat(getContext()); |
| 360 |
} |
| 361 |
|
| 362 |
void clearFormatAndUpdate() { |
| 363 |
mLastFormat = null; |
| 364 |
update(); |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 369 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 370 |
if (mShowRelativeTime) { |
| 371 |
|
| 372 |
|
| 373 |
long now = System.currentTimeMillis(); |
| 374 |
long duration = Math.abs(now - mTimeMillis); |
| 375 |
int count; |
| 376 |
boolean past = (now >= mTimeMillis); |
| 377 |
String result; |
| 378 |
Map<String, Object> arguments = new HashMap<>(); |
| 379 |
if (duration < MINUTE_IN_MILLIS) { |
| 380 |
result = mNowText; |
| 381 |
} else if (duration < HOUR_IN_MILLIS) { |
| 382 |
count = (int)(duration / MINUTE_IN_MILLIS); |
| 383 |
arguments.put("count", count); |
| 384 |
result = PluralsMessageFormatter.format( |
| 385 |
getContext().getResources(), |
| 386 |
arguments, |
| 387 |
past ? R.string.duration_minutes_relative |
| 388 |
: R.string.duration_minutes_relative_future); |
| 389 |
} else if (duration < DAY_IN_MILLIS) { |
| 390 |
count = (int)(duration / HOUR_IN_MILLIS); |
| 391 |
arguments.put("count", count); |
| 392 |
result = PluralsMessageFormatter.format( |
| 393 |
getContext().getResources(), |
| 394 |
arguments, |
| 395 |
past ? R.string.duration_hours_relative |
| 396 |
: R.string.duration_hours_relative_future); |
| 397 |
} else if (duration < YEAR_IN_MILLIS) { |
| 398 |
|
| 399 |
LocalDateTime localDateTime = mLocalTime; |
| 400 |
ZoneId zoneId = ZoneId.systemDefault(); |
| 401 |
LocalDateTime localNow = toLocalDateTime(now, zoneId); |
| 402 |
|
| 403 |
count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1); |
| 404 |
arguments.put("count", count); |
| 405 |
result = PluralsMessageFormatter.format( |
| 406 |
getContext().getResources(), |
| 407 |
arguments, |
| 408 |
past ? R.string.duration_days_relative |
| 409 |
: R.string.duration_days_relative_future); |
| 410 |
} else { |
| 411 |
count = (int)(duration / YEAR_IN_MILLIS); |
| 412 |
arguments.put("count", count); |
| 413 |
result = PluralsMessageFormatter.format( |
| 414 |
getContext().getResources(), |
| 415 |
arguments, |
| 416 |
past ? R.string.duration_years_relative |
| 417 |
: R.string.duration_years_relative_future); |
| 418 |
} |
| 419 |
info.setText(result); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public static void setReceiverHandler(Handler handler) { |
| 427 |
ReceiverInfo ri = sReceiverInfo.get(); |
| 428 |
if (ri == null) { |
| 429 |
ri = new ReceiverInfo(); |
| 430 |
sReceiverInfo.set(ri); |
| 431 |
} |
| 432 |
ri.setHandler(handler); |
| 433 |
} |
| 434 |
|
| 435 |
private static class ReceiverInfo { |
| 436 |
private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>(); |
| 437 |
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 438 |
@Override |
| 439 |
public void onReceive(Context context, Intent intent) { |
| 440 |
String action = intent.getAction(); |
| 441 |
if (Intent.ACTION_TIME_TICK.equals(action)) { |
| 442 |
if (System.currentTimeMillis() < getSoonestUpdateTime()) { |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
return; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
updateAll(); |
| 451 |
} |
| 452 |
}; |
| 453 |
|
| 454 |
private final ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 455 |
@Override |
| 456 |
public void onChange(boolean selfChange) { |
| 457 |
updateAll(); |
| 458 |
} |
| 459 |
}; |
| 460 |
|
| 461 |
private Handler mHandler = new Handler(); |
| 462 |
|
| 463 |
public void addView(DateTimeView v) { |
| 464 |
synchronized (mAttachedViews) { |
| 465 |
final boolean register = mAttachedViews.isEmpty(); |
| 466 |
mAttachedViews.add(v); |
| 467 |
if (register) { |
| 468 |
register(getApplicationContextIfAvailable(v.getContext())); |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
public void removeView(DateTimeView v) { |
| 474 |
synchronized (mAttachedViews) { |
| 475 |
final boolean removed = mAttachedViews.remove(v); |
| 476 |
|
| 477 |
|
| 478 |
if (removed && mAttachedViews.isEmpty()) { |
| 479 |
unregister(getApplicationContextIfAvailable(v.getContext())); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
void updateAll() { |
| 485 |
synchronized (mAttachedViews) { |
| 486 |
final int count = mAttachedViews.size(); |
| 487 |
for (int i = 0; i < count; i++) { |
| 488 |
DateTimeView view = mAttachedViews.get(i); |
| 489 |
view.post(() -> view.clearFormatAndUpdate()); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
long getSoonestUpdateTime() { |
| 495 |
long result = Long.MAX_VALUE; |
| 496 |
synchronized (mAttachedViews) { |
| 497 |
final int count = mAttachedViews.size(); |
| 498 |
for (int i = 0; i < count; i++) { |
| 499 |
final long time = mAttachedViews.get(i).mUpdateTimeMillis; |
| 500 |
if (time < result) { |
| 501 |
result = time; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
return result; |
| 506 |
} |
| 507 |
|
| 508 |
static final Context getApplicationContextIfAvailable(Context context) { |
| 509 |
final Context ac = context.getApplicationContext(); |
| 510 |
return ac != null ? ac : ActivityThread.currentApplication().getApplicationContext(); |
| 511 |
} |
| 512 |
|
| 513 |
void register(Context context) { |
| 514 |
final IntentFilter filter = new IntentFilter(); |
| 515 |
filter.addAction(Intent.ACTION_TIME_TICK); |
| 516 |
filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 517 |
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 518 |
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 519 |
context.registerReceiver(mReceiver, filter, null, mHandler); |
| 520 |
} |
| 521 |
|
| 522 |
void unregister(Context context) { |
| 523 |
context.unregisterReceiver(mReceiver); |
| 524 |
} |
| 525 |
|
| 526 |
public void setHandler(Handler handler) { |
| 527 |
mHandler = handler; |
| 528 |
synchronized (mAttachedViews) { |
| 529 |
if (!mAttachedViews.isEmpty()) { |
| 530 |
unregister(mAttachedViews.get(0).getContext()); |
| 531 |
register(mAttachedViews.get(0).getContext()); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
private static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zoneId) { |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
Instant instant = Instant.ofEpochMilli(timeMillis); |
| 542 |
return LocalDateTime.ofInstant(instant, zoneId); |
| 543 |
} |
| 544 |
|
| 545 |
private static long toEpochMillis(LocalDateTime time, ZoneId zoneId) { |
| 546 |
Instant instant = time.toInstant(zoneId.getRules().getOffset(time)); |
| 547 |
return instant.toEpochMilli(); |
| 548 |
} |
| 549 |
} |