Example usage for android.app.admin DevicePolicyManager getDeviceOwner

List of usage examples for android.app.admin DevicePolicyManager getDeviceOwner

Introduction

In this page you can find the example usage for android.app.admin DevicePolicyManager getDeviceOwner.

Prototype

@SystemApi
@RequiresPermission(android.Manifest.permission.MANAGE_USERS)
public @Nullable String getDeviceOwner() 

Source Link

Document

Returns the device owner package name, only if it's running on the calling user.

Usage

From source file:com.android.managedprovisioning.ProfileOwnerPreProvisioningActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final LayoutInflater inflater = getLayoutInflater();
    View contentView = inflater.inflate(R.layout.user_consent, null);
    setContentView(contentView);/*  ww w  .  j  ava 2 s  . c  o  m*/

    // Check whether system has the required managed profile feature.
    if (!systemHasManagedProfileFeature()) {
        showErrorAndClose(R.string.managed_provisioning_not_supported,
                "Exiting managed profile provisioning, " + "managed profiles feature is not available");
        return;
    }
    if (Process.myUserHandle().getIdentifier() != UserHandle.USER_OWNER) {
        showErrorAndClose(R.string.user_is_not_owner,
                "Exiting managed profile provisioning, calling user is not owner.");
        return;
    }

    // Initialize member variables from the intent, stop if the intent wasn't valid.
    try {
        initialize(getIntent());
    } catch (ProvisioningFailedException e) {
        showErrorAndClose(R.string.managed_provisioning_error_text, e.getMessage());
        return;
    }

    setMdmIcon(mMdmPackageName);

    // If the caller started us via ALIAS_NO_CHECK_CALLER then they must have permission to
    // MANAGE_USERS since it is a restricted intent. Otherwise, check the calling package.
    boolean hasManageUsersPermission = (getComponentName().equals(ALIAS_NO_CHECK_CALLER));
    if (!hasManageUsersPermission) {
        // Calling package has to equal the requested device admin package or has to be system.
        String callingPackage = getCallingPackage();
        if (callingPackage == null) {
            showErrorAndClose(R.string.managed_provisioning_error_text,
                    "Calling package is null. " + "Was startActivityForResult used to start this activity?");
            return;
        }
        if (!callingPackage.equals(mMdmPackageName) && !packageHasManageUsersPermission(callingPackage)) {
            showErrorAndClose(R.string.managed_provisioning_error_text,
                    "Permission denied, "
                            + "calling package tried to set a different package as profile owner. "
                            + "The system MANAGE_USERS permission is required.");
            return;
        }
    }

    DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    String deviceOwner = dpm.getDeviceOwner();
    if (deviceOwner != null && !deviceOwner.equals(mMdmPackageName)) {
        showErrorAndClose(R.string.managed_provisioning_error_text,
                "Permission denied, " + "profile owner must be in the same package as device owner.");
        return;
    }

    // If there is already a managed profile, allow the user to cancel or delete it.
    int existingManagedProfileUserId = alreadyHasManagedProfile();
    if (existingManagedProfileUserId != -1) {
        showManagedProfileExistsDialog(existingManagedProfileUserId);
    } else {
        showStartProvisioningScreen();
    }
}