0

I'm using the Android Mgmt. API to provision tablet devices. I have policies for each of these apps, and within the policies, I set a managedConfiguration for 'terminal,' which I was intending to be able to use this string value as the identifier for which 'terminal' I have setup. Here is the application policy:

  "applications": [
    {
      "packageName": "packageName",
      "installType": "KIOSK",
      "defaultPermissionPolicy": "GRANT",
      "managedConfiguration": {
        "terminal": "2208"
      }
    }, 

My res > xml > app_restrictions.xml:

<?xml version="1.0" encoding="utf-8"?>
<restrictions xmlns:android="http://schemas.android.com/apk/res/android">

    <restriction
        android:key="facility"
        android:title="@strings/facility_title"
        android:restrictionType="string" />

    <restriction
        android:key="terminal"
        android:title="@strings/terminal_title"
        android:restrictionType="string" />

</restrictions>

A call to enterprises.applications.get shows that the managed config settings are recognized:

    {
      "key": "terminal",
      "type": "STRING",
      "title": "@strings/terminal_title"
    } 

Inside my app code(both following snippets are inside onCreate):

val config = this.getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager

val identifier = TerminalIdentifier("")
val appRestrictions = config.applicationRestrictions

if (appRestrictions.containsKey("terminal")) {
    identifier.id = appRestrictions.getString("terminal").toString()
}

// listener for changes while app is active
val restrictionsReceiver = this.setupRestrictionsReceiver(identifier, appRestrictions)
val restrictionsFilter = IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED)

registerReceiver(restrictionsReceiver, restrictionsFilter) 

And here's the listener bit. Note, I will move this logic outside of onCreate. For now, with my main app, I just wanted to test if each app would receive the MC 'terminal' value that I setup in each policy.

private fun setupRestrictionsReceiver(identifier: IdentifierService, restrictions : Bundle) : BroadcastReceiver {
        return object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if (restrictions.containsKey("terminal")) {
                    identifier.id = restrictions.getString("terminal").toString()
                }
            }
        }
    }

My app isn't receiving the value. Am I missing something with this? Why isn't my identifier.id (a string field) receiving '2208'?

theDude
  • 132
  • 1
  • 9
  • Have you defined your app's supported restrictions in a xml definitions file like described in the docs? https://developer.android.com/work/managed-configurations#define-configuration – petarov Nov 05 '20 at 14:50
  • @petarov yes, I made res > xml > app_restrictions.xml, and also have the addition to my manifest under 'application,' which I copied that part directly from the docs. The restriction seems to be noticed by enterprises.applications.get, like I said in the post, but not sure what's going on within the app. Thank you for helping me. I'll update the question with my app_restrictions.xml in case it helps. – theDude Nov 06 '20 at 15:47
  • The only other reason seems to be that the device is not managed, i.e., the app is not part of the managed profile. See here for more info - https://stackoverflow.com/a/41941367/10364676 – petarov Nov 07 '20 at 10:52
  • @petarov thank you. I've seen the testdpc app and am using it now. In case anyone else reads this and is doing a fully managed device, I followed this part: [link](https://developer.android.com/work/guide.html#provision-a-fully-managed-device). What I did is plug my android device to my computer via usb, and within android studio I brought up a terminal, ran the adb command from the 'platform-tools' dir. Then on the device, go to testdpc app. Can set managed configs from there, for your app. Will follow up once debug my post issues. – theDude Nov 07 '20 at 16:15

2 Answers2

0

I used the TestDPC app and was able to test my managed config settings. I don't know for certain, but I believe that my app errors were due to minification and resource shrinking being enabled. I don't know how anyone could survive without having TestDPC in place. This sped up the development cycle significantly.

Something to add: TestDPC installed fine via adb on a Samsung Tablet provided by work (I can't remember the exact model, but it was essentially brand new). However, I tried another, SM-T500, and there was an error when running adb. The only way I could get the TestDPC provisioning to work was this method, so I factory reset and used the identifier afw#testdpc.

I also ended up following this guide, more specifically the area on AppRestrictionsSchema. I updated my code to use a resolveRestrictions function, as well as putting the broadcast receiver handling in the various lifecycle functions. Again, this worked swimmingly, so I'd follow that format for anyone wanting to set this up.

I've deployed to a device and the managed configurations are working fine, so hope this can help someone else going down the same path.

theDude
  • 132
  • 1
  • 9
0

First thing you need to do when setting up managed configurations for your app is to define the managed configurations.

Once the managed configs are defined, you then need to check what the managed configurations are when your app starts or resumes, and listen for a system intent to find out if the configurations change while your app is running because it does not automatically notify your app when a change in managed configs are made.

Lastly, you need to listen to managed configurations changes. For more information about how to set this up, you may refer to this documentation.

Here’s a sample code added inside onCreate:

val config = this.getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager
val identifier = "welcome_msg"
val appRestrictions = config.applicationRestrictions

if (appRestrictions.containsKey(identifier)) {
   val myText  = findViewById<TextView>(R.id.txtMessage)
   myText.text = appRestrictions.getString(identifier)
}

// listener for changes while app is active
val restrictionsReceiver = object : BroadcastReceiver() {
   override fun onReceive(context: Context, intent: Intent) {

       // Get the current configuration bundle
       val appRestrictions = config.applicationRestrictions

       if (appRestrictions.containsKey(identifier)) {
           val myText  = findViewById<TextView>(R.id.txtMessage)
           myText.text = appRestrictions.getString(identifier)
       }
   }
}
val restrictionsFilter = IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED)
registerReceiver(restrictionsReceiver, restrictionsFilter)

Here’s the restriction.xml:

<?xml version="1.0" encoding="utf-8"?>
<restrictions xmlns:android="http://schemas.android.com/apk/res/android">

   <restriction
       android:key="welcome_msg"
       android:title="Welcome Message"
       android:restrictionType="string"
       android:defaultValue="" />

</restrictions>
Dave Paurillo
  • 231
  • 1
  • 12