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

Commit e6200089 authored by David van Tonder's avatar David van Tonder Committed by Gerrit Code Review
Browse files

Merge "Reduce observers in SystemUI: StatusBarIconView" into cm-10.1

parents 831de7fe 31342f10
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);
            }
        }
    }
}