Add LLCP version check for M or later.

M devices need to report LLCP version 1.2 or higher.

Bug: 20311887
Change-Id: Iaf716d7aecfcc7718af2f7f6485cdbdbe701a3d6
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index 98e8acd..ee90a8e 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -429,6 +429,10 @@
                 android:label="@string/nfc_ndef_push_receiver"
                 android:configChanges="keyboardHidden|orientation|screenSize" />
 
+        <activity android:name=".nfc.LlcpVersionActivity"
+                android:label="@string/nfc_llcp_version_check"
+                android:configChanges="keyboardHidden|orientation|screenSize" />
+
         <activity android:name=".nfc.TagVerifierActivity"
                 android:label="@string/nfc_tag_verifier"
                 android:configChanges="keyboardHidden|orientation|screenSize" />
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index fa4203d..01c7e07 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -361,6 +361,7 @@
     <string name="nfc_pee_2_pee">Peer-to-Peer Data Exchange</string>
     <string name="nfc_ndef_push_sender">NDEF Push Sender</string>
     <string name="nfc_ndef_push_receiver">NDEF Push Receiver</string>
+    <string name="nfc_llcp_version_check">LLCP version check</string>
 
     <string name="nfc_tag_verification">Tag Verification</string>
     <string name="nfc_ndef">NDEF</string>
@@ -382,6 +383,13 @@
     <string name="nfc_ndef_push_receive_failure">Failed to receive the correct NDEF push
         message.</string>
 
+    <string name="nfc_llcp_version_check_info">This test requires two candidate devices
+       with NFC enabled to exchange P2P messages. Start the \"LLCP version check\" test on
+       the other candidate device also, and touch the devices back to back. This test
+       then verifies that the candidate device correctly advises the LLCP version as 1.2</string>
+    <string name="nfc_llcp_version_check_failure">The candidate devices does not report LLCP
+       version 1.2 or higher.</string>
+    <string name="nfc_llcp_version_check_success">The candidate device has a valid LLCP version.</string>
     <string name="nfc_tag_verifier">NFC Tag Verifier</string>
     <string name="nfc_tag_verifier_info">Follow the on-screen instructions to write and read
         a tag of the chosen technology.</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/LlcpVersionActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/LlcpVersionActivity.java
new file mode 100644
index 0000000..ce5a3d4
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/LlcpVersionActivity.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2011 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.cts.verifier.nfc;
+
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.nfc.NdefMessage;
+import android.nfc.NdefRecord;
+import android.nfc.NfcAdapter;
+import android.nfc.NfcEvent;
+import android.nfc.NfcManager;
+import android.os.Build;
+import android.os.Bundle;
+import android.widget.TextView;
+
+import java.nio.charset.Charset;
+
+/**
+ * Test activity that sends a particular NDEF Push message to another NFC device.
+ */
+public class LlcpVersionActivity extends PassFailButtons.Activity implements
+        NfcAdapter.CreateNdefMessageCallback {
+
+    private static final int NFC_NOT_ENABLED_DIALOG_ID = 1;
+    private static final int NDEF_PUSH_NOT_ENABLED_DIALOG_ID = 2;
+
+    private NfcAdapter mNfcAdapter;
+    private TextView mTextView;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.pass_fail_text);
+        setInfoResources(R.string.nfc_llcp_version_check, R.string.nfc_llcp_version_check_info, 0);
+        setPassFailButtonClickListeners();
+        getPassButton().setEnabled(false);
+
+        mTextView = (TextView) findViewById(R.id.text);
+        mTextView.setText(R.string.nfc_llcp_version_check_info);
+
+        NfcManager nfcManager = (NfcManager) getSystemService(NFC_SERVICE);
+        mNfcAdapter = nfcManager.getDefaultAdapter();
+    }
+
+    private static NdefMessage getTestMessage() {
+        byte[] mimeBytes = "application/com.android.cts.verifier.nfc"
+                .getBytes(Charset.forName("US-ASCII"));
+        byte[] id = new byte[] {1, 3, 3, 7};
+        byte[] payload = "CTS Verifier NDEF Push Tag".getBytes(Charset.forName("US-ASCII"));
+        return new NdefMessage(new NdefRecord[] {
+                new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload)
+        });
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+
+        if (!mNfcAdapter.isEnabled()) {
+            showDialog(NFC_NOT_ENABLED_DIALOG_ID);
+        } else if (!mNfcAdapter.isNdefPushEnabled()) {
+            /* Sender must have NDEF push enabled */
+            showDialog(NDEF_PUSH_NOT_ENABLED_DIALOG_ID);
+        }
+
+        mNfcAdapter.setNdefPushMessageCallback(this, this);
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+    }
+
+    @Override
+    public Dialog onCreateDialog(int id, Bundle args) {
+        switch (id) {
+            case NFC_NOT_ENABLED_DIALOG_ID:
+                return NfcDialogs.createNotEnabledDialog(this);
+            case NDEF_PUSH_NOT_ENABLED_DIALOG_ID:
+                return NfcDialogs.createNdefPushNotEnabledDialog(this);
+            default:
+                return super.onCreateDialog(id, args);
+        }
+    }
+
+    @Override
+    public NdefMessage createNdefMessage(NfcEvent event) {
+        if (event.peerLlcpMajorVersion <= 1 && event.peerLlcpMinorVersion < 2) {
+            runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+                    mTextView.setText(R.string.nfc_llcp_version_check_failure);
+                }
+            });
+        } else {
+            runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+                    getPassButton().setEnabled(true);
+                    mTextView.setText(R.string.nfc_llcp_version_check_success);
+                }
+            });
+        }
+        return null;
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NdefPushSenderActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NdefPushSenderActivity.java
index f3f37c4..2f77895 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NdefPushSenderActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NdefPushSenderActivity.java
@@ -23,6 +23,7 @@
 import android.nfc.NdefMessage;
 import android.nfc.NdefRecord;
 import android.nfc.NfcAdapter;
+import android.nfc.NfcEvent;
 import android.nfc.NfcManager;
 import android.os.Bundle;
 import android.widget.TextView;
@@ -32,7 +33,8 @@
 /**
  * Test activity that sends a particular NDEF Push message to another NFC device.
  */
-public class NdefPushSenderActivity extends PassFailButtons.Activity {
+public class NdefPushSenderActivity extends PassFailButtons.Activity implements
+        NfcAdapter.CreateNdefMessageCallback {
 
     static final NdefMessage TEST_MESSAGE = getTestMessage();
 
@@ -76,13 +78,12 @@
             showDialog(NDEF_PUSH_NOT_ENABLED_DIALOG_ID);
         }
 
-        mNfcAdapter.enableForegroundNdefPush(this, TEST_MESSAGE);
+        mNfcAdapter.setNdefPushMessageCallback(this, this);
     }
 
     @Override
     protected void onPause() {
         super.onPause();
-        mNfcAdapter.disableForegroundNdefPush(this);
     }
 
     @Override
@@ -96,4 +97,9 @@
                 return super.onCreateDialog(id, args);
         }
     }
+
+    @Override
+    public NdefMessage createNdefMessage(NfcEvent event) {
+        return getTestMessage();
+    }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
index cb90241..68fc027 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
@@ -28,6 +28,7 @@
 import android.nfc.tech.MifareUltralight;
 import android.nfc.tech.Ndef;
 import android.nfc.tech.TagTechnology;
+import android.os.Build;
 import android.os.Bundle;
 
 /** Activity that lists all the NFC tests. */
@@ -58,6 +59,11 @@
                 NdefPushReceiverActivity.class.getName(),
                 new Intent(this, NdefPushReceiverActivity.class), null));
 
+        if ("MNC".equals(Build.VERSION.CODENAME) || Build.VERSION.SDK_INT >= 23) {
+            adapter.add(TestListItem.newTest(this, R.string.nfc_llcp_version_check,
+                    LlcpVersionActivity.class.getName(),
+                    new Intent(this, LlcpVersionActivity.class), null));
+        }
         adapter.add(TestListItem.newCategory(this, R.string.nfc_tag_verification));
         adapter.add(TestListItem.newTest(this, R.string.nfc_ndef,
                 NDEF_ID, getTagIntent(Ndef.class), null));
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/HceReaderTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/HceReaderTestActivity.java
index 035ce86..879916b 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/HceReaderTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/HceReaderTestActivity.java
@@ -40,12 +40,6 @@
 
         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
             adapter.add(TestListItem.newCategory(this, R.string.nfc_hce_reader_tests));
-            /*
-             * Only add this test when supported in platform
-            adapter.add(TestListItem.newTest(this, R.string.nfc_hce_default_route_reader,
-                    SimpleReaderActivity.class.getName(),
-                    DefaultRouteEmulatorActivity.buildReaderIntent(this), null));
-             */
 
             adapter.add(TestListItem.newTest(this, R.string.nfc_hce_protocol_params_reader,
                     ProtocolParamsReaderActivity.class.getName(),