Rename DownloadRetryOperationCode to DownloadRetryResultCode am: 60960ae35c

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/services/AlternativeNetworkAccess/+/19669833

Change-Id: Idb499beaf253bf68779a90e9791697172645f134
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/ons/ONSProfileActivator.java b/src/com/android/ons/ONSProfileActivator.java
index 9da0006..db63391 100644
--- a/src/com/android/ons/ONSProfileActivator.java
+++ b/src/com/android/ons/ONSProfileActivator.java
@@ -35,6 +35,7 @@
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.ons.ONSProfileDownloader.DownloadRetryResultCode;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -308,9 +309,9 @@
     }
 
     @Override
-    public void onDownloadError(ONSProfileDownloader.DownloadRetryOperationCode operationCode,
-                                int pSIMSubId) {
-        switch (operationCode) {
+    public void onDownloadError(int pSIMSubId, DownloadRetryResultCode resultCode,
+             int detailedErrorCode) {
+        switch (resultCode) {
             case ERR_MEMORY_FULL: {
                 //eUICC Memory full occurred while downloading opportunistic eSIM.
 
@@ -354,8 +355,9 @@
             }
             break;
 
-            case ERR_UNRESOLVABLE: {
-                //Stop download until SIM change or device reboot.
+            default: {
+                // Stop download until SIM change or device reboot.
+                Log.e(TAG, "Download failed with cause=" + resultCode);
             }
         }
     }
@@ -363,8 +365,7 @@
     /**
      * Called when eSIM download fails. Listener is called after a delay based on retry count with
      * the error code: BACKOFF_TIMER_EXPIRED
-     * @param pSIMSubId
-     * @param retryCount
+     * @param pSIMSubId Primary Subscription ID
      */
     @VisibleForTesting
     protected void startBackoffTimer(int pSIMSubId) {
@@ -417,7 +418,7 @@
      * attempts will not be made until next device reboot.
      *
      * @param subscriptionId subscription Id of the primary SIM.
-     * @return
+     * @return integer value for maximum allowed retry attempts.
      */
     private int getDownloadRetryMaxAttemptsVal(int subscriptionId) {
         PersistableBundle config = mCarrierConfigMgr.getConfigForSubId(subscriptionId);
diff --git a/src/com/android/ons/ONSProfileDownloader.java b/src/com/android/ons/ONSProfileDownloader.java
index affcdf6..728f189 100644
--- a/src/com/android/ons/ONSProfileDownloader.java
+++ b/src/com/android/ons/ONSProfileDownloader.java
@@ -37,7 +37,7 @@
 
     interface IONSProfileDownloaderListener {
         void onDownloadComplete(int primarySubId);
-        void onDownloadError(DownloadRetryOperationCode operationCode, int pSIMSubId);
+        void onDownloadError(int pSIMSubId, DownloadRetryResultCode resultCode, int detailErrCode);
     }
 
     private static final String TAG = ONSProfileDownloader.class.getName();
@@ -59,7 +59,7 @@
     private int mDownloadingPSimSubId;
 
     @VisibleForTesting
-    protected enum DownloadRetryOperationCode{
+    protected enum DownloadRetryResultCode {
         DOWNLOAD_SUCCESSFUL,
         ERR_UNRESOLVABLE,
         ERR_MEMORY_FULL,
@@ -110,23 +110,23 @@
                     Log.d(TAG, "Operation Code : " + operationCode);
                     Log.d(TAG, "Error Code : " + errorCode);
 
-                    DownloadRetryOperationCode opCode = mapDownloaderErrorCode(msg.arg1,
+                    DownloadRetryResultCode resultCode = mapDownloaderErrorCode(msg.arg1,
                             detailedErrCode, operationCode, errorCode);
-                    Log.d(TAG, "DownloadRetryOperationCode: " + opCode);
+                    Log.d(TAG, "DownloadRetryResultCode: " + resultCode);
 
-                    switch (opCode) {
+                    switch (resultCode) {
                         case DOWNLOAD_SUCCESSFUL:
                             mListener.onDownloadComplete(pSIMSubId);
                             break;
 
                         case ERR_UNRESOLVABLE:
-                            mListener.onDownloadError(opCode, pSIMSubId);
+                            mListener.onDownloadError(pSIMSubId, resultCode, detailedErrCode);
                             Log.e(TAG, "Unresolvable download error: "
                                     + getUnresolvableErrorDescription(errorCode));
                             break;
 
                         default:
-                            mListener.onDownloadError(opCode, pSIMSubId);
+                            mListener.onDownloadError(pSIMSubId, resultCode, detailedErrCode);
                             break;
                     }
                 }
@@ -135,7 +135,7 @@
         }
 
         @VisibleForTesting
-        protected DownloadRetryOperationCode mapDownloaderErrorCode(int resultCode,
+        protected DownloadRetryResultCode mapDownloaderErrorCode(int resultCode,
                                                                     int detailedErrCode,
                                                                     int operationCode,
                                                                     int errorCode) {
@@ -149,17 +149,17 @@
                 //8.1 - eUICC, 4.8 - Insufficient Memory
                 // eUICC does not have sufficient space for this Profile.
                 if (errCode.equals(Pair.create("8.1.0", "4.8"))) {
-                    return DownloadRetryOperationCode.ERR_MEMORY_FULL;
+                    return DownloadRetryResultCode.ERR_MEMORY_FULL;
                 }
 
                 //8.8.5 - Download order, 4.10 - Time to Live Expired
                 //The Download order has expired
                 if (errCode.equals(Pair.create("8.8.5", "4.10"))) {
-                    return DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD;
+                    return DownloadRetryResultCode.ERR_RETRY_DOWNLOAD;
                 }
 
                 //All other errors are unresolvable or retry after SIM State Change
-                return DownloadRetryOperationCode.ERR_UNRESOLVABLE;
+                return DownloadRetryResultCode.ERR_UNRESOLVABLE;
 
             }
 
@@ -167,29 +167,29 @@
 
                 //Success Cases
                 case EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK: {
-                    return DownloadRetryOperationCode.DOWNLOAD_SUCCESSFUL;
+                    return DownloadRetryResultCode.DOWNLOAD_SUCCESSFUL;
                 }
 
                 //Low eUICC memory cases
                 case EuiccManager.ERROR_EUICC_INSUFFICIENT_MEMORY: {
                     Log.d(TAG, "Download ERR: EUICC_INSUFFICIENT_MEMORY");
-                    return DownloadRetryOperationCode.ERR_MEMORY_FULL;
+                    return DownloadRetryResultCode.ERR_MEMORY_FULL;
                 }
 
                 //Temporary download error cases
                 case EuiccManager.ERROR_TIME_OUT:
                 case EuiccManager.ERROR_CONNECTION_ERROR:
                 case EuiccManager.ERROR_OPERATION_BUSY: {
-                    return DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD;
+                    return DownloadRetryResultCode.ERR_RETRY_DOWNLOAD;
                 }
 
                 //Profile installation failure cases
                 case EuiccManager.ERROR_INSTALL_PROFILE: {
-                    return DownloadRetryOperationCode.ERR_INSTALL_ESIM_PROFILE_FAILED;
+                    return DownloadRetryResultCode.ERR_INSTALL_ESIM_PROFILE_FAILED;
                 }
 
                 default: {
-                    return DownloadRetryOperationCode.ERR_UNRESOLVABLE;
+                    return DownloadRetryResultCode.ERR_UNRESOLVABLE;
                 }
             }
         }
@@ -236,6 +236,12 @@
     protected DownloadProfileResult downloadProfile(int primarySubId) {
         Log.d(TAG, "downloadProfile");
 
+        //Get SMDP address from carrier configuration.
+        String smdpAddress = getSMDPServerAddress(primarySubId);
+        if (smdpAddress == null || smdpAddress.length() <= 0) {
+            return DownloadProfileResult.INVALID_SMDP_ADDRESS;
+        }
+
         synchronized (this) {
             if (mDownloadingPSimSubId == primarySubId) {
                 Log.d(TAG, "Download already in progress.");
@@ -245,12 +251,6 @@
             mDownloadingPSimSubId = primarySubId;
         }
 
-        //Get SMDP address from carrier configuration.
-        String smdpAddress = getSMDPServerAddress(primarySubId);
-        if (smdpAddress == null || smdpAddress.length() <= 0) {
-            return DownloadProfileResult.INVALID_SMDP_ADDRESS;
-        }
-
         //Generate Activation code 1${SM-DP+ FQDN}$
         String activationCode = "1$" + smdpAddress + "$";
         Intent intent = new Intent(mContext, ONSProfileResultReceiver.class);
diff --git a/tests/src/com/android/ons/ONSProfileActivatorTest.java b/tests/src/com/android/ons/ONSProfileActivatorTest.java
index fb46167..dce106c 100644
--- a/tests/src/com/android/ons/ONSProfileActivatorTest.java
+++ b/tests/src/com/android/ons/ONSProfileActivatorTest.java
@@ -650,8 +650,9 @@
         onsProfileActivator.mIsInternetConnAvailable = true;
 
         for (int idx = 0; idx <= maxRetryCount; idx++) {
-            onsProfileActivator.onDownloadError(ONSProfileDownloader
-                    .DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD, TEST_SUBID_0);
+            onsProfileActivator.onDownloadError(
+                    TEST_SUBID_0,
+                    ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
             //Wait for Handler to process download message. Backoff delay + 500 milli secs.
             try {
diff --git a/tests/src/com/android/ons/ONSProfileDownloaderTest.java b/tests/src/com/android/ons/ONSProfileDownloaderTest.java
index f42f802..e82df6a 100644
--- a/tests/src/com/android/ons/ONSProfileDownloaderTest.java
+++ b/tests/src/com/android/ons/ONSProfileDownloaderTest.java
@@ -36,6 +36,8 @@
 import android.util.Log;
 import android.util.Pair;
 
+import com.android.ons.ONSProfileDownloader.DownloadProfileResult;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -120,8 +122,9 @@
 
                     @Override
                     public void onDownloadError(
-                            ONSProfileDownloader.DownloadRetryOperationCode operationCode,
-                            int pSIMSubId) {
+                            int pSIMSubId,
+                            ONSProfileDownloader.DownloadRetryResultCode operationCode,
+                            int detailedErrorCode) {
 
                     }
                 };
@@ -236,8 +239,7 @@
         }
 
         verify(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_MEMORY_FULL,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_MEMORY_FULL, 0);
         workerThread.exit();
     }
 
@@ -249,8 +251,7 @@
         config.putString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, TEST_SMDP_ADDRESS);
         doReturn(config).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID);
         doNothing().when(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         Runnable runnable = new Runnable() {
             @Override
@@ -291,8 +292,7 @@
                 .getEncodedActivationCode();
 
         verify(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         workerThread.exit();
     }
@@ -305,8 +305,7 @@
         config.putString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, TEST_SMDP_ADDRESS);
         doReturn(config).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID);
         doNothing().when(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         Runnable runnable = new Runnable() {
             @Override
@@ -347,8 +346,7 @@
                 .getEncodedActivationCode();
 
         verify(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         workerThread.exit();
     }
@@ -361,8 +359,7 @@
         config.putString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, TEST_SMDP_ADDRESS);
         doReturn(config).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID);
         doNothing().when(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         Runnable runnable = new Runnable() {
             @Override
@@ -403,8 +400,7 @@
                 .getEncodedActivationCode();
 
         verify(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         workerThread.exit();
     }
@@ -417,8 +413,7 @@
         config.putString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, TEST_SMDP_ADDRESS);
         doReturn(config).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID);
         doNothing().when(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_RETRY_DOWNLOAD, 0);
 
         Runnable runnable = new Runnable() {
             @Override
@@ -453,8 +448,7 @@
         }
 
         verify(mMockDownloadListener).onDownloadError(
-                ONSProfileDownloader.DownloadRetryOperationCode.ERR_UNRESOLVABLE,
-                TEST_SUB_ID);
+                TEST_SUB_ID, ONSProfileDownloader.DownloadRetryResultCode.ERR_UNRESOLVABLE, 0);
         workerThread.exit();
     }
 
@@ -472,51 +466,51 @@
                 ONSProfileDownloader.DownloadHandler downloadHandler =
                         onsProfileDownloader.new DownloadHandler();
 
-                ONSProfileDownloader.DownloadRetryOperationCode res =
+                ONSProfileDownloader.DownloadRetryResultCode res =
                         downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0, 0, 0);
                 assertEquals(
-                        ONSProfileDownloader.DownloadRetryOperationCode.DOWNLOAD_SUCCESSFUL, res);
+                        ONSProfileDownloader.DownloadRetryResultCode.DOWNLOAD_SUCCESSFUL, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 0,
                         EuiccManager.OPERATION_EUICC_GSMA,
                         EuiccManager.ERROR_EUICC_INSUFFICIENT_MEMORY);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_MEMORY_FULL, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 0,
                         EuiccManager.OPERATION_SIM_SLOT,
                         EuiccManager.ERROR_TIME_OUT);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_RETRY_DOWNLOAD, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 0,
                         EuiccManager.OPERATION_SMDX,
                         EuiccManager.ERROR_CONNECTION_ERROR);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_RETRY_DOWNLOAD, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 0,
                         EuiccManager.OPERATION_SMDX,
                         EuiccManager.ERROR_INVALID_RESPONSE);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_UNRESOLVABLE, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 0,
                         EuiccManager.OPERATION_SMDX,
                         EuiccManager.ERROR_INVALID_RESPONSE);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_UNRESOLVABLE, res);
 
                 res = downloadHandler.mapDownloaderErrorCode(
                         EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 0xA810048,
                         EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE, 0);
-                assertEquals(ONSProfileDownloader.DownloadRetryOperationCode
+                assertEquals(ONSProfileDownloader.DownloadRetryResultCode
                         .ERR_MEMORY_FULL, res);
 
                 synchronized (lock) {
@@ -644,6 +638,53 @@
         verify(mMockEUICCManager, times(1)).downloadSubscription(any(), eq(true), any());
     }
 
+    @Test
+    public void testDownloadRequestFailures() {
+        doReturn(TEST_SUB_ID).when(mMockSubInfo).getSubscriptionId();
+        PersistableBundle invalidConfig = new PersistableBundle();
+        invalidConfig.putString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, "");
+
+        PersistableBundle validConfig = new PersistableBundle();
+        validConfig.putString(
+                CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING, TEST_SMDP_ADDRESS);
+
+        // Only the first download request, will receive invalid SMDP server address.
+        doReturn(invalidConfig, validConfig)
+                .when(mMockCarrierConfigManager)
+                .getConfigForSubId(TEST_SUB_ID);
+
+        ONSProfileDownloader onsProfileDownloader =
+                new ONSProfileDownloader(
+                        mContext,
+                        mMockCarrierConfigManager,
+                        mMockEUICCManager,
+                        mMockONSProfileConfig,
+                        mMockDownloadListener);
+
+        // First download request to be failed with INVALID_SMDP_ADDRESS error because of empty SMDP
+        // server address in configuration.
+        DownloadProfileResult retryResultCode =
+                onsProfileDownloader.downloadProfile(mMockSubInfo.getSubscriptionId());
+        assertEquals(DownloadProfileResult.INVALID_SMDP_ADDRESS, retryResultCode);
+
+        verify(mMockEUICCManager, never()).downloadSubscription(any(), eq(true), any());
+
+        // Second Download request should be success and processed to EuiccManager.
+        retryResultCode = onsProfileDownloader.downloadProfile(mMockSubInfo.getSubscriptionId());
+        assertEquals(DownloadProfileResult.SUCCESS, retryResultCode);
+        verify(mMockEUICCManager).downloadSubscription(any(), eq(true), any());
+
+        // Since download request is in progress, no further request to be sent to EuiccManager.
+        // They should return with DUPLICATE_REQUEST error.
+        retryResultCode = onsProfileDownloader.downloadProfile(mMockSubInfo.getSubscriptionId());
+        assertEquals(DownloadProfileResult.DUPLICATE_REQUEST, retryResultCode);
+
+        retryResultCode = onsProfileDownloader.downloadProfile(mMockSubInfo.getSubscriptionId());
+        assertEquals(DownloadProfileResult.DUPLICATE_REQUEST, retryResultCode);
+
+        verify(mMockEUICCManager).downloadSubscription(any(), eq(true), any());
+    }
+
     @After
     public void tearDown() throws Exception {
         super.tearDown();