NotificationAssistantService.java revision 85aa6cb1779635bb3b6b3ba739fc4ee3813bba3a
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 /** Notification was canceled by the user banning the topic. */ 99 public static final int REASON_TOPIC_BANNED = 14; 100 101 public class Adjustment { 102 int mImportance; 103 CharSequence mExplanation; 104 Uri mReference; 105 106 /** 107 * Create a notification importance adjustment. 108 * 109 * @param importance The final importance of the notification. 110 * @param explanation A human-readable justification for the adjustment. 111 * @param reference A reference to an external object that augments the 112 * explanation, such as a 113 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}, 114 * or null. 115 */ 116 public Adjustment(int importance, CharSequence explanation, Uri reference) { 117 mImportance = importance; 118 mExplanation = explanation; 119 mReference = reference; 120 } 121 } 122 123 @Override 124 public IBinder onBind(Intent intent) { 125 if (mWrapper == null) { 126 mWrapper = new NotificationAssistantWrapper(); 127 } 128 return mWrapper; 129 } 130 131 /** 132 * A notification was posted by an app. Called before alert. 133 * 134 * @param sbn the new notification 135 * @param importance the initial importance of the notification. 136 * @param user true if the initial importance reflects an explicit user preference. 137 * @return an adjustment or null to take no action, within 100ms. 138 */ 139 abstract public Adjustment onNotificationEnqueued(StatusBarNotification sbn, 140 int importance, boolean user); 141 142 /** 143 * The visibility of a notification has changed. 144 * 145 * @param key the notification key 146 * @param time milliseconds since midnight, January 1, 1970 UTC. 147 * @param visible true if the notification became visible, false if hidden. 148 */ 149 public void onNotificationVisibilityChanged(String key, long time, boolean visible) 150 { 151 // Do nothing, Override this to collect visibility statistics. 152 } 153 154 /** 155 * The user clicked on a notification. 156 * 157 * @param key the notification key 158 * @param time milliseconds since midnight, January 1, 1970 UTC. 159 */ 160 public void onNotificationClick(String key, long time) 161 { 162 // Do nothing, Override this to collect click statistics 163 } 164 165 /** 166 * The user clicked on a notification action. 167 * 168 * @param key the notification key 169 * @param time milliseconds since midnight, January 1, 1970 UTC. 170 * @param actionIndex the index of the action button that was pressed. 171 */ 172 public void onNotificationActionClick(String key, long time, int actionIndex) 173 { 174 // Do nothing, Override this to collect action button click statistics 175 } 176 177 /** 178 * A notification was removed. 179 180 * @param key the notification key 181 * @param time milliseconds since midnight, January 1, 1970 UTC. 182 * @param reason see {@link #REASON_LISTENER_CANCEL}, etc. 183 */ 184 public void onNotificationRemoved(String key, long time, int reason) { 185 // Do nothing, Override this to collect dismissal statistics 186 } 187 188 /** 189 * Change the importance of an existing notification. N.B. this won’t cause 190 * an existing notification to alert, but might allow a future update to 191 * this notification to alert. 192 * 193 * @param key the notification key 194 * @param adjustment the new importance with an explanation 195 */ 196 public final void adjustImportance(String key, Adjustment adjustment) 197 { 198 if (!isBound()) return; 199 try { 200 getNotificationInterface().setImportanceFromAssistant(mWrapper, key, 201 adjustment.mImportance, adjustment.mExplanation); 202 } catch (android.os.RemoteException ex) { 203 Log.v(TAG, "Unable to contact notification manager", ex); 204 } 205 } 206 207 /** 208 * Add an annotation to a an existing notification. The delete intent will 209 * be fired when the host notification is deleted, or when this annotation 210 * is removed or replaced. 211 * 212 * @param key the key of the notification to be annotated 213 * @param annotation the new annotation object 214 */ 215 public final void setAnnotation(String key, Notification annotation) 216 { 217 // TODO: pack up the annotation and send it to the NotificationManager. 218 } 219 220 /** 221 * Remove the annotation from a notification. 222 * 223 * @param key the key of the notification to be cleansed of annotatons 224 */ 225 public final void clearAnnotation(String key) 226 { 227 // TODO: ask the NotificationManager to clear the annotation. 228 } 229 230 private class NotificationAssistantWrapper extends NotificationListenerWrapper { 231 @Override 232 public void onNotificationEnqueued(IStatusBarNotificationHolder sbnHolder, 233 int importance, boolean user) throws RemoteException { 234 StatusBarNotification sbn; 235 try { 236 sbn = sbnHolder.get(); 237 } catch (RemoteException e) { 238 Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e); 239 return; 240 } 241 242 try { 243 Adjustment adjustment = 244 NotificationAssistantService.this.onNotificationEnqueued(sbn, importance, user); 245 if (adjustment != null) { 246 adjustImportance(sbn.getKey(), adjustment); 247 } 248 } catch (Throwable t) { 249 Log.w(TAG, "Error running onNotificationEnqueued", t); 250 } 251 } 252 253 @Override 254 public void onNotificationVisibilityChanged(String key, long time, boolean visible) 255 throws RemoteException { 256 try { 257 NotificationAssistantService.this.onNotificationVisibilityChanged(key, time, 258 visible); 259 } catch (Throwable t) { 260 Log.w(TAG, "Error running onNotificationVisibilityChanged", t); 261 } 262 } 263 264 @Override 265 public void onNotificationClick(String key, long time) throws RemoteException { 266 try { 267 NotificationAssistantService.this.onNotificationClick(key, time); 268 } catch (Throwable t) { 269 Log.w(TAG, "Error running onNotificationClick", t); 270 } 271 } 272 273 @Override 274 public void onNotificationActionClick(String key, long time, int actionIndex) 275 throws RemoteException { 276 try { 277 NotificationAssistantService.this.onNotificationActionClick(key, time, actionIndex); 278 } catch (Throwable t) { 279 Log.w(TAG, "Error running onNotificationActionClick", t); 280 } 281 } 282 283 @Override 284 public void onNotificationRemovedReason(String key, long time, int reason) 285 throws RemoteException { 286 try { 287 NotificationAssistantService.this.onNotificationRemoved(key, time, reason); 288 } catch (Throwable t) { 289 Log.w(TAG, "Error running onNotificationRemoved", t); 290 } 291 } 292 } 293} 294