Snap for 4859140 from 6bfbbc0c613344461c619ca1af938d25ddd4e8e2 to pi-dr1-release

Change-Id: I784d988d0bb8664c9694df390e0f3b88b7a69dc0
diff --git a/Common/src/com/googlecode/android_scripting/facade/EventFacade.java b/Common/src/com/googlecode/android_scripting/facade/EventFacade.java
index 9ce6628..42c3f83 100644
--- a/Common/src/com/googlecode/android_scripting/facade/EventFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/EventFacade.java
@@ -371,6 +371,12 @@
         return eventWaitFor(eventName, removeEvent, timeout);
     }
 
+    @Rpc(description = "sl4a session is shutting down, send terminate event to client.")
+    public void closeSl4aSession() {
+        eventClearBuffer();
+        postEvent("EventDispatcherShutdown", null);
+    }
+
     @Override
     public void shutdown() {
         mGlobalEventObservers.clear();
diff --git a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
index 6945f9e..f5e65a3 100644
--- a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
@@ -305,6 +305,7 @@
                 return;
             }
 
+            // Switch Only Necessary for Old implementation. Left in for backwards compatability.
             int profile = -1;
             switch (action) {
                 case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
@@ -337,11 +338,18 @@
                 Log.e("Action does not match any given profiles " + action);
             }
 
+            // The newer implementation will just post the Bundle with the literal event
+            // intead of the old implemenatation of posting BluetoothProfileConnectionStateChanged
+            // with the action inside of the Bundle. This makes for cleaner connection handling
+            // from test frameworks. Left the old implemenation in for backwards compatability.
+
             // Post an event to Facade.
             Bundle news = new Bundle();
-            news.putInt("profile", profile);
             news.putInt("state", state);
             news.putString("addr", device.getAddress());
+            mEventFacade.postEvent(action, news);
+
+            news.putInt("profile", profile);
             news.putString("action", action);
             mEventFacade.postEvent("BluetoothProfileConnectionStateChanged", news);
         }
@@ -515,6 +523,25 @@
         return results;
     }
 
+    /**
+     * Return a list of service UUIDS supported by the bonded device.
+     * @param macAddress the String mac address of the bonded device.
+     *
+     * @return the String list of supported UUIDS.
+     * @throws Exception
+     */
+    @Rpc(description = "Return a list of service UUIDS supported by the bonded device")
+    public List<String> bluetoothGetBondedDeviceUuids(
+        @RpcParameter(name = "macAddress") String macAddress) throws Exception {
+        BluetoothDevice mDevice = BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(),
+                macAddress);
+        ArrayList<String> uuidStrings = new ArrayList<>();
+        for (ParcelUuid parcelUuid : mDevice.getUuids()) {
+            uuidStrings.add(parcelUuid.toString());
+        }
+        return uuidStrings;
+    }
+
     @Rpc(description = "Return a list of devices connected through bluetooth LE")
     public List<BluetoothDevice> bluetoothGetConnectedLeDevices(Integer profile) {
         return mBluetoothManager.getConnectedDevices(profile);
diff --git a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java
index 4efe857..9c65324 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java
@@ -420,6 +420,27 @@
         public static final String SYSTEM_ID = "systemId";
         public static final String SUBSCRIPTION_ID = "subscriptionId";
         public static final String SERVICE_STATE = "serviceState";
+        public static final String CHANNEL_NUMBER = "channelNumber";
+        public static final String CELL_BANDWIDTHS = "cellBandwidths";
+        public static final String DUPLEX_MODE = "duplexMode";
+
+        public static final String VOICE_ROAMING_TYPE = "voiceRoamingType";
+        public static final String DATA_ROAMING_TYPE = "dataRoamingType";
+        public static final String VOICE_OPERATOR_ALPHA_LONG = "voiceOperatorAlphaLong";
+        public static final String VOICE_OPERATOR_ALPHA_SHORT = "voiceOperatorAlphaShort";
+        public static final String DATA_OPERATOR_ALPHA_LONG = "dataOperatorAlphaLong";
+        public static final String DATA_OPERATOR_ALPHA_SHORT = "dataOperatorAlphaShort";
+        public static final String VOICE_OPERATOR_NUMERIC = "voiceOperatorNumeric";
+        public static final String DATA_OPERATOR_NUMERIC = "dataOperatorNumeric";
+        public static final String VOICE_RADIO_TECHNOLOGY = "voiceRadioTechnology";
+        public static final String DATA_RADIO_TECHNOLOGY = "dataRadioTechnology";
+        public static final String CSS_INDICATOR = "cssIndicator";
+        public static final String CDMA_ROAMING_INDICATOR = "cdmaRoamingIndicator";
+        public static final String CDMA_DEFAULT_ROAMING_INDICATOR = "cdmaDefaultRoamingIndicator";
+        public static final String IS_DATA_ROAMING_FROM_REGISTRATION =
+                "isDataRoamingFromRegistration";
+        public static final String IS_USING_CARRIER_AGGREGATION = "isUsingCarrierAggregation";
+        public static final String LTE_EARFCN_RSRP_BOOST = "lteEarfcnRsrpBoost";
     }
 
     public static class MessageWaitingIndicatorContainer {
diff --git a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyEvents.java b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyEvents.java
index 863f7d4..41785c7 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyEvents.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyEvents.java
@@ -16,13 +16,16 @@
 
 package com.googlecode.android_scripting.facade.telephony;
 
-import org.json.JSONException;
-import org.json.JSONObject;
 import android.telephony.DataConnectionRealTimeInfo;
 import android.telephony.PreciseCallState;
 import android.telephony.ServiceState;
+
 import com.googlecode.android_scripting.jsonrpc.JsonSerializable;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
 public class TelephonyEvents {
 
     public static class CallStateEvent implements JsonSerializable {
@@ -228,51 +231,12 @@
         }
 
         public JSONObject toJSON() throws JSONException {
-            JSONObject serviceState = new JSONObject();
-
+            JSONObject serviceState =
+                    com.googlecode.android_scripting.jsonrpc.JsonBuilder.buildServiceState(
+                            mServiceState);
             serviceState.put(
                     TelephonyConstants.ServiceStateContainer.SUBSCRIPTION_ID,
                     mSubscriptionId);
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.VOICE_REG_STATE,
-                    TelephonyUtils.getNetworkStateString(
-                            mServiceState.getVoiceRegState()));
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.VOICE_NETWORK_TYPE,
-                    TelephonyUtils.getNetworkTypeString(
-                            mServiceState.getVoiceNetworkType()));
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.DATA_REG_STATE,
-                    TelephonyUtils.getNetworkStateString(
-                            mServiceState.getDataRegState()));
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.DATA_NETWORK_TYPE,
-                    TelephonyUtils.getNetworkTypeString(
-                            mServiceState.getDataNetworkType()));
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.OPERATOR_NAME,
-                    mServiceState.getOperatorAlphaLong());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.OPERATOR_ID,
-                    mServiceState.getOperatorNumeric());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.IS_MANUAL_NW_SELECTION,
-                    mServiceState.getIsManualSelection());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.ROAMING,
-                    mServiceState.getRoaming());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.IS_EMERGENCY_ONLY,
-                    mServiceState.isEmergencyOnly());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.NETWORK_ID,
-                    mServiceState.getCdmaNetworkId());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.SYSTEM_ID,
-                    mServiceState.getCdmaSystemId());
-            serviceState.put(
-                    TelephonyConstants.ServiceStateContainer.SERVICE_STATE,
-                    mServiceStateString);
 
             return serviceState;
         }
diff --git a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java
index 0f7d5b1..0e3c8cf 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java
@@ -1235,16 +1235,15 @@
     }
 
     @Rpc(description = "Returns the service state string for default subscription ID")
-    public String telephonyGetServiceState() {
+    public ServiceState telephonyGetServiceState() {
         return telephonyGetServiceStateForSubscription(
                                  SubscriptionManager.getDefaultSubscriptionId());
     }
 
     @Rpc(description = "Returns the service state string for specified subscription ID")
-    public String telephonyGetServiceStateForSubscription(
+    public ServiceState telephonyGetServiceStateForSubscription(
                   @RpcParameter(name = "subId") Integer subId) {
-        ServiceState ss = mTelephonyManager.getServiceStateForSubscriber(subId);
-        return ServiceState.rilServiceStateToString(ss.getState());
+        return mTelephonyManager.getServiceStateForSubscriber(subId);
     }
 
     @Rpc(description = "Returns the call state for default subscription ID")
diff --git a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
index 80fb803..3e23c0c 100644
--- a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
+++ b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
@@ -70,6 +70,7 @@
 import android.telephony.CellSignalStrengthWcdma;
 import android.telephony.ModemActivityInfo;
 import android.telephony.NeighboringCellInfo;
+import android.telephony.ServiceState;
 import android.telephony.SignalStrength;
 import android.telephony.SmsMessage;
 import android.telephony.SubscriptionInfo;
@@ -334,6 +335,9 @@
         if (data instanceof SignalStrength) {
             return buildSignalStrength((SignalStrength) data);
         }
+        if (data instanceof ServiceState) {
+            return buildServiceState((ServiceState) data);
+        }
 
         return data.toString();
         // throw new JSONException("Failed to build JSON result. " +
@@ -1283,6 +1287,67 @@
         return info;
     }
 
+    public static JSONObject buildServiceState(ServiceState ss) throws JSONException {
+        JSONObject info = new JSONObject();
+
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_REG_STATE,
+            TelephonyUtils.getNetworkStateString(ss.getVoiceRegState()));
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_NETWORK_TYPE,
+            TelephonyUtils.getNetworkTypeString(ss.getVoiceNetworkType()));
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_REG_STATE,
+            TelephonyUtils.getNetworkStateString(ss.getDataRegState()));
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_NETWORK_TYPE,
+            TelephonyUtils.getNetworkTypeString(ss.getDataNetworkType()));
+    info.put(TelephonyConstants.ServiceStateContainer.OPERATOR_NAME, ss.getOperatorAlphaLong());
+    info.put(TelephonyConstants.ServiceStateContainer.OPERATOR_ID, ss.getOperatorNumeric());
+    info.put(TelephonyConstants.ServiceStateContainer.IS_MANUAL_NW_SELECTION,
+            ss.getIsManualSelection());
+    info.put(TelephonyConstants.ServiceStateContainer.ROAMING, ss.getRoaming());
+    info.put(TelephonyConstants.ServiceStateContainer.IS_EMERGENCY_ONLY, ss.isEmergencyOnly());
+    info.put(TelephonyConstants.ServiceStateContainer.NETWORK_ID, ss.getCdmaNetworkId());
+    info.put(TelephonyConstants.ServiceStateContainer.SYSTEM_ID, ss.getCdmaSystemId());
+    info.put(TelephonyConstants.ServiceStateContainer.SERVICE_STATE,
+            TelephonyUtils.getNetworkStateString(ss.getState()));
+    info.put(TelephonyConstants.ServiceStateContainer.CHANNEL_NUMBER, ss.getChannelNumber());
+    info.put(TelephonyConstants.ServiceStateContainer.CELL_BANDWIDTHS,
+            ss.getCellBandwidths() != null
+                    ? new JSONArray(ss.getCellBandwidths())
+                    : JSONObject.NULL);
+    info.put(TelephonyConstants.ServiceStateContainer.DUPLEX_MODE, ss.getDuplexMode());
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_ROAMING_TYPE,
+            ss.getVoiceRoamingType());
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_ROAMING_TYPE,
+            ss.getDataRoamingType());
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_OPERATOR_ALPHA_LONG,
+            ss.getVoiceOperatorAlphaLong());
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_OPERATOR_ALPHA_SHORT,
+            ss.getVoiceOperatorAlphaShort());
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_OPERATOR_NUMERIC,
+            ss.getVoiceOperatorNumeric());
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_OPERATOR_ALPHA_LONG,
+            ss.getDataOperatorAlphaLong());
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_OPERATOR_ALPHA_SHORT,
+            ss.getDataOperatorAlphaShort());
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_OPERATOR_NUMERIC,
+            ss.getDataOperatorNumeric());
+    info.put(TelephonyConstants.ServiceStateContainer.VOICE_RADIO_TECHNOLOGY,
+            ss.getRilVoiceRadioTechnology());
+    info.put(TelephonyConstants.ServiceStateContainer.DATA_RADIO_TECHNOLOGY,
+            ss.getRilDataRadioTechnology());
+    info.put(TelephonyConstants.ServiceStateContainer.CSS_INDICATOR, ss.getCssIndicator());
+    info.put(TelephonyConstants.ServiceStateContainer.CDMA_ROAMING_INDICATOR,
+            ss.getCdmaRoamingIndicator());
+    info.put(TelephonyConstants.ServiceStateContainer.CDMA_DEFAULT_ROAMING_INDICATOR,
+            ss.getCdmaDefaultRoamingIndicator());
+    info.put(TelephonyConstants.ServiceStateContainer.IS_DATA_ROAMING_FROM_REGISTRATION,
+            ss.getDataRoamingFromRegistration());
+    info.put(TelephonyConstants.ServiceStateContainer.IS_USING_CARRIER_AGGREGATION,
+            ss.isUsingCarrierAggregation());
+    info.put(TelephonyConstants.ServiceStateContainer.LTE_EARFCN_RSRP_BOOST,
+            ss.getLteEarfcnRsrpBoost());
+    return info;
+    }
+
     private JsonBuilder() {
         // This is a utility class.
     }