Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ab3e993 authored by Pawit Pornkitprasan's avatar Pawit Pornkitprasan
Browse files

SystemUI: fix memory leaks

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
parent b82a3b5f
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -68,6 +68,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;
@@ -89,8 +90,7 @@ public class SearchPanelView extends FrameLayout implements
        mWm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
        mActionTarget = new ActionTarget(context);

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

    class GlowPadTriggerListener implements GlowPadView.OnTriggerListener {
@@ -137,9 +137,6 @@ public class SearchPanelView extends FrameLayout implements
        // TODO: fetch views
        mGlowPadView = (GlowPadView) findViewById(R.id.glow_pad_view);
        mGlowPadView.setOnTriggerListener(mGlowPadViewListener);

        updateSettings();
        setDrawables();
    }

    private void setDrawables() {
@@ -275,6 +272,21 @@ public class SearchPanelView extends FrameLayout implements
        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.
@@ -330,7 +342,10 @@ public class SearchPanelView extends FrameLayout implements
                        Settings.System.getUriFor(Settings.System.NAVIGATION_RING_TARGETS[i]),
                        false, this);
            }
            updateSettings();
        }

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

        @Override
+21 −2
Original line number Diff line number Diff line
@@ -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);
@@ -73,8 +74,8 @@ public class StatusBarIconView extends AnimatedImageView {
                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) {
@@ -245,6 +246,24 @@ public class StatusBarIconView extends AnimatedImageView {
        }
    }

    @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);