Merge "Incoming connection dialog tweaks."
diff --git a/core/java/android/bluetooth/BluetoothDeviceProfileState.java b/core/java/android/bluetooth/BluetoothDeviceProfileState.java
index ab3a426..095cd11 100644
--- a/core/java/android/bluetooth/BluetoothDeviceProfileState.java
+++ b/core/java/android/bluetooth/BluetoothDeviceProfileState.java
@@ -120,6 +120,7 @@
     private Pair<Integer, String> mIncomingConnections;
     private PowerManager.WakeLock mWakeLock;
     private PowerManager mPowerManager;
+    private boolean mPairingRequestRcvd = false;
 
     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
         @Override
@@ -187,27 +188,38 @@
                 Message msg = obtainMessage(CONNECTION_ACCESS_REQUEST_REPLY);
                 msg.arg1 = val;
                 sendMessage(msg);
+            } else if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
+                mPairingRequestRcvd = true;
+            } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
+                int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
+                        BluetoothDevice.ERROR);
+                if (state == BluetoothDevice.BOND_BONDED && mPairingRequestRcvd) {
+                    setTrust(BluetoothDevice.CONNECTION_ACCESS_YES);
+                    mPairingRequestRcvd = false;
+                } else if (state == BluetoothDevice.BOND_NONE) {
+                    mPairingRequestRcvd = false;
+                }
             }
         }
     };
 
     private boolean isPhoneDocked(BluetoothDevice autoConnectDevice) {
-      // This works only because these broadcast intents are "sticky"
-      Intent i = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT));
-      if (i != null) {
-          int state = i.getIntExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
-          if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
-              BluetoothDevice device = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-              if (device != null && autoConnectDevice.equals(device)) {
-                  return true;
-              }
-          }
-      }
-      return false;
-  }
+        // This works only because these broadcast intents are "sticky"
+        Intent i = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT));
+        if (i != null) {
+            int state = i.getIntExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
+            if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+                BluetoothDevice device = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+                if (device != null && autoConnectDevice.equals(device)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
     public BluetoothDeviceProfileState(Context context, String address,
-          BluetoothService service, BluetoothA2dpService a2dpService) {
+          BluetoothService service, BluetoothA2dpService a2dpService, boolean setTrust) {
         super(address);
         mContext = context;
         mDevice = new BluetoothDevice(address);
@@ -231,6 +243,8 @@
         filter.addAction(BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED);
         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
         filter.addAction(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
+        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
+        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
 
         mContext.registerReceiver(mBroadcastReceiver, filter);
 
@@ -247,6 +261,10 @@
                                               PowerManager.ACQUIRE_CAUSES_WAKEUP |
                                               PowerManager.ON_AFTER_RELEASE, TAG);
         mWakeLock.setReferenceCounted(false);
+
+        if (setTrust) {
+            setTrust(BluetoothDevice.CONNECTION_ACCESS_YES);
+        }
     }
 
     private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
diff --git a/core/java/android/server/BluetoothBondState.java b/core/java/android/server/BluetoothBondState.java
index 75f38f9..30a8b2a 100644
--- a/core/java/android/server/BluetoothBondState.java
+++ b/core/java/android/server/BluetoothBondState.java
@@ -21,8 +21,13 @@
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothHeadset;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.provider.Settings;
 import android.util.Log;
 
 import java.io.BufferedReader;
@@ -74,11 +79,17 @@
     private BluetoothA2dp mA2dpProxy;
     private BluetoothHeadset mHeadsetProxy;
 
+    private ArrayList<String> mPairingRequestRcvd = new ArrayList<String>();
+
     BluetoothBondState(Context context, BluetoothService service) {
         mContext = context;
         mService = service;
         mBluetoothInputProfileHandler =
             BluetoothInputProfileHandler.getInstance(mContext, mService);
+
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
+        mContext.registerReceiver(mReceiver, filter);
     }
 
     synchronized void setPendingOutgoingBonding(String address) {
@@ -137,11 +148,18 @@
         }
 
         if (state == BluetoothDevice.BOND_BONDED) {
-            mService.addProfileState(address);
+            boolean setTrust = false;
+            if (mPairingRequestRcvd.contains(address)) setTrust = true;
+
+            mService.addProfileState(address, setTrust);
+            mPairingRequestRcvd.remove(address);
+
         } else if (state == BluetoothDevice.BOND_BONDING) {
             if (mA2dpProxy == null || mHeadsetProxy == null) {
                 getProfileProxy();
             }
+        } else if (state == BluetoothDevice.BOND_NONE) {
+            mPairingRequestRcvd.remove(address);
         }
 
         setProfilePriorities(address, state);
@@ -452,4 +470,17 @@
         }
     }
 
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (intent == null) return;
+
+            String action = intent.getAction();
+            if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
+                BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+                String address = dev.getAddress();
+                mPairingRequestRcvd.add(address);
+            }
+        }
+    };
 }
diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java
index ff16c18..ab569a1 100755
--- a/core/java/android/server/BluetoothService.java
+++ b/core/java/android/server/BluetoothService.java
@@ -2277,11 +2277,11 @@
         return false;
     }
 
-    BluetoothDeviceProfileState addProfileState(String address) {
+    BluetoothDeviceProfileState addProfileState(String address, boolean setTrust) {
         BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
         if (state != null) return state;
 
-        state = new BluetoothDeviceProfileState(mContext, address, this, mA2dpService);
+        state = new BluetoothDeviceProfileState(mContext, address, this, mA2dpService, setTrust);
         mDeviceProfileState.put(address, state);
         state.start();
         return state;
@@ -2311,7 +2311,7 @@
         }
         for (String path : bonds) {
             String address = getAddressFromObjectPath(path);
-            BluetoothDeviceProfileState state = addProfileState(address);
+            BluetoothDeviceProfileState state = addProfileState(address, false);
         }
     }