* A {@link NotificationListenerService} that sends updates to its
* {@link NotificationsChangedListener} when notifications are posted or canceled,
* as well and when this service first connects. An instance of NotificationListener,
- * and its methods for getting notifications, can be obtained via {@link #getInstance()}.
+ * and its methods for getting notifications, can be obtained via {@link #getInstanceIfConnected()}.
*/
public class NotificationListener extends NotificationListenerService {
private static NotificationListener sNotificationListenerInstance = null;
private static NotificationsChangedListener sNotificationsChangedListener;
+ private static boolean sIsConnected;
private final Handler mWorkerHandler;
private final Handler mUiHandler;
mUiHandler.obtainMessage(message.what, message.obj).sendToTarget();
break;
case MSG_NOTIFICATION_FULL_REFRESH:
- final List<StatusBarNotification> activeNotifications
- = filterNotifications(getActiveNotifications());
+ final List<StatusBarNotification> activeNotifications = sIsConnected
+ ? filterNotifications(getActiveNotifications())
+ : new ArrayList<StatusBarNotification>();
mUiHandler.obtainMessage(message.what, activeNotifications).sendToTarget();
break;
}
super();
mWorkerHandler = new Handler(LauncherModel.getWorkerLooper(), mWorkerCallback);
mUiHandler = new Handler(Looper.getMainLooper(), mUiCallback);
+ sNotificationListenerInstance = this;
}
- public static @Nullable NotificationListener getInstance() {
- return sNotificationListenerInstance;
+ public static @Nullable NotificationListener getInstanceIfConnected() {
+ return sIsConnected ? sNotificationListenerInstance : null;
}
public static void setNotificationsChangedListener(NotificationsChangedListener listener) {
}
sNotificationsChangedListener = listener;
- NotificationListener notificationListener = getInstance();
- if (notificationListener != null) {
- notificationListener.onNotificationFullRefresh();
+ if (sNotificationListenerInstance != null) {
+ sNotificationListenerInstance.onNotificationFullRefresh();
}
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
- sNotificationListenerInstance = this;
+ sIsConnected = true;
onNotificationFullRefresh();
}
@Override
public void onListenerDisconnected() {
super.onListenerDisconnected();
- sNotificationListenerInstance = null;
+ sIsConnected = false;
}
@Override
private boolean updateBadgeIcon(BadgeInfo badgeInfo) {
boolean hadNotificationToShow = badgeInfo.hasNotificationToShow();
NotificationInfo notificationInfo = null;
- NotificationListener notificationListener = NotificationListener.getInstance();
+ NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
if (notificationListener != null && badgeInfo.getNotificationKeys().size() == 1) {
StatusBarNotification[] activeNotifications = notificationListener
.getActiveNotifications(new String[] {badgeInfo.getNotificationKeys().get(0)});
/** This makes a potentially expensive binder call and should be run on a background thread. */
public List<StatusBarNotification> getStatusBarNotificationsForKeys(String[] notificationKeys) {
- NotificationListener notificationListener = NotificationListener.getInstance();
+ NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
return notificationListener == null ? Collections.EMPTY_LIST
: notificationListener.getNotificationsForKeys(notificationKeys);
}
public void cancelNotification(String notificationKey) {
- NotificationListener notificationListener = NotificationListener.getInstance();
+ NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
if (notificationListener == null) {
return;
}