How to: Manage a device configuration through the ConfigurationManager class

With the release of Datalogic SDK 1.32, Datalogic has completed the revamp of the Datalogic Android Mobile Devices configuration paradigm that began with the introduction of the ConfigurationManager class in SDK 1.27 and with the ACTION_CONFIGURATION_COMMIT of the Intents class in SDK 1.29.

In this new paradigm, the device exposes its settings organized as a tree, where the intermediate nodes are logical groups of properties and the leaves are the properties themselves, each with its own unique identifier.

The ConfigurationManager class gives the developer the ability to directly access each device configuration property, or browse and set them thruogh a simple and iterable collection of PropertyID, organized in PropertyGroupID.

The following code snippets show how to use the ConfigurationManager class for managing the device configuration:

Use case #1:
How to change a device setting directly accessing its specific property:

import com.datalogic.device.configuration.ConfigurationManager;
import com.datalogic.device.configuration.Property;
import com.datalogic.device.configuration.PropertyGroup;
...
ConfigurationManager cm = new ConfigurationManager(getApplicationContext());

//Change a property
void changeProperty() {
  //set one or more properties
  cm.getPropertyById(PropertyID.CODE39_ENABLE).set(true);
  //Applies all the modified values since the last commit() and make them persistent.
  cm.commit();
}

Use case #2:
How to browse the device configuration navigating the property tree.

import com.datalogic.device.configuration.ConfigurationManager;
import com.datalogic.device.configuration.Property;
import com.datalogic.device.configuration.PropertyGroup;
...
ConfigurationManager cm = new ConfigurationManager(getApplicationContext());

void printDeviceConfiguration() {
   PropertyGroup rootGroup = cm.getTreeRoot();
   printGroup(rootGroup);
}

void printGroup(PropertyGroup pg) {
   Log.d("SAMPLE", pg.getName());
   for (Property p : pg.getProperties()) {
      Log.d("SAMPLE", " " + p.getName() + ": " + p.get());
   }
   for (PropertyGroup p : pg.getGroups()) {
      printGroup(p);
   }
}

Use case #3:
How to listen for notifications about changes in device properties

Optionally, a third party app can also implement the ConfigurationChangeListener interface, to be notified about any configuration changes operated by the user, or by other apps with a commit() while the app is in use.
If a change occurs, onConfigurationChanged metod will be called to report the list of all the changed property and their related new values.

import com.datalogic.device.configuration.ConfigurationManager;
import com.datalogic.device.configuration.Property;
import com.datalogic.device.configuration.PropertyGroup;
...
private ConfigurationManager cm = new ConfigurationManager(getApplicationContext());
private ConfigurationChangeListener configurationChangeListener;

//Listen for device configuration change notifications
private void registerToListener() {

   configurationChangeListener = new ConfigurationChangeListener() {
      @Override
      public void onConfigurationChanged(HashMap<Integer, Object> map) {
         for (int propID : map.keySet()) {
            String text= " Property " + cm.getPropertyById(propID).getName() + " has new value: " + map.get(propID) + "\n";
            Log.d(TAG, text);
         }
      }
   };
   cm.registerListener(configurationChangeListener);
}

An exhaustive Android Studio sample project with further use cases is available in the official Datalogic SDK samples gallery at this link: ConfigurationManagerExample .

See Also:

Simone Callegari
Mobile Computer Specialist Support L3 Engineer | Datalogic

4 Likes