From 23bf54028d7973975b2c8f7fd038e0fcb44e06aa Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Thu, 20 Jul 2023 18:37:33 -0700 Subject: Added getPhoneNumber support Fix: 290953736 Test: atest Ts43OperationTest Change-Id: I80db72039ecace9bd882734278fe2d89b729c204 --- .../libraries/entitlement/Ts43Operation.java | 59 +++++++++++++++++++- .../entitlement/odsa/GetPhoneNumberOperation.java | 65 ++++++++++++++++++++++ .../libraries/entitlement/Ts43OperationTest.java | 30 ++++++++++ 3 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 java/com/android/libraries/entitlement/odsa/GetPhoneNumberOperation.java diff --git a/java/com/android/libraries/entitlement/Ts43Operation.java b/java/com/android/libraries/entitlement/Ts43Operation.java index a95ff10..681220a 100644 --- a/java/com/android/libraries/entitlement/Ts43Operation.java +++ b/java/com/android/libraries/entitlement/Ts43Operation.java @@ -36,6 +36,7 @@ import com.android.libraries.entitlement.odsa.CheckEligibilityOperation; import com.android.libraries.entitlement.odsa.CheckEligibilityOperation.CheckEligibilityRequest; import com.android.libraries.entitlement.odsa.CheckEligibilityOperation.CheckEligibilityResponse; import com.android.libraries.entitlement.odsa.DownloadInfo; +import com.android.libraries.entitlement.odsa.GetPhoneNumberOperation.GetPhoneNumberResponse; import com.android.libraries.entitlement.odsa.ManageServiceOperation.ManageServiceRequest; import com.android.libraries.entitlement.odsa.ManageServiceOperation.ManageServiceResponse; import com.android.libraries.entitlement.odsa.ManageSubscriptionOperation.ManageSubscriptionRequest; @@ -853,14 +854,66 @@ public class Ts43Operation { * Get the phone number as described in GSMA Service Entitlement Configuration section 6.2 and * 6.5.8. * - * @return The phone number in E.164 format. + * @return The phone number response from the network. * @throws ServiceEntitlementException The exception for error case. If it's an HTTP response * error from the server, the error code can be retrieved by * {@link ServiceEntitlementException#getHttpStatus()} */ @NonNull - public String getPhoneNumber() throws ServiceEntitlementException { - return ""; + public GetPhoneNumberResponse getPhoneNumber() throws ServiceEntitlementException { + ServiceEntitlementRequest.Builder builder = + ServiceEntitlementRequest.builder() + .setEntitlementVersion(mEntitlementVersion) + .setTerminalId(mImei); + + if (mTokenType == TOKEN_TYPE_NORMAL) { + builder.setAuthenticationToken(mAuthToken); + } else if (mTokenType == TOKEN_TYPE_TEMPORARY) { + builder.setTemporaryToken(mTemporaryToken); + } + + ServiceEntitlementRequest request = builder.build(); + + EsimOdsaOperation operation = + EsimOdsaOperation.builder() + .setOperation(EsimOdsaOperation.OPERATION_GET_PHONE_NUMBER) + .build(); + + String rawXml; + try { + rawXml = + mServiceEntitlement.performEsimOdsa( + EsimOdsaOperation.OPERATION_GET_PHONE_NUMBER, request, operation); + } catch (ServiceEntitlementException e) { + Log.w(TAG, "getPhoneNumber: Failed to perform ODSA operation. e=" + e); + throw e; + } + + // Build the response of get phone number operation. Refer to GSMA Service Entitlement + // Configuration section 6.5.8. + GetPhoneNumberResponse.Builder responseBuilder = GetPhoneNumberResponse.builder(); + + Ts43XmlDoc ts43XmlDoc = new Ts43XmlDoc(rawXml); + + try { + processGeneralResult(ts43XmlDoc, responseBuilder); + } catch (MalformedURLException e) { + throw new ServiceEntitlementException( + ServiceEntitlementException.ERROR_MALFORMED_HTTP_RESPONSE, + "getPhoneNumber: Malformed URL " + rawXml); + } + + // Parse msisdn. + String msisdn = + ts43XmlDoc.get( + ImmutableList.of(Ts43XmlDoc.CharacteristicType.APPLICATION), + Ts43XmlDoc.Parm.MSISDN); + + if (!TextUtils.isEmpty(msisdn)) { + responseBuilder.setMsisdn(msisdn); + } + + return responseBuilder.build(); } /** diff --git a/java/com/android/libraries/entitlement/odsa/GetPhoneNumberOperation.java b/java/com/android/libraries/entitlement/odsa/GetPhoneNumberOperation.java new file mode 100644 index 0000000..4ae07f1 --- /dev/null +++ b/java/com/android/libraries/entitlement/odsa/GetPhoneNumberOperation.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.libraries.entitlement.odsa; + +import androidx.annotation.NonNull; + +import com.google.auto.value.AutoValue; + +/** + * Get phone number operation described in GSMA Service Entitlement Configuration section 6. + */ +public final class GetPhoneNumberOperation { + /** + * Get phone number response described in GSMA Service Entitlement Configuration section + * 6.5.8. + */ + @AutoValue + public abstract static class GetPhoneNumberResponse extends OdsaResponse { + + /** The phone number of the subscriber in E.164 format. */ + public abstract String msisdn(); + + /** Returns a new {@link GetPhoneNumberResponse.Builder} object. */ + @NonNull + public static Builder builder() { + return new AutoValue_GetPhoneNumberOperation_GetPhoneNumberResponse + .Builder() + .setMsisdn(""); + } + + /** Builder. */ + @AutoValue.Builder + public abstract static class Builder extends OdsaResponse.Builder { + /** + * Sets the phone number of the subscriber. + * + * @param msisdn The phone number of the subscriber in E.164 format. + * @return The builder. + */ + @NonNull + public abstract Builder setMsisdn(@NonNull String msisdn); + + /** Returns the {@link GetPhoneNumberResponse} object. */ + @NonNull + public abstract GetPhoneNumberResponse build(); + } + } + + private GetPhoneNumberOperation() { + } +} diff --git a/tests/src/com/android/libraries/entitlement/Ts43OperationTest.java b/tests/src/com/android/libraries/entitlement/Ts43OperationTest.java index daf135f..6d90c5d 100644 --- a/tests/src/com/android/libraries/entitlement/Ts43OperationTest.java +++ b/tests/src/com/android/libraries/entitlement/Ts43OperationTest.java @@ -35,6 +35,7 @@ import com.android.libraries.entitlement.odsa.AcquireTemporaryTokenOperation.Acq import com.android.libraries.entitlement.odsa.CheckEligibilityOperation; import com.android.libraries.entitlement.odsa.CheckEligibilityOperation.CheckEligibilityRequest; import com.android.libraries.entitlement.odsa.CheckEligibilityOperation.CheckEligibilityResponse; +import com.android.libraries.entitlement.odsa.GetPhoneNumberOperation.GetPhoneNumberResponse; import com.android.libraries.entitlement.odsa.ManageServiceOperation.ManageServiceRequest; import com.android.libraries.entitlement.odsa.ManageServiceOperation.ManageServiceResponse; import com.android.libraries.entitlement.odsa.ManageSubscriptionOperation.ManageSubscriptionRequest; @@ -74,6 +75,8 @@ public class Ts43OperationTest { private static final String NOT_ENABLED_USER_DATA = "msisdn=XX"; + private static final String MSISDN = "+16502530000"; + private static final String MANAGE_SUBSCRIPTION_RESPONSE_CONTINUE_TO_WEBSHEET = "" + "" @@ -201,6 +204,23 @@ public class Ts43OperationTest { + "\n" + ""; + public String GET_PHONE_NUMBER_RESPONSE = + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + "\n" + + "\n" + + " \n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n" + + ""; + @Mock private EapAkaApi mMockEapAkaApi; @@ -351,4 +371,14 @@ public class Ts43OperationTest { assertThat(response.serviceStatus()).isEqualTo( EsimOdsaOperation.SERVICE_STATUS_DEACTIVATED); } + + @Test + public void testGetPhoneNumber() throws Exception { + doReturn(GET_PHONE_NUMBER_RESPONSE).when(mMockHttpResponse).body(); + + GetPhoneNumberResponse response = mTs43Operation.getPhoneNumber(); + assertThat(response.operationResult()).isEqualTo( + EsimOdsaOperation.OPERATION_RESULT_SUCCESS); + assertThat(response.msisdn()).isEqualTo(MSISDN); + } } -- cgit v1.2.3