Snap for 5631427 from 91c987a929af0374584e53fbd227f4dfb0575b10 to qt-release

Change-Id: Ia0be7fb971198e3b24e52c7be55a45f9bd1127ad
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 3cd7d54..9cd6aaa 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyConstants.java
@@ -343,6 +343,7 @@
     public static final String EventPreciseStateChanged = "PreciseStateChanged";
     public static final String EventDataConnectionRealTimeInfoChanged = "DataConnectionRealTimeInfoChanged";
     public static final String EventDataConnectionStateChanged = "DataConnectionStateChanged";
+    public static final String EventActiveDataSubIdChanged = "ActiveDataSubIdChanged";
     public static final String EventServiceStateChanged = "ServiceStateChanged";
     public static final String EventSignalStrengthChanged = "SignalStrengthChanged";
     public static final String EventSrvccStateChanged = "SrvccStateChanged";
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 72efd24..2631e49 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyManagerFacade.java
@@ -47,6 +47,8 @@
 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
                                                    .CallStateChangeListener;
 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
+                                                   .ActiveDataSubIdChangeListener;
+import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
                                                    .CellInfoChangeListener;
 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
                                                    .DataConnectionRealTimeInfoChangeListener;
@@ -264,6 +266,28 @@
         return true;
     }
 
+    @Rpc(description = "Starts tracking active opportunistic data change" +
+                       "for default subscription ID.")
+    public Boolean telephonyStartTrackingActiveDataChange() {
+        return telephonyStartTrackingActiveDataChangeForSubscription(
+                              SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+    }
+
+    @Rpc(description = "Starts tracking active opportunistic data change" +
+                       "for specified subscription ID.")
+    public Boolean telephonyStartTrackingActiveDataChangeForSubscription(
+                @RpcParameter(name = "subId") Integer subId) {
+        StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
+        if(listener == null) {
+            Log.e("Invalid subscription ID");
+            return false;
+        }
+        mTelephonyManager.createForSubscriptionId(subId).listen(
+            listener.mActiveDataSubIdChangeListener,
+            PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
+        return true;
+    }
+
     @Rpc(description = "Turn on/off precise listening on fore/background or" +
                        " ringing calls for default voice subscription ID.")
     public Boolean telephonyAdjustPreciseCallStateListenLevel(
@@ -319,6 +343,29 @@
             PhoneStateListener.LISTEN_NONE);
         return true;
     }
+
+    @Rpc(description = "Stops tracking active opportunistic data " +
+            "for default subscription ID.")
+    public Boolean telephonyStopTrackingActiveDataChange() {
+        return telephonyStopTrackingActiveDataChangeForSubscription(
+                SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+    }
+
+    @Rpc(description = "Stops tracking active opportunistic data " +
+                       "for specified subscription ID.")
+    public Boolean telephonyStopTrackingActiveDataChangeForSubscription(
+                   @RpcParameter(name = "subId") Integer subId) {
+        StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
+        if(listener == null) {
+            Log.e("Invalid subscription ID");
+            return false;
+        }
+        mTelephonyManager.createForSubscriptionId(subId).listen(
+            listener.mActiveDataSubIdChangeListener,
+            PhoneStateListener.LISTEN_NONE);
+        return true;
+    }
+
     @Rpc(description = "Stops tracking call state change " +
             "for default voice subscription ID.")
     public Boolean telephonyStopTrackingCallStateChange() {
@@ -690,6 +737,11 @@
             mTelephonyManager.getPhoneType());
     }
 
+    @Rpc(description = "Returns preferred opportunistic data subscription Id")
+    public Integer telephonyGetPreferredOpportunisticDataSubscription() {
+        return mTelephonyManager.getPreferredOpportunisticDataSubscription();
+    }
+
     /**
     * Get device phone type for a subscription.
     * @param subId the subscriber id
@@ -1392,6 +1444,7 @@
         public CallStateChangeListener mCallStateChangeListener;
         public CellInfoChangeListener mCellInfoChangeListener;
         public DataConnectionStateChangeListener mDataConnectionStateChangeListener;
+        public ActiveDataSubIdChangeListener mActiveDataSubIdChangeListener;
         public DataConnectionRealTimeInfoChangeListener mDataConnectionRTInfoChangeListener;
         public VoiceMailStateChangeListener mVoiceMailStateChangeListener;
 
@@ -1403,6 +1456,9 @@
             mDataConnectionStateChangeListener =
                 new DataConnectionStateChangeListener(
                         mEventFacade, mTelephonyManager, subId, mService.getMainLooper());
+            mActiveDataSubIdChangeListener =
+                new ActiveDataSubIdChangeListener(
+                        mEventFacade, mTelephonyManager, subId, mService.getMainLooper());
             mCallStateChangeListener =
                 new CallStateChangeListener(mEventFacade, subId, mService.getMainLooper());
             mCellInfoChangeListener =
@@ -1425,6 +1481,9 @@
                     mCallStateChangeListener,
                     PhoneStateListener.LISTEN_NONE);
             mTelephonyManager.listen(
+                    mActiveDataSubIdChangeListener,
+                    PhoneStateListener.LISTEN_NONE);
+            mTelephonyManager.listen(
                     mCellInfoChangeListener,
                     PhoneStateListener.LISTEN_NONE);
             mTelephonyManager.listen(
diff --git a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyStateListeners.java b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyStateListeners.java
index c67b434..581b69f 100644
--- a/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyStateListeners.java
+++ b/Common/src/com/googlecode/android_scripting/facade/telephony/TelephonyStateListeners.java
@@ -181,6 +181,43 @@
         }
     }
 
+    public static class ActiveDataSubIdChangeListener extends PhoneStateListener {
+
+        private final EventFacade mEventFacade;
+        private final TelephonyManager mTelephonyManager;
+        public static final int sListeningStates =
+                PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE;
+        public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
+        public ActiveDataSubIdChangeListener(EventFacade ef, TelephonyManager tm) {
+            super();
+            mEventFacade = ef;
+            mTelephonyManager = tm;
+            subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
+        }
+
+        public ActiveDataSubIdChangeListener(EventFacade ef, TelephonyManager tm, int subId) {
+            super();
+            mEventFacade = ef;
+            mTelephonyManager = tm;
+            subscriptionId = subId;
+        }
+
+        public ActiveDataSubIdChangeListener(
+                EventFacade ef, TelephonyManager tm, int subId, Looper looper) {
+            super(looper);
+            mEventFacade = ef;
+            mTelephonyManager = tm;
+            subscriptionId = subId;
+        }
+
+        @Override
+        public void onActiveDataSubscriptionIdChanged(int subId) {
+            mEventFacade.postEvent(
+                TelephonyConstants.EventActiveDataSubIdChanged, subId);
+        }
+    }
+
     public static class ServiceStateChangeListener extends PhoneStateListener {
 
         private final EventFacade mEventFacade;