Search code examples
c#jsonmdmpolicyandroid-management-api

Policy body to install an Application on the Managed device using AndroidManagement API


I'm building an MDM application using Google's AndroidManagement API in a C# Console application and I have created a policy for the device as well which has the default values.

I'm trying to create an Application Policy to install a new app package on the device and all the blogs suggest to add the application policy in a JSON :

{
  "applications": [
    {
      "packageName": "com.example",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": true,
      "defaultPermissionPolicy": "GRANT"
    },
    {
      "packageName": "example2",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": false,
      "defaultPermissionPolicy": "GRANT"
    },
    {
      "packageName": "example3",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": true,
      "defaultPermissionPolicy": "GRANT"
    }
  ],
  "cameraDisabled": true,
  "screenCaptureDisabled": true,
  "adjustVolumeDisabled" :  true
}

And the Policy body instance is created :

public static Google.Apis.AndroidManagement.v1.Data.Policy body = new Google.Apis.AndroidManagement.v1.Data.Policy();

Json loads perfectly but the application policy lists cannot be saved into the ApplicationPolicy because of type conversions.

{
    var appPolicy = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(applicationPolicyJson));

    policyBody.CameraDisabled = appPolicy.CameraDisabled;
    policyBody.ScreenCaptureDisabled = appPolicy.ScreenCaptureDisabled;
    policyBody.AdjustVolumeDisabled = appPolicy.AdjustVolumeDisabled;

    policyBody.Applications = new List<ApplicationPolicy> {***appPolicy*** };
}


public class Rootobject
{
   public Application[] Applications { get; set; }
   public bool CameraDisabled { get; set; }
   public bool ScreenCaptureDisabled { get; set; }
   public bool AdjustVolumeDisabled { get; set; }
}
public class Application
{
   public string PackageName { get; set; }
   public string InstallType { get; set; }
   public bool LockTaskAllowed { get; set; }
   public string DefaultPermissionPolicy { get; set; }
}

How can I achieve this?

Thanks!


Solution

  • When using the C# library you cannot "load" the JSON directly, the Patch method takes a Policy object that you need to build using C# semantics. It's the same for Java. On the other hand the Python library takes a dictionary which is easy to generate from a JSON.

    For C# this would look like:

    using Google.Apis.AndroidManagement.v1.Data;
    
    var policy = new Policy();
    
    policy.CameraDisabled = true;
    policy.ScreenCaptureDisabled = true;
    policy.AdjustVolumeDisabled = true;
    
    var app1 = new ApplicationPolicy();
    app.PackageName = "com.android.chrome";
    app.InstallType = "FORCE_INSTALLED";
    app.LockTaskAllowed = true;
    app.DefaultPermissionPolicy = "GRANT";
    
    var app2 = new ApplicationPolicy();
    app.PackageName = "com.example";
    app.InstallType = "FORCE_INSTALLED";
    
    policy.Applications = new List<ApplicationPolicy> {app1, app2};
    

    Disclaimer: I'm not used to C#, so it's possible that this code doesn't compile, but hopefully that gives you the idea.