1 /*
2  * Copyright (C) 2016 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 package com.android.cts.net.hostside;
17 
18 import android.app.Notification;
19 import android.app.PendingIntent;
20 import android.app.PendingIntent.CanceledException;
21 import android.app.RemoteInput;
22 import android.os.Bundle;
23 import android.service.notification.NotificationListenerService;
24 import android.service.notification.StatusBarNotification;
25 import android.util.Log;
26 
27 /**
28  * NotificationListenerService implementation that executes the notification actions once they're
29  * created.
30  */
31 public class MyNotificationListenerService extends NotificationListenerService {
32     private static final String TAG = "MyNotificationListenerService";
33 
34     @Override
onListenerConnected()35     public void onListenerConnected() {
36         Log.d(TAG, "onListenerConnected()");
37     }
38 
39     @Override
onNotificationPosted(StatusBarNotification sbn)40     public void onNotificationPosted(StatusBarNotification sbn) {
41         Log.d(TAG, "onNotificationPosted(): "  + sbn);
42         if (!sbn.getPackageName().startsWith(getPackageName())) {
43             Log.v(TAG, "ignoring notification from a different package");
44             return;
45         }
46         final PendingIntentSender sender = new PendingIntentSender();
47         final Notification notification = sbn.getNotification();
48         if (notification.contentIntent != null) {
49             sender.send("content", notification.contentIntent);
50         }
51         if (notification.deleteIntent != null) {
52             sender.send("delete", notification.deleteIntent);
53         }
54         if (notification.fullScreenIntent != null) {
55             sender.send("full screen", notification.fullScreenIntent);
56         }
57         if (notification.actions != null) {
58             for (Notification.Action action : notification.actions) {
59                 sender.send("action", action.actionIntent);
60                 sender.send("action extras", action.getExtras());
61                 final RemoteInput[] remoteInputs = action.getRemoteInputs();
62                 if (remoteInputs != null && remoteInputs.length > 0) {
63                     for (RemoteInput remoteInput : remoteInputs) {
64                         sender.send("remote input extras", remoteInput.getExtras());
65                     }
66                 }
67             }
68         }
69         sender.send("notification extras", notification.extras);
70     }
71 
getId()72     static String getId() {
73         return String.format("%s/%s", MyNotificationListenerService.class.getPackage().getName(),
74                 MyNotificationListenerService.class.getName());
75     }
76 
77     private static final class PendingIntentSender {
78         private PendingIntent mSentIntent = null;
79         private String mReason = null;
80 
send(String reason, PendingIntent pendingIntent)81         private void send(String reason, PendingIntent pendingIntent) {
82             if (pendingIntent == null) {
83                 // Could happen on action that only has extras
84                 Log.v(TAG, "Not sending null pending intent for " + reason);
85                 return;
86             }
87             if (mSentIntent != null || mReason != null) {
88                 // Sanity check: make sure test case set up just one pending intent in the
89                 // notification, otherwise it could pass because another pending intent caused the
90                 // whitelisting.
91                 throw new IllegalStateException("Already sent a PendingIntent (" + mSentIntent
92                         + ") for reason '" + mReason + "' when requested another for '" + reason
93                         + "' (" + pendingIntent + ")");
94             }
95             Log.i(TAG, "Sending pending intent for " + reason + ":" + pendingIntent);
96             try {
97                 pendingIntent.send();
98                 mSentIntent = pendingIntent;
99                 mReason = reason;
100             } catch (CanceledException e) {
101                 Log.w(TAG, "Pending intent " + pendingIntent + " canceled");
102             }
103         }
104 
send(String reason, Bundle extras)105         private void send(String reason, Bundle extras) {
106             if (extras != null) {
107                 for (String key : extras.keySet()) {
108                     Object value = extras.get(key);
109                     if (value instanceof PendingIntent) {
110                         send(reason + " with key '" + key + "'", (PendingIntent) value);
111                     }
112                 }
113             }
114         }
115 
116     }
117 }
118