diff options
| author | 2022-05-04 19:35:32 +0100 | |
|---|---|---|
| committer | 2022-05-09 14:48:37 +0100 | |
| commit | 13bb7f3c1a3ff513fa9d91cd31f9e0d50974e57c (patch) | |
| tree | 4ef267d6e9af7de0e5acd33e748cd09b7125c793 | |
| parent | 064b49a1a1cf9fdec2a2391336b61d6d6af04773 (diff) | |
Minor refactoring in WorkPolicyUtils
Making some methods public in order to use them from Permission
Controller.
Test: manually tested
Change-Id: Ie1967ae629521ae790afc51f2460f13ba3d6b2b0
| -rw-r--r-- | packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java | 89 |
1 files changed, 56 insertions, 33 deletions
diff --git a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java index 5693c2f22d1e..281f7ba6a5ea 100644 --- a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java +++ b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java @@ -27,17 +27,20 @@ import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; +import androidx.annotation.Nullable; + import java.util.List; + /** * Utility class for find out when to show WorkPolicyInfo */ public class WorkPolicyUtils { - Context mContext; - PackageManager mPackageManager; - UserManager mUserManager; - DevicePolicyManager mDevicePolicyManager; + private final Context mContext; + private final PackageManager mPackageManager; + private final UserManager mUserManager; + private final DevicePolicyManager mDevicePolicyManager; private static final int USER_NULL = -10000; @@ -81,7 +84,12 @@ public class WorkPolicyUtils { return false; } - private Intent getWorkPolicyInfoIntentDO() { + /** + * Returns the work policy info intent if the device owner component exists, + * and returns {@code null} otherwise + */ + @Nullable + public Intent getWorkPolicyInfoIntentDO() { final ComponentName ownerComponent = getDeviceOwnerComponent(); if (ownerComponent == null) { return null; @@ -99,43 +107,55 @@ public class WorkPolicyUtils { return null; } - private Intent getWorkPolicyInfoIntentPO() { + @Nullable + private ComponentName getManagedProfileOwnerComponent(int managedUserId) { + if (managedUserId == USER_NULL) { + return null; + } + Context managedProfileContext; try { - final int managedUserId = getManagedProfileUserId(); - if (managedUserId == USER_NULL) { - return null; - } - - Context managedProfileContext = + managedProfileContext = mContext.createPackageContextAsUser( mContext.getPackageName(), 0, UserHandle.of(managedUserId) ); + } catch (PackageManager.NameNotFoundException e) { + return null; + } - DevicePolicyManager managedProfileDevicePolicyManager = - (DevicePolicyManager) - managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE); - ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner(); - if (ownerComponent == null) { - return null; - } - - // Only search for the required action in the Profile Owner's package - final Intent intent = - new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO) - .setPackage(ownerComponent.getPackageName()); - final List<ResolveInfo> activities = - mPackageManager.queryIntentActivitiesAsUser( - intent, 0, UserHandle.of(managedUserId)); - if (activities.size() != 0) { - return intent; - } + DevicePolicyManager managedProfileDevicePolicyManager = + (DevicePolicyManager) + managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE); + ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner(); + return ownerComponent; + } - return null; - } catch (PackageManager.NameNotFoundException e) { + /** + * Returns the work policy info intent if the profile owner component exists, + * and returns {@code null} otherwise + */ + @Nullable + public Intent getWorkPolicyInfoIntentPO() { + final int managedUserId = getManagedProfileUserId(); + ComponentName ownerComponent = getManagedProfileOwnerComponent(managedUserId); + if (ownerComponent == null) { return null; } + + // Only search for the required action in the Profile Owner's package + final Intent intent = + new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO) + .setPackage(ownerComponent.getPackageName()); + final List<ResolveInfo> activities = + mPackageManager.queryIntentActivitiesAsUser( + intent, 0, UserHandle.of(managedUserId)); + if (activities.size() != 0) { + return intent; + } + + return null; } + @Nullable private ComponentName getDeviceOwnerComponent() { if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { return null; @@ -143,7 +163,10 @@ public class WorkPolicyUtils { return mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser(); } - private int getManagedProfileUserId() { + /** + * Returns the user id of the managed profile, and returns {@code USER_NULL} otherwise + */ + public int getManagedProfileUserId() { List<UserHandle> allProfiles = mUserManager.getAllProfiles(); for (UserHandle uh : allProfiles) { int id = uh.getIdentifier(); |