diff options
| author | 2016-10-05 22:46:12 +0000 | |
|---|---|---|
| committer | 2016-10-05 22:46:12 +0000 | |
| commit | 4d855f15abb49085ce5b2794e63e684fd32f5db3 (patch) | |
| tree | ac44cea807868ee66bd36ae674fefb2e88645aec | |
| parent | 152c5695bc9bd55e2b48d0e25aec73bd91b929f4 (diff) | |
| parent | e12cd3bb3b855a8c51273d11a6ca14f829bb27bd (diff) | |
Do not listen for clock ticks, time changes, timezone changes, or time format changes when TextClock is not visible
am: e12cd3bb3b
Change-Id: I4ce1dcc5184f8cf03904a19631b9efe4be1b47fa
| -rw-r--r-- | core/java/android/widget/TextClock.java | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/core/java/android/widget/TextClock.java b/core/java/android/widget/TextClock.java index a585d75e5d7a..49380b745564 100644 --- a/core/java/android/widget/TextClock.java +++ b/core/java/android/widget/TextClock.java @@ -130,7 +130,7 @@ public class TextClock extends TextView { private CharSequence mDescFormat; - private boolean mAttached; + private boolean mRegistered; private Calendar mTime; private String mTimeZone; @@ -250,7 +250,7 @@ public class TextClock extends TextView { } createTime(mTimeZone); - // Wait until onAttachedToWindow() to handle the ticker + // Wait until registering for events to handle the ticker chooseFormat(false); } @@ -501,7 +501,7 @@ public class TextClock extends TextView { boolean hadSeconds = mHasSeconds; mHasSeconds = DateFormat.hasSeconds(mFormat); - if (handleTicker && mAttached && hadSeconds != mHasSeconds) { + if (handleTicker && mRegistered && hadSeconds != mHasSeconds) { if (hadSeconds) getHandler().removeCallbacks(mTicker); else mTicker.run(); } @@ -515,11 +515,9 @@ public class TextClock extends TextView { } @Override - protected void onAttachedToWindow() { - super.onAttachedToWindow(); - - if (!mAttached) { - mAttached = true; + public void onVisibilityAggregated(boolean isVisible) { + if (!mRegistered && isVisible) { + mRegistered = true; registerReceiver(); registerObserver(); @@ -531,20 +529,13 @@ public class TextClock extends TextView { } else { onTimeChanged(); } - } - } - - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - - if (mAttached) { + } else if (mRegistered && !isVisible) { unregisterReceiver(); unregisterObserver(); getHandler().removeCallbacks(mTicker); - mAttached = false; + mRegistered = false; } } @@ -567,7 +558,7 @@ public class TextClock extends TextView { } private void registerObserver() { - if (isAttachedToWindow()) { + if (mRegistered) { if (mFormatChangeObserver == null) { mFormatChangeObserver = new FormatChangeObserver(getHandler()); } |