Merge "Attaching broadcast receiver on attach/detach instead of everytime visibility changes" into sc-dev am: 27500d78b3
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14648155 Change-Id: I26133a553000b635cbfaea14f9287b7eb1f444b0
This commit is contained in:
@@ -567,12 +567,12 @@ public class AnalogClock extends View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onVisible() {
|
@Override
|
||||||
if (!mVisible) {
|
protected void onAttachedToWindow() {
|
||||||
mVisible = true;
|
super.onAttachedToWindow();
|
||||||
IntentFilter filter = new IntentFilter();
|
IntentFilter filter = new IntentFilter();
|
||||||
|
|
||||||
filter.addAction(Intent.ACTION_TIME_TICK);
|
if (!mReceiverAttached) {
|
||||||
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
||||||
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
||||||
|
|
||||||
@@ -585,8 +585,7 @@ public class AnalogClock extends View {
|
|||||||
// user not the one the context is for.
|
// user not the one the context is for.
|
||||||
getContext().registerReceiverAsUser(mIntentReceiver,
|
getContext().registerReceiverAsUser(mIntentReceiver,
|
||||||
android.os.Process.myUserHandle(), filter, null, getHandler());
|
android.os.Process.myUserHandle(), filter, null, getHandler());
|
||||||
|
mReceiverAttached = true;
|
||||||
mSecondsTick.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: It's safe to do these after registering the receiver since the receiver always runs
|
// 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();
|
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() {
|
private void onInvisible() {
|
||||||
if (mVisible) {
|
if (mVisible) {
|
||||||
getContext().unregisterReceiver(mIntentReceiver);
|
|
||||||
removeCallbacks(mSecondsTick);
|
removeCallbacks(mSecondsTick);
|
||||||
mVisible = false;
|
mVisible = false;
|
||||||
}
|
}
|
||||||
@@ -751,6 +766,7 @@ public class AnalogClock extends View {
|
|||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private boolean mReceiverAttached;
|
||||||
|
|
||||||
private final Runnable mSecondsTick = new Runnable() {
|
private final Runnable mSecondsTick = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user