diff options
| author | Sunny Goyal <sunnygoyal@google.com> | 2021-05-19 12:49:04 -0700 |
|---|---|---|
| committer | Sunny Goyal <sunnygoyal@google.com> | 2021-05-19 12:51:15 -0700 |
| commit | 2d3879ea2dfcd72a625adc40c2bc0ce871a458ca (patch) | |
| tree | 9816ad4705e55021d7a7f6f4f386549046cb4281 /core/java/android/widget/AnalogClock.java | |
| parent | 85cf59de2ffab839eff2705f654a9d9fdc8665c9 (diff) | |
Attaching broadcast receiver on attach/detach
instead of everytime visibility changes
> Adding a broadcast receiver makes a synchronous system API call
which can delay draw pass
> Also removing TIME_TICK broadcast, instead using an internal timer
to aboud unnecessary calls while app is stopped
Bug: 188574817
Test: Manual
Change-Id: I37677aa40189bf4fb75e7e2372bda81ea3e6fda3
Diffstat (limited to 'core/java/android/widget/AnalogClock.java')
| -rw-r--r-- | core/java/android/widget/AnalogClock.java | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/core/java/android/widget/AnalogClock.java b/core/java/android/widget/AnalogClock.java index 786e6fce3af8..5bb8a8eccf50 100644 --- a/core/java/android/widget/AnalogClock.java +++ b/core/java/android/widget/AnalogClock.java @@ -567,12 +567,12 @@ public class AnalogClock extends View { } } - private void onVisible() { - if (!mVisible) { - mVisible = true; - IntentFilter filter = new IntentFilter(); + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + IntentFilter filter = new IntentFilter(); - filter.addAction(Intent.ACTION_TIME_TICK); + if (!mReceiverAttached) { filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); @@ -585,8 +585,7 @@ public class AnalogClock extends View { // user not the one the context is for. getContext().registerReceiverAsUser(mIntentReceiver, android.os.Process.myUserHandle(), filter, null, getHandler()); - - mSecondsTick.run(); + mReceiverAttached = true; } // NOTE: It's safe to do these after registering the receiver since the receiver always runs @@ -599,9 +598,25 @@ public class AnalogClock extends View { onTimeChanged(); } + @Override + protected void onDetachedFromWindow() { + if (mReceiverAttached) { + getContext().unregisterReceiver(mIntentReceiver); + mReceiverAttached = false; + } + super.onDetachedFromWindow(); + } + + private void onVisible() { + if (!mVisible) { + mVisible = true; + mSecondsTick.run(); + } + + } + private void onInvisible() { if (mVisible) { - getContext().unregisterReceiver(mIntentReceiver); removeCallbacks(mSecondsTick); mVisible = false; } @@ -751,6 +766,7 @@ public class AnalogClock extends View { invalidate(); } }; + private boolean mReceiverAttached; private final Runnable mSecondsTick = new Runnable() { @Override |
