1 /*
2  * Copyright (C) 2014 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.robot;
17 
18 import android.app.Activity;
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.util.Log;
25 
26 
27 public class NotificationBot extends BroadcastReceiver {
28     private static final String TAG = "NotificationBot";
29     private static final String EXTRA_ID = "ID";
30     private static final String EXTRA_NOTIFICATION = "NOTIFICATION";
31     private static final String ACTION_POST = "com.android.cts.robot.ACTION_POST";
32     private static final String ACTION_CANCEL = "com.android.cts.robot.ACTION_CANCEL";
33 
34     @Override
onReceive(Context context, Intent intent)35     public void onReceive(Context context, Intent intent) {
36         Log.i(TAG, "received intent: " + intent);
37         if (ACTION_POST.equals(intent.getAction())) {
38             Log.i(TAG, ACTION_POST);
39             if (!intent.hasExtra(EXTRA_NOTIFICATION) || !intent.hasExtra(EXTRA_ID)) {
40                 Log.e(TAG, "received post action with missing content");
41                 return;
42             }
43             int id = intent.getIntExtra(EXTRA_ID, -1);
44             Log.i(TAG, "id: " + id);
45             Notification n = (Notification) intent.getParcelableExtra(EXTRA_NOTIFICATION);
46             Log.i(TAG, "n: " + n);
47             NotificationManager noMa =
48                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
49             noMa.notify(id, n);
50 
51         } else if (ACTION_CANCEL.equals(intent.getAction())) {
52             Log.i(TAG, ACTION_CANCEL);
53             int id = intent.getIntExtra(EXTRA_ID, -1);
54             Log.i(TAG, "id: " + id);
55             if (id < 0) {
56                 Log.e(TAG, "received cancel action with no ID");
57                 return;
58             }
59             NotificationManager noMa =
60                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
61             noMa.cancel(id);
62 
63         } else {
64             Log.i(TAG, "received unexpected action: " + intent.getAction());
65         }
66     }
67 }
68