Detect when BT Pairing Dialog is Incorrectly Shown

- Show a dialog when a pairing dialog intent is shown when
  an insecure connection is made between devices. The
  pairing dialog should only be shown when trying to make
  a secure connection.

- Fix a bug where the results from the "Secure client" or
  "Insecure client" tests were not being recorded.

Change-Id: I9886945df4cd0e1e652a7121624e9cc52497cc64
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 93b5078..08bd98b 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -75,6 +75,8 @@
     <string name="bt_sent_messages">Sent Messages</string>
     <string name="bt_no_messages">No messages</string>
     <string name="bt_make_discoverable">Make Discoverable</string>
+    <string name="bt_insecure_pairing_error_title">Pairing dialog shown?</string>
+    <string name="bt_insecure_pairing_error_message">Insecure connections should not show the pairing dialog!</string>
 
     <string name="bt_secure_client">Secure Client</string>
     <string name="bt_insecure_client">Insecure Client</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/BluetoothTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/BluetoothTestActivity.java
index a6f9919..b617fc2 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/BluetoothTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/BluetoothTestActivity.java
@@ -254,6 +254,7 @@
         if (data != null) {
             TestResult result = TestResult.fromActivityResult(resultCode, data);
             TestListItem test = secure ? mSecureClientTest : mInsecureClientTest;
+            test.setResult(result.getResult());
             updateTest(test);
         }
     }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/MessageTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/MessageTestActivity.java
index aabe00c..1d6706d 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/MessageTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/MessageTestActivity.java
@@ -18,11 +18,16 @@
 
 import com.android.cts.verifier.PassFailButtons;
 import com.android.cts.verifier.R;
+import com.android.cts.verifier.TestResult;
 
 import android.app.AlertDialog;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
@@ -43,12 +48,17 @@
     static final String EXTRA_DEVICE_ADDRESS = "deviceAddress";
     static final String EXTRA_SECURE = "secure";
 
+    /** Broadcast action that should only be fired when pairing for a secure connection. */
+    private static final String ACTION_PAIRING_REQUEST =
+            "android.bluetooth.device.action.PAIRING_REQUEST";
+
     private static final int ENABLE_BLUETOOTH_REQUEST = 1;
 
     private static final String MESSAGE_DELIMITER = "\n";
     private static final Pattern MESSAGE_PATTERN = Pattern.compile("Message (\\d+) to .*");
 
     private BluetoothAdapter mBluetoothAdapter;
+    private PairingActionReceiver mPairingActionReceiver;
     private BluetoothChatService mChatService;
 
     private ArrayAdapter<String> mReceivedMessagesAdapter;
@@ -112,6 +122,10 @@
 
         getPassButton().setEnabled(false);
 
+        mPairingActionReceiver = new PairingActionReceiver();
+        IntentFilter intentFilter = new IntentFilter(ACTION_PAIRING_REQUEST);
+        registerReceiver(mPairingActionReceiver, intentFilter);
+
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         if (mBluetoothAdapter.isEnabled()) {
             startChatService();
@@ -284,9 +298,41 @@
         Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
     }
 
+    class PairingActionReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (!mSecure && ACTION_PAIRING_REQUEST.equals(intent.getAction())) {
+                runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        showPairingErrorDialog();
+                    }
+                });
+            }
+        }
+    }
+
+    private void showPairingErrorDialog() {
+        new AlertDialog.Builder(MessageTestActivity.this)
+            .setIcon(android.R.drawable.ic_dialog_alert)
+            .setTitle(R.string.bt_insecure_pairing_error_title)
+            .setMessage(R.string.bt_insecure_pairing_error_message)
+            .setPositiveButton(android.R.string.ok,
+                    new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    TestResult.setFailedResult(MessageTestActivity.this);
+                    finish();
+                }
+            })
+            .setCancelable(false)
+            .show();
+    }
+
     @Override
     protected void onDestroy() {
         super.onDestroy();
         mChatService.stop();
+        unregisterReceiver(mPairingActionReceiver);
     }
 }