| 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.view.ViewDebug.ExportedProperty; |
| 20 |
import static android.widget.RemoteViews.RemoteView; |
| 21 |
|
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.app.ActivityManager; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.BroadcastReceiver; |
| 27 |
import android.content.ContentResolver; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.Intent; |
| 30 |
import android.content.IntentFilter; |
| 31 |
import android.content.res.TypedArray; |
| 32 |
import android.database.ContentObserver; |
| 33 |
import android.icu.text.DateTimePatternGenerator; |
| 34 |
import android.net.Uri; |
| 35 |
import android.os.Build; |
| 36 |
import android.os.Handler; |
| 37 |
import android.os.UserHandle; |
| 38 |
import android.provider.Settings; |
| 39 |
import android.text.format.DateFormat; |
| 40 |
import android.util.AttributeSet; |
| 41 |
import android.view.RemotableViewMethod; |
| 42 |
import android.view.ViewHierarchyEncoder; |
| 43 |
import android.view.inspector.InspectableProperty; |
| 44 |
|
| 45 |
import com.android.internal.R; |
| 46 |
|
| 47 |
import java.time.Duration; |
| 48 |
import java.time.Instant; |
| 49 |
import java.time.ZoneId; |
| 50 |
import java.time.ZonedDateTime; |
| 51 |
import java.util.Calendar; |
| 52 |
import java.util.TimeZone; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@RemoteView |
| 97 |
public class TextClock extends TextView { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@Deprecated |
| 112 |
public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm a"; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@Deprecated |
| 127 |
public static final CharSequence DEFAULT_FORMAT_24_HOUR = "H:mm"; |
| 128 |
|
| 129 |
private CharSequence mFormat12; |
| 130 |
private CharSequence mFormat24; |
| 131 |
private CharSequence mDescFormat12; |
| 132 |
private CharSequence mDescFormat24; |
| 133 |
|
| 134 |
@ExportedProperty |
| 135 |
private CharSequence mFormat; |
| 136 |
@ExportedProperty |
| 137 |
private boolean mHasSeconds; |
| 138 |
|
| 139 |
private CharSequence mDescFormat; |
| 140 |
|
| 141 |
private boolean mRegistered; |
| 142 |
private boolean mShouldRunTicker; |
| 143 |
|
| 144 |
private Calendar mTime; |
| 145 |
private String mTimeZone; |
| 146 |
|
| 147 |
private boolean mShowCurrentUserTime; |
| 148 |
|
| 149 |
private ContentObserver mFormatChangeObserver; |
| 150 |
|
| 151 |
private boolean mStopTicking; |
| 152 |
|
| 153 |
private class FormatChangeObserver extends ContentObserver { |
| 154 |
|
| 155 |
public FormatChangeObserver(Handler handler) { |
| 156 |
super(handler); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public void onChange(boolean selfChange) { |
| 161 |
chooseFormat(); |
| 162 |
onTimeChanged(); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void onChange(boolean selfChange, Uri uri) { |
| 167 |
chooseFormat(); |
| 168 |
onTimeChanged(); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 173 |
@Override |
| 174 |
public void onReceive(Context context, Intent intent) { |
| 175 |
if (mStopTicking) { |
| 176 |
return; |
| 177 |
} |
| 178 |
if (mTimeZone == null && Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 179 |
final String timeZone = intent.getStringExtra(Intent.EXTRA_TIMEZONE); |
| 180 |
createTime(timeZone); |
| 181 |
} else if (!mShouldRunTicker && (Intent.ACTION_TIME_TICK.equals(intent.getAction()) |
| 182 |
|| Intent.ACTION_TIME_CHANGED.equals(intent.getAction()))) { |
| 183 |
return; |
| 184 |
} |
| 185 |
onTimeChanged(); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
private final Runnable mTicker = new Runnable() { |
| 190 |
public void run() { |
| 191 |
removeCallbacks(this); |
| 192 |
if (mStopTicking || !mShouldRunTicker) { |
| 193 |
return; |
| 194 |
} |
| 195 |
onTimeChanged(); |
| 196 |
|
| 197 |
Instant now = mTime.toInstant(); |
| 198 |
ZoneId zone = mTime.getTimeZone().toZoneId(); |
| 199 |
|
| 200 |
ZonedDateTime nextTick; |
| 201 |
if (mHasSeconds) { |
| 202 |
nextTick = now.atZone(zone).plusSeconds(1).withNano(0); |
| 203 |
} else { |
| 204 |
nextTick = now.atZone(zone).plusMinutes(1).withSecond(0).withNano(0); |
| 205 |
} |
| 206 |
|
| 207 |
long millisUntilNextTick = Duration.between(now, nextTick.toInstant()).toMillis(); |
| 208 |
if (millisUntilNextTick <= 0) { |
| 209 |
|
| 210 |
millisUntilNextTick = 1000; |
| 211 |
} |
| 212 |
|
| 213 |
postDelayed(this, millisUntilNextTick); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@SuppressWarnings("UnusedDeclaration") |
| 224 |
public TextClock(Context context) { |
| 225 |
super(context); |
| 226 |
init(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
@SuppressWarnings("UnusedDeclaration") |
| 241 |
public TextClock(Context context, AttributeSet attrs) { |
| 242 |
this(context, attrs, 0); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public TextClock(Context context, AttributeSet attrs, int defStyleAttr) { |
| 257 |
this(context, attrs, defStyleAttr, 0); |
| 258 |
} |
| 259 |
|
| 260 |
public TextClock(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 261 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 262 |
|
| 263 |
final TypedArray a = context.obtainStyledAttributes( |
| 264 |
attrs, R.styleable.TextClock, defStyleAttr, defStyleRes); |
| 265 |
saveAttributeDataForStyleable(context, R.styleable.TextClock, |
| 266 |
attrs, a, defStyleAttr, defStyleRes); |
| 267 |
try { |
| 268 |
mFormat12 = a.getText(R.styleable.TextClock_format12Hour); |
| 269 |
mFormat24 = a.getText(R.styleable.TextClock_format24Hour); |
| 270 |
mTimeZone = a.getString(R.styleable.TextClock_timeZone); |
| 271 |
} finally { |
| 272 |
a.recycle(); |
| 273 |
} |
| 274 |
|
| 275 |
init(); |
| 276 |
} |
| 277 |
|
| 278 |
private void init() { |
| 279 |
if (mFormat12 == null) { |
| 280 |
mFormat12 = getBestDateTimePattern("hm"); |
| 281 |
} |
| 282 |
if (mFormat24 == null) { |
| 283 |
mFormat24 = getBestDateTimePattern("Hm"); |
| 284 |
} |
| 285 |
|
| 286 |
createTime(mTimeZone); |
| 287 |
chooseFormat(); |
| 288 |
} |
| 289 |
|
| 290 |
private void createTime(String timeZone) { |
| 291 |
if (timeZone != null) { |
| 292 |
mTime = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); |
| 293 |
} else { |
| 294 |
mTime = Calendar.getInstance(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
@InspectableProperty |
| 309 |
@ExportedProperty |
| 310 |
public CharSequence getFormat12Hour() { |
| 311 |
return mFormat12; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
@RemotableViewMethod |
| 340 |
public void setFormat12Hour(CharSequence format) { |
| 341 |
mFormat12 = format; |
| 342 |
|
| 343 |
chooseFormat(); |
| 344 |
onTimeChanged(); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public void setContentDescriptionFormat12Hour(CharSequence format) { |
| 352 |
mDescFormat12 = format; |
| 353 |
|
| 354 |
chooseFormat(); |
| 355 |
onTimeChanged(); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
@InspectableProperty |
| 369 |
@ExportedProperty |
| 370 |
public CharSequence getFormat24Hour() { |
| 371 |
return mFormat24; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
@RemotableViewMethod |
| 399 |
public void setFormat24Hour(CharSequence format) { |
| 400 |
mFormat24 = format; |
| 401 |
|
| 402 |
chooseFormat(); |
| 403 |
onTimeChanged(); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public void setContentDescriptionFormat24Hour(CharSequence format) { |
| 411 |
mDescFormat24 = format; |
| 412 |
|
| 413 |
chooseFormat(); |
| 414 |
onTimeChanged(); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void setShowCurrentUserTime(boolean showCurrentUserTime) { |
| 425 |
mShowCurrentUserTime = showCurrentUserTime; |
| 426 |
|
| 427 |
chooseFormat(); |
| 428 |
onTimeChanged(); |
| 429 |
unregisterObserver(); |
| 430 |
registerObserver(); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public void refreshTime() { |
| 437 |
onTimeChanged(); |
| 438 |
invalidate(); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
@InspectableProperty(hasAttributeId = false) |
| 460 |
public boolean is24HourModeEnabled() { |
| 461 |
if (mShowCurrentUserTime) { |
| 462 |
return DateFormat.is24HourFormat(getContext(), ActivityManager.getCurrentUser()); |
| 463 |
} else { |
| 464 |
return DateFormat.is24HourFormat(getContext()); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
@InspectableProperty |
| 479 |
public String getTimeZone() { |
| 480 |
return mTimeZone; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
@RemotableViewMethod |
| 499 |
public void setTimeZone(String timeZone) { |
| 500 |
mTimeZone = timeZone; |
| 501 |
|
| 502 |
createTime(timeZone); |
| 503 |
onTimeChanged(); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 513 |
public CharSequence getFormat() { |
| 514 |
return mFormat; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
private void chooseFormat() { |
| 522 |
final boolean format24Requested = is24HourModeEnabled(); |
| 523 |
|
| 524 |
if (format24Requested) { |
| 525 |
mFormat = abc(mFormat24, mFormat12, getBestDateTimePattern("Hm")); |
| 526 |
mDescFormat = abc(mDescFormat24, mDescFormat12, mFormat); |
| 527 |
} else { |
| 528 |
mFormat = abc(mFormat12, mFormat24, getBestDateTimePattern("hm")); |
| 529 |
mDescFormat = abc(mDescFormat12, mDescFormat24, mFormat); |
| 530 |
} |
| 531 |
|
| 532 |
boolean hadSeconds = mHasSeconds; |
| 533 |
mHasSeconds = DateFormat.hasSeconds(mFormat); |
| 534 |
|
| 535 |
if (mShouldRunTicker && hadSeconds != mHasSeconds) { |
| 536 |
mTicker.run(); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
private String getBestDateTimePattern(String skeleton) { |
| 541 |
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance( |
| 542 |
getContext().getResources().getConfiguration().locale); |
| 543 |
return dtpg.getBestPattern(skeleton); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
private static CharSequence abc(CharSequence a, CharSequence b, CharSequence c) { |
| 550 |
return a == null ? (b == null ? c : b) : a; |
| 551 |
} |
| 552 |
|
| 553 |
@Override |
| 554 |
protected void onAttachedToWindow() { |
| 555 |
super.onAttachedToWindow(); |
| 556 |
|
| 557 |
if (!mRegistered) { |
| 558 |
mRegistered = true; |
| 559 |
|
| 560 |
registerReceiver(); |
| 561 |
registerObserver(); |
| 562 |
|
| 563 |
createTime(mTimeZone); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
@Override |
| 568 |
public void onVisibilityAggregated(boolean isVisible) { |
| 569 |
super.onVisibilityAggregated(isVisible); |
| 570 |
|
| 571 |
if (!mShouldRunTicker && isVisible) { |
| 572 |
mShouldRunTicker = true; |
| 573 |
mTicker.run(); |
| 574 |
} else if (mShouldRunTicker && !isVisible) { |
| 575 |
mShouldRunTicker = false; |
| 576 |
removeCallbacks(mTicker); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
@Override |
| 581 |
protected void onDetachedFromWindow() { |
| 582 |
super.onDetachedFromWindow(); |
| 583 |
|
| 584 |
if (mRegistered) { |
| 585 |
unregisterReceiver(); |
| 586 |
unregisterObserver(); |
| 587 |
|
| 588 |
mRegistered = false; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
@TestApi |
| 597 |
public void disableClockTick() { |
| 598 |
mStopTicking = true; |
| 599 |
} |
| 600 |
|
| 601 |
private void registerReceiver() { |
| 602 |
final IntentFilter filter = new IntentFilter(); |
| 603 |
|
| 604 |
filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 605 |
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
getContext().registerReceiverAsUser(mIntentReceiver, android.os.Process.myUserHandle(), |
| 615 |
filter, null, getHandler()); |
| 616 |
} |
| 617 |
|
| 618 |
private void registerObserver() { |
| 619 |
if (mRegistered) { |
| 620 |
if (mFormatChangeObserver == null) { |
| 621 |
mFormatChangeObserver = new FormatChangeObserver(getHandler()); |
| 622 |
} |
| 623 |
final ContentResolver resolver = getContext().getContentResolver(); |
| 624 |
Uri uri = Settings.System.getUriFor(Settings.System.TIME_12_24); |
| 625 |
if (mShowCurrentUserTime) { |
| 626 |
resolver.registerContentObserver(uri, true, |
| 627 |
mFormatChangeObserver, UserHandle.USER_ALL); |
| 628 |
} else { |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
resolver.registerContentObserver(uri, true, |
| 638 |
mFormatChangeObserver, UserHandle.myUserId()); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
private void unregisterReceiver() { |
| 644 |
getContext().unregisterReceiver(mIntentReceiver); |
| 645 |
} |
| 646 |
|
| 647 |
private void unregisterObserver() { |
| 648 |
if (mFormatChangeObserver != null) { |
| 649 |
final ContentResolver resolver = getContext().getContentResolver(); |
| 650 |
resolver.unregisterContentObserver(mFormatChangeObserver); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
@UnsupportedAppUsage |
| 658 |
private void onTimeChanged() { |
| 659 |
mTime.setTimeInMillis(System.currentTimeMillis()); |
| 660 |
setText(DateFormat.format(mFormat, mTime)); |
| 661 |
setContentDescription(DateFormat.format(mDescFormat, mTime)); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
@Override |
| 666 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 667 |
super.encodeProperties(stream); |
| 668 |
|
| 669 |
CharSequence s = getFormat12Hour(); |
| 670 |
stream.addProperty("format12Hour", s == null ? null : s.toString()); |
| 671 |
|
| 672 |
s = getFormat24Hour(); |
| 673 |
stream.addProperty("format24Hour", s == null ? null : s.toString()); |
| 674 |
stream.addProperty("format", mFormat == null ? null : mFormat.toString()); |
| 675 |
stream.addProperty("hasSeconds", mHasSeconds); |
| 676 |
} |
| 677 |
} |