Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 13bb7f3c authored by Prabal Singh's avatar Prabal Singh
Browse files

Minor refactoring in WorkPolicyUtils

Making some methods public in order to use them from Permission
Controller.

Test: manually tested
Change-Id: Ie1967ae629521ae790afc51f2460f13ba3d6b2b0
parent 064b49a1
Loading
Loading
Loading
Loading
+56 −33
Original line number Diff line number Diff line
@@ -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,22 +107,36 @@ public class WorkPolicyUtils {
        return null;
    }

    private Intent getWorkPolicyInfoIntentPO() {
        try {
            final int managedUserId = getManagedProfileUserId();
    @Nullable
    private ComponentName getManagedProfileOwnerComponent(int managedUserId) {
        if (managedUserId == USER_NULL) {
            return null;
        }

            Context managedProfileContext =
        Context managedProfileContext;
        try {
            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();
        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) {
            return null;
        }
@@ -131,11 +153,9 @@ public class WorkPolicyUtils {
        }

        return null;
        } catch (PackageManager.NameNotFoundException e) {
            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();