NotificationAssistantService.java revision 77b2cc920fb27adaa156b463dccb0bd1b5c87eb9
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.content.Context; 21import android.content.Intent; 22import android.os.Handler; 23import android.os.IBinder; 24import android.os.Looper; 25import android.os.Message; 26import android.os.RemoteException; 27import android.util.Log; 28import com.android.internal.os.SomeArgs; 29 30import java.util.List; 31 32/** 33 * A service that helps the user manage notifications. 34 */ 35public abstract class NotificationAssistantService extends NotificationListenerService { 36 private static final String TAG = "NotificationAssistants"; 37 38 /** 39 * The {@link Intent} that must be declared as handled by the service. 40 */ 41 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 42 public static final String SERVICE_INTERFACE 43 = "android.service.notification.NotificationAssistantService"; 44 45 /** Notification was canceled by the status bar reporting a click. */ 46 public static final int REASON_DELEGATE_CLICK = 1; 47 48 /** Notification was canceled by the status bar reporting a user dismissal. */ 49 public static final int REASON_DELEGATE_CANCEL = 2; 50 51 /** Notification was canceled by the status bar reporting a user dismiss all. */ 52 public static final int REASON_DELEGATE_CANCEL_ALL = 3; 53 54 /** Notification was canceled by the status bar reporting an inflation error. */ 55 public static final int REASON_DELEGATE_ERROR = 4; 56 57 /** Notification was canceled by the package manager modifying the package. */ 58 public static final int REASON_PACKAGE_CHANGED = 5; 59 60 /** Notification was canceled by the owning user context being stopped. */ 61 public static final int REASON_USER_STOPPED = 6; 62 63 /** Notification was canceled by the user banning the package. */ 64 public static final int REASON_PACKAGE_BANNED = 7; 65 66 /** Notification was canceled by the app canceling this specific notification. */ 67 public static final int REASON_APP_CANCEL = 8; 68 69 /** Notification was canceled by the app cancelling all its notifications. */ 70 public static final int REASON_APP_CANCEL_ALL = 9; 71 72 /** Notification was canceled by a listener reporting a user dismissal. */ 73 public static final int REASON_LISTENER_CANCEL = 10; 74 75 /** Notification was canceled by a listener reporting a user dismiss all. */ 76 public static final int REASON_LISTENER_CANCEL_ALL = 11; 77 78 /** Notification was canceled because it was a member of a canceled group. */ 79 public static final int REASON_GROUP_SUMMARY_CANCELED = 12; 80 81 /** Notification was canceled because it was an invisible member of a group. */ 82 public static final int REASON_GROUP_OPTIMIZATION = 13; 83 84 /** Notification was canceled by the device administrator suspending the package. */ 85 public static final int REASON_PACKAGE_SUSPENDED = 14; 86 87 /** Notification was canceled by the owning managed profile being turned off. */ 88 public static final int REASON_PROFILE_TURNED_OFF = 15; 89 90 /** Autobundled summary notification was canceled because its group was unbundled */ 91 public static final int REASON_UNAUTOBUNDLED = 16; 92 93 /** Notification was canceled by the user banning the channel. */ 94 public static final int REASON_CHANNEL_BANNED = 17; 95 96 /** Notification was snoozed. */ 97 public static final int REASON_SNOOZED = 18; 98 99 private Handler mHandler; 100 101 @Override 102 protected void attachBaseContext(Context base) { 103 super.attachBaseContext(base); 104 mHandler = new MyHandler(getContext().getMainLooper()); 105 } 106 107 @Override 108 public final IBinder onBind(Intent intent) { 109 if (mWrapper == null) { 110 mWrapper = new NotificationAssistantServiceWrapper(); 111 } 112 return mWrapper; 113 } 114 115 /** 116 * A notification was posted by an app. Called before alert. 117 * 118 * @param sbn the new notification 119 * @param importance the initial importance of the notification. 120 * @param user true if the initial importance reflects an explicit user preference. 121 * @return an adjustment or null to take no action, within 100ms. 122 */ 123 abstract public Adjustment onNotificationEnqueued(StatusBarNotification sbn, 124 int importance, boolean user); 125 126 /** 127 * A notification was removed. 128 129 * @param key the notification key 130 * @param time milliseconds since midnight, January 1, 1970 UTC. 131 * @param reason see {@link #REASON_LISTENER_CANCEL}, etc. 132 */ 133 public void onNotificationRemoved(String key, long time, int reason) { 134 // Do nothing, Override this to collect dismissal statistics 135 } 136 137 /** 138 * Updates a notification. N.B. this won’t cause 139 * an existing notification to alert, but might allow a future update to 140 * this notification to alert. 141 * 142 * @param adjustment the adjustment with an explanation 143 */ 144 public final void adjustNotification(Adjustment adjustment) { 145 if (!isBound()) return; 146 try { 147 getNotificationInterface().applyAdjustmentFromAssistantService(mWrapper, adjustment); 148 } catch (android.os.RemoteException ex) { 149 Log.v(TAG, "Unable to contact notification manager", ex); 150 } 151 } 152 153 /** 154 * Updates existing notifications. Re-ranking won't occur until all adjustments are applied. 155 * N.B. this won’t cause an existing notification to alert, but might allow a future update to 156 * these notifications to alert. 157 * 158 * @param adjustments a list of adjustments with explanations 159 */ 160 public final void adjustNotifications(List<Adjustment> adjustments) { 161 if (!isBound()) return; 162 try { 163 getNotificationInterface().applyAdjustmentsFromAssistantService(mWrapper, adjustments); 164 } catch (android.os.RemoteException ex) { 165 Log.v(TAG, "Unable to contact notification manager", ex); 166 } 167 } 168 169 private class NotificationAssistantServiceWrapper extends NotificationListenerWrapper { 170 @Override 171 public void onNotificationEnqueued(IStatusBarNotificationHolder sbnHolder, 172 int importance, boolean user) { 173 StatusBarNotification sbn; 174 try { 175 sbn = sbnHolder.get(); 176 } catch (RemoteException e) { 177 Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e); 178 return; 179 } 180 181 SomeArgs args = SomeArgs.obtain(); 182 args.arg1 = sbn; 183 args.argi1 = importance; 184 args.argi2 = user ? 1 : 0; 185 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_ENQUEUED, 186 args).sendToTarget(); 187 } 188 189 @Override 190 public void onNotificationRemovedReason(String key, long time, int reason) { 191 SomeArgs args = SomeArgs.obtain(); 192 args.arg1 = key; 193 args.arg2 = time; 194 args.argi1 = reason; 195 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_REMOVED_REASON, 196 args).sendToTarget(); 197 } 198 } 199 200 private final class MyHandler extends Handler { 201 public static final int MSG_ON_NOTIFICATION_ENQUEUED = 1; 202 public static final int MSG_ON_NOTIFICATION_REMOVED_REASON = 5; 203 204 public MyHandler(Looper looper) { 205 super(looper, null, false); 206 } 207 208 @Override 209 public void handleMessage(Message msg) { 210 switch (msg.what) { 211 case MSG_ON_NOTIFICATION_ENQUEUED: { 212 SomeArgs args = (SomeArgs) msg.obj; 213 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 214 final int importance = args.argi1; 215 final boolean user = args.argi2 == 1; 216 args.recycle(); 217 Adjustment adjustment = onNotificationEnqueued(sbn, importance, user); 218 if (adjustment != null) { 219 adjustNotification(adjustment); 220 } 221 } break; 222 223 case MSG_ON_NOTIFICATION_REMOVED_REASON: { 224 SomeArgs args = (SomeArgs) msg.obj; 225 final String key = (String) args.arg1; 226 final long time = (long) args.arg2; 227 final int reason = args.argi1; 228 args.recycle(); 229 onNotificationRemoved(key, time, reason); 230 } break; 231 } 232 } 233 } 234} 235