/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app.admin; import android.annotation.CallbackExecutor; import android.annotation.ColorInt; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresFeature; import android.annotation.RequiresPermission; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.annotation.StringDef; import android.annotation.SuppressLint; import android.annotation.SystemApi; import android.annotation.SystemService; import android.annotation.TestApi; import android.annotation.UserIdInt; import android.annotation.WorkerThread; import android.app.Activity; import android.app.IServiceConnection; import android.app.KeyguardManager; import android.app.admin.SecurityLog.SecurityEvent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.pm.ApplicationInfo; import android.content.pm.IPackageDataObserver; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ParceledListSlice; import android.content.pm.UserInfo; import android.graphics.Bitmap; import android.net.ProxyInfo; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.os.PersistableBundle; import android.os.Process; import android.os.RemoteCallback; import android.os.RemoteException; import android.os.ServiceSpecificException; import android.os.UserHandle; import android.os.UserManager; import android.os.UserManager.UserOperationException; import android.os.UserManager.UserOperationResult; import android.provider.ContactsContract.Directory; import android.provider.Settings; import android.security.AttestedKeyPair; import android.security.Credentials; import android.security.KeyChain; import android.security.KeyChainException; import android.security.keymaster.KeymasterCertificateChain; import android.security.keystore.AttestationUtils; import android.security.keystore.KeyAttestationException; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.ParcelableKeyGenParameterSpec; import android.service.restrictions.RestrictionsReceiver; import android.telephony.TelephonyManager; import android.telephony.data.ApnSetting; import android.util.ArraySet; import android.util.Log; import com.android.internal.R; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.Preconditions; import com.android.org.conscrypt.TrustedCertificateStore; import java.io.ByteArrayInputStream; import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.net.InetSocketAddress; import java.net.Proxy; import java.security.KeyFactory; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.concurrent.Executor; /** * Public interface for managing policies enforced on a device. Most clients of this class must be * registered with the system as a device * administrator. Additionally, a device administrator may be registered as either a profile or * device owner. A given method is accessible to all device administrators unless the documentation * for that method specifies that it is restricted to either device or profile owners. Any * application calling an api may only pass as an argument a device administrator component it * owns. Otherwise, a {@link SecurityException} will be thrown. *
*

Developer Guides

*

* For more information about managing policies for device administration, read the Device Administration developer * guide.

*/ @SystemService(Context.DEVICE_POLICY_SERVICE) @RequiresFeature(PackageManager.FEATURE_DEVICE_ADMIN) public class DevicePolicyManager { private static String TAG = "DevicePolicyManager"; private final Context mContext; private final IDevicePolicyManager mService; private final boolean mParentInstance; /** @hide */ public DevicePolicyManager(Context context, IDevicePolicyManager service) { this(context, service, false); } /** @hide */ @VisibleForTesting protected DevicePolicyManager(Context context, IDevicePolicyManager service, boolean parentInstance) { mContext = context; mService = service; mParentInstance = parentInstance; } /** @hide test will override it. */ @VisibleForTesting protected int myUserId() { return mContext.getUserId(); } /** * Activity action: Starts the provisioning flow which sets up a managed profile. * *

A managed profile allows data separation for example for the usage of a * device as a personal and corporate device. The user which provisioning is started from and * the managed profile share a launcher. * *

This intent will typically be sent by a mobile device management application (MDM). * Provisioning adds a managed profile and sets the MDM as the profile owner who has full * control over the profile. * *

It is possible to check if provisioning is allowed or not by querying the method * {@link #isProvisioningAllowed(String)}. * *

In version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this intent must contain the * extra {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME}. * As of {@link android.os.Build.VERSION_CODES#M}, it should contain the extra * {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME} instead, although specifying only * {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME} is still supported. * *

The intent may also contain the following extras: *

* *

When managed provisioning has completed, broadcasts are sent to the application specified * in the provisioning intent. The * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE} broadcast is sent in the * managed profile and the {@link #ACTION_MANAGED_PROFILE_PROVISIONED} broadcast is sent in * the primary profile. * *

From version {@link android.os.Build.VERSION_CODES#O}, when managed provisioning has * completed, along with the above broadcast, activity intent * {@link #ACTION_PROVISIONING_SUCCESSFUL} will also be sent to the profile owner. * *

If provisioning fails, the managedProfile is removed so the device returns to its * previous state. * *

If launched with {@link android.app.Activity#startActivityForResult(Intent, int)} a * result code of {@link android.app.Activity#RESULT_OK} implies that the synchronous part of * the provisioning flow was successful, although this doesn't guarantee the full flow will * succeed. Conversely a result code of {@link android.app.Activity#RESULT_CANCELED} implies * that the user backed-out of provisioning, or some precondition for provisioning wasn't met. */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String ACTION_PROVISION_MANAGED_PROFILE = "android.app.action.PROVISION_MANAGED_PROFILE"; /** * Activity action: Starts the provisioning flow which sets up a managed user. * *

This intent will typically be sent by a mobile device management application (MDM). * Provisioning configures the user as managed user and sets the MDM as the profile * owner who has full control over the user. Provisioning can only happen before user setup has * been completed. Use {@link #isProvisioningAllowed(String)} to check if provisioning is * allowed. * *

The intent contains the following extras: *

* *

If provisioning fails, the device returns to its previous state. * *

If launched with {@link android.app.Activity#startActivityForResult(Intent, int)} a * result code of {@link android.app.Activity#RESULT_OK} implies that the synchronous part of * the provisioning flow was successful, although this doesn't guarantee the full flow will * succeed. Conversely a result code of {@link android.app.Activity#RESULT_CANCELED} implies * that the user backed-out of provisioning, or some precondition for provisioning wasn't met. * * @hide */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String ACTION_PROVISION_MANAGED_USER = "android.app.action.PROVISION_MANAGED_USER"; /** * Activity action: Starts the provisioning flow which sets up a managed device. * Must be started with {@link android.app.Activity#startActivityForResult(Intent, int)}. * *

During device owner provisioning a device admin app is set as the owner of the device. * A device owner has full control over the device. The device owner can not be modified by the * user. * *

A typical use case would be a device that is owned by a company, but used by either an * employee or client. * *

An intent with this action can be sent only on an unprovisioned device. * It is possible to check if provisioning is allowed or not by querying the method * {@link #isProvisioningAllowed(String)}. * *

The intent contains the following extras: *

* *

When device owner provisioning has completed, an intent of the type * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE} is broadcast to the * device owner. * *

From version {@link android.os.Build.VERSION_CODES#O}, when device owner provisioning has * completed, along with the above broadcast, activity intent * {@link #ACTION_PROVISIONING_SUCCESSFUL} will also be sent to the device owner. * *

If provisioning fails, the device is factory reset. * *

A result code of {@link android.app.Activity#RESULT_OK} implies that the synchronous part * of the provisioning flow was successful, although this doesn't guarantee the full flow will * succeed. Conversely a result code of {@link android.app.Activity#RESULT_CANCELED} implies * that the user backed-out of provisioning, or some precondition for provisioning wasn't met. */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String ACTION_PROVISION_MANAGED_DEVICE = "android.app.action.PROVISION_MANAGED_DEVICE"; /** * Activity action: launch when user provisioning completed, i.e. * {@link #getUserProvisioningState()} returns one of the complete state. * *

Please note that the API behavior is not necessarily consistent across various releases, * and devices, as it's contract between SetupWizard and ManagedProvisioning. The default * implementation is that ManagedProvisioning launches SetupWizard in NFC provisioning only. * *

The activity must be protected by permission * {@link android.Manifest.permission#BIND_DEVICE_ADMIN}, and the process must hold * {@link android.Manifest.permission#DISPATCH_PROVISIONING_MESSAGE} to be launched. * Only one {@link ComponentName} in the entire system should be enabled, and the rest of the * components are not started by this intent. * @hide */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) @SystemApi public static final String ACTION_STATE_USER_SETUP_COMPLETE = "android.app.action.STATE_USER_SETUP_COMPLETE"; /** * Activity action: Starts the provisioning flow which sets up a managed device. * *

During device owner provisioning, a device admin app is downloaded and set as the owner of * the device. A device owner has full control over the device. The device owner can not be * modified by the user and the only way of resetting the device is via factory reset. * *

A typical use case would be a device that is owned by a company, but used by either an * employee or client. * *

The provisioning message should be sent to an unprovisioned device. * *

Unlike {@link #ACTION_PROVISION_MANAGED_DEVICE}, the provisioning message can only be sent * by a privileged app with the permission * {@link android.Manifest.permission#DISPATCH_PROVISIONING_MESSAGE}. * *

The provisioning intent contains the following properties: *