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

Commit 2d67ce65 authored by Aishwarya Mallampati's avatar Aishwarya Mallampati Committed by Android (Google) Code Review
Browse files

Merge "Add setUserHandle and getUserHandle apis."

parents 883ac9c1 72bd993f
Loading
Loading
Loading
Loading
+78 −1
Original line number Diff line number Diff line
@@ -315,7 +315,8 @@ public class SubscriptionController extends ISub.Stub {
            SubscriptionManager.UICC_APPLICATIONS_ENABLED,
            SubscriptionManager.IMS_RCS_UCE_ENABLED,
            SubscriptionManager.CROSS_SIM_CALLING_ENABLED,
            SubscriptionManager.NR_ADVANCED_CALLING_ENABLED
            SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
            SubscriptionManager.USER_HANDLE
    ));

    public static SubscriptionController init(Context c) {
@@ -466,6 +467,11 @@ public class SubscriptionController extends ISub.Stub {
                Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
    }

    private void enforceManageSubscriptionUserAssociation(String message) {
        mContext.enforceCallingOrSelfPermission(
                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION, message);
    }

    /**
     * Returns whether the {@code callingPackage} has access to subscriber identifiers on the
     * specified {@code subId} using the provided {@code message} in any resulting
@@ -2294,6 +2300,7 @@ public class SubscriptionController extends ISub.Stub {
            case SubscriptionManager.IMS_RCS_UCE_ENABLED:
            case SubscriptionManager.CROSS_SIM_CALLING_ENABLED:
            case SubscriptionManager.NR_ADVANCED_CALLING_ENABLED:
            case SubscriptionManager.USER_HANDLE:
                values.put(propKey, cursor.getInt(columnIndex));
                break;
            case SubscriptionManager.DISPLAY_NAME:
@@ -3241,6 +3248,7 @@ public class SubscriptionController extends ISub.Stub {
            case SubscriptionManager.VOIMS_OPT_IN_STATUS:
            case SubscriptionManager.NR_ADVANCED_CALLING_ENABLED:
            case SubscriptionManager.USAGE_SETTING:
            case SubscriptionManager.USER_HANDLE:
                value.put(propKey, Integer.parseInt(propValue));
                break;
            case SubscriptionManager.ALLOWED_NETWORK_TYPES:
@@ -3337,6 +3345,7 @@ public class SubscriptionController extends ISub.Stub {
                        case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER:
                        case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS:
                        case SubscriptionManager.USAGE_SETTING:
                        case SubscriptionManager.USER_HANDLE:
                            resultValue = cursor.getString(0);
                            break;
                        default:
@@ -4796,6 +4805,74 @@ public class SubscriptionController extends ISub.Stub {
        }
    }

    /**
     * Set UserHandle for this subscription
     *
     * @param userHandle the userHandle associated with the subscription
     * Pass {@code null} user handle to clear the association
     * @param subId the unique SubscriptionInfo index in database
     * @param callingPackage the package making the IPC
     * @return the number of records updated.
     *
     * @throws SecurityException if doesn't have required permission.
     * @throws IllegalArgumentException if subId is invalid.
     */
    @Override
    public int setUserHandle(@Nullable UserHandle userHandle, int subId,
            @NonNull String callingPackage) {
        enforceManageSubscriptionUserAssociation("setUserHandle");

        if (userHandle == null) {
            userHandle = UserHandle.of(UserHandle.USER_NULL);
        }

        long token = Binder.clearCallingIdentity();
        try {
            int ret = setSubscriptionProperty(subId, SubscriptionManager.USER_HANDLE,
                    String.valueOf(userHandle.getIdentifier()));
            // ret is the number of records updated in the DB
            if (ret != 0) {
                notifySubscriptionInfoChanged();
            } else {
                throw new IllegalArgumentException("[setUserHandle]: Invalid subId: " + subId);
            }
            return ret;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    /**
     * Get UserHandle of this subscription.
     *
     * @param subId the unique SubscriptionInfo index in database
     * @param callingPackage the package making the IPC
     * @return userHandle associated with this subscription
     * or {@code null} if subscription is not associated with any user.
     *
     * @throws SecurityException if doesn't have required permission.
     * @throws IllegalArgumentException if subId is invalid.
     */
    @Override
    public UserHandle getUserHandle(int subId, @NonNull String callingPackage) {
        enforceManageSubscriptionUserAssociation("getUserHandle");

        long token = Binder.clearCallingIdentity();
        try {
            String userHandleStr = getSubscriptionProperty(subId, SubscriptionManager.USER_HANDLE);
            if (userHandleStr == null) {
                throw new IllegalArgumentException("[getUserHandle]: Invalid subId: " + subId);
            }
            UserHandle userHandle = UserHandle.of(Integer.parseInt(userHandleStr));
            if (userHandle.getIdentifier() == UserHandle.USER_NULL) {
                return null;
            }
            return userHandle;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    /**
     * @hide
     */
+35 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.Context;
import android.os.HandlerThread;
import android.os.ParcelUuid;
import android.os.TelephonyServiceManager;
import android.os.UserHandle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
@@ -552,6 +553,40 @@ public class SubscriptionManagerService extends ISub.Stub {
        return 0;
    }

    /**
     * Set UserHandle for this subscription
     *
     * @param userHandle the userHandle associated with the subscription
     * Pass {@code null} user handle to clear the association
     * @param subId the unique SubscriptionInfo index in database
     * @param callingPackage the package making the IPC
     * @return the number of records updated.
     *
     * @throws SecurityException if doesn't have required permission.
     * @throws IllegalArgumentException if subId is invalid.
     */
    @Override
    public int setUserHandle(@Nullable UserHandle userHandle, int subId,
            @NonNull String callingPackage) {
        return 0;
    }

    /**
     * Get UserHandle of this subscription.
     *
     * @param subId the unique SubscriptionInfo index in database
     * @param callingPackage the package making the IPC
     * @return userHandle associated with this subscription
     * or {@code null} if subscription is not associated with any user.
     *
     * @throws SecurityException if doesn't have required permission.
     * @throws IllegalArgumentException if subId is invalid.
     */
    @Override
    public UserHandle getUserHandle(int subId, @NonNull String callingPackage) {
        return null;
    }

    /**
     * Log debug messages.
     *
+4 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.BaseColumns;
import android.provider.Telephony;
import android.telephony.SubscriptionManager;
@@ -124,7 +125,9 @@ public class FakeTelephonyProvider extends MockContentProvider {
                    + Telephony.SimInfo.COLUMN_USAGE_SETTING + " INTEGER DEFAULT "
                            + SubscriptionManager.USAGE_SETTING_UNKNOWN
                    + "," + Telephony.SimInfo.COLUMN_TP_MESSAGE_REF
                    + "  INTEGER DEFAULT -1"
                    + "  INTEGER DEFAULT -1,"
                    + Telephony.SimInfo.COLUMN_USER_HANDLE + " INTEGER DEFAULT "
                    + UserHandle.USER_NULL
                    + ");";
        }

+72 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import static android.telephony.TelephonyManager.SET_OPPORTUNISTIC_SUB_REMOTE_SE

import static com.android.internal.telephony.SubscriptionController.REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID;
import static com.android.internal.telephony.uicc.IccCardStatus.CardState.CARDSTATE_PRESENT;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -2110,4 +2111,74 @@ public class SubscriptionControllerTest extends TelephonyTest {
        int messageRef = SubscriptionController.getInstance().getMessageRef(subId);
        assertTrue("201 :", messageRef == 201);
    }

    @Test
    public void setUserHandle_withoutPermission() {
        testInsertSim();
        /* Get SUB ID */
        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
        assertTrue(subIds != null && subIds.length != 0);
        final int subId = subIds[0];
        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);

        assertThrows(SecurityException.class,
                () -> mSubscriptionControllerUT.setUserHandle(UserHandle.of(UserHandle.USER_SYSTEM),
                        subId, mCallingPackage));
    }

    @Test
    public void setGetUserHandle_userHandleNull() {
        testInsertSim();
        /* Get SUB ID */
        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
        assertTrue(subIds != null && subIds.length != 0);
        final int subId = subIds[0];

        mSubscriptionControllerUT.setUserHandle(null, subId, mCallingPackage);

        assertThat(mSubscriptionControllerUT.getUserHandle(subId, mCallingPackage))
                .isEqualTo(null);
    }

    @Test
    public void setUserHandle_invalidSubId() {
        assertThrows(IllegalArgumentException.class,
                () -> mSubscriptionControllerUT.setUserHandle(UserHandle.of(UserHandle.USER_SYSTEM),
                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mCallingPackage));
    }

    @Test
    public void setGetUserHandle_withValidUserHandleAndSubId() {
        testInsertSim();
        /* Get SUB ID */
        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
        assertTrue(subIds != null && subIds.length != 0);
        final int subId = subIds[0];

        mSubscriptionControllerUT.setUserHandle(UserHandle.of(UserHandle.USER_SYSTEM), subId,
                mCallingPackage);

        assertThat(mSubscriptionControllerUT.getUserHandle(subId, mCallingPackage))
                .isEqualTo(UserHandle.of(UserHandle.USER_SYSTEM));
    }

    @Test
    public void getUserHandle_withoutPermission() {
        testInsertSim();
        /* Get SUB ID */
        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
        assertTrue(subIds != null && subIds.length != 0);
        final int subId = subIds[0];
        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);

        assertThrows(SecurityException.class,
                () -> mSubscriptionControllerUT.getUserHandle(subId, mCallingPackage));
    }

    @Test
    public void getUserHandle_invalidSubId() {
        assertThrows(IllegalArgumentException.class,
                () -> mSubscriptionControllerUT.getUserHandle(
                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mCallingPackage));
    }
}
 No newline at end of file