NotificationAssistantService.java revision cf63ff1532e793560f62e1c75f3402b48b0f09ba
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     * Inform the notification manager about un-snoozing a specific notification.
123     * <p>
124     * This should only be used for notifications snoozed by this listener using
125     * {@link #snoozeNotification(String, String)}. Once un-snoozed, you will get a
126     * {@link #onNotificationPosted(StatusBarNotification, RankingMap)} callback for the
127     * notification.
128     * @param key The key of the notification to snooze
129     */
130    public final void unsnoozeNotification(String key) {
131        if (!isBound()) return;
132        try {
133            getNotificationInterface().unsnoozeNotificationFromAssistant(mWrapper, key);
134        } catch (android.os.RemoteException ex) {
135            Log.v(TAG, "Unable to contact notification manager", ex);
136        }
137    }
138
139    /**
140     * Creates a notification channel that notifications can be posted to for a given package.
141     *
142     * @param pkg The package to create a channel for.
143     * @param channel  the channel to attempt to create.
144     */
145    public void createNotificationChannel(@NonNull String pkg,
146            @NonNull NotificationChannel channel) {
147        if (!isBound()) return;
148        try {
149            getNotificationInterface().createNotificationChannelFromAssistant(
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     * Updates a notification channel for a given package.
159     *
160     * @param pkg The package to the channel belongs to.
161     * @param channel the channel to attempt to update.
162     */
163    public void updateNotificationChannel(@NonNull String pkg,
164            @NonNull NotificationChannel channel) {
165        if (!isBound()) return;
166        try {
167            getNotificationInterface().updateNotificationChannelFromAssistant(
168                    mWrapper, pkg, channel);
169        } catch (RemoteException e) {
170            Log.v(TAG, "Unable to contact notification manager", e);
171            throw e.rethrowFromSystemServer();
172        }
173    }
174
175    /**
176     * Returns all notification channels belonging to the given package.
177     */
178    public List<NotificationChannel> getNotificationChannels(@NonNull String pkg) {
179        if (!isBound()) return null;
180        try {
181            return getNotificationInterface().getNotificationChannelsFromAssistant(
182                    mWrapper, pkg).getList();
183        } catch (RemoteException e) {
184            Log.v(TAG, "Unable to contact notification manager", e);
185            throw e.rethrowFromSystemServer();
186        }
187    }
188
189    /**
190     * Deletes the given notification channel.
191     */
192    public void deleteNotificationChannel(@NonNull String pkg, @NonNull String channelId) {
193        if (!isBound()) return;
194        try {
195            getNotificationInterface().deleteNotificationChannelFromAssistant(
196                    mWrapper, pkg, channelId);
197        } catch (RemoteException e) {
198            throw e.rethrowFromSystemServer();
199        }
200    }
201
202
203    private class NotificationAssistantServiceWrapper extends NotificationListenerWrapper {
204        @Override
205        public void onNotificationEnqueued(IStatusBarNotificationHolder sbnHolder,
206                int importance, boolean user) {
207            StatusBarNotification sbn;
208            try {
209                sbn = sbnHolder.get();
210            } catch (RemoteException e) {
211                Log.w(TAG, "onNotificationEnqueued: Error receiving StatusBarNotification", e);
212                return;
213            }
214
215            SomeArgs args = SomeArgs.obtain();
216            args.arg1 = sbn;
217            args.argi1 = importance;
218            args.argi2 = user ? 1 : 0;
219            mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_ENQUEUED,
220                    args).sendToTarget();
221        }
222
223        @Override
224        public void onNotificationSnoozedUntilContext(
225                IStatusBarNotificationHolder sbnHolder, String snoozeCriterionId)
226                throws RemoteException {
227            StatusBarNotification sbn;
228            try {
229                sbn = sbnHolder.get();
230            } catch (RemoteException e) {
231                Log.w(TAG, "onNotificationSnoozed: Error receiving StatusBarNotification", e);
232                return;
233            }
234
235            SomeArgs args = SomeArgs.obtain();
236            args.arg1 = sbn;
237            args.arg2 = snoozeCriterionId;
238            mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_SNOOZED,
239                    args).sendToTarget();
240        }
241    }
242
243    private final class MyHandler extends Handler {
244        public static final int MSG_ON_NOTIFICATION_ENQUEUED = 1;
245        public static final int MSG_ON_NOTIFICATION_SNOOZED = 2;
246
247        public MyHandler(Looper looper) {
248            super(looper, null, false);
249        }
250
251        @Override
252        public void handleMessage(Message msg) {
253            switch (msg.what) {
254                case MSG_ON_NOTIFICATION_ENQUEUED: {
255                    SomeArgs args = (SomeArgs) msg.obj;
256                    StatusBarNotification sbn = (StatusBarNotification) args.arg1;
257                    final int importance = args.argi1;
258                    final boolean user = args.argi2 == 1;
259                    args.recycle();
260                    Adjustment adjustment = onNotificationEnqueued(sbn, importance, user);
261                    if (adjustment != null) {
262                        if (!isBound()) return;
263                        try {
264                            getNotificationInterface().applyEnqueuedAdjustmentFromAssistant(
265                                    mWrapper, adjustment);
266                        } catch (android.os.RemoteException ex) {
267                            Log.v(TAG, "Unable to contact notification manager", ex);
268                            throw ex.rethrowFromSystemServer();
269                        }
270                    }
271                    break;
272                }
273                case MSG_ON_NOTIFICATION_SNOOZED: {
274                    SomeArgs args = (SomeArgs) msg.obj;
275                    StatusBarNotification sbn = (StatusBarNotification) args.arg1;
276                    String snoozeCriterionId = (String) args.arg2;
277                    args.recycle();
278                    onNotificationSnoozedUntilContext(sbn, snoozeCriterionId);
279                    break;
280                }
281            }
282        }
283    }
284}
285