Loading src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java +58 −3 Original line number Diff line number Diff line Loading @@ -29,6 +29,7 @@ import static android.telephony.TelephonyManager.SIM_STATE_UNKNOWN; import android.annotation.NonNull; import android.annotation.Nullable; import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading @@ -48,6 +49,7 @@ import android.os.Registrant; import android.os.RegistrantList; import android.os.UserHandle; import android.os.UserManager; import android.telephony.Annotation.CarrierPrivilegeStatus; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; Loading @@ -63,6 +65,7 @@ import com.android.internal.annotations.GuardedBy; import com.android.internal.telephony.uicc.IccUtils; import com.android.internal.telephony.uicc.UiccPort; import com.android.internal.telephony.uicc.UiccProfile; import com.android.internal.util.ArrayUtils; import com.android.telephony.Rlog; import java.io.FileDescriptor; Loading Loading @@ -514,10 +517,13 @@ public class CarrierPrivilegesTracker extends Handler { } private void refreshInstalledPackageCache() { // Include DISABLED_UNTIL_USED components. This facilitates cases where a carrier app // is disabled by default, and some other component wants to enable it when it has // gained carrier privileges (as an indication that a matching SIM has been inserted). int flags = PackageManager.MATCH_DISABLED_COMPONENTS PackageManager.GET_SIGNING_CERTIFICATES | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS | PackageManager.GET_SIGNING_CERTIFICATES; | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS; List<PackageInfo> installedPackages = mPackageManager.getInstalledPackagesAsUser( flags, UserHandle.SYSTEM.getIdentifier()); Loading Loading @@ -581,6 +587,11 @@ public class CarrierPrivilegesTracker extends Handler { } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } // Update set of enabled carrier apps now that the privilege rules may have changed. ActivityManager am = mContext.getSystemService(ActivityManager.class); CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(), mTelephonyManager, am.getCurrentUser(), mContext); } private PrivilegedPackageInfo getCurrentPrivilegedPackagesForAllUsers() { Loading Loading @@ -730,7 +741,51 @@ public class CarrierPrivilegesTracker extends Handler { // best effort. refreshInstalledPackageCache(); } maybeUpdatePrivilegedPackagesAndNotifyRegistrants(); } /** Backing of {@link TelephonyManager#checkCarrierPrivilegesForPackage}. */ public @CarrierPrivilegeStatus int getCarrierPrivilegeStatusForPackage(String packageName) { // TODO(b/205736323) consider if/how we want to account for the RULES_NOT_LOADED and // ERROR_LOADING_RULES constants. Technically those will never be returned today since those // results are only from the SIM rules, but the CC rules' result (which never has these // errors) always supersede them unless something goes super wrong when getting CC. mPrivilegedPackageInfoLock.readLock().lock(); try { return mPrivilegedPackageInfo.mPackageNames.contains(packageName) ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS; } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } /** Backing of {@link TelephonyManager#getPackagesWithCarrierPrivileges}. */ public Set<String> getPackagesWithCarrierPrivileges() { mPrivilegedPackageInfoLock.readLock().lock(); try { return Collections.unmodifiableSet(mPrivilegedPackageInfo.mPackageNames); } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } /** * Backing of {@link TelephonyManager#hasCarrierPrivileges} and {@link * TelephonyManager#getCarrierPrivilegeStatus(int)}. */ public @CarrierPrivilegeStatus int getCarrierPrivilegeStatusForUid(int uid) { // TODO(b/205736323) consider if/how we want to account for the RULES_NOT_LOADED and // ERROR_LOADING_RULES constants. Technically those will never be returned today since those // results are only from the SIM rules, but the CC rules' result (which never has these // errors) always supersede them unless something goes super wrong when getting CC. mPrivilegedPackageInfoLock.readLock().lock(); try { return ArrayUtils.contains(mPrivilegedPackageInfo.mUids, uid) ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS; } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } } src/java/com/android/internal/telephony/uicc/UiccPort.java +18 −6 Original line number Diff line number Diff line Loading @@ -378,8 +378,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatus(Signature, String)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(Signature, String)} * instead. */ @Deprecated public int getCarrierPrivilegeStatus(Signature signature, String packageName) { Loading @@ -392,8 +395,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatus(PackageManager, String)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(PackageManager, String)} * instead. */ @Deprecated public int getCarrierPrivilegeStatus(PackageManager packageManager, String packageName) { Loading @@ -406,6 +412,9 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(PackageInfo)} instead. */ @Deprecated Loading @@ -419,8 +428,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatusForCurrentTransaction}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatusForCurrentTransaction(PackageManager)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link * UiccProfile#getCarrierPrivilegeStatusForCurrentTransaction(PackageManager)} instead. */ @Deprecated public int getCarrierPrivilegeStatusForCurrentTransaction(PackageManager packageManager) { Loading src/java/com/android/internal/telephony/uicc/UiccProfile.java +1 −0 Original line number Diff line number Diff line Loading @@ -1334,6 +1334,7 @@ public class UiccProfile extends IccCard { } private void onCarrierPrivilegesLoadedMessage() { // TODO(b/211796398): clean up logic below once all carrier privilege check migration done // Update set of enabled carrier apps now that the privilege rules may have changed. ActivityManager am = mContext.getSystemService(ActivityManager.class); CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(), Loading tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java +46 −3 Original line number Diff line number Diff line Loading @@ -105,9 +105,9 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { private static final int[] PRIVILEGED_UIDS = {UID_1, UID_2}; private static final int PM_FLAGS = PackageManager.MATCH_DISABLED_COMPONENTS PackageManager.GET_SIGNING_CERTIFICATES | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS | PackageManager.GET_SIGNING_CERTIFICATES; | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS; @Mock private Signature mSignature; Loading Loading @@ -257,6 +257,21 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { } } private void verifyCurrentState(Set<String> expectedPackageNames, int[] expectedUids) { assertEquals( expectedPackageNames, mCarrierPrivilegesTracker.getPackagesWithCarrierPrivileges()); for (String packageName : expectedPackageNames) { assertEquals( TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS, mCarrierPrivilegesTracker.getCarrierPrivilegeStatusForPackage(packageName)); } for (int uid : expectedUids) { assertEquals( TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS, mCarrierPrivilegesTracker.getCarrierPrivilegeStatusForUid(uid)); } } private void verifyRegistrantUpdates(@Nullable int[] expectedUids, int expectedUidUpdates) { assertArrayEquals(expectedUids, mHandler.privilegedUids); assertEquals(expectedUidUpdates, mHandler.numUidUpdates); Loading Loading @@ -291,8 +306,9 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mHandler, REGISTRANT_WHAT, null); mTestableLooper.processAllMessages(); // No updates triggered, but the registrant gets an empty update. verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); // No updates triggered, so TelephonyRegistry doesn't get any updates. verifyCarrierPrivilegesChangedUpdates(List.of()); } Loading @@ -307,6 +323,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mHandler, REGISTRANT_WHAT, null); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -316,6 +333,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mCarrierPrivilegesTracker.unregisterCarrierPrivilegesListener(mHandler); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of()); Loading @@ -323,6 +341,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of(new Pair<>(Set.of(), new int[0]))); } Loading @@ -340,6 +359,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -353,6 +373,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID_INCORRECT); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -366,6 +387,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -384,6 +406,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of(new Pair<>(Set.of(), new int[0]))); } Loading @@ -403,6 +426,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -415,6 +439,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -433,6 +458,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -451,6 +477,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimApplicationStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -466,6 +493,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -483,6 +511,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID_INCORRECT, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -496,6 +525,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_NOT_READY); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -518,6 +548,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -531,6 +562,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -547,6 +579,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -566,6 +599,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), PRIVILEGED_UIDS))); Loading @@ -586,6 +620,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REPLACED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), PRIVILEGED_UIDS))); Loading @@ -610,6 +645,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -633,6 +669,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -651,6 +688,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REMOVED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -665,6 +703,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REMOVED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(null /* expectedUidUpdates */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of()); } Loading @@ -679,6 +718,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect no package will have privilege at last verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -690,6 +730,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect all privileges from Carrier Config come back verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -705,6 +746,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect only PACKAGE_3 will have privilege at last verifyCurrentState(Set.of(PACKAGE_3), new int[]{UID_3}); verifyRegistrantUpdates(new int[]{UID_3}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -717,6 +759,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect all privileges from UICC come back verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading Loading
src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java +58 −3 Original line number Diff line number Diff line Loading @@ -29,6 +29,7 @@ import static android.telephony.TelephonyManager.SIM_STATE_UNKNOWN; import android.annotation.NonNull; import android.annotation.Nullable; import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading @@ -48,6 +49,7 @@ import android.os.Registrant; import android.os.RegistrantList; import android.os.UserHandle; import android.os.UserManager; import android.telephony.Annotation.CarrierPrivilegeStatus; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; Loading @@ -63,6 +65,7 @@ import com.android.internal.annotations.GuardedBy; import com.android.internal.telephony.uicc.IccUtils; import com.android.internal.telephony.uicc.UiccPort; import com.android.internal.telephony.uicc.UiccProfile; import com.android.internal.util.ArrayUtils; import com.android.telephony.Rlog; import java.io.FileDescriptor; Loading Loading @@ -514,10 +517,13 @@ public class CarrierPrivilegesTracker extends Handler { } private void refreshInstalledPackageCache() { // Include DISABLED_UNTIL_USED components. This facilitates cases where a carrier app // is disabled by default, and some other component wants to enable it when it has // gained carrier privileges (as an indication that a matching SIM has been inserted). int flags = PackageManager.MATCH_DISABLED_COMPONENTS PackageManager.GET_SIGNING_CERTIFICATES | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS | PackageManager.GET_SIGNING_CERTIFICATES; | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS; List<PackageInfo> installedPackages = mPackageManager.getInstalledPackagesAsUser( flags, UserHandle.SYSTEM.getIdentifier()); Loading Loading @@ -581,6 +587,11 @@ public class CarrierPrivilegesTracker extends Handler { } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } // Update set of enabled carrier apps now that the privilege rules may have changed. ActivityManager am = mContext.getSystemService(ActivityManager.class); CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(), mTelephonyManager, am.getCurrentUser(), mContext); } private PrivilegedPackageInfo getCurrentPrivilegedPackagesForAllUsers() { Loading Loading @@ -730,7 +741,51 @@ public class CarrierPrivilegesTracker extends Handler { // best effort. refreshInstalledPackageCache(); } maybeUpdatePrivilegedPackagesAndNotifyRegistrants(); } /** Backing of {@link TelephonyManager#checkCarrierPrivilegesForPackage}. */ public @CarrierPrivilegeStatus int getCarrierPrivilegeStatusForPackage(String packageName) { // TODO(b/205736323) consider if/how we want to account for the RULES_NOT_LOADED and // ERROR_LOADING_RULES constants. Technically those will never be returned today since those // results are only from the SIM rules, but the CC rules' result (which never has these // errors) always supersede them unless something goes super wrong when getting CC. mPrivilegedPackageInfoLock.readLock().lock(); try { return mPrivilegedPackageInfo.mPackageNames.contains(packageName) ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS; } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } /** Backing of {@link TelephonyManager#getPackagesWithCarrierPrivileges}. */ public Set<String> getPackagesWithCarrierPrivileges() { mPrivilegedPackageInfoLock.readLock().lock(); try { return Collections.unmodifiableSet(mPrivilegedPackageInfo.mPackageNames); } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } /** * Backing of {@link TelephonyManager#hasCarrierPrivileges} and {@link * TelephonyManager#getCarrierPrivilegeStatus(int)}. */ public @CarrierPrivilegeStatus int getCarrierPrivilegeStatusForUid(int uid) { // TODO(b/205736323) consider if/how we want to account for the RULES_NOT_LOADED and // ERROR_LOADING_RULES constants. Technically those will never be returned today since those // results are only from the SIM rules, but the CC rules' result (which never has these // errors) always supersede them unless something goes super wrong when getting CC. mPrivilegedPackageInfoLock.readLock().lock(); try { return ArrayUtils.contains(mPrivilegedPackageInfo.mUids, uid) ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS; } finally { mPrivilegedPackageInfoLock.readLock().unlock(); } } }
src/java/com/android/internal/telephony/uicc/UiccPort.java +18 −6 Original line number Diff line number Diff line Loading @@ -378,8 +378,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatus(Signature, String)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(Signature, String)} * instead. */ @Deprecated public int getCarrierPrivilegeStatus(Signature signature, String packageName) { Loading @@ -392,8 +395,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatus(PackageManager, String)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(PackageManager, String)} * instead. */ @Deprecated public int getCarrierPrivilegeStatus(PackageManager packageManager, String packageName) { Loading @@ -406,6 +412,9 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatus}. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link UiccProfile#getCarrierPrivilegeStatus(PackageInfo)} instead. */ @Deprecated Loading @@ -419,8 +428,11 @@ public class UiccPort { /** * Exposes {@link UiccCarrierPrivilegeRules#getCarrierPrivilegeStatusForCurrentTransaction}. * @deprecated Please use * {@link UiccProfile#getCarrierPrivilegeStatusForCurrentTransaction(PackageManager)} instead. * * <p>TODO(b/205736323) remove this and downstream once fully moved to CarrierPrivilegesTracker * * @deprecated Please use {@link * UiccProfile#getCarrierPrivilegeStatusForCurrentTransaction(PackageManager)} instead. */ @Deprecated public int getCarrierPrivilegeStatusForCurrentTransaction(PackageManager packageManager) { Loading
src/java/com/android/internal/telephony/uicc/UiccProfile.java +1 −0 Original line number Diff line number Diff line Loading @@ -1334,6 +1334,7 @@ public class UiccProfile extends IccCard { } private void onCarrierPrivilegesLoadedMessage() { // TODO(b/211796398): clean up logic below once all carrier privilege check migration done // Update set of enabled carrier apps now that the privilege rules may have changed. ActivityManager am = mContext.getSystemService(ActivityManager.class); CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(), Loading
tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java +46 −3 Original line number Diff line number Diff line Loading @@ -105,9 +105,9 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { private static final int[] PRIVILEGED_UIDS = {UID_1, UID_2}; private static final int PM_FLAGS = PackageManager.MATCH_DISABLED_COMPONENTS PackageManager.GET_SIGNING_CERTIFICATES | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS | PackageManager.GET_SIGNING_CERTIFICATES; | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS; @Mock private Signature mSignature; Loading Loading @@ -257,6 +257,21 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { } } private void verifyCurrentState(Set<String> expectedPackageNames, int[] expectedUids) { assertEquals( expectedPackageNames, mCarrierPrivilegesTracker.getPackagesWithCarrierPrivileges()); for (String packageName : expectedPackageNames) { assertEquals( TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS, mCarrierPrivilegesTracker.getCarrierPrivilegeStatusForPackage(packageName)); } for (int uid : expectedUids) { assertEquals( TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS, mCarrierPrivilegesTracker.getCarrierPrivilegeStatusForUid(uid)); } } private void verifyRegistrantUpdates(@Nullable int[] expectedUids, int expectedUidUpdates) { assertArrayEquals(expectedUids, mHandler.privilegedUids); assertEquals(expectedUidUpdates, mHandler.numUidUpdates); Loading Loading @@ -291,8 +306,9 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mHandler, REGISTRANT_WHAT, null); mTestableLooper.processAllMessages(); // No updates triggered, but the registrant gets an empty update. verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); // No updates triggered, so TelephonyRegistry doesn't get any updates. verifyCarrierPrivilegesChangedUpdates(List.of()); } Loading @@ -307,6 +323,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mHandler, REGISTRANT_WHAT, null); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -316,6 +333,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mCarrierPrivilegesTracker.unregisterCarrierPrivilegesListener(mHandler); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of()); Loading @@ -323,6 +341,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of(new Pair<>(Set.of(), new int[0]))); } Loading @@ -340,6 +359,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -353,6 +373,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID_INCORRECT); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -366,6 +387,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -384,6 +406,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0] /* expectedUids */, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of(new Pair<>(Set.of(), new int[0]))); } Loading @@ -403,6 +426,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -415,6 +439,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -433,6 +458,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -451,6 +477,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimApplicationStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -466,6 +493,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -483,6 +511,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID_INCORRECT, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(null /* expectedUids */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -496,6 +525,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_NOT_READY); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -518,6 +548,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -531,6 +562,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendSimCardStateChangedIntent(PHONE_ID, SIM_STATE_LOADED); mTestableLooper.processAllMessages(); verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -547,6 +579,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1}); verifyRegistrantUpdates(new int[] {UID_1}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), new int[] {UID_1}))); Loading @@ -566,6 +599,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), PRIVILEGED_UIDS))); Loading @@ -586,6 +620,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REPLACED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_1), PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_1), PRIVILEGED_UIDS))); Loading @@ -610,6 +645,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -633,6 +669,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_ADDED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -651,6 +688,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REMOVED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(PACKAGE_2), new int[] {UID_2}); verifyRegistrantUpdates(new int[] {UID_2}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(Set.of(PACKAGE_2), new int[] {UID_2}))); Loading @@ -665,6 +703,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { sendPackageChangedIntent(Intent.ACTION_PACKAGE_REMOVED, PACKAGE_1); mTestableLooper.processAllMessages(); verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(null /* expectedUidUpdates */, 0 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates(List.of()); } Loading @@ -679,6 +718,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect no package will have privilege at last verifyCurrentState(Set.of(), new int[0]); verifyRegistrantUpdates(new int[0], 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -690,6 +730,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect all privileges from Carrier Config come back verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading @@ -705,6 +746,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect only PACKAGE_3 will have privilege at last verifyCurrentState(Set.of(PACKAGE_3), new int[]{UID_3}); verifyRegistrantUpdates(new int[]{UID_3}, 1 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of( Loading @@ -717,6 +759,7 @@ public class CarrierPrivilegesTrackerTest extends TelephonyTest { mTestableLooper.processAllMessages(); // Expect all privileges from UICC come back verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS); verifyRegistrantUpdates(PRIVILEGED_UIDS, 2 /* expectedUidUpdates */); verifyCarrierPrivilegesChangedUpdates( List.of(new Pair<>(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS))); Loading