Remove persistentPreferrredActivity policy on activity removal
Fixes: 266924257 Test: btest a.d.c.PersistentPreferredActivitiesTest Change-Id: I17b8c66cdf991a11217df7fba5722ec955d3c8cb
This commit is contained in:
@@ -32,13 +32,17 @@ import android.app.BroadcastOptions;
|
||||
import android.app.admin.DevicePolicyIdentifiers;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.app.admin.DevicePolicyState;
|
||||
import android.app.admin.IntentFilterPolicyKey;
|
||||
import android.app.admin.PolicyKey;
|
||||
import android.app.admin.PolicyUpdateReceiver;
|
||||
import android.app.admin.PolicyValue;
|
||||
import android.app.admin.TargetUser;
|
||||
import android.app.admin.UserRestrictionPolicyKey;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.IPackageManager;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
@@ -1043,10 +1047,56 @@ final class DevicePolicyEngine {
|
||||
}
|
||||
if (updatedPackage != null) {
|
||||
updateDeviceAdminServiceOnPackageChanged(updatedPackage, userId);
|
||||
removePersistentPreferredActivityPoliciesForPackage(updatedPackage, userId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void removePersistentPreferredActivityPoliciesForPackage(
|
||||
@NonNull String packageName, int userId) {
|
||||
Set<PolicyKey> policyKeys = getLocalPolicyKeysSetByAllAdmins(
|
||||
PolicyDefinition.GENERIC_PERSISTENT_PREFERRED_ACTIVITY, userId);
|
||||
for (PolicyKey key : policyKeys) {
|
||||
if (!(key instanceof IntentFilterPolicyKey)) {
|
||||
throw new IllegalStateException("PolicyKey for "
|
||||
+ "PERSISTENT_PREFERRED_ACTIVITY is not of type "
|
||||
+ "IntentFilterPolicyKey");
|
||||
}
|
||||
IntentFilterPolicyKey parsedKey =
|
||||
(IntentFilterPolicyKey) key;
|
||||
IntentFilter intentFilter = Objects.requireNonNull(parsedKey.getIntentFilter());
|
||||
PolicyDefinition<ComponentName> policyDefinition =
|
||||
PolicyDefinition.PERSISTENT_PREFERRED_ACTIVITY(intentFilter);
|
||||
LinkedHashMap<EnforcingAdmin, PolicyValue<ComponentName>> policies =
|
||||
getLocalPoliciesSetByAdmins(
|
||||
policyDefinition,
|
||||
userId);
|
||||
IPackageManager packageManager = AppGlobals.getPackageManager();
|
||||
for (EnforcingAdmin admin : policies.keySet()) {
|
||||
if (policies.get(admin).getValue() != null
|
||||
&& policies.get(admin).getValue().getPackageName().equals(packageName)) {
|
||||
try {
|
||||
if (packageManager.getPackageInfo(
|
||||
packageName, 0, userId) == null
|
||||
|| packageManager.getReceiverInfo(policies.get(admin).getValue(),
|
||||
PackageManager.MATCH_DIRECT_BOOT_AWARE
|
||||
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
|
||||
userId) == null) {
|
||||
Slogf.e(TAG, String.format(
|
||||
"Persistent preferred activity in package %s not found for "
|
||||
+ "user %d, removing policy for admin",
|
||||
packageName, userId));
|
||||
removeLocalPolicy(policyDefinition, admin, userId);
|
||||
}
|
||||
} catch (RemoteException re) {
|
||||
// Shouldn't happen.
|
||||
Slogf.wtf(TAG, "Error handling package changes", re);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPackageInstalled(String packageName, int userId) {
|
||||
try {
|
||||
return AppGlobals.getPackageManager().getPackageInfo(
|
||||
|
||||
@@ -11444,6 +11444,10 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
|
||||
|| isDefaultDeviceOwner(caller) || isFinancedDeviceOwner(caller));
|
||||
enforcingAdmin = getEnforcingAdminForCaller(who, callerPackageName);
|
||||
}
|
||||
if (!isPackageInstalledForUser(activity.getPackageName(), userId)) {
|
||||
// Fail early as packageManager doesn't persist the activity if its not installed.
|
||||
return;
|
||||
}
|
||||
mDevicePolicyEngine.setLocalPolicy(
|
||||
PolicyDefinition.PERSISTENT_PREFERRED_ACTIVITY(filter),
|
||||
enforcingAdmin,
|
||||
|
||||
Reference in New Issue
Block a user