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

Commit 31342f10 authored by Jens Doll's avatar Jens Doll
Browse files

Reduce observers in SystemUI: StatusBarIconView

Currently, for each status bar icon view there is a observer that
listens for changes of STATUS_BAR_NOTIF_COUNT. With one
notification usually having one or more status bar icons
you may end up easily with 10+ more observers for ~5 notifications.

With this commit the internal observer class becomes a singleton.
This means there is now only one observer for all status bar icons
and additional binder calls on new notification are avoided.

See "dumpsys content"

Change-Id: I8c11b165b34b87e7363a8e96524ea70fe6b28342
parent a6ae221c
Loading
Loading
Loading
Loading
+41 −9
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.view.accessibility.AccessibilityEvent;
import android.widget.ImageView;

import java.text.NumberFormat;
import java.util.ArrayList;

import com.android.internal.statusbar.StatusBarIcon;

@@ -55,7 +56,7 @@ public class StatusBarIconView extends AnimatedImageView {
    private String mNumberText;
    private Notification mNotification;
    private boolean mShowNotificationCount;
    private SettingsObserver mObserver;
    private GlobalSettingsObserver mObserver;

    public StatusBarIconView(Context context, String slot, Notification notification) {
        super(context);
@@ -74,7 +75,7 @@ public class StatusBarIconView extends AnimatedImageView {
                Settings.System.STATUS_BAR_NOTIF_COUNT, 0) == 1;
        setContentDescription(notification);

        mObserver = new SettingsObserver(new Handler());
        mObserver = GlobalSettingsObserver.getInstance(context);

        // We do not resize and scale system icons (on the right), only notification icons (on the
        // left).
@@ -251,7 +252,7 @@ public class StatusBarIconView extends AnimatedImageView {
        super.onAttachedToWindow();

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

@@ -260,7 +261,7 @@ public class StatusBarIconView extends AnimatedImageView {
        super.onDetachedFromWindow();

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

@@ -318,24 +319,55 @@ public class StatusBarIconView extends AnimatedImageView {
            + " notification=" + mNotification + ")";
    }

    class SettingsObserver extends ContentObserver {
        SettingsObserver(Handler handler) {
    static class GlobalSettingsObserver extends ContentObserver {
        private static GlobalSettingsObserver sInstance;
        private ArrayList<StatusBarIconView> mIconViews = new ArrayList<StatusBarIconView> ();
        private Context mContext;

        GlobalSettingsObserver(Handler handler, Context context) {
            super(handler);
            mContext = context.getApplicationContext();
        }

        static GlobalSettingsObserver getInstance(Context context) {
            if (sInstance == null) {
                sInstance = new GlobalSettingsObserver(new Handler(), context);
            }
            return sInstance;
        }

        void attach(StatusBarIconView sbiv) {
            if (mIconViews.isEmpty()) {
                observe();
            }
            mIconViews.add(sbiv);
        }

        void detach(StatusBarIconView sbiv) {
            mIconViews.remove(sbiv);
            if (mIconViews.isEmpty()) {
                unobserve();
            }
        }

        void observe() {
            mContext.getContentResolver().registerContentObserver(
                    Settings.System.getUriFor(Settings.System.STATUS_BAR_NOTIF_COUNT),
                    false, this);
        }

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

        @Override
        public void onChange(boolean selfChange) {
            mShowNotificationCount = Settings.System.getInt(
                    mContext.getContentResolver(),
            boolean showIconCount = Settings.System.getInt(mContext.getContentResolver(),
                    Settings.System.STATUS_BAR_NOTIF_COUNT, 0) == 1;
            set(mIcon, true);
            for (StatusBarIconView sbiv : mIconViews) {
                sbiv.mShowNotificationCount = showIconCount;
                sbiv.set(sbiv.mIcon, true);
            }
        }
    }
}