1 /*
2  * Copyright (C) 2020 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 
17 package com.android.cts.verifier.notifications;
18 
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 
27 import com.android.cts.verifier.R;
28 
29 public class ActionTriggeredReceiver extends BroadcastReceiver {
30 
31     public static String ACTION = "com.android.cts.verifier.notifications.ActionTriggeredReceiver";
32 
33     @Override
onReceive(Context context, Intent intent)34     public void onReceive(Context context, Intent intent) {
35         if (ACTION.equals(intent.getAction())) {
36             sendNotification(context, false);
37         }
38     }
39 
sendNotification(Context context, boolean initialSend)40     public static void sendNotification(Context context, boolean initialSend) {
41         String initialSendText = context.getString(R.string.action_not_sent);
42         String updateSendText = context.getString(R.string.action_received);
43         NotificationManager nm = context.getSystemService(NotificationManager.class);
44         Notification n1 = new Notification.Builder(
45                 context, NotificationListenerVerifierActivity.TAG)
46                 .setContentTitle(initialSend ?  initialSendText: updateSendText)
47                 .setContentText(initialSend ? initialSendText : updateSendText)
48                 .setSmallIcon(R.drawable.ic_stat_charlie)
49                 .setCategory(Notification.CATEGORY_REMINDER)
50                 .addAction(new Notification.Action.Builder(R.drawable.ic_stat_charlie,
51                         context.getString(R.string.action_test_title),
52                         makeBroadcastIntent(context))
53                         .setAuthenticationRequired(true).build())
54                 .build();
55         nm.notify(ACTION, 0, n1);
56     }
57 
makeBroadcastIntent(Context context)58     protected static PendingIntent makeBroadcastIntent(Context context) {
59         Intent intent = new Intent(ACTION);
60         intent.setComponent(new ComponentName(context, ActionTriggeredReceiver.class));
61         PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
62                 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE_UNAUDITED);
63         return pi;
64     }
65 }
66