android.app.admin.DevicePolicyManager#setApplicationRestrictions ( )源码实例Demo

下面列出了android.app.admin.DevicePolicyManager#setApplicationRestrictions ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android-testdpc   文件: Util.java
/**
 * Sets the persistent device owner state by setting a special app restriction on GmsCore and
 * notifies GmsCore about the change by sending a broadcast.
 *
 * @param state The device owner state to be preserved across factory resets. If null, the
 * persistent device owner state and the corresponding restiction are cleared.
 */
@TargetApi(VERSION_CODES.O)
public static void setPersistentDoStateWithApplicationRestriction(
        Context context, DevicePolicyManager dpm, ComponentName admin, String state) {
    Bundle restrictions = dpm.getApplicationRestrictions(admin, GMSCORE_PACKAGE);
    if (state == null) {
        // Clear the restriction
        restrictions.remove(PERSISTENT_DEVICE_OWNER_STATE);
    } else {
        // Set the restriction
        restrictions.putString(PERSISTENT_DEVICE_OWNER_STATE, state);
    }
    dpm.setApplicationRestrictions(admin, GMSCORE_PACKAGE, restrictions);
    Intent broadcastIntent = new Intent(BROADCAST_ACTION_FRP_CONFIG_CHANGED);
    broadcastIntent.setPackage(GMSCORE_PACKAGE);
    broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    context.sendBroadcast(broadcastIntent);
}
 
/**
 * Saves all the restrictions.
 *
 * @param activity The activity.
 */
private void saveRestrictions(Activity activity) {
    DevicePolicyManager devicePolicyManager
            = (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    devicePolicyManager.setApplicationRestrictions(
            EnforcerDeviceAdminReceiver.getComponentName(activity),
            Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, mCurrentRestrictions);
}
 
/**
 * Clears restrictions to Chrome
 */
private void clearChromeRestrictions() {
    final Activity activity = getActivity();
    if (null == activity) {
        return;
    }
    final DevicePolicyManager manager =
            (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    // In order to clear restrictions, pass null as the restriction Bundle for
    // setApplicationRestrictions
    manager.setApplicationRestrictions
            (BasicDeviceAdminReceiver.getComponentName(activity),
                    PACKAGE_NAME_CHROME, null);
    Toast.makeText(activity, R.string.cleared, Toast.LENGTH_SHORT).show();
}
 
private void setApplicationRestrictions(String packageName, Bundle appRestrictions){
    if (packageName == null) {
        throw new IllegalArgumentException("packageName cannot be null.");
    }
    if (appRestrictions == null) {
        throw new IllegalArgumentException("applicationRestrictions bundle " +
                "cannot be null.");
    }
    Log.d(TAG, "Setting application restrictions for package " + packageName);
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager)
            mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
    devicePolicyManager.setApplicationRestrictions(mAdmin, packageName,
            appRestrictions);
}
 
/**
 * Saves all the restrictions.
 *
 * @param activity The activity.
 */
private void saveRestrictions(Activity activity) {
    DevicePolicyManager devicePolicyManager
            = (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    devicePolicyManager.setApplicationRestrictions(
            EnforcerDeviceAdminReceiver.getComponentName(activity),
            Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, mCurrentRestrictions);
}
 
/**
 * Clears restrictions to Chrome
 */
private void clearChromeRestrictions() {
    final Activity activity = getActivity();
    if (null == activity) {
        return;
    }
    final DevicePolicyManager manager =
            (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
    // In order to clear restrictions, pass null as the restriction Bundle for
    // setApplicationRestrictions
    manager.setApplicationRestrictions
            (BasicDeviceAdminReceiver.getComponentName(activity),
                    PACKAGE_NAME_CHROME, null);
    Toast.makeText(activity, R.string.cleared, Toast.LENGTH_SHORT).show();
}