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

Commit 74a537c6 authored by Meng Wang's avatar Meng Wang Committed by Android (Google) Code Review
Browse files

Merge "Add getPhoneNumber/setPhoneNumber API"

parents 6d3fe108 cd7a578f
Loading
Loading
Loading
Loading
+106 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.internal.telephony;

import static android.Manifest.permission.READ_PHONE_NUMBERS;
import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.telephony.TelephonyManager.MULTISIM_ALLOWED;
import static android.telephony.TelephonyManager.SET_OPPORTUNISTIC_SUB_REMOTE_SERVICE_EXCEPTION;
@@ -45,6 +47,7 @@ import android.os.RemoteException;
import android.os.TelephonyServiceManager.ServiceRegisterer;
import android.os.UserHandle;
import android.provider.Settings;
import android.provider.Telephony.SimInfo;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.AnomalyReporter;
@@ -3220,6 +3223,8 @@ public class SubscriptionController extends ISub.Stub {
                value.put(propKey, Integer.parseInt(propValue));
                break;
            case SubscriptionManager.ALLOWED_NETWORK_TYPES:
            case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER:
            case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS:
                value.put(propKey, propValue);
                break;
            default:
@@ -3298,6 +3303,8 @@ public class SubscriptionController extends ISub.Stub {
                        case SubscriptionManager.VOIMS_OPT_IN_STATUS:
                        case SubscriptionManager.D2D_STATUS_SHARING_SELECTED_CONTACTS:
                        case SubscriptionManager.NR_ADVANCED_CALLING_ENABLED:
                        case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER:
                        case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS:
                            resultValue = cursor.getString(0);
                            break;
                        default:
@@ -4530,6 +4537,105 @@ public class SubscriptionController extends ISub.Stub {
        }
    }

    /*
     * Returns the phone number for the given {@code subId} and {@code source},
     * or an empty string if not available.
     */
    @Override
    public String getPhoneNumber(int subId, int source,
            String callingPackage, String callingFeatureId) {
        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
                mContext, subId, Binder.getCallingUid(), "getPhoneNumber",
                READ_PHONE_NUMBERS, READ_PRIVILEGED_PHONE_STATE);

        final long identity = Binder.clearCallingIdentity();
        try {
            String number = getPhoneNumber(subId, source);
            return number == null ? "" : number;
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    /*
     * Returns the phone number for the given {@code subId} or an empty string if not available.
     *
     * <p>Built up on getPhoneNumber(int subId, int source) this API picks the 1st available
     * source based on a priority order.
     */
    @Override
    public String getPhoneNumberFromFirstAvailableSource(int subId,
            String callingPackage, String callingFeatureId) {
        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
                mContext, subId, Binder.getCallingUid(), "getPhoneNumberFromFirstAvailableSource",
                READ_PHONE_NUMBERS, READ_PRIVILEGED_PHONE_STATE);

        final long identity = Binder.clearCallingIdentity();
        try {
            String numberFromCarrier = getPhoneNumber(
                    subId, SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER);
            if (!TextUtils.isEmpty(numberFromCarrier)) {
                return numberFromCarrier;
            }
            String numberFromUicc = getPhoneNumber(
                    subId, SubscriptionManager.PHONE_NUMBER_SOURCE_UICC);
            if (!TextUtils.isEmpty(numberFromUicc)) {
                return numberFromUicc;
            }
            String numberFromIms = getPhoneNumber(
                    subId, SubscriptionManager.PHONE_NUMBER_SOURCE_IMS);
            if (!TextUtils.isEmpty(numberFromIms)) {
                return numberFromIms;
            }
            return "";
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    // Internal helper method for implementing getPhoneNumber() API.
    @Nullable
    private String getPhoneNumber(int subId, int source) {
        if (source == SubscriptionManager.PHONE_NUMBER_SOURCE_UICC) {
            Phone phone = PhoneFactory.getPhone(getPhoneId(subId));
            return phone != null ? phone.getLine1Number() : null;
        }
        if (source == SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER) {
            return getSubscriptionProperty(subId, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER);
        }
        if (source == SubscriptionManager.PHONE_NUMBER_SOURCE_IMS) {
            return getSubscriptionProperty(subId, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS);
        }
        throw new IllegalArgumentException("setPhoneNumber doesn't accept source " + source);
    }

    /**
     * Sets the phone number for the given {@code subId}.
     *
     * <p>The only accepted {@code source} is {@link
     * SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER}.
     */
    @Override
    public void setPhoneNumber(int subId, int source, String number,
            String callingPackage, String callingFeatureId) {
        if (source != SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER) {
            throw new IllegalArgumentException("setPhoneNumber doesn't accept source " + source);
        }
        if (!TelephonyPermissions.checkCarrierPrivilegeForSubId(mContext, subId)) {
            throw new SecurityException("setPhoneNumber for CARRIER needs carrier privilege");
        }
        if (number == null) {
            throw new NullPointerException("invalid number null");
        }

        final long identity = Binder.clearCallingIdentity();
        try {
            setSubscriptionProperty(subId, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER, number);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    /**
     * @hide
     */