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

Commit 4ff0aa6a authored by Nathan Harold's avatar Nathan Harold
Browse files

Add setter and getter for UsageSetting

Add a setting and getter for the usage settings.

Bug: 210023167
Test: atest SubscriptionControllerTest#testUsageSettingProperty
Change-Id: Id98eaab16bbb6e924af793fd9894936fc42c3159
parent 6a694762
Loading
Loading
Loading
Loading
+48 −2
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import android.telephony.RadioAccessFamily;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.SimDisplayNameSource;
import android.telephony.SubscriptionManager.UsageSetting;
import android.telephony.TelephonyFrameworkInitializer;
import android.telephony.TelephonyManager;
import android.telephony.TelephonyRegistryManager;
@@ -624,6 +625,8 @@ public class SubscriptionController extends ISub.Stub {
        if (!TextUtils.isEmpty(line1Number) && !line1Number.equals(number)) {
            number = line1Number;
        }
        // FIXME(b/210771052): constructing a complete SubscriptionInfo requires a port index,
        // but the port index isn't available here. Should it actually be part of SubscriptionInfo?
        SubscriptionInfo info = new SubscriptionInfo(id, iccId, simSlotIndex, displayName,
                carrierName, nameSource, iconTint, number, dataRoaming, /* icon= */ null,
                mcc, mnc, countryIso, isEmbedded, accessRules, cardId, publicCardId,
@@ -3060,9 +3063,9 @@ public class SubscriptionController extends ISub.Stub {
    private void validateSubId(int subId) {
        if (DBG) logd("validateSubId subId: " + subId);
        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
            throw new RuntimeException("Invalid sub id passed as parameter");
            throw new IllegalArgumentException("Invalid sub id passed as parameter");
        } else if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
            throw new RuntimeException("Default sub id passed as parameter");
            throw new IllegalArgumentException("Default sub id passed as parameter");
        }
    }

@@ -4675,6 +4678,49 @@ public class SubscriptionController extends ISub.Stub {
        }
    }

    /**
     * Set the Usage Setting for this subscription.
     *
     * @param usageSetting the cellular usage setting
     * @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.
     */
    @Override
    public int setUsageSetting(@UsageSetting int usageSetting, int subId, String callingPackage) {
        try {
            TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
                    mContext, subId, callingPackage);
        } catch (SecurityException e) {
            enforceCarrierPrivilegeOnInactiveSub(subId, callingPackage,
                    "Caller requires permission on sub " + subId);
        }

        if (usageSetting < SubscriptionManager.USAGE_SETTING_DEFAULT
                || usageSetting > SubscriptionManager.USAGE_SETTING_DATA_CENTRIC) {
            throw new IllegalArgumentException("setUsageSetting: Invalid usage setting: "
                    + usageSetting);
        }

        final long token = Binder.clearCallingIdentity();
        int ret;
        try {
            ret = setSubscriptionProperty(subId, SubscriptionManager.USAGE_SETTING,
                    String.valueOf(usageSetting));

            // ret is the number of records updated in the DB, which should always be 1.
            // TODO(b/205027930): move this check prior to the database mutation request
            if (ret != 1) throw new IllegalArgumentException(
                    "Invalid SubscriptionId for setUsageSetting");
        } finally {
            Binder.restoreCallingIdentity(token);
            // FIXME(b/205726099) return void
        }
        return ret;
    }

    /**
     * @hide
     */
+37 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
@@ -194,6 +195,42 @@ public class SubscriptionControllerTest extends TelephonyTest {
        }
    }

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

        /* Getting, there is no direct getter function for each fields of property */
        SubscriptionInfo subInfo = mSubscriptionControllerUT
                .getActiveSubscriptionInfo(subId, mCallingPackage, mCallingFeature);

        // assertEquals(SubscriptionManager.USAGE_SETTING_UNKNOWN, subInfo.getUsageSetting());

        assertThrows(IllegalArgumentException.class,
                () -> mSubscriptionControllerUT.setUsageSetting(
                        SubscriptionManager.USAGE_SETTING_UNKNOWN,
                        subId,
                        mCallingPackage));

        assertThrows(IllegalArgumentException.class,
                () -> mSubscriptionControllerUT.setUsageSetting(
                        SubscriptionManager.USAGE_SETTING_DEFAULT,
                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID,
                        mCallingPackage));

        mSubscriptionControllerUT.setUsageSetting(
                SubscriptionManager.USAGE_SETTING_DATA_CENTRIC,
                subId,
                mCallingPackage);

        subInfo = mSubscriptionControllerUT
                .getActiveSubscriptionInfo(subId, mCallingPackage, mCallingFeature);
        assertEquals(SubscriptionManager.USAGE_SETTING_DATA_CENTRIC, subInfo.getUsageSetting());
    }

    @Test @SmallTest
    public void testChangeSIMProperty() {
        int dataRoaming = 1;