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

Commit 8caa16cb authored by Grace Jia's avatar Grace Jia
Browse files

Fix IllegalStateException when creating context for work profile.

Currently, when we trying to create the work profile context to check
necessary permissions, we'll encounter IllegalStateException due to
com.android.phone won't be installed for secondary user. Change to use
AppOpsManager#checkPackage instead to check calling package to avoid
this exception.

Bug: 258316639
Test: atest PhoneSubInfoControllerTest, manually tested on device by
starting google play store in work profile
Change-Id: I57ccf58b285df10126fafb4945df144ad251b99d
parent ede5d6ad
Loading
Loading
Loading
Loading
+1 −25
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.os.Binder;
import android.os.Build;
import android.os.RemoteException;
import android.os.TelephonyServiceManager.ServiceRegisterer;
import android.os.UserHandle;
import android.telephony.ImsiEncryptionInfo;
import android.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionManager;
@@ -147,8 +146,7 @@ public class PhoneSubInfoController extends IPhoneSubInfo.Stub {
    public String getSubscriberIdForSubscriber(int subId, String callingPackage,
            String callingFeatureId) {
        String message = "getSubscriberIdForSubscriber";

        enforceCallingPackage(callingPackage, Binder.getCallingUid(), message);
        mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);

        long identity = Binder.clearCallingIdentity();
        boolean isActive;
@@ -316,28 +314,6 @@ public class PhoneSubInfoController extends IPhoneSubInfo.Stub {
                "Requires MODIFY_PHONE_STATE");
    }

    /**
     * Make sure the caller is the calling package itself
     *
     * @throws SecurityException if the caller is not the calling package
     */
    private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
        int packageUid = -1;
        PackageManager pm = mContext.createContextAsUser(
                UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
        if (pm != null) {
            try {
                packageUid = pm.getPackageUid(callingPackage, 0);
            } catch (PackageManager.NameNotFoundException e) {
                // packageUid is -1
            }
        }
        if (packageUid != callingUid) {
            throw new SecurityException(message + ": Package " + callingPackage
                    + " does not belong to " + callingUid);
        }
    }

    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    private int getDefaultSubscription() {
        return  PhoneFactory.getDefaultSubscription();