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