NotificationAssistantService.java revision 7967230de20aeb6993d8332347752c8e508769e4
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.NonNull; 20import android.annotation.Nullable; 21import android.annotation.SdkConstant; 22import android.app.NotificationChannel; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Handler; 26import android.os.IBinder; 27import android.os.Looper; 28import android.os.Message; 29import android.os.RemoteException; 30import android.util.Log; 31import com.android.internal.os.SomeArgs; 32 33import java.util.ArrayList; 34import java.util.List; 35 36/** 37 * A service that helps the user manage notifications. 38 */ 39public abstract class NotificationAssistantService extends NotificationListenerService { 40 private static final String TAG = "NotificationAssistants"; 41 42 /** 43 * The {@link Intent} that must be declared as handled by the service. 44 */ 45 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 46 public static final String SERVICE_INTERFACE 47 = "android.service.notification.NotificationAssistantService"; 48 49 private Handler mHandler; 50 51 @Override 52 protected void attachBaseContext(Context base) { 53 super.attachBaseContext(base); 54 mHandler = new MyHandler(getContext().getMainLooper()); 55 } 56 57 @Override 58 public final IBinder onBind(Intent intent) { 59 if (mWrapper == null) { 60 mWrapper = new NotificationAssistantServiceWrapper(); 61 } 62 return mWrapper; 63 } 64 65 /** 66 * A notification was snoozed until a context. For use with 67 * {@link Adjustment#KEY_SNOOZE_CRITERIA}. When the device reaches the given context, the 68 * assistant should restore the notification with {@link #unsnoozeNotification(String)}. 69 * 70 * @param sbn the notification to snooze 71 * @param snoozeCriterionId the {@link SnoozeCriterion#getId()} representing a device context. 72 */ 73 abstract public void onNotificationSnoozedUntilContext(StatusBarNotification sbn, 74 String snoozeCriterionId); 75 76 /** 77 * A notification was posted by an app. Called before alert. 78 * 79 * @param sbn the new notification 80 * @param importance the initial importance of the notification. 81 * @param user true if the initial importance reflects an explicit user preference. 82 * @return an adjustment or null to take no action, within 100ms. 83 */ 84 abstract public Adjustment onNotificationEnqueued(StatusBarNotification sbn, 85 int importance, boolean user); 86 87 /** 88 * Updates a notification. N.B. this won’t cause 89 * an existing notification to alert, but might allow a future update to 90 * this notification to alert. 91 * 92 * @param adjustment the adjustment with an explanation 93 */ 94 public final void adjustNotification(Adjustment adjustment) { 95 if (!isBound()) return; 96 try { 97 getNotificationInterface().applyAdjustmentFromAssistant(mWrapper, adjustment); 98 } catch (android.os.RemoteException ex) { 99 Log.v(TAG, "Unable to contact notification manager", ex); 100 throw ex.rethrowFromSystemServer(); 101 } 102 } 103 104 /** 105 * Updates existing notifications. Re-ranking won't occur until all adjustments are applied. 106 * N.B. this won’t cause an existing notification to alert, but might allow a future update to 107 * these notifications to alert. 108 * 109 * @param adjustments a list of adjustments with explanations 110 */ 111 public final void adjustNotifications(List<Adjustment> adjustments) { 112 if (!isBound()) return; 113 try { 114 getNotificationInterface().applyAdjustmentsFromAssistant(mWrapper, adjustments); 115 } catch (android.os.RemoteException ex) { 116 Log.v(TAG, "Unable to contact notification manager", ex); 117 throw ex.rethrowFromSystemServer(); 118 } 119 } 120 121 /** 122 * Creates a notification channel that notifications can be posted to for a given package. 123 * 124 * @param pkg The package to create a channel for. 125 * @param channel the channel to attempt to create. 126 */ 127 public void createNotificationChannel(@NonNull String pkg, 128 @NonNull NotificationChannel channel) { 129 if (!isBound()) return; 130 try { 131 getNotificationInterface().createNotificationChannelFromAssistant( 132 mWrapper, pkg, channel); 133 } catch (RemoteException e) { 134 Log.v(TAG, "Unable to contact notification manager", e); 135 throw e.rethrowFromSystemServer(); 136 } 137 } 138 139 /** 140 * Updates a notification channel for a given package. 141 * 142 * @param pkg The package to the channel belongs to. 143 * @param channel the channel to attempt to update. 144 */ 145 public void updateNotificationChannel(@NonNull String pkg, 146 @NonNull NotificationChannel channel) { 147 if (!isBound()) return; 148 try { 149 getNotificationInterface().updateNotificationChannelFromAssistant( 150 mWrapper, pkg, channel); 151 } catch (RemoteException e) { 152 Log.v(TAG, "Unable to contact notification manager", e); 153 throw e.rethrowFromSystemServer(); 154 } 155 } 156 157 /** 158 * Returns all notification channels belonging to the given package. 159 */ 160 public List<NotificationChannel> getNotificationChannels(@NonNull String pkg) { 161 if (!isBound()) return null; 162 try { 163 return getNotificationInterface().getNotificationChannelsFromAssistant( 164 mWrapper, pkg).getList(); 165 } catch (RemoteException e) { 166 Log.v(TAG, "Unable to contact notification manager", e); 167 throw e.rethrowFromSystemServer(); 168 } 169 } 170 171 /** 172 * Deletes the given notification channel. 173 */ 174 public void deleteNotificationChannel(@NonNull String pkg, @NonNull String channelId) { 175 if (!isBound()) return; 176 try { 177 getNotificationInterface().deleteNotificationChannelFromAssistant( 178 mWrapper, pkg, channelId); 179 } catch (RemoteException e) { 180 throw e.rethrowFromSystemServer(); 181 } 182 } 183 184 185 private class NotificationAssistantServiceWrapper extends NotificationListenerWrapper { 186 @Override 187 public void onNotificationEnqueued(IStatusBarNotificationHolder sbnHolder, 188 int importance, boolean user) { 189 StatusBarNotification sbn; 190 try { 191 sbn = sbnHolder.get(); 192 } catch (RemoteException e) { 193 Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e); 194 return; 195 } 196 197 SomeArgs args = SomeArgs.obtain(); 198 args.arg1 = sbn; 199 args.argi1 = importance; 200 args.argi2 = user ? 1 : 0; 201 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_ENQUEUED, 202 args).sendToTarget(); 203 } 204 205 @Override 206 public void onNotificationSnoozedUntilContext( 207 IStatusBarNotificationHolder sbnHolder, String snoozeCriterionId) 208 throws RemoteException { 209 StatusBarNotification sbn; 210 try { 211 sbn = sbnHolder.get(); 212 } catch (RemoteException e) { 213 Log.w(TAG, "onNotificationSnoozed: Error receiving StatusBarNotification", e); 214 return; 215 } 216 217 SomeArgs args = SomeArgs.obtain(); 218 args.arg1 = sbn; 219 args.arg2 = snoozeCriterionId; 220 mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_SNOOZED, 221 args).sendToTarget(); 222 } 223 } 224 225 private final class MyHandler extends Handler { 226 public static final int MSG_ON_NOTIFICATION_ENQUEUED = 1; 227 public static final int MSG_ON_NOTIFICATION_SNOOZED = 2; 228 229 public MyHandler(Looper looper) { 230 super(looper, null, false); 231 } 232 233 @Override 234 public void handleMessage(Message msg) { 235 switch (msg.what) { 236 case MSG_ON_NOTIFICATION_ENQUEUED: { 237 SomeArgs args = (SomeArgs) msg.obj; 238 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 239 final int importance = args.argi1; 240 final boolean user = args.argi2 == 1; 241 args.recycle(); 242 Adjustment adjustment = onNotificationEnqueued(sbn, importance, user); 243 if (adjustment != null) { 244 if (!isBound()) return; 245 try { 246 getNotificationInterface().applyEnqueuedAdjustmentFromAssistant( 247 mWrapper, adjustment); 248 } catch (android.os.RemoteException ex) { 249 Log.v(TAG, "Unable to contact notification manager", ex); 250 throw ex.rethrowFromSystemServer(); 251 } 252 } 253 break; 254 } 255 case MSG_ON_NOTIFICATION_SNOOZED: { 256 SomeArgs args = (SomeArgs) msg.obj; 257 StatusBarNotification sbn = (StatusBarNotification) args.arg1; 258 String snoozeCriterionId = (String) args.arg2; 259 args.recycle(); 260 onNotificationSnoozedUntilContext(sbn, snoozeCriterionId); 261 break; 262 } 263 } 264 } 265 } 266} 267