Merge "Update HCE return rules" am: 4572485a33

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Nfc/+/1860257

Change-Id: I67d4668058f1ec07e83437f4b990567165e89e80
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index 4f55194..94ce3ab 100644
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -2485,6 +2485,22 @@
         mHandler.sendMessage(msg);
     }
 
+    /**
+     * Send require device unlock for NFC intent to system UI.
+     */
+    public void sendRequireUnlockIntent() {
+        if (!mIsRequestUnlockShowed && mKeyguard.isKeyguardLocked()) {
+            if (DBG) Log.d(TAG, "Request unlock");
+            mIsRequestUnlockShowed = true;
+            mRequireUnlockWakeLock.acquire();
+            Intent requireUnlockIntent =
+                    new Intent(NfcAdapter.ACTION_REQUIRE_UNLOCK_FOR_NFC);
+            requireUnlockIntent.setPackage(SYSTEM_UI);
+            mContext.sendBroadcast(requireUnlockIntent);
+            mRequireUnlockWakeLock.release();
+        }
+    }
+
     final class NfcServiceHandler extends Handler {
         @Override
         public void handleMessage(Message msg) {
@@ -2710,16 +2726,8 @@
                 case MSG_RF_FIELD_ACTIVATED:
                     Intent fieldOnIntent = new Intent(ACTION_RF_FIELD_ON_DETECTED);
                     sendNfcPermissionProtectedBroadcast(fieldOnIntent);
-                    if (!mIsRequestUnlockShowed
-                            && mIsSecureNfcEnabled && mKeyguard.isKeyguardLocked()) {
-                        if (DBG) Log.d(TAG, "Request unlock");
-                        mIsRequestUnlockShowed = true;
-                        mRequireUnlockWakeLock.acquire();
-                        Intent requireUnlockIntent =
-                                new Intent(NfcAdapter.ACTION_REQUIRE_UNLOCK_FOR_NFC);
-                        requireUnlockIntent.setPackage(SYSTEM_UI);
-                        mContext.sendBroadcast(requireUnlockIntent);
-                        mRequireUnlockWakeLock.release();
+                    if (mIsSecureNfcEnabled) {
+                        sendRequireUnlockIntent();
                     }
                     break;
                 case MSG_RF_FIELD_DEACTIVATED:
diff --git a/src/com/android/nfc/cardemulation/HostEmulationManager.java b/src/com/android/nfc/cardemulation/HostEmulationManager.java
index e2866ba..4427cbf 100644
--- a/src/com/android/nfc/cardemulation/HostEmulationManager.java
+++ b/src/com/android/nfc/cardemulation/HostEmulationManager.java
@@ -188,16 +188,17 @@
                     // Resolve to default
                     // Check if resolvedService requires unlock
                     ApduServiceInfo defaultServiceInfo = resolveInfo.defaultService;
-                    if (defaultServiceInfo.requiresUnlock() &&
-                            mKeyguard.isKeyguardLocked() && mKeyguard.isKeyguardSecure()) {
-                        // Just ignore all future APDUs until next tap
-                        mState = STATE_W4_DEACTIVATE;
+                    if (defaultServiceInfo.requiresUnlock() && mKeyguard.isKeyguardLocked()) {
+                        NfcService.getInstance().sendRequireUnlockIntent();
+                        NfcService.getInstance().sendData(AID_NOT_FOUND);
+                        if (DBG) Log.d(TAG, "requiresUnlock()! show toast");
                         launchTapAgain(resolveInfo.defaultService, resolveInfo.category);
                         return;
                     }
                     if (defaultServiceInfo.requiresScreenOn() && !mPowerManager.isScreenOn()) {
-                        // Just ignore all future APDUs
-                        mState = STATE_W4_DEACTIVATE;
+                        NfcService.getInstance().sendRequireUnlockIntent();
+                        NfcService.getInstance().sendData(AID_NOT_FOUND);
+                        if (DBG) Log.d(TAG, "requiresScreenOn()!");
                         return;
                     }
                     // In no circumstance should this be an OffHostService -