Search code examples
androidandroid-management-api

Android Management API's Delegation Scopes do not seem to be acquired by Companion app


Following the Android Management API reference, we've configured our Android Companion app to be able to : enable system apps, hide and unhide packages.

We've added the following information in our policy configuration:

{
  '
  '
  '
  "applications": [
    {
      "packageName": "com.domain.app",
      "installType": "REQUIRED_FOR_SETUP",
      "defaultPermissionPolicy": "GRANT",
      "delegatedScopes": [
        "ENABLE_SYSTEM_APP",
        "PACKAGE_ACCESS"
      ]
    }
  ],
  '
  '
  '
}

Then, in our Android Companion app, we've added the following lines of code in compliance with Google documentation here and here :

DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName admin = new ComponentName(mContext, DeviceAdminReceiver.class);

// Iterate through system application package names list
for (String packageName : systemAppsList) {
  if (packageName != null && !packageName.equals("")) {
    try {
      // Re-enable a system app that was disabled by default when the user was initialized
      dpm.enableSystemApp(admin, packageName);

      // Unhide a package (it could be any app : system, managed, etc...)
      dpm.setApplicationHidden(admin, packageName, false);
    } catch (SecurityException e) {
      e.printStackTrace();
      Log.e(TAG, e.getMessage());
    }
  }
}

We expect the Android launcher to show the enabled system apps, but Android catches a SecurityException and prints the following error log :

No active admin ComponentInfo{com.domain.app/android.app.admin.DeviceAdminReceiver}

Do you have any idea about what could be wrong ?


Solution

  • in your method call , set admin param to null if you are using delegated scope as explained in Android Documentation

    ComponentName: Which DeviceAdminReceiver this request is associated with, or null if the caller is a package access delegate. This value must never be null.

    a bit confusing,admin param can be set to null if using delegated scope and in the same time just after ... this value must never be null , (Great Google)