Snap for 8589293 from 6875e0ba04572899d3af8c5c83c2d5816a8389a3 to sc-v2-platform-release

Change-Id: Ic10c66572b0013118155d0e5980b221fde4e0c0c
diff --git a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
index 7c1bff0..50fa876 100644
--- a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
@@ -16,6 +16,7 @@
 
 package com.googlecode.android_scripting.facade.bluetooth;
 
+import android.app.PendingIntent;
 import android.app.Service;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothAdapter.LeScanCallback;
@@ -26,6 +27,11 @@
 import android.bluetooth.le.ScanFilter.Builder;
 import android.bluetooth.le.ScanResult;
 import android.bluetooth.le.ScanSettings;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.os.Bundle;
 import android.os.ParcelUuid;
 
@@ -195,12 +201,12 @@
     }
 
     /**
-     * Starts a ble scan
+     * Starts a ble scan with ScanCallback callbacks.
      *
      * @param index the id of the myScan whose ScanCallback to start
      * @throws Exception
      */
-    @Rpc(description = "Starts a ble advertisement scan")
+    @Rpc(description = "Starts a ble advertisement scan using scan callback")
     public void bleStartBleScan(
             @RpcParameter(name = "filterListIndex")
             Integer filterListIndex,
@@ -234,6 +240,69 @@
         }
     }
 
+    private PendingIntent createPendingIntent() {
+      return PendingIntent
+          .getBroadcast(mService, 0,
+              new Intent(mService, ScanBroadcastReceiver.class)
+                  .setAction("com.googlecode.android_scripting.ACTION_FOUND")
+                  .setComponent(new ComponentName("com.googlecode.android_scripting",
+                          "com.googlecode.android_scripting.facade.bluetooth.ScanBroadcastReceiver")),
+                  PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_MUTABLE);
+    }
+
+    /**
+     * Starts a ble scan with pending intent callback.
+     *
+     * @param index the id of the myScan whose ScanCallback to start
+     * @throws Exception
+     */
+    @Rpc(description = "Starts a ble advertisement scan")
+    public void bleStartBleScanPendingIntent(
+            @RpcParameter(name = "filterListIndex")
+            Integer filterListIndex,
+            @RpcParameter(name = "scanSettingsIndex")
+            Integer scanSettingsIndex
+            ) throws Exception {
+        Log.d("bluetooth_le_scan starting a background scan using pending intent");
+        ArrayList<ScanFilter> mScanFilters = new ArrayList<ScanFilter>();
+        mScanFilters.add(new ScanFilter.Builder().build());
+        ScanSettings mScanSettings = new ScanSettings.Builder().build();
+        if (mScanFilterList.get(filterListIndex) != null) {
+            mScanFilters = mScanFilterList.get(filterListIndex);
+        } else {
+            throw new IllegalArgumentException("Invalid filterListIndex input:"
+                    + Integer.toString(filterListIndex));
+        }
+        if (mScanSettingsList.get(scanSettingsIndex) != null) {
+            mScanSettings = mScanSettingsList.get(scanSettingsIndex);
+        } else if (!mScanSettingsList.isEmpty()) {
+            throw new IllegalArgumentException("Invalid scanSettingsIndex input:"
+                    + Integer.toString(scanSettingsIndex));
+        }
+        Log.d("Registering receiver");
+        mService.registerReceiver(new TestBroadcastReceiver() ,
+               new IntentFilter(ScanBroadcastReceiver.ACTION_FOUND_SIDESTEP));
+        Log.d("Starting Scan");
+        mScanner.startScan(mScanFilters, mScanSettings, createPendingIntent());
+    }
+
+    private class TestBroadcastReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Log.d("TEST onReceive( " + context + ", " + intent + ")");
+            ArrayList<ScanResult> results = intent.getParcelableArrayListExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT);
+            if (results != null && results.size() > 0) {
+                for (final ScanResult result : results) {
+                    Log.d("onScanResult : " + result.getDevice().getName() + " " + result.getDevice()
+                            .getAddress());
+                    new myScanCallback(1).onScanResult(ScanSettings.CALLBACK_TYPE_ALL_MATCHES, result);
+                }
+            } else {
+                Log.d("Empty results");
+            }
+        }
+    }
+
     /**
      * Starts a classic ble scan
      *
@@ -817,9 +886,9 @@
     public void bleSetScanFilterDeviceAddressTypeAndIrk(
             @RpcParameter(name = "macAddress") String macAddress,
             @RpcParameter(name = "addressType") Integer addressType,
-            @RpcParameter(name = "irk") byte[] irk
+            @RpcParameter(name = "irk") String irk
     ) {
-        mScanFilterBuilder.setDeviceAddress(macAddress, addressType, irk);
+        mScanFilterBuilder.setDeviceAddress(macAddress, addressType, irk.getBytes());
     }
 
     /**
diff --git a/Common/src/com/googlecode/android_scripting/facade/bluetooth/ScanBroadcastReceiver.java b/Common/src/com/googlecode/android_scripting/facade/bluetooth/ScanBroadcastReceiver.java
new file mode 100644
index 0000000..ae56d32
--- /dev/null
+++ b/Common/src/com/googlecode/android_scripting/facade/bluetooth/ScanBroadcastReceiver.java
@@ -0,0 +1,35 @@
+package com.googlecode.android_scripting.facade.bluetooth;
+
+import android.app.Service;
+import android.bluetooth.le.BluetoothLeScanner;
+import android.bluetooth.le.ScanCallback;
+import android.bluetooth.le.ScanResult;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import com.googlecode.android_scripting.facade.EventFacade;
+import com.googlecode.android_scripting.facade.FacadeManager;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ScanBroadcastReceiver extends BroadcastReceiver {
+    private static final String TAG = "SL4A Bluetooth";
+    public static final String ACTION_FOUND_SIDESTEP = "com.googlecode.android_scripting.ACTION_FOUND_SIDE_STEP";
+
+    public ScanBroadcastReceiver() {
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+      Log.d(TAG, "Received Intent data.");
+      Intent i = new Intent(ACTION_FOUND_SIDESTEP);
+      i.putParcelableArrayListExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT,
+          intent.getParcelableArrayListExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT));
+      context.sendBroadcast(i);
+    }
+
+}
diff --git a/ScriptingLayerForAndroid/AndroidManifest.xml b/ScriptingLayerForAndroid/AndroidManifest.xml
index 441342c..5eedf40 100644
--- a/ScriptingLayerForAndroid/AndroidManifest.xml
+++ b/ScriptingLayerForAndroid/AndroidManifest.xml
@@ -20,6 +20,12 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.googlecode.android_scripting"
     android:sharedUserId="android.uid.system">
+
+    <permission android:name="com.googlecode.android_scripting.ACTION_FOUND_PERMISSION" />
+
+    <protected-broadcast android:name="com.googlecode.android_scripting.ACTION_FOUND"
+        android:protectionLevel="signatureOrSystem"/>
+
     <uses-permission android:name="android.permission.ACCESS_BLUETOOTH_SHARE" />
     <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
@@ -247,5 +253,13 @@
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
+        <receiver android:name=".facade.bluetooth.ScanBroadcastReceiver"
+            android:exported="true" >
+            <intent-filter>
+                <action
+                    android:name="com.googlecode.android_scripting.ACTION_FOUND"
+                    android:permission="com.googlecode.android_scripting.ACTION_FOUND_PERMISSION" />
+            </intent-filter>
+        </receiver>
     </application>
 </manifest>