@@ -2254,6 +2254,46 @@ private void revokeStoragePermissionsIfScopeExpandedInternal(
22542254
22552255 }
22562256
2257+ /**
2258+ * If the package was below api 23, got the SYSTEM_ALERT_WINDOW permission automatically, and
2259+ * then updated past api 23, and the app does not satisfy any of the other SAW permission flags,
2260+ * the permission should be revoked.
2261+ *
2262+ * @param newPackage The new package that was installed
2263+ * @param oldPackage The old package that was updated
2264+ */
2265+ private void revokeSystemAlertWindowIfUpgradedPast23 (
2266+ @ NonNull AndroidPackage newPackage ,
2267+ @ NonNull AndroidPackage oldPackage ) {
2268+ if (oldPackage .getTargetSdkVersion () >= Build .VERSION_CODES .M
2269+ || newPackage .getTargetSdkVersion () < Build .VERSION_CODES .M
2270+ || !newPackage .getRequestedPermissions ()
2271+ .contains (Manifest .permission .SYSTEM_ALERT_WINDOW )) {
2272+ return ;
2273+ }
2274+
2275+ Permission saw ;
2276+ synchronized (mLock ) {
2277+ saw = mRegistry .getPermission (Manifest .permission .SYSTEM_ALERT_WINDOW );
2278+ }
2279+ final PackageSetting ps = (PackageSetting )
2280+ mPackageManagerInt .getPackageSetting (newPackage .getPackageName ());
2281+ if (shouldGrantPermissionByProtectionFlags (newPackage , ps , saw , new ArraySet <>())
2282+ || shouldGrantPermissionBySignature (newPackage , saw )) {
2283+ return ;
2284+ }
2285+ for (int userId : getAllUserIds ()) {
2286+ try {
2287+ revokePermissionFromPackageForUser (newPackage .getPackageName (),
2288+ Manifest .permission .SYSTEM_ALERT_WINDOW , false , userId ,
2289+ mDefaultPermissionCallback );
2290+ } catch (IllegalStateException | SecurityException e ) {
2291+ Log .e (TAG , "unable to revoke SYSTEM_ALERT_WINDOW for "
2292+ + newPackage .getPackageName () + " user " + userId , e );
2293+ }
2294+ }
2295+ }
2296+
22572297 /**
22582298 * We might auto-grant permissions if any permission of the group is already granted. Hence if
22592299 * the group of a granted permission changes we need to revoke it to avoid having permissions of
@@ -4831,6 +4871,7 @@ private void onPackageAddedInternal(@NonNull AndroidPackage pkg, boolean isInsta
48314871 if (hasOldPkg ) {
48324872 revokeRuntimePermissionsIfGroupChangedInternal (pkg , oldPkg );
48334873 revokeStoragePermissionsIfScopeExpandedInternal (pkg , oldPkg );
4874+ revokeSystemAlertWindowIfUpgradedPast23 (pkg , oldPkg );
48344875 }
48354876 if (hasPermissionDefinitionChanges ) {
48364877 revokeRuntimePermissionsIfPermissionDefinitionChangedInternal (
0 commit comments