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

Commit cd4ae07a authored by Jack Yu's avatar Jack Yu
Browse files

Gracefully handle subscription id not existing case

When subscription id does not exist in the database, use null
as the user handle.

Fix: 265859569
Test: atest GsmCdmaPhoneTest
Change-Id: Ic667fd66b7b667807898707d4318af0cbc09756e
parent 383e2a17
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -5160,14 +5160,24 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
    }

    /**
     * @return UserHandle from phone sub id, or null if subscription is invalid.
     * @return User handle associated with the phone's subscription id. {@code null} if subscription
     * is invalid or not found.
     */
    @Nullable
    public UserHandle getUserHandle() {
        SubscriptionManager subManager = mContext.getSystemService(SubscriptionManager.class);
        int subId = getSubId();
        return subManager.isValidSubscriptionId(subId)
                ? subManager.getSubscriptionUserHandle(subId)
                : null;

        UserHandle userHandle = null;
        try {
            SubscriptionManager subManager = mContext.getSystemService(SubscriptionManager.class);
            if (subManager != null) {
                userHandle = subManager.getSubscriptionUserHandle(subId);
            }
        } catch (IllegalArgumentException ex) {
            loge("getUserHandle: ex=" + ex);
        }

        return userHandle;
    }

    /**
+15 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.WorkSource;
import android.preference.PreferenceManager;
@@ -2574,4 +2575,18 @@ public class GsmCdmaPhoneTest extends TelephonyTest {

        verify(est).exitEmergencyCallbackMode();
    }

    @Test
    public void testGetUserHandle() {
        UserHandle userHandle = new UserHandle(123);
        doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(anyInt());
        assertEquals(userHandle, mPhoneUT.getUserHandle());

        doReturn(null).when(mSubscriptionManager).getSubscriptionUserHandle(anyInt());
        assertNull(mPhoneUT.getUserHandle());

        doThrow(IllegalArgumentException.class).when(mSubscriptionManager)
                .getSubscriptionUserHandle(anyInt());
        assertNull(mPhoneUT.getUserHandle());
    }
}