Merge "Added notification pendingIntent to handle the click event." into main
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 348e798..d8aff68 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -130,6 +130,12 @@
             </intent-filter>
         </receiver>
 
+        <receiver android:name="com.android.stk.UserPresentReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.USER_PRESENT" />
+            </intent-filter>
+        </receiver>
         <service android:name="StkAppService" />
 
     </application>
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 52b06fb..4d26fa6 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -74,7 +74,6 @@
 import com.android.internal.telephony.GsmAlphabet;
 import com.android.internal.telephony.ITelephony;
 import com.android.internal.telephony.PhoneConfigurationManager;
-import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.cat.AppInterface;
 import com.android.internal.telephony.cat.CatCmdMessage;
@@ -300,6 +299,7 @@
     private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
     private static final String SYSTEM_DIALOG_REASON_RECENTAPPS_KEY = "recentapps";
     private BroadcastReceiver mHomeKeyEventReceiver = null;
+    private static final int NOTIFICATION_PENDING_INTENT_REQUEST_CODE = 0;
 
     @Override
     public void onCreate() {
@@ -1690,47 +1690,26 @@
         builder.setOnlyAlertOnce(true);
         builder.setColor(getResources().getColor(
                 com.android.internal.R.color.system_notification_accent_color));
-
-        registerUserPresentReceiver();
+        Intent userPresentIntent = new Intent(mContext, UserPresentReceiver.class);
+        userPresentIntent.setAction(Intent.ACTION_USER_PRESENT);
+        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
+                NOTIFICATION_PENDING_INTENT_REQUEST_CODE, userPresentIntent,
+                PendingIntent.FLAG_IMMUTABLE);
+        builder.setContentIntent(pendingIntent);
         mNotificationManager.notify(getNotificationId(NOTIFICATION_ON_KEYGUARD, slotId),
                 builder.build());
         mStkContext[slotId].mNotificationOnKeyguard = true;
     }
 
+    public void cancelNotificationOnKeyguard() {
+        for (int slot = 0; slot < mSimCount; slot++) {
+            cancelNotificationOnKeyguard(slot);
+        }
+    }
+
     private void cancelNotificationOnKeyguard(int slotId) {
         mNotificationManager.cancel(getNotificationId(NOTIFICATION_ON_KEYGUARD, slotId));
         mStkContext[slotId].mNotificationOnKeyguard = false;
-        unregisterUserPresentReceiver(slotId);
-    }
-
-    private synchronized void registerUserPresentReceiver() {
-        if (mUserPresentReceiver == null) {
-            mUserPresentReceiver = new BroadcastReceiver() {
-                @Override public void onReceive(Context context, Intent intent) {
-                    if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
-                        for (int slot = 0; slot < mSimCount; slot++) {
-                            cancelNotificationOnKeyguard(slot);
-                        }
-                    }
-                }
-            };
-            registerReceiver(mUserPresentReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
-        }
-    }
-
-    private synchronized void unregisterUserPresentReceiver(int slotId) {
-        if (mUserPresentReceiver != null) {
-            for (int slot = 0; slot < mSimCount; slot++) {
-                if (slot != slotId) {
-                    if (mStkContext[slot].mNotificationOnKeyguard) {
-                        // The broadcast receiver is still necessary for other SIM card.
-                        return;
-                    }
-                }
-            }
-            unregisterReceiver(mUserPresentReceiver);
-            mUserPresentReceiver = null;
-        }
     }
 
     private int getNotificationId(int notificationType, int slotId) {
diff --git a/src/com/android/stk/UserPresentReceiver.java b/src/com/android/stk/UserPresentReceiver.java
new file mode 100644
index 0000000..25c945c
--- /dev/null
+++ b/src/com/android/stk/UserPresentReceiver.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2023 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.stk;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import com.android.internal.telephony.cat.CatLog;
+
+public class UserPresentReceiver extends BroadcastReceiver {
+
+    private static final String LOG_TAG = UserPresentReceiver.class.getSimpleName();
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
+            CatLog.d(LOG_TAG, "Broadcast received and clearing the notification");
+            StkAppService.getInstance().cancelNotificationOnKeyguard();
+        }
+    }
+}