Skip to content

Commit

Permalink
SystemUI: fix memory leaks
Browse files Browse the repository at this point in the history
ContentObservers are being registered without being unregistered,
causing old objects not to be garbage collected. This causes memory
leak if the classes are destroyed and recreated throughout the
device's uptime.

SearchPanelView is recreated every time the screen is turned on.
StatusBarIconView is created every time a notification is triggered.

JIRA: CYAN-650
Change-Id: Iabd3221fc2b0c848396af1790fd09424be602e7e

Conflicts:

	packages/SystemUI/src/com/android/systemui/SearchPanelView.java
  • Loading branch information
pawitp authored and davros- committed Apr 27, 2013
1 parent 3226225 commit d2fd508
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
23 changes: 20 additions & 3 deletions packages/SystemUI/src/com/android/systemui/SearchPanelView.java
Expand Up @@ -103,6 +103,7 @@ public class SearchPanelView extends FrameLayout implements
private final Context mContext;
private BaseStatusBar mBar;
private StatusBarTouchProxy mStatusBarTouchProxy;
private SettingsObserver mObserver;

private boolean mShowing;
private View mSearchTargetsContainer;
Expand Down Expand Up @@ -238,9 +239,6 @@ protected void onFinishInflate() {
// TODO: fetch views
mGlowPadView = (GlowPadView) findViewById(R.id.glow_pad_view);
mGlowPadView.setOnTriggerListener(mGlowPadViewListener);

updateSettings();
setDrawables();
}

private void setDrawables() {
Expand Down Expand Up @@ -477,6 +475,21 @@ public boolean dispatchHoverEvent(MotionEvent event) {
return true;
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();

mObserver.observe();
updateSettings();
setDrawables();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mObserver.unobserve();
}

/**
* Whether the panel is showing, or, if it's animating, whether it will be
* when the animation is done.
Expand Down Expand Up @@ -561,6 +574,10 @@ void observe() {

}

void unobserve() {
mContext.getContentResolver().unregisterContentObserver(this);
}

@Override
public void onChange(boolean selfChange) {
updateSettings();
Expand Down
Expand Up @@ -55,6 +55,7 @@ public class StatusBarIconView extends AnimatedImageView {
private String mNumberText;
private Notification mNotification;
private boolean mShowNotificationCount;
private SettingsObserver mObserver;

public StatusBarIconView(Context context, String slot, Notification notification) {
super(context);
Expand All @@ -73,8 +74,8 @@ public StatusBarIconView(Context context, String slot, Notification notification
Settings.System.STATUS_BAR_NOTIF_COUNT, 0) == 1;
setContentDescription(notification);

SettingsObserver observer = new SettingsObserver(new Handler());
observer.observe();
mObserver = new SettingsObserver(new Handler());

// We do not resize and scale system icons (on the right), only notification icons (on the
// left).
if (notification != null) {
Expand Down Expand Up @@ -245,6 +246,24 @@ protected void onDraw(Canvas canvas) {
}
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();

if (mObserver != null) {
mObserver.observe();
}
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();

if (mObserver != null) {
mObserver.unobserve();
}
}

@Override
protected void debug(int depth) {
super.debug(depth);
Expand Down

0 comments on commit d2fd508

Please sign in to comment.