diff options
author | Adam Powell <adamp@google.com> | 2012-06-22 10:44:27 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-06-22 10:44:27 -0700 |
commit | 3cd4624448dcaddecc75461ef645da72e7e5c098 (patch) | |
tree | cf834d0a0d2caf42537f681c43e6ef1ef2e8428e | |
parent | 0b9b053ce6fdc48e922b6af37fe747b4ef40324a (diff) | |
parent | 101c44915259fb76d14f917712a51fb0b08a7594 (diff) | |
download | frameworks_base-3cd4624448dcaddecc75461ef645da72e7e5c098.zip frameworks_base-3cd4624448dcaddecc75461ef645da72e7e5c098.tar.gz frameworks_base-3cd4624448dcaddecc75461ef645da72e7e5c098.tar.bz2 |
Merge "Avoid memory leak by only registering callback while attached to window." into jb-dev
-rw-r--r-- | core/java/android/app/MediaRouteButton.java | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/core/java/android/app/MediaRouteButton.java b/core/java/android/app/MediaRouteButton.java index 5863ad0..65f9f87 100644 --- a/core/java/android/app/MediaRouteButton.java +++ b/core/java/android/app/MediaRouteButton.java @@ -39,6 +39,8 @@ public class MediaRouteButton extends View { private final MediaRouteCallback mRouterCallback = new MediaRouteCallback(); private int mRouteTypes; + private boolean mAttachedToWindow; + private Drawable mRemoteIndicator; private boolean mRemoteActive; private boolean mToggleMode; @@ -132,13 +134,22 @@ public class MediaRouteButton extends View { // Already registered; nothing to do. return; } - if (mRouteTypes != 0) { + + if (mAttachedToWindow && mRouteTypes != 0) { mRouter.removeCallback(mRouterCallback); } + mRouteTypes = types; + + if (mAttachedToWindow) { + updateRouteInfo(); + mRouter.addCallback(types, mRouterCallback); + } + } + + private void updateRouteInfo() { updateRemoteIndicator(); updateRouteCount(); - mRouter.addCallback(types, mRouterCallback); } public int getRouteTypes() { @@ -214,6 +225,25 @@ public class MediaRouteButton extends View { } @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + mAttachedToWindow = true; + if (mRouteTypes != 0) { + mRouter.addCallback(mRouteTypes, mRouterCallback); + updateRouteInfo(); + } + } + + @Override + public void onDetachedFromWindow() { + if (mRouteTypes != 0) { + mRouter.removeCallback(mRouterCallback); + } + mAttachedToWindow = false; + super.onDetachedFromWindow(); + } + + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int widthSize = MeasureSpec.getSize(widthMeasureSpec); final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |