NotificationAssistantService.java revision ab41eecf22352f54167ce9a272a397715ffd0015
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.service.notification; 18 19import android.annotation.SdkConstant; 20import android.annotation.SystemApi; 21import android.app.INotificationManager; 22import android.app.Notification; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.net.Uri; 27import android.os.IBinder; 28import android.os.Parcel; 29import android.os.Parcelable; 30import android.os.RemoteException; 31import android.os.ServiceManager; 32import android.util.Log; 33 34/** 35 * A service that helps the user manage notifications by modifying the 36 * relative importance of notifications. 37 * <p>To extend this class, you must declare the service in your manifest file with 38 * the {@link android.Manifest.permission#BIND_NOTIFICATION_ASSISTANT_SERVICE} permission 39 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p> 40 * <pre> 41 * <service android:name=".NotificationAssistant" 42 * android:label="@string/service_name" 43 * android:permission="android.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE"> 44 * <intent-filter> 45 * <action android:name="android.service.notification.NotificationAssistantService" /> 46 * </intent-filter> 47 * </service></pre> 48 */ 49public abstract class NotificationAssistantService extends NotificationListenerService { 50 private static final String TAG = "NotificationAssistant"; 51 52 /** 53 * The {@link Intent} that must be declared as handled by the service. 54 */ 55 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 56 public static final String SERVICE_INTERFACE 57 = "android.service.notification.NotificationAssistantService"; 58 59 /** Notification was canceled by the status bar reporting a click. */ 60 public static final int REASON_DELEGATE_CLICK = 1; 61 62 /** Notification was canceled by the status bar reporting a user dismissal. */ 63 public static final int REASON_DELEGATE_CANCEL = 2; 64 65 /** Notification was canceled by the status bar reporting a user dismiss all. */ 66 public static final int REASON_DELEGATE_CANCEL_ALL = 3; 67 68 /** Notification was canceled by the status bar reporting an inflation error. */ 69 public static final int REASON_DELEGATE_ERROR = 4; 70 71 /** Notification was canceled by the package manager modifying the package. */ 72 public static final int REASON_PACKAGE_CHANGED = 5; 73 74 /** Notification was canceled by the owning user context being stopped. */ 75 public static final int REASON_USER_STOPPED = 6; 76 77 /** Notification was canceled by the user banning the package. */ 78 public static final int REASON_PACKAGE_BANNED = 7; 79 80 /** Notification was canceled by the app canceling this specific notification. */ 81 public static final int REASON_APP_CANCEL = 8; 82 83 /** Notification was canceled by the app cancelling all its notifications. */ 84 public static final int REASON_APP_CANCEL_ALL = 9; 85 86 /** Notification was canceled by a listener reporting a user dismissal. */ 87 public static final int REASON_LISTENER_CANCEL = 10; 88 89 /** Notification was canceled by a listener reporting a user dismiss all. */ 90 public static final int REASON_LISTENER_CANCEL_ALL = 11; 91 92 /** Notification was canceled because it was a member of a canceled group. */ 93 public static final int REASON_GROUP_SUMMARY_CANCELED = 12; 94 95 /** Notification was canceled because it was an invisible member of a group. */ 96 public static final int REASON_GROUP_OPTIMIZATION = 13; 97 98 public class Adjustment { 99 int mImportance; 100 CharSequence mExplanation; 101 Uri mReference; 102 103 /** 104 * Create a notification importance adjustment. 105 * 106 * @param importance The final importance of the notification. 107 * @param explanation A human-readable justification for the adjustment. 108 * @param reference A reference to an external object that augments the 109 * explanation, such as a 110 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}, 111 * or null. 112 */ 113 public Adjustment(int importance, CharSequence explanation, Uri reference) { 114 mImportance = importance; 115 mExplanation = explanation; 116 mReference = reference; 117 } 118 } 119 120 @Override 121 public IBinder onBind(Intent intent) { 122 if (mWrapper == null) { 123 mWrapper = new NotificationAssistantWrapper(); 124 } 125 return mWrapper; 126 } 127 128 /** 129 * A notification was posted by an app. Called before alert. 130 * 131 * @param sbn the new notification 132 * @param importance the initial importance of the notification. 133 * @param user true if the initial importance reflects an explicit user preference. 134 * @return an adjustment or null to take no action, within 100ms. 135 */ 136 abstract public Adjustment onNotificationEnqueued(StatusBarNotification sbn, 137 int importance, boolean user); 138 139 /** 140 * The visibility of a notification has changed. 141 * 142 * @param key the notification key 143 * @param time milliseconds since midnight, January 1, 1970 UTC. 144 * @param visible true if the notification became visible, false if hidden. 145 */ 146 public void onNotificationVisibilityChanged(String key, long time, boolean visible) 147 { 148 // Do nothing, Override this to collect visibility statistics. 149 } 150 151 /** 152 * The user clicked on a notification. 153 * 154 * @param key the notification key 155 * @param time milliseconds since midnight, January 1, 1970 UTC. 156 */ 157 public void onNotificationClick(String key, long time) 158 { 159 // Do nothing, Override this to collect click statistics 160 } 161 162 /** 163 * The user clicked on a notification action. 164 * 165 * @param key the notification key 166 * @param time milliseconds since midnight, January 1, 1970 UTC. 167 * @param actionIndex the index of the action button that was pressed. 168 */ 169 public void onNotificationActionClick(String key, long time, int actionIndex) 170 { 171 // Do nothing, Override this to collect action button click statistics 172 } 173 174 /** 175 * A notification was removed. 176 177 * @param key the notification key 178 * @param time milliseconds since midnight, January 1, 1970 UTC. 179 * @param reason see {@link #REASON_LISTENER_CANCEL}, etc. 180 */ 181 public void onNotificationRemoved(String key, long time, int reason) { 182 // Do nothing, Override this to collect dismissal statistics 183 } 184 185 /** 186 * Change the importance of an existing notification. N.B. this won’t cause 187 * an existing notification to alert, but might allow a future update to 188 * this notification to alert. 189 * 190 * @param key the notification key 191 * @param adjustment the new importance with an explanation 192 */ 193 public final void adjustImportance(String key, Adjustment adjustment) 194 { 195 if (!isBound()) return; 196 try { 197 getNotificationInterface().setImportanceFromAssistant(mWrapper, key, 198 adjustment.mImportance, adjustment.mExplanation); 199 } catch (android.os.RemoteException ex) { 200 Log.v(TAG, "Unable to contact notification manager", ex); 201 } 202 } 203 204 /** 205 * Add an annotation to a an existing notification. The delete intent will 206 * be fired when the host notification is deleted, or when this annotation 207 * is removed or replaced. 208 * 209 * @param key the key of the notification to be annotated 210 * @param annotation the new annotation object 211 */ 212 public final void setAnnotation(String key, Notification annotation) 213 { 214 // TODO: pack up the annotation and send it to the NotificationManager. 215 } 216 217 /** 218 * Remove the annotation from a notification. 219 * 220 * @param key the key of the notification to be cleansed of annotatons 221 */ 222 public final void clearAnnotation(String key) 223 { 224 // TODO: ask the NotificationManager to clear the annotation. 225 } 226 227 private class NotificationAssistantWrapper extends NotificationListenerWrapper { 228 @Override 229 public void onNotificationEnqueued(IStatusBarNotificationHolder sbnHolder, 230 int importance, boolean user) throws RemoteException { 231 StatusBarNotification sbn; 232 try { 233 sbn = sbnHolder.get(); 234 } catch (RemoteException e) { 235 Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e); 236 return; 237 } 238 239 try { 240 Adjustment adjustment = 241 NotificationAssistantService.this.onNotificationEnqueued(sbn, importance, user); 242 if (adjustment != null) { 243 adjustImportance(sbn.getKey(), adjustment); 244 } 245 } catch (Throwable t) { 246 Log.w(TAG, "Error running onNotificationEnqueued", t); 247 } 248 } 249 250 @Override 251 public void onNotificationVisibilityChanged(String key, long time, boolean visible) 252 throws RemoteException { 253 try { 254 NotificationAssistantService.this.onNotificationVisibilityChanged(key, time, 255 visible); 256 } catch (Throwable t) { 257 Log.w(TAG, "Error running onNotificationVisibilityChanged", t); 258 } 259 } 260 261 @Override 262 public void onNotificationClick(String key, long time) throws RemoteException { 263 try { 264 NotificationAssistantService.this.onNotificationClick(key, time); 265 } catch (Throwable t) { 266 Log.w(TAG, "Error running onNotificationClick", t); 267 } 268 } 269 270 @Override 271 public void onNotificationActionClick(String key, long time, int actionIndex) 272 throws RemoteException { 273 try { 274 NotificationAssistantService.this.onNotificationActionClick(key, time, actionIndex); 275 } catch (Throwable t) { 276 Log.w(TAG, "Error running onNotificationActionClick", t); 277 } 278 } 279 280 @Override 281 public void onNotificationRemovedReason(String key, long time, int reason) 282 throws RemoteException { 283 try { 284 NotificationAssistantService.this.onNotificationRemoved(key, time, reason); 285 } catch (Throwable t) { 286 Log.w(TAG, "Error running onNotificationRemoved", t); 287 } 288 } 289 } 290} 291