summaryrefslogtreecommitdiffstats
path: root/apps/CtsVerifier/src/com/android/cts/verifier/notifications/MockListener.java
blob: b4863fa551d0b29076ce05a4ee26ccaba46c0036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.cts.verifier.notifications;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.ArrayMap;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MockListener extends NotificationListenerService {
    static final String TAG = "MockListener";

    static final String SERVICE_BASE = "android.service.notification.cts.";
    static final String SERVICE_CHECK = SERVICE_BASE + "SERVICE_CHECK";
    static final String SERVICE_POSTED = SERVICE_BASE + "SERVICE_POSTED";
    static final String SERVICE_PAYLOADS = SERVICE_BASE + "SERVICE_PAYLOADS";
    static final String SERVICE_REMOVED = SERVICE_BASE + "SERVICE_REMOVED";
    static final String SERVICE_RESET = SERVICE_BASE + "SERVICE_RESET";
    static final String SERVICE_CLEAR_ONE = SERVICE_BASE + "SERVICE_CLEAR_ONE";
    static final String SERVICE_CLEAR_ALL = SERVICE_BASE + "SERVICE_CLEAR_ALL";
    public static final String SERVICE_ORDER = SERVICE_BASE + "SERVICE_ORDER";
    public static final String SERVICE_DND = SERVICE_BASE + "SERVICE_DND";

    static final String EXTRA_PAYLOAD = "PAYLOAD";
    static final String EXTRA_INT = "INT";
    static final String EXTRA_TAG = "TAG";
    static final String EXTRA_CODE = "CODE";

    static final int RESULT_TIMEOUT = Activity.RESULT_FIRST_USER;
    static final int RESULT_NO_SERVER = Activity.RESULT_FIRST_USER + 1;

    public static final String JSON_FLAGS = "flag";
    public static final String JSON_ICON = "icon";
    public static final String JSON_ID = "id";
    public static final String JSON_PACKAGE = "pkg";
    public static final String JSON_WHEN = "when";
    public static final String JSON_TAG = "tag";
    public static final String JSON_RANK = "rank";
    public static final String JSON_AMBIENT = "ambient";
    public static final String JSON_MATCHES_ZEN_FILTER = "matches_zen_filter";

    private ArrayList<String> mPosted = new ArrayList<String>();
    private ArrayMap<String, JSONObject> mNotifications = new ArrayMap<>();
    private ArrayMap<String, String> mNotificationKeys = new ArrayMap<>();
    private ArrayList<String> mRemoved = new ArrayList<String>();
    private ArrayList<String> mOrder = new ArrayList<>();
    private Set<String> mTestPackages = new HashSet<>();
    private BroadcastReceiver mReceiver;
    private int mDND = -1;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "created");

        mTestPackages.add("com.android.cts.verifier");

        mPosted = new ArrayList<String>();
        mRemoved = new ArrayList<String>();

        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (SERVICE_CHECK.equals(action)) {
                    Log.d(TAG, "SERVICE_CHECK");
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_POSTED.equals(action)) {
                    Log.d(TAG, "SERVICE_POSTED");
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList(EXTRA_PAYLOAD, mPosted);
                    setResultExtras(bundle);
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_DND.equals(action)) {
                    Log.d(TAG, "SERVICE_DND");
                    Bundle bundle = new Bundle();
                    bundle.putInt(EXTRA_INT, mDND);
                    setResultExtras(bundle);
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_ORDER.equals(action)) {
                    Log.d(TAG, "SERVICE_ORDER");
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList(EXTRA_PAYLOAD, mOrder);
                    setResultExtras(bundle);
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_PAYLOADS.equals(action)) {
                    Log.d(TAG, "SERVICE_PAYLOADS");
                    Bundle bundle = new Bundle();
                    ArrayList<String> payloadData = new ArrayList<>(mNotifications.size());
                    for (JSONObject payload: mNotifications.values()) {
                        payloadData.add(payload.toString());
                    }
                    bundle.putStringArrayList(EXTRA_PAYLOAD, payloadData);
                    setResultExtras(bundle);
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_REMOVED.equals(action)) {
                    Log.d(TAG, "SERVICE_REMOVED");
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList(EXTRA_PAYLOAD, mRemoved);
                    setResultExtras(bundle);
                    setResultCode(Activity.RESULT_OK);
                } else if (SERVICE_CLEAR_ONE.equals(action)) {
                    Log.d(TAG, "SERVICE_CLEAR_ONE");
                    String tag = intent.getStringExtra(EXTRA_TAG);
                    String key = mNotificationKeys.get(tag);
                    if (key != null) {
                        MockListener.this.cancelNotification(key);
                    } else {
                        Log.w(TAG, "Notification does not exist: " + tag);
                    }
                } else if (SERVICE_CLEAR_ALL.equals(action)) {
                    Log.d(TAG, "SERVICE_CLEAR_ALL");
                    MockListener.this.cancelAllNotifications();
                } else if (SERVICE_RESET.equals(action)) {
                    Log.d(TAG, "SERVICE_RESET");
                    resetData();
                } else {
                    Log.w(TAG, "unknown action");
                    setResultCode(Activity.RESULT_CANCELED);
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(SERVICE_CHECK);
        filter.addAction(SERVICE_DND);
        filter.addAction(SERVICE_POSTED);
        filter.addAction(SERVICE_ORDER);
        filter.addAction(SERVICE_PAYLOADS);
        filter.addAction(SERVICE_REMOVED);
        filter.addAction(SERVICE_CLEAR_ONE);
        filter.addAction(SERVICE_CLEAR_ALL);
        filter.addAction(SERVICE_RESET);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
        mReceiver = null;
        Log.d(TAG, "destroyed");
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        mDND = getCurrentInterruptionFilter();
        Log.d(TAG, "initial value of CurrentInterruptionFilter is " + mDND);
    }

    @Override
    public void onInterruptionFilterChanged(int interruptionFilter) {
        super.onInterruptionFilterChanged(interruptionFilter);
        mDND = interruptionFilter;
        Log.d(TAG, "value of CurrentInterruptionFilter changed to " + mDND);
    }

    public void resetData() {
        mPosted.clear();
        mNotifications.clear();
        mRemoved.clear();
        mOrder.clear();
    }

    @Override
    public void onNotificationRankingUpdate(RankingMap rankingMap) {
        String[] orderedKeys = rankingMap.getOrderedKeys();
        mOrder.clear();
        Ranking rank = new Ranking();
        for( int i = 0; i < orderedKeys.length; i++) {
            String key = orderedKeys[i];
            mOrder.add(key);
            rankingMap.getRanking(key, rank);
            JSONObject note = mNotifications.get(key);
            if (note != null) {
                try {
                    note.put(JSON_RANK, rank.getRank());
                    note.put(JSON_AMBIENT, rank.isAmbient());
                    note.put(JSON_MATCHES_ZEN_FILTER, rank.matchesInterruptionFilter());
                } catch (JSONException e) {
                    Log.e(TAG, "failed to pack up notification payload", e);
                }
            }
        }
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
        if (!mTestPackages.contains(sbn.getPackageName())) { return; }
        Log.d(TAG, "posted: " + sbn.getTag());
        mPosted.add(sbn.getTag());
        JSONObject notification = new JSONObject();
        try {
            notification.put(JSON_TAG, sbn.getTag());
            notification.put(JSON_ID, sbn.getId());
            notification.put(JSON_PACKAGE, sbn.getPackageName());
            notification.put(JSON_WHEN, sbn.getNotification().when);
            notification.put(JSON_ICON, sbn.getNotification().icon);
            notification.put(JSON_FLAGS, sbn.getNotification().flags);
            mNotifications.put(sbn.getKey(), notification);
            mNotificationKeys.put(sbn.getTag(), sbn.getKey());
        } catch (JSONException e) {
            Log.e(TAG, "failed to pack up notification payload", e);
        }
        onNotificationRankingUpdate(rankingMap);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        Log.d(TAG, "removed: " + sbn.getTag());
        mRemoved.add(sbn.getTag());
        mNotifications.remove(sbn.getKey());
        onNotificationRankingUpdate(rankingMap);
        mNotificationKeys.remove(sbn.getTag());
    }

    public static void resetListenerData(Context context) {
        sendCommand(context, SERVICE_RESET, null, 0);
    }

    public static void probeListenerStatus(Context context, StatusCatcher catcher) {
        requestStatus(context, SERVICE_CHECK, catcher);
    }

    public static void probeFilter(Context context, IntegerResultCatcher catcher) {
        requestIntegerResult(context, SERVICE_DND, catcher);
    }

    public static void probeListenerPosted(Context context, StringListResultCatcher catcher) {
        requestStringListResult(context, SERVICE_POSTED, catcher);
    }

    public static void probeListenerOrder(Context context, StringListResultCatcher catcher) {
        requestStringListResult(context, SERVICE_ORDER, catcher);
    }

    public static void probeListenerPayloads(Context context, StringListResultCatcher catcher) {
        requestStringListResult(context, SERVICE_PAYLOADS, catcher);
    }

    public static void probeListenerRemoved(Context context, StringListResultCatcher catcher) {
        requestStringListResult(context, SERVICE_REMOVED, catcher);
    }

    public static void clearOne(Context context, String tag, int code) {
        sendCommand(context, SERVICE_CLEAR_ONE, tag, code);
    }

    public static void clearAll(Context context) {
        sendCommand(context, SERVICE_CLEAR_ALL, null, 0);
    }

    private static void sendCommand(Context context, String action, String tag, int code) {
        Intent broadcast = new Intent(action);
        if (tag != null) {
            broadcast.putExtra(EXTRA_TAG, tag);
            broadcast.putExtra(EXTRA_CODE, code);
        }
        context.sendBroadcast(broadcast);
    }

    public abstract static class StatusCatcher extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            accept(Integer.valueOf(getResultCode()));
        }

        abstract public void accept(int result);
    }

    private static void requestStatus(Context context, String action,
            StatusCatcher catcher) {
        Intent broadcast = new Intent(action);
        context.sendOrderedBroadcast(broadcast, null, catcher, null, RESULT_NO_SERVER, null, null);
    }

    public abstract static class IntegerResultCatcher extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            accept(getResultExtras(true).getInt(EXTRA_INT, -1));
        }

        abstract public void accept(int result);
    }

    private static void requestIntegerResult(Context context, String action,
            IntegerResultCatcher catcher) {
        Intent broadcast = new Intent(action);
        context.sendOrderedBroadcast(broadcast, null, catcher, null, RESULT_NO_SERVER, null, null);
    }

    public abstract static class StringListResultCatcher extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            accept(getResultExtras(true).getStringArrayList(EXTRA_PAYLOAD));
        }

        abstract public void accept(List<String> result);
    }

    private static void requestStringListResult(Context context, String action,
            StringListResultCatcher catcher) {
        Intent broadcast = new Intent(action);
        context.sendOrderedBroadcast(broadcast, null, catcher, null, RESULT_NO_SERVER, null, null);
    }
}