Should not turn on validation for data

Should not turn on validation for data switch every time.
Provide result as a callback

Test: verified manually
Bug: 126264455
Merged-In: I03afe9584bbe5b2c417ca7023c6475efd36f7bbf
Change-Id: I03afe9584bbe5b2c417ca7023c6475efd36f7bbf
diff --git a/src/com/android/ons/ONSProfileSelector.java b/src/com/android/ons/ONSProfileSelector.java
index 05b51bf..4f2d279 100644
--- a/src/com/android/ons/ONSProfileSelector.java
+++ b/src/com/android/ons/ONSProfileSelector.java
@@ -29,6 +29,7 @@
 import android.os.HandlerThread;
 import android.os.Message;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.telephony.CellInfo;
 import android.telephony.CellInfoLte;
 import android.telephony.Rlog;
@@ -41,6 +42,8 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.IUpdateAvailableNetworksCallback;
+import com.android.internal.telephony.ISetOpportunisticDataCallback;
+import com.android.internal.telephony.ISub;
 
 import java.util.Collections;
 import java.util.Comparator;
@@ -338,12 +341,6 @@
         mSubscriptionManager.switchToSubscription(subId, replyIntent);
     }
 
-    private void switchPreferredData(int subId) {
-        mSubscriptionManager.setPreferredDataSubscriptionId(
-                subId, true, (command) -> {}, null);
-        mCurrentDataSubId = subId;
-    }
-
     private void onSubSwitchComplete(int subId) {
         enableModem(subId, true);
         mProfileSelectionCallback.onProfileSelectionDone();
@@ -566,21 +563,45 @@
         message.sendToTarget();
     }
 
+    private void sendSetOpptCallbackHelper(ISetOpportunisticDataCallback callback, int result) {
+        if (callback == null) return;
+        try {
+            callback.onComplete(result);
+        } catch (RemoteException exception) {
+            log("RemoteException " + exception);
+        }
+    }
+
     /**
      * select opportunistic profile for data if passing a valid subId.
      * @param subId : opportunistic subId or SubscriptionManager.DEFAULT_SUBSCRIPTION_ID if
      *              deselecting previously set preference.
      */
-    public boolean selectProfileForData(int subId) {
+    public void selectProfileForData(int subId, boolean needValidation,
+            ISetOpportunisticDataCallback callbackStub) {
         if ((subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)
                 || (isOpprotunisticSub(subId) && isActiveSub(subId))) {
-            mSubscriptionManager.setPreferredDataSubscriptionId(
-                    subId, true, (command) -> {}, null);
+            ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
+            if (iSub == null) {
+                log("Could not get Subscription Service handle");
+                sendSetOpptCallbackHelper(callbackStub,
+                    TelephonyManager.SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
+                return;
+            }
+            try {
+                iSub.setPreferredDataSubscriptionId(subId, needValidation, callbackStub);
+            } catch (RemoteException ex) {
+                log("Could not connect to Subscription Service");
+                sendSetOpptCallbackHelper(callbackStub,
+                        TelephonyManager.SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
+                return;
+            }
             mCurrentDataSubId = subId;
-            return true;
+            sendSetOpptCallbackHelper(callbackStub, TelephonyManager.SET_OPPORTUNISTIC_SUB_SUCCESS);
         } else {
             log("Inactive sub passed for preferred data " + subId);
-            return false;
+            sendSetOpptCallbackHelper(callbackStub,
+                    TelephonyManager.SET_OPPORTUNISTIC_SUB_INVALID_PARAMETER);
         }
     }
 
diff --git a/src/com/android/ons/OpportunisticNetworkService.java b/src/com/android/ons/OpportunisticNetworkService.java
index dcc5a60..c8204de 100644
--- a/src/com/android/ons/OpportunisticNetworkService.java
+++ b/src/com/android/ons/OpportunisticNetworkService.java
@@ -38,6 +38,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.IOns;
 import com.android.internal.telephony.IUpdateAvailableNetworksCallback;
+import com.android.internal.telephony.ISetOpportunisticDataCallback;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.TelephonyPermissions;
 
@@ -216,11 +217,13 @@
          * @param subId which opportunistic subscription
          * {@link SubscriptionManager#getOpportunisticSubscriptions} is preferred for cellular data.
          * Pass {@link SubscriptionManager#DEFAULT_SUBSCRIPTION_ID} to unset the preference
+         * @param needValidation whether validation is needed before switch happens.
+         * @param callback callback upon request completion.
          * @param callingPackage caller's package name
-         * @return true if request is accepted, else false.
          *
          */
-        public boolean setPreferredDataSubscriptionId(int subId, String callingPackage) {
+        public void setPreferredDataSubscriptionId(int subId, boolean needValidation,
+                ISetOpportunisticDataCallback callbackStub, String callingPackage) {
             logDebug("setPreferredDataSubscriptionId subId:" + subId + "callingPackage: " + callingPackage);
             if (!enforceModifyPhoneStatePermission(mContext)) {
                 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
@@ -232,7 +235,7 @@
             }
             final long identity = Binder.clearCallingIdentity();
             try {
-                return mProfileSelector.selectProfileForData(subId);
+                mProfileSelector.selectProfileForData(subId, needValidation, callbackStub);
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
diff --git a/tests/src/com/android/ons/ONSProfileSelectorTest.java b/tests/src/com/android/ons/ONSProfileSelectorTest.java
index 84ebc58..7d34ecc 100644
--- a/tests/src/com/android/ons/ONSProfileSelectorTest.java
+++ b/tests/src/com/android/ons/ONSProfileSelectorTest.java
@@ -224,8 +224,7 @@
 
         // Testing selectProfileForData with no oppotunistic sub and the function should
         // return false.
-        boolean ret = mONSProfileSelector.selectProfileForData(1);
-        assertFalse(ret);
+        mONSProfileSelector.selectProfileForData(1, false, null);
     }
 
     @Test
@@ -254,8 +253,7 @@
         waitUntilReady();
 
         // Testing selectProfileForData with in active sub and the function should return false.
-        boolean ret = mONSProfileSelector.selectProfileForData(5);
-        assertFalse(ret);
+        mONSProfileSelector.selectProfileForData(5, false, null);
     }
 
     @Test
@@ -287,9 +285,8 @@
 
         // Testing selectProfileForData with INVALID_SUBSCRIPTION_ID and the function should
         // return true.
-        boolean ret = mONSProfileSelector.selectProfileForData(
-                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-        assertTrue(ret);
+        mONSProfileSelector.selectProfileForData(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID, false, null);
     }
 
     @Test
@@ -325,7 +322,6 @@
 
         // Testing selectProfileForData with valid opportunistic sub and the function should
         // return true.
-        boolean ret = mONSProfileSelector.selectProfileForData(5);
-        assertTrue(ret);
+        mONSProfileSelector.selectProfileForData(5, false, null);
     }
 }