Bluetooth: Use Trust feature in SAP/FTP/MAP applications

Add change in SAP/FTP/MAP application to use bluetoth framework
api to set/get remote device trust value. This marks remote
devices as trusted devices and stores it in NVRAM for subsequent
connections.

Change-Id: I6060698b6aa9c4f7dd6ae19b7b6a6b089b895239
CRs-fixed: 523135
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
index ceec144..2cb8a14 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
@@ -216,7 +216,6 @@
 
 
     private BluetoothSocket mConnSocket = null;
-    private static HashSet<BluetoothDevice> trustDevices = new HashSet<BluetoothDevice>();
     private BluetoothDevice mRemoteDevice = null;
 
     private static String sRemoteDeviceName = null;
@@ -313,8 +312,10 @@
             isWaitingAuthorization = false;
 
                 if (intent.getBooleanExtra(BluetoothFtpService.EXTRA_ALWAYS_ALLOWED, false)) {
-                   trustDevices.add(mRemoteDevice);
-                  Log.v(TAG, "setTrust() D: " + mRemoteDevice.getName()+ "ADDED: " + trustDevices.contains(mRemoteDevice));
+                    if(mRemoteDevice != null) {
+                       mRemoteDevice.setTrust(true);
+                       Log.v(TAG, "setTrust() TRUE " + mRemoteDevice.getName());
+                    }
                 }
                 try {
                     if (mConnSocket != null) {
@@ -350,21 +351,6 @@
                    removeTimeoutMsg = false;
                  }
             }
-        } else if ( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
-
-            if (intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) {
-               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-                if(device != null)
-                    Log.d(TAG,"device: "+ device.getName());
-                if(mRemoteDevice != null)
-                    Log.d(TAG," Remtedevie: "+mRemoteDevice.getName());
-               if (device != null && trustDevices.contains(device) &&
-                     intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE) == BluetoothDevice.BOND_NONE) {
-                   Log.d(TAG,"BOND_STATE_CHANGED RFRSH trustDevices"+ device.getName());
-                   trustDevices.remove(device);
-               }
-            }
-
         } else {
             removeTimeoutMsg = false;
         }
@@ -644,8 +630,8 @@
                     mSessionStatusHandler.sendMessage(mSessionStatusHandler
                            .obtainMessage(MSG_INTERNAL_OBEX_RFCOMM_SESSION_UP));
                         boolean trust = false;
-                    if (trustDevices != null)
-                       trust = trustDevices.contains(mRemoteDevice);
+                    if (mRemoteDevice != null)
+                       trust = mRemoteDevice.getTrustState();
 
                     if (VERBOSE) Log.v(RTAG, "GetTrustState() = " + trust);
 
diff --git a/src/org/codeaurora/bluetooth/map/BluetoothMasService.java b/src/org/codeaurora/bluetooth/map/BluetoothMasService.java
index cbdbdba..a9434ef 100755
--- a/src/org/codeaurora/bluetooth/map/BluetoothMasService.java
+++ b/src/org/codeaurora/bluetooth/map/BluetoothMasService.java
@@ -182,7 +182,6 @@
 
     BluetoothMns mnsClient;
     private static BluetoothDevice mRemoteDevice = null;
-    private static HashSet<BluetoothDevice> trustDevices = new HashSet<BluetoothDevice>();
 
     private boolean mHasStarted = false;
     private int mStartId = -1;
@@ -389,8 +388,10 @@
                return;
             }
             if (intent.getBooleanExtra(BluetoothMasService.EXTRA_ALWAYS_ALLOWED, false) == true) {
-                   trustDevices.add(mRemoteDevice);
-                  Log.v(TAG, "setTrust() TRUE " + mRemoteDevice.getName());
+                if(mRemoteDevice != null) {
+                   mRemoteDevice.setTrust(true);
+                   Log.v(TAG, "setTrust() TRUE " + mRemoteDevice.getName());
+                }
             }
             Log.v(TAG, "parseIntent 2: mIsEmailEnabled: " + mIsEmailEnabled);
             if(mIsEmailEnabled) {
@@ -405,22 +406,6 @@
             notifyAuthKeyInput(sessionkey);
         } else if (AUTH_CANCELLED_ACTION.equals(action)) {
             notifyAuthCancelled();
-        } else if ( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
-
-            if (intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) {
-               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-                if(device != null)
-                    Log.d(TAG,"device: "+ device.getName());
-                if(mRemoteDevice != null)
-                    Log.d(TAG," Remtedevie: "+mRemoteDevice.getName());
-               if (device != null && trustDevices.contains(device) &&
-                     intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE) == BluetoothDevice.BOND_NONE) {
-                   Log.d(TAG,"BOND_STATE_CHANGED REFRESH trustDevices"+ device.getName());
-                   trustDevices.remove(device);
-               }
-            }
-
-            removeTimeoutMsg = false;
         } else {
             removeTimeoutMsg = false;
         }
@@ -957,8 +942,8 @@
                             continue;
                         }
                         boolean trust = false;
-                        if (trustDevices != null)
-                           trust = trustDevices.contains(mRemoteDevice);
+                        if (mRemoteDevice != null)
+                           trust = mRemoteDevice.getTrustState();
                         if (VERBOSE) Log.v(TAG, "GetTrustState() = " + trust);
                         if (mIsRequestBeingNotified) {
                             if (VERBOSE) Log.v(TAG, "Request notification is still on going.");
diff --git a/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java b/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
index f95fa39..aef74ec 100644
--- a/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
+++ b/src/org/codeaurora/bluetooth/sap/BluetoothSapService.java
@@ -270,8 +270,6 @@
 
     private HashMap<BluetoothDevice, BluetoothSapDevice> mSapDevices;
 
-    private static HashSet<BluetoothDevice> trustDevices = new HashSet<BluetoothDevice>();
-
     private IBinder mSapBinder;
 
     private BluetoothAdapter mAdapter;
@@ -484,8 +482,10 @@
             }
 
             if (intent.getBooleanExtra(BluetoothSapService.SAP_EXTRA_ALWAYS_ALLOWED, false) == true) {
-                   trustDevices.add(mRemoteDevice);
-                  Log.v(TAG, "setTrust() TRUE " + mRemoteDevice.getName());
+                  if(mRemoteDevice != null) {
+                     mRemoteDevice.setTrust(true);
+                     Log.v(TAG, "setTrust() TRUE " + mRemoteDevice.getName());
+                  }
             }
             /* start the uplink thread */
             startUplinkThread();
@@ -495,21 +495,6 @@
             sendErrorConnResp();
             closeRfcommSocket();
             startRfcommListenerThread();
-        } else if ( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
-
-            if (intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) {
-               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-                if(device != null)
-                    Log.d(TAG,"device: "+ device.getName());
-                if(mRemoteDevice != null)
-                    Log.d(TAG," Remtedevie: "+mRemoteDevice.getName());
-               if (device != null && trustDevices.contains(device) &&
-                     intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE) == BluetoothDevice.BOND_NONE) {
-                   Log.d(TAG,"BOND_STATE_CHANGED REFRESH trustDevices"+ device.getName());
-                   trustDevices.remove(device);
-               }
-            }
-
         } else {
             removeTimeoutMsg = false;
         }
@@ -835,8 +820,8 @@
                         break;
                     }
                     boolean trust = false;
-                    if (trustDevices != null)
-                        trust = trustDevices.contains(mRemoteDevice);
+                    if (mRemoteDevice != null)
+                        trust = mRemoteDevice.getTrustState();
                     if (VERBOSE) Log.v(TAG, "GetTrustState() = " + trust);
 
                     if (trust) {