On Android N and after, it allows the selected app to call the DevicePolicyManager APIs using: * {@link DevicePolicyManager#setApplicationRestrictionsManagingPackage} * {@link DevicePolicyManager#getApplicationRestrictionsManagingPackage} * *
On Android M and before, it allows the selected app to proxy API calls to DevicePolicyManager * via TestDPC, see {@link AppRestrictionsProxyHandler}. */ public class AppRestrictionsManagingPackageFragment extends SelectAppFragment { private DevicePolicyManager mDpm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); } @Override public void onResume() { super.onResume(); getActivity().getActionBar().setTitle(R.string.app_restrictions_managing_package); } @Override protected void setSelectedPackage(String pkgName) { // If the input pkgName is an empty string, we clear the app restriction manager. if (TextUtils.isEmpty(pkgName)) { pkgName = null; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { setApplicationRestrictionsManagingPackage(pkgName); } else { setApplicationRestrictionsManagingPackageWithProxy(pkgName); } } @Override protected void clearSelectedPackage() { setSelectedPackage(null); } @Override protected String getSelectedPackage() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return getApplicationRestrictionsManagingPackage(); } else { return getApplicationRestrictionsManagingPackageWithProxy(); } } @TargetApi(Build.VERSION_CODES.N) private void setApplicationRestrictionsManagingPackage(String pkgName) { try { mDpm.setApplicationRestrictionsManagingPackage( DeviceAdminReceiver.getComponentName(getActivity()), pkgName); } catch (NameNotFoundException e) { throw new IllegalArgumentException(e); } } private void setApplicationRestrictionsManagingPackageWithProxy(String pkgName) { AppRestrictionsProxyHandler.setApplicationRestrictionsManagingPackage( getActivity(), pkgName); } @TargetApi(Build.VERSION_CODES.N) private String getApplicationRestrictionsManagingPackage() { return mDpm.getApplicationRestrictionsManagingPackage( DeviceAdminReceiver.getComponentName(getActivity())); } private String getApplicationRestrictionsManagingPackageWithProxy() { return AppRestrictionsProxyHandler.getApplicationRestrictionsManagingPackage(getActivity()); } }