diff options
author | John Spurlock <jspurlock@google.com> | 2014-02-12 12:12:26 -0500 |
---|---|---|
committer | John Spurlock <jspurlock@google.com> | 2014-02-12 12:35:10 -0500 |
commit | da9a3bed8e1aa7d7867291a123466bb0a3be5bb0 (patch) | |
tree | 5ff443cee4c06f021e2ea9325fd0b50c52c59b1b /core/java/android/service | |
parent | 3ff18faac21553f027cdb1ff4a98447f333750bd (diff) | |
download | frameworks_base-da9a3bed8e1aa7d7867291a123466bb0a3be5bb0.zip frameworks_base-da9a3bed8e1aa7d7867291a123466bb0a3be5bb0.tar.gz frameworks_base-da9a3bed8e1aa7d7867291a123466bb0a3be5bb0.tar.bz2 |
Improve error handling in listener services.
Check explicitly for null listeners in NMS, throwing
IllegalArgumentException (on the small list of exceptions
that survive RPC boundaries) with a message.
Normally this situation is caused by listeners that attempt to
perform NM-related actions before they are bound. Check for
this case in the base NLS class and avoid the call to NM if we
know it will fail.
Although it's tempting to throw an IllegalStateException on the
client side, preserve the existing semantics for backwards-compatibility
purposes. That is, silently fail (or return null) - and provide a
log warning.
Bug:12805707
Change-Id: I0d92fd0d460a8592e8a23fd8fd718ae2ba3bd4c7
Diffstat (limited to 'core/java/android/service')
-rw-r--r-- | core/java/android/service/notification/NotificationListenerService.java | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/core/java/android/service/notification/NotificationListenerService.java b/core/java/android/service/notification/NotificationListenerService.java index 2e0e59b..cf862b8 100644 --- a/core/java/android/service/notification/NotificationListenerService.java +++ b/core/java/android/service/notification/NotificationListenerService.java @@ -112,6 +112,7 @@ public abstract class NotificationListenerService extends Service { * {@link android.app.NotificationManager#notify(String, int, android.app.Notification)}. */ public final void cancelNotification(String pkg, String tag, int id) { + if (!isBound()) return; try { getNotificationInterface().cancelNotificationFromListener(mWrapper, pkg, tag, id); } catch (android.os.RemoteException ex) { @@ -131,6 +132,7 @@ public abstract class NotificationListenerService extends Service { * {@see #cancelNotification(String, String, int)} */ public final void cancelAllNotifications() { + if (!isBound()) return; try { getNotificationInterface().cancelAllNotificationsFromListener(mWrapper); } catch (android.os.RemoteException ex) { @@ -145,6 +147,7 @@ public abstract class NotificationListenerService extends Service { * @return An array of active notifications. */ public StatusBarNotification[] getActiveNotifications() { + if (!isBound()) return null; try { return getNotificationInterface().getActiveNotificationsFromListener(mWrapper); } catch (android.os.RemoteException ex) { @@ -161,6 +164,14 @@ public abstract class NotificationListenerService extends Service { return mWrapper; } + private boolean isBound() { + if (mWrapper == null) { + Log.w(TAG, "Notification listener service not yet bound."); + return false; + } + return true; + } + private class INotificationListenerWrapper extends INotificationListener.Stub { @Override public void onNotificationPosted(StatusBarNotification sbn) { |