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.nfc;
18 
19 import android.app.Notification;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.text.TextUtils;
27 
28 /**
29  * This class handles the Notification Manager for the antenna blocked notification
30  */
31 
32 public class NfcBlockedNotification {
33     private static final String NFC_NOTIFICATION_CHANNEL = "nfc_notification_channel";
34     private NotificationChannel mNotificationChannel;
35     public static final int NOTIFICATION_ID_NFC = -1000001;
36     Context mContext;
37 
38     /**
39      * Constructor
40      *
41      * @param ctx The context to use to obtain access to the resources
42      */
NfcBlockedNotification(Context ctx)43     public NfcBlockedNotification(Context ctx) {
44         mContext = ctx;
45     }
46 
47     /**
48      * Start the notification.
49      */
startNotification()50     public void startNotification() {
51         Intent infoIntent;
52         if (TextUtils.isEmpty(mContext.getString(R.string.antenna_blocked_alert_link))) {
53             // Do nothing after user click the notification if antenna_blocked_alert_link is empty
54             infoIntent = new Intent();
55         } else {
56             // Open the link after user click the notification
57             infoIntent = new Intent(Intent.ACTION_VIEW);
58             infoIntent.setData(Uri.parse(mContext.getString(R.string.antenna_blocked_alert_link)));
59         }
60         Notification.Builder builder = new Notification.Builder(mContext, NFC_NOTIFICATION_CHANNEL);
61         builder.setContentTitle(mContext.getString(R.string.nfc_blocking_alert_title))
62                 .setContentText(mContext.getString(R.string.nfc_blocking_alert_message))
63                 .setSmallIcon(android.R.drawable.stat_sys_warning)
64                 .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
65                 .setAutoCancel(true)
66                 .setContentIntent(PendingIntent.getActivity(mContext, 0, infoIntent,
67                       PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE));
68         mNotificationChannel = new NotificationChannel(NFC_NOTIFICATION_CHANNEL,
69                 mContext.getString(R.string.nfcUserLabel), NotificationManager.IMPORTANCE_DEFAULT);
70         NotificationManager notificationManager =
71                 mContext.getSystemService(NotificationManager.class);
72         notificationManager.createNotificationChannel(mNotificationChannel);
73         notificationManager.notify(NOTIFICATION_ID_NFC, builder.build());
74     }
75 }
76 
77