NFC: HCE: Deal with pre-installed payment apps.

Bug: 13245674
Previously HCE CTS tests could not be completed when a payment
application was already installed on the device and marked
as default. This change takes that scenario into account,
and changes the default to the CTS apps HCE services to be able
to properly execute the tests.

Change-Id: Ia7e334ed9b83092b0cd9cffcbbb53e49298a419e
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index e0e6dfc..e1f08f3 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -381,6 +381,7 @@
     <string name="nfc_hce_throughput_emulator">HCE throughput test (Emulator)</string>
     <string name="nfc_hce_throughput_reader">HCE throughput test (Reader)</string>
     <string name="nfc_hce_throughput_emulator_help">This tests verifies that your HCE implementation can reach a decent throughput. While Android does not have any requirements on HCE performance, many HCE applications such as transport and payment apps do. If the average APDU roundtrip time is more than 50ms, please take a look at your implementation to see where the delay is coming from.</string>
+    <string name="nfc_hce_change_preinstalled_wallet">The device has an installed payment application that is currently set as default. To complete the test, you will be asked whether you want to make Payment Service #1 or #2 the default app. Select yes.</string>
     <string name="nfc_hce_change_default_help">You will now be asked whether you want to make Payment Service #1 the default app. Select yes.</string>
     <string name="nfc_hce_conflicting_non_payment_help">When tapping the first time, you will be shown a dialog that asks you to choose between TransportService #1 and TransportService #2. Select TransportService #2. Verify a dialog is shown that asks you to tap again to complete. Now tap again, and if communication with TransportService #2 is successfull the pass button will be enabled."</string>
     <string name="nfc_payment_service_desc">NFC Payment service</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/ChangeDefaultEmulatorActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/ChangeDefaultEmulatorActivity.java
index f5584ec..0681f49 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/ChangeDefaultEmulatorActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/ChangeDefaultEmulatorActivity.java
@@ -18,7 +18,9 @@
     final static int STATE_IDLE = 0;
     final static int STATE_SERVICE1_SETTING_UP = 1;
     final static int STATE_SERVICE2_SETTING_UP = 2;
-    final static int STATE_DEFAULT_CHANGED = 3;
+    final static int STATE_MAKING_SERVICE1_DEFAULT = 3;
+    final static int STATE_MAKING_SERVICE2_DEFAULT = 4;
+    final static int STATE_DEFAULT_CHANGED = 5;
 
     boolean mReceiverRegistered = false;
     int mState = STATE_IDLE;
@@ -49,15 +51,14 @@
         // Verify HCE service 2 is the default
         if (!mCardEmulation.isDefaultServiceForCategory(
                 PaymentService2.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
-            // Popup dialog-box, fail test
+            mState = STATE_MAKING_SERVICE2_DEFAULT;
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
-            builder.setTitle("Test failed.");
-            builder.setMessage("PaymentService2 is not the default service according " +
-                    "to CardEmulation.getDefaultServiceForCategory(). Do you have" +
-                    "another Payment application installed?");
-            builder.setPositiveButton("OK", null);
+            builder.setTitle("Note");
+            builder.setMessage(R.string.nfc_hce_change_preinstalled_wallet);
+            builder.setPositiveButton("OK", this);
             builder.show();
         } else {
+            mState = STATE_MAKING_SERVICE1_DEFAULT;
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
             builder.setTitle("Note");
             builder.setMessage(R.string.nfc_hce_change_default_help);
@@ -94,16 +95,56 @@
 
     @Override
     public void onClick(DialogInterface dialog, int which) {
-        Intent changeDefault = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
-        changeDefault.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
-        changeDefault.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, PaymentService1.COMPONENT);
-        startActivityForResult(changeDefault, 0);
+        if (mState == STATE_MAKING_SERVICE1_DEFAULT) {
+            Intent changeDefault = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
+            changeDefault.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
+            changeDefault.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, PaymentService1.COMPONENT);
+            startActivityForResult(changeDefault, 0);
+        } else if (mState == STATE_MAKING_SERVICE2_DEFAULT) {
+            Intent changeDefault = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
+            changeDefault.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
+            changeDefault.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, PaymentService2.COMPONENT);
+            startActivityForResult(changeDefault, 0);
+        }
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
-        mState = STATE_DEFAULT_CHANGED;
-        NfcDialogs.createHceTapReaderDialog(this, null).show();
+        if (mState == STATE_MAKING_SERVICE1_DEFAULT) {
+            if (!mCardEmulation.isDefaultServiceForCategory(
+                    PaymentService1.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
+                // Popup dialog-box, fail test
+                AlertDialog.Builder builder = new AlertDialog.Builder(this);
+                builder.setTitle("Test failed.");
+                builder.setMessage("PaymentService1 is not the default service according " +
+                        "to CardEmulation.getDefaultServiceForCategory(), verify the make " +
+                        "default implementation is correct.");
+                builder.setPositiveButton("OK", null);
+                builder.show();
+            } else {
+                mState = STATE_DEFAULT_CHANGED;
+                NfcDialogs.createHceTapReaderDialog(this, null).show();
+            }
+        } else if (mState == STATE_MAKING_SERVICE2_DEFAULT) {
+            if (!mCardEmulation.isDefaultServiceForCategory(
+                    PaymentService2.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
+                // Popup dialog-box, fail test
+                AlertDialog.Builder builder = new AlertDialog.Builder(this);
+                builder.setTitle("Test failed.");
+                builder.setMessage("PaymentService2 is not the default service according " +
+                        "to CardEmulation.getDefaultServiceForCategory(), verify the make " +
+                        "default implementation is correct.");
+                builder.setPositiveButton("OK", null);
+                builder.show();
+            } else {
+                mState = STATE_MAKING_SERVICE1_DEFAULT;
+                AlertDialog.Builder builder = new AlertDialog.Builder(this);
+                builder.setTitle("Note");
+                builder.setMessage(R.string.nfc_hce_change_default_help);
+                builder.setPositiveButton("OK", this);
+                builder.show();
+            }
+        }
     }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/DualPaymentEmulatorActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/DualPaymentEmulatorActivity.java
index eaba131..125045f 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/DualPaymentEmulatorActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/DualPaymentEmulatorActivity.java
@@ -4,6 +4,8 @@
 import android.app.AlertDialog;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.nfc.cardemulation.CardEmulation;
 import android.os.Bundle;
@@ -12,10 +14,11 @@
 import com.android.cts.verifier.nfc.NfcDialogs;
 
 @TargetApi(19)
-public class DualPaymentEmulatorActivity extends BaseEmulatorActivity {
+public class DualPaymentEmulatorActivity extends BaseEmulatorActivity implements OnClickListener {
     final static int STATE_IDLE = 0;
     final static int STATE_SERVICE1_SETTING_UP = 1;
     final static int STATE_SERVICE2_SETTING_UP = 2;
+    final static int STATE_MAKING_SERVICE2_DEFAULT = 3;
 
     boolean mReceiverRegistered = false;
     int mState = STATE_IDLE;
@@ -45,12 +48,11 @@
         // Verify HCE service 2 is the default
         if (!mCardEmulation.isDefaultServiceForCategory(
                 PaymentService2.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
-            // Popup dialog-box, fail test
+            mState = STATE_MAKING_SERVICE2_DEFAULT;
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
-            builder.setTitle("Test failed.");
-            builder.setMessage("PaymentService2 is not the default service according " +
-                    "to CardEmulation.getDefaultServiceForCategory()");
-            builder.setPositiveButton("OK", null);
+            builder.setTitle("Note");
+            builder.setMessage(R.string.nfc_hce_change_preinstalled_wallet);
+            builder.setPositiveButton("OK", this);
             builder.show();
         } else {
             NfcDialogs.createHceTapReaderDialog(this,null).show();
@@ -64,7 +66,23 @@
             unregisterReceiver(mReceiver);
         }
     }
-
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (!mCardEmulation.isDefaultServiceForCategory(
+                PaymentService2.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
+            // Popup dialog-box, fail test
+            AlertDialog.Builder builder = new AlertDialog.Builder(this);
+            builder.setTitle("Test failed.");
+            builder.setMessage("PaymentService2 is not the default service according " +
+                    "to CardEmulation.getDefaultServiceForCategory(), verify the make " +
+                    "default implementation is correct.");
+            builder.setPositiveButton("OK", null);
+            builder.show();
+        } else {
+            NfcDialogs.createHceTapReaderDialog(this, null).show();
+        }
+    }
     public static Intent buildReaderIntent(Context context) {
         Intent readerIntent = new Intent(context, SimpleReaderActivity.class);
         readerIntent.putExtra(SimpleReaderActivity.EXTRA_APDUS,
@@ -83,5 +101,11 @@
         }
     }
 
-
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        Intent changeDefault = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
+        changeDefault.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
+        changeDefault.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, PaymentService2.COMPONENT);
+        startActivityForResult(changeDefault, 0);
+    }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/SinglePaymentEmulatorActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/SinglePaymentEmulatorActivity.java
index e5f9f17..13b2fe8 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/SinglePaymentEmulatorActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hce/SinglePaymentEmulatorActivity.java
@@ -4,15 +4,16 @@
 import android.app.AlertDialog;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.nfc.cardemulation.CardEmulation;
 import android.os.Bundle;
-
 import com.android.cts.verifier.R;
 import com.android.cts.verifier.nfc.NfcDialogs;
 
 @TargetApi(19)
-public class SinglePaymentEmulatorActivity extends BaseEmulatorActivity {
+public class SinglePaymentEmulatorActivity extends BaseEmulatorActivity implements OnClickListener {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -32,13 +33,10 @@
         // Verify HCE service 1 is the default
         if (!mCardEmulation.isDefaultServiceForCategory(
                 PaymentService1.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
-            // Popup dialog-box, fail test
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
-            builder.setTitle("Test failed.");
-            builder.setMessage("PaymentService1 is not the default service according " +
-                    "to CardEmulation.getDefaultServiceForCategory(), verify no other " +
-                    "payment HCE apps are installed.");
-            builder.setPositiveButton("OK", null);
+            builder.setTitle("Note");
+            builder.setMessage(R.string.nfc_hce_change_preinstalled_wallet);
+            builder.setPositiveButton("OK", this);
             builder.show();
         } else {
 	        NfcDialogs.createHceTapReaderDialog(this, null).show();
@@ -62,4 +60,30 @@
             getPassButton().setEnabled(true);
         }
     }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (!mCardEmulation.isDefaultServiceForCategory(
+                PaymentService1.COMPONENT, CardEmulation.CATEGORY_PAYMENT)) {
+            // Popup dialog-box, fail test
+            AlertDialog.Builder builder = new AlertDialog.Builder(this);
+            builder.setTitle("Test failed.");
+            builder.setMessage("PaymentService1 is not the default service according " +
+                    "to CardEmulation.getDefaultServiceForCategory(), verify the make " +
+                    "default implementation is correct.");
+            builder.setPositiveButton("OK", null);
+            builder.show();
+        } else {
+            NfcDialogs.createHceTapReaderDialog(this, null).show();
+        }
+    }
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        Intent changeDefault = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
+        changeDefault.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
+        changeDefault.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, PaymentService1.COMPONENT);
+        startActivityForResult(changeDefault, 0);
+    }
 }