Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9186bbd authored by Alex Lin's avatar Alex Lin Committed by Gerrit Code Review
Browse files

Merge "Add public error codes to Euicc public API"

parents 04fe63f1 2762677d
Loading
Loading
Loading
Loading
+93 −24
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import android.telephony.euicc.EuiccManager;
import android.telephony.euicc.EuiccManager.OtaStatus;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.SubscriptionController;
@@ -58,6 +59,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@@ -395,6 +397,7 @@ public class EuiccController extends IEuiccController.Stub {
                    extrasIntent.putExtra(
                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                            result.getResult());
                    addExtrasToResultIntent(extrasIntent, result.getResult());
                    break;
            }

@@ -420,6 +423,87 @@ public class EuiccController extends IEuiccController.Stub {
                false /* forceDeactivateSim */, resolvedBundle, callbackIntent);
    }

    /**
     * Given encoded error code described in
     * {@link android.telephony.euicc.EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE} decode it
     * into SubjectCode[5.2.6.1] and ReasonCode[5.2.6.2] from GSMA (SGP.22 v2.2)
     *
     * @param resultCode from
     *               {@link android.telephony.euicc.EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE}
     * @return a pair containing SubjectCode[5.2.6.1] and ReasonCode[5.2.6.2] from GSMA (SGP.22
     * v2.2)
     */
    Pair<String, String> decodeSmdxSubjectAndReasonCode(int resultCode) {
        final int numOfSections = 6;
        final int bitsPerSection = 4;
        final int sectionMask = 0xF;

        final Stack<Integer> sections = new Stack<>();

        // Extracting each section of digits backwards.
        for (int i = 0; i < numOfSections; ++i) {
            int sectionDigit = resultCode & sectionMask;
            sections.push(sectionDigit);
            resultCode = resultCode >>> bitsPerSection;
        }

        String subjectCode = sections.pop() + "." + sections.pop() + "." + sections.pop();
        String reasonCode = sections.pop() + "." + sections.pop() + "." + sections.pop();

        // drop the leading zeros, e.g 0.1 -> 1, 0.0.3 -> 3, 0.5.1 -> 5.1
        subjectCode = subjectCode.replaceAll("^(0\\.)*", "");
        reasonCode = reasonCode.replaceAll("^(0\\.)*", "");

        return Pair.create(subjectCode, reasonCode);
    }

    /**
     * Add more detailed information to the resulting intent.
     * Fields added includes(key -> value):
     * 1. {@link EuiccManager#EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE} -> original error code
     * 2. {@link EuiccManager#EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE} ->
     * EuiccManager.OperationCode such as {@link EuiccManager#OPERATION_DOWNLOAD}
     * 3. if @link EuiccManager.OperationCode is not
     * {@link EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE}:
     * {@link EuiccManager#EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE} -> @link
     * EuiccManager.ErrorCode such as {@link EuiccManager#OPERATION_SMDX}
     * 4. if EuiccManager.OperationCode is
     * {@link EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE}:
     * a) {@link EuiccManager#EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE} ->
     * SubjectCode[5.2.6.1] from GSMA (SGP.22 v2.2)
     * b) {@link EuiccManager#EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE} ->
     * ReasonCode[5.2.6.2] from GSMA (SGP.22 v2.2
     */
    Intent addExtrasToResultIntent(Intent intent, int resultCode) {
        final int firstByteBitOffset = 24;
        int errorCodeMask = 0xFFFFFF;
        int operationCodeMask = 0xFF << firstByteBitOffset;

        intent.putExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, resultCode);

        intent.putExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE,
                (resultCode & operationCodeMask) >> firstByteBitOffset);

        // check to see if the operation code is EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE
        final boolean isSmdxSubjectReasonCode = (resultCode >> firstByteBitOffset)
                == EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE;

        if (isSmdxSubjectReasonCode) {
            final Pair<String, String> subjectReasonCode = decodeSmdxSubjectAndReasonCode(
                    resultCode);
            final String subjectCode = subjectReasonCode.first;
            final String reasonCode = subjectReasonCode.second;
            intent.putExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE,
                    subjectCode);
            intent.putExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE, reasonCode);
        } else {
            final int errorCode = resultCode & errorCodeMask;
            intent.putExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE, errorCode);
        }
        return intent;
    }

    void downloadSubscription(int cardId, DownloadableSubscription subscription,
            boolean switchAfterDownload, String callingPackage, boolean forceDeactivateSim,
            Bundle resolvedBundle, PendingIntent callbackIntent) {
@@ -638,9 +722,8 @@ public class EuiccController extends IEuiccController.Stub {
                                break;
                            default:
                                resultCode = ERROR;
                                extrasIntent.putExtra(
                                        EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                        result.getResult());

                                addExtrasToResultIntent(extrasIntent, result.getResult());
                                break;
                        }

@@ -751,9 +834,7 @@ public class EuiccController extends IEuiccController.Stub {
                    break;
                default:
                    resultCode = ERROR;
                    extrasIntent.putExtra(
                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                            result.getResult());
                    addExtrasToResultIntent(extrasIntent, result.getResult());
                    break;
            }

@@ -833,9 +914,7 @@ public class EuiccController extends IEuiccController.Stub {
                                return;
                            default:
                                resultCode = ERROR;
                                extrasIntent.putExtra(
                                        EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                        result);
                                addExtrasToResultIntent(extrasIntent, result);
                                break;
                        }

@@ -968,9 +1047,7 @@ public class EuiccController extends IEuiccController.Stub {
                                break;
                            default:
                                resultCode = ERROR;
                                extrasIntent.putExtra(
                                        EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                        result);
                                addExtrasToResultIntent(extrasIntent, result);
                                break;
                        }

@@ -1024,9 +1101,7 @@ public class EuiccController extends IEuiccController.Stub {
                                    return;
                                default:
                                    resultCode = ERROR;
                                    extrasIntent.putExtra(
                                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                            result);
                                    addExtrasToResultIntent(extrasIntent, result);
                                    break;
                            }

@@ -1065,9 +1140,7 @@ public class EuiccController extends IEuiccController.Stub {
                                    return;
                                default:
                                    resultCode = ERROR;
                                    extrasIntent.putExtra(
                                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                            result);
                                    addExtrasToResultIntent(extrasIntent, result);
                                    break;
                            }

@@ -1107,9 +1180,7 @@ public class EuiccController extends IEuiccController.Stub {
                            return;
                        default:
                            resultCode = ERROR;
                            extrasIntent.putExtra(
                                    EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                    result);
                                    addExtrasToResultIntent(extrasIntent, result);
                            break;
                    }

@@ -1144,9 +1215,7 @@ public class EuiccController extends IEuiccController.Stub {
                                    break;
                                default:
                                    resultCode = ERROR;
                                    extrasIntent.putExtra(
                                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                                            result);
                                    addExtrasToResultIntent(extrasIntent, result);
                                    break;
                            }

+72 −0
Original line number Diff line number Diff line
@@ -1054,6 +1054,78 @@ public class EuiccControllerTest extends TelephonyTest {
        verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
    }

    @Test
    public void testAddExtrasToResultIntent_withSmdxOperationCode_normal_case() {
        // Same setup as testGetDownloadableSubscriptionMetadata_error
        setHasWriteEmbeddedPermission(true);
        GetDownloadableSubscriptionMetadataResult result =
                new GetDownloadableSubscriptionMetadataResult(0xA8b1051 /* result */,
                        null /* subscription */);
        callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);

        assertEquals(mController.mExtrasIntent.getIntExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1),
                EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE);
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "8.11.1");
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "5.1");

    }

    @Test
    public void testAddExtrasToResultIntent_withSmdxOperationCode_general_case() {
        // Same setup as testGetDownloadableSubscriptionMetadata_error
        setHasWriteEmbeddedPermission(true);
        GetDownloadableSubscriptionMetadataResult result =
                new GetDownloadableSubscriptionMetadataResult(0xA123456 /* result */,
                        null /* subscription */);
        callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);

        assertEquals(mController.mExtrasIntent.getIntExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1),
                EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE);
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "1.2.3");
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "4.5.6");

    }

    @Test
    public void testAddExtrasToResultIntent_withSmdxOperationCode_and_padding() {
        // Same setup as testGetDownloadableSubscriptionMetadata_error
        setHasWriteEmbeddedPermission(true);
        GetDownloadableSubscriptionMetadataResult result =
                new GetDownloadableSubscriptionMetadataResult(0xA003006 /* result */,
                        null /* subscription */);
        callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);

        assertEquals(mController.mExtrasIntent.getIntExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1),
                EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE);
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "3");
        assertEquals(mController.mExtrasIntent.getStringExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "6");
    }

    @Test
    public void testAddExtrasToResultIntent_withOperationCode() {
        // Same setup as testGetDownloadableSubscriptionMetadata_error
        setHasWriteEmbeddedPermission(true);
        GetDownloadableSubscriptionMetadataResult result =
                new GetDownloadableSubscriptionMetadataResult(0x12345678 /* result */,
                        null /* subscription */);
        callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);

        assertEquals(mController.mExtrasIntent.getIntExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1),
                0x12);
        assertEquals(mController.mExtrasIntent.getIntExtra(
                EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE, -1), 0x345678);
    }

    private void setGetEidPermissions(
            boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges) throws Exception {
        doReturn(hasPhoneStatePrivileged