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

Commit 60748e71 authored by Christoph Studer's avatar Christoph Studer Committed by Dan Sandler
Browse files

SysUI: Implement NotificationListenerService in StatusBar

This is currently disabled by constant that is off.

Bug: 15131411
Change-Id: I0da6e5b3b81c87004f0794d3056c4cf0ecbb1d61
parent 3e144d3d
Loading
Loading
Loading
Loading
+89 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.app.Notification;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -46,6 +47,7 @@ import android.os.UserManager;
import android.provider.Settings;
import android.service.dreams.DreamService;
import android.service.dreams.IDreamManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;
@@ -79,6 +81,7 @@ import com.android.systemui.statusbar.phone.KeyguardTouchDelegate;
import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

public abstract class BaseStatusBar extends SystemUI implements
@@ -86,6 +89,7 @@ public abstract class BaseStatusBar extends SystemUI implements
    public static final String TAG = "StatusBar";
    public static final boolean DEBUG = false;
    public static final boolean MULTIUSER_DEBUG = false;
    private static final boolean USE_NOTIFICATION_LISTENER = false;

    protected static final int MSG_SHOW_RECENT_APPS = 1019;
    protected static final int MSG_HIDE_RECENT_APPS = 1020;
@@ -158,6 +162,7 @@ public abstract class BaseStatusBar extends SystemUI implements

    protected WindowManager mWindowManager;
    protected IWindowManager mWindowManagerService;

    protected abstract void refreshLayout(int layoutDirection);

    protected Display mDisplay;
@@ -253,6 +258,49 @@ public abstract class BaseStatusBar extends SystemUI implements
        }
    };

    private final NotificationListenerService mNotificationListener =
            new NotificationListenerService() {
        @Override
        public void onListenerConnected() {
            if (DEBUG) Log.d(TAG, "onListenerConnected");
            final StatusBarNotification[] notifications = getActiveNotifications();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    for (StatusBarNotification sbn : notifications) {
                        addNotificationInternal(sbn);
                    }
                }
            });
        }

        @Override
        public void onNotificationPosted(final StatusBarNotification sbn) {
            if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mNotificationData.findByKey(sbn.getKey()) != null) {
                        updateNotificationInternal(sbn);
                    } else {
                        addNotificationInternal(sbn);
                    }
                }
            });
        }

        @Override
        public void onNotificationRemoved(final StatusBarNotification sbn) {
            if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    removeNotificationInternal(sbn.getKey());
                }
            });
        }
    };

    private void updateCurrentProfilesCache() {
        synchronized (mCurrentProfiles) {
            mCurrentProfiles.clear();
@@ -332,10 +380,21 @@ public abstract class BaseStatusBar extends SystemUI implements
        }

        // Set up the initial notification state.
        if (USE_NOTIFICATION_LISTENER) {
            try {
                mNotificationListener.registerAsSystemService(
                        new ComponentName(mContext.getPackageName(), getClass().getCanonicalName()),
                        UserHandle.USER_ALL);
            } catch (RemoteException e) {
                Log.e(TAG, "Unable to register notification listener", e);
            }
        } else {
            N = notifications.size();
            for (int i=0; i<N; i++) {
                addNotification(notifications.get(i));
            }
        }


        if (DEBUG) {
            Log.d(TAG, String.format(
@@ -1161,7 +1220,32 @@ public abstract class BaseStatusBar extends SystemUI implements
        return parent != null && parent.indexOfChild(entry.row) == 0;
    }


    @Override
    public void addNotification(StatusBarNotification notification) {
        if (!USE_NOTIFICATION_LISTENER) {
            addNotificationInternal(notification);
        }
    }

    public abstract void addNotificationInternal(StatusBarNotification notification);

    @Override
    public void removeNotification(String key) {
        if (!USE_NOTIFICATION_LISTENER) {
            removeNotificationInternal(key);
        }
    }

    protected abstract void removeNotificationInternal(String key);

    public void updateNotification(StatusBarNotification notification) {
        if (!USE_NOTIFICATION_LISTENER) {
            updateNotificationInternal(notification);
        }
    }

    public void updateNotificationInternal(StatusBarNotification notification) {
        if (DEBUG) Log.d(TAG, "updateNotification(" + notification + ")");

        final NotificationData.Entry oldEntry = mNotificationData.findByKey(notification.getKey());
+4 −4
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ public class InterceptedNotifications {
        for (int i = 0; i < n; i++) {
            final StatusBarNotification sbn = mIntercepted.valueAt(i);
            sbn.getNotification().extras.putBoolean(EXTRA_INTERCEPT, false);
            mBar.addNotification(sbn);
            mBar.addNotificationInternal(sbn);
        }
        mIntercepted.clear();
        updateSyntheticNotification();
@@ -88,7 +88,7 @@ public class InterceptedNotifications {
    private void updateSyntheticNotification() {
        if (mIntercepted.isEmpty()) {
            if (mSynKey != null) {
                mBar.removeNotification(mSynKey);
                mBar.removeNotificationInternal(mSynKey);
                mSynKey = null;
            }
            return;
@@ -107,9 +107,9 @@ public class InterceptedNotifications {
                mBar.getCurrentUserHandle());
        if (mSynKey == null) {
            mSynKey = sbn.getKey();
            mBar.addNotification(sbn);
            mBar.addNotificationInternal(sbn);
        } else {
           mBar.updateNotification(sbn);
           mBar.updateNotificationInternal(sbn);
        }
        final NotificationData.Entry entry = mBar.mNotificationData.findByKey(mSynKey);
        entry.row.setOnClickListener(mSynClickListener);
+2 −2
Original line number Diff line number Diff line
@@ -1028,7 +1028,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
    }

    @Override
    public void addNotification(StatusBarNotification notification) {
    public void addNotificationInternal(StatusBarNotification notification) {
        if (DEBUG) Log.d(TAG, "addNotification score=" + notification.getScore());
        Entry shadeEntry = createNotificationViews(notification);
        if (shadeEntry == null) {
@@ -1096,7 +1096,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
    }

    @Override
    public void removeNotification(String key) {
    public void removeNotificationInternal(String key) {
        StatusBarNotification old = removeNotificationViews(key);
        if (SPEW) Log.d(TAG, "removeNotification key=" + key + " old=" + old);

+8 −0
Original line number Diff line number Diff line
@@ -49,10 +49,18 @@ public class TvStatusBar extends BaseStatusBar {
    public void addNotification(StatusBarNotification notification) {
    }

    @Override
    public void addNotificationInternal(StatusBarNotification notification) {
    }

    @Override
    public void updateNotification(StatusBarNotification notification) {
    }

    @Override
    protected void removeNotificationInternal(String key) {
    }

    @Override
    public void removeNotification(String key) {
    }