Merge "Minor refactoring in WorkPolicyUtils" into tm-dev

This commit is contained in:
Prabal Singh
2022-05-10 03:58:34 +00:00
committed by Android (Google) Code Review

View File

@@ -27,17 +27,20 @@ import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import androidx.annotation.Nullable;
import java.util.List; import java.util.List;
/** /**
* Utility class for find out when to show WorkPolicyInfo * Utility class for find out when to show WorkPolicyInfo
*/ */
public class WorkPolicyUtils { public class WorkPolicyUtils {
Context mContext; private final Context mContext;
PackageManager mPackageManager; private final PackageManager mPackageManager;
UserManager mUserManager; private final UserManager mUserManager;
DevicePolicyManager mDevicePolicyManager; private final DevicePolicyManager mDevicePolicyManager;
private static final int USER_NULL = -10000; private static final int USER_NULL = -10000;
@@ -81,7 +84,12 @@ public class WorkPolicyUtils {
return false; 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(); final ComponentName ownerComponent = getDeviceOwnerComponent();
if (ownerComponent == null) { if (ownerComponent == null) {
return null; return null;
@@ -99,22 +107,36 @@ public class WorkPolicyUtils {
return null; return null;
} }
private Intent getWorkPolicyInfoIntentPO() { @Nullable
try { private ComponentName getManagedProfileOwnerComponent(int managedUserId) {
final int managedUserId = getManagedProfileUserId();
if (managedUserId == USER_NULL) { if (managedUserId == USER_NULL) {
return null; return null;
} }
Context managedProfileContext;
Context managedProfileContext = try {
managedProfileContext =
mContext.createPackageContextAsUser( mContext.createPackageContextAsUser(
mContext.getPackageName(), 0, UserHandle.of(managedUserId) mContext.getPackageName(), 0, UserHandle.of(managedUserId)
); );
} catch (PackageManager.NameNotFoundException e) {
return null;
}
DevicePolicyManager managedProfileDevicePolicyManager = DevicePolicyManager managedProfileDevicePolicyManager =
(DevicePolicyManager) (DevicePolicyManager)
managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE); managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner(); ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner();
return ownerComponent;
}
/**
* 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) { if (ownerComponent == null) {
return null; return null;
} }
@@ -131,11 +153,9 @@ public class WorkPolicyUtils {
} }
return null; return null;
} catch (PackageManager.NameNotFoundException e) {
return null;
}
} }
@Nullable
private ComponentName getDeviceOwnerComponent() { private ComponentName getDeviceOwnerComponent() {
if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) {
return null; return null;
@@ -143,7 +163,10 @@ public class WorkPolicyUtils {
return mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser(); 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(); List<UserHandle> allProfiles = mUserManager.getAllProfiles();
for (UserHandle uh : allProfiles) { for (UserHandle uh : allProfiles) {
int id = uh.getIdentifier(); int id = uh.getIdentifier();