Skip to content

Commit

Permalink
Added feedback channel reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrudu committed Feb 3, 2021
1 parent 86fd657 commit 41debfd
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 115 deletions.
5 changes: 3 additions & 2 deletions ZebraPrintService/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.zebra.zebraprintservice"
minSdkVersion 24
targetSdkVersion 29
versionCode 15
versionName "1.1.3"
versionCode 26
versionName "1.1.4"
externalNativeBuild {
cmake {
cppFlags "-std=c++14"
Expand Down Expand Up @@ -54,5 +54,6 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:+'
implementation 'org.parceler:parceler-api:1.1.12'
implementation 'androidx.enterprise:enterprise-feedback:+'
annotationProcessor 'org.parceler:parceler:1.1.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ public class APP_Restrictions_Changed_BroadcastReceiver extends BroadcastReceive

@Override
public void onReceive(Context context, Intent intent) {
RestrictionsManager myRestrictionsMgr =
(RestrictionsManager) context.getApplicationContext().getSystemService(Context.RESTRICTIONS_SERVICE);
Bundle appRestrictions = myRestrictionsMgr.getApplicationRestrictions();
Log.d(TAG, "Managed config" + appRestrictions.toString());

Parcelable[] printers_lists = appRestrictions.getParcelableArray("printers_list");
if(printers_lists != null && printers_lists.length > 0)
{
for(int i = 0; i < printers_lists.length; i++)
{
Bundle printer = (Bundle)printers_lists[i];
Log.d(TAG, "********************************************************************");
Log.d(TAG, "Managed config: Printer description (" + i + "/" + printers_lists.length +")");
Log.d(TAG, "Managed config, name: " + appRestrictions.getString("name"));
Log.d(TAG, "Managed config, address: " + appRestrictions.getString("address"));
Log.d(TAG, "Managed config, port: " + appRestrictions.getString("port"));
Log.d(TAG, "Managed config, connexion: " + appRestrictions.getString("connexion"));
Log.d(TAG, "Managed config, type: " + appRestrictions.getString("type"));
}
}
ManagedConfigurationHelper.readConfigFromManagedConfiguration(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.zebra.zebraprintservice.managedconfiguration;

import android.content.Context;
import android.content.RestrictionsManager;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.util.Log;

import com.zebra.zebraprintservice.database.PrinterDatabase;
import com.zebra.zebraprintservice.service.ZebraPrintService;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import androidx.annotation.Nullable;
import androidx.enterprise.feedback.KeyedAppState;
import androidx.enterprise.feedback.KeyedAppStatesCallback;
import androidx.enterprise.feedback.KeyedAppStatesReporter;

public class ManagedConfigurationHelper {

private static final String TAG = ZebraPrintService.class.getSimpleName();

/**********************************************************************************************/
public static void readConfigFromManagedConfiguration(Context context) {
Log.d(TAG, "Managed config");
// Retrieve the restriction manager that contains the Managed Configuration data
RestrictionsManager myRestrictionsMgr =
(RestrictionsManager) context.getSystemService(Context.RESTRICTIONS_SERVICE);
// Get the application restrictions bundle
Bundle appRestrictions = myRestrictionsMgr.getApplicationRestrictions();
Log.d(TAG, "Managed config" + appRestrictions.toString());

if(appRestrictions != null)
{
Log.d(TAG, "Managed configuration bundle found.");
// Get the Application Feedback Channel reporter
KeyedAppStatesReporter reporter = KeyedAppStatesReporter.create(context.getApplicationContext());
// Create the states collection that will be sent to feedback channel
Collection states = new HashSet<>();

// Try to retrieve the printer list from managed configuration bundle
Parcelable[] printers_lists = appRestrictions.getParcelableArray("printers_list");
if(printers_lists != null && printers_lists.length > 0)
{
Log.d(TAG, "Managed configuration bundle contains " + printers_lists.length + " printers descriptions.");

// The managed config contains a printer list
// Get the database object
PrinterDatabase mDb = new PrinterDatabase(context);

// Remove all printers from database
mDb.deleteAll();

// Store printer information to provide data inside the feedback channel message
String fcDataPrinterInfo = "";

for(int i = 0; i < printers_lists.length; i++)
{
Bundle printer = (Bundle)printers_lists[i];
// Log some data for debugging purposes
Log.d(TAG, "********************************************************************");
Log.d(TAG, "Managed config: Printer description (" + i + "/" + printers_lists.length +")");
Log.d(TAG, "Managed config, name: " + printer.getString("name"));
Log.d(TAG, "Managed config, address: " + printer.getString("address"));
Log.d(TAG, "Managed config, description: " + printer.getString("description"));
Log.d(TAG, "Managed config, type: " + printer.getString("type"));
Log.d(TAG, "Managed config, address: " + printer.getString("address"));
Log.d(TAG, "Managed config, port: " + printer.getInt("port"));
Log.d(TAG, "Managed config, dpi: " + printer.getInt("dpi"));
Log.d(TAG, "Managed config, label size unit: " + printer.getString("unit"));
Log.d(TAG, "Managed config, width: " + printer.getString("width"));
Log.d(TAG, "Managed config, height: " + printer.getString("height"));

// Create a Database Printer object
PrinterDatabase.Printer dbPrinter = new PrinterDatabase.Printer();
dbPrinter.mName = printer.getString("name");
dbPrinter.mAddress = printer.getString("address");
dbPrinter.mDescription = printer.getString("description");
dbPrinter.mType = printer.getString("type");
dbPrinter.mAddress = printer.getString("address");
if(dbPrinter.mType.equalsIgnoreCase("network"))
{
// Fill the port only for networked printers
dbPrinter.mPort = printer.getInt("port");
}
else
{
// Leave to zero for non networked printers
dbPrinter.mPort = 0;
}
dbPrinter.mDPI = printer.getInt("dpi");
String labelSizeUnit = printer.getString("unit");

// Retrieve width and height as float values
float fWidth = Float.parseFloat(printer.getString("width"));
float fHeight = Float.parseFloat(printer.getString("height"));

if(labelSizeUnit.equalsIgnoreCase("inches"))
{
dbPrinter.mWidth = (int)(fWidth * dbPrinter.mDPI);
dbPrinter.mHeight = (int)(fHeight * dbPrinter.mDPI);
}
else
{
dbPrinter.mWidth = (int)(fWidth * 0.393701f * dbPrinter.mDPI);
dbPrinter.mHeight = (int)(fHeight * 0.393701f * dbPrinter.mDPI);
}

switch(dbPrinter.mType)
{
case "network":
dbPrinter.mPrinterId = "tcp:" + dbPrinter.mAddress;
break;
case "bt":
dbPrinter.mPrinterId = "bt:" + dbPrinter.mAddress;
break;
case "usb":
dbPrinter.mPrinterId = "usb:" + dbPrinter.mName;
break;
}
dbPrinter.mPrinter = dbPrinter.mName;
dbPrinter.mTimeStamp = new Date();

// Add the printer to the database
mDb.insertPrinter(dbPrinter);

// Add information to feedback channel data message.
fcDataPrinterInfo += "(Printer " + (i+1) + "/" + printers_lists.length + "||"
+ "||Name:" + dbPrinter.mName
+ "||Description:" + dbPrinter.mDescription
+ "||Type:" + dbPrinter.mType
+ "||Address:" + dbPrinter.mAddress
+ ") ";
}
mDb.close();
// Report success to feedback channel
states.add(KeyedAppState.builder()
.setKey("ADDPRINTER")
.setSeverity(KeyedAppState.SEVERITY_INFO)
.setMessage(printers_lists.length + " printer(s) added successfully to the database")
.setData(fcDataPrinterInfo)
.build());
}
else
{
// No printers found in the list
states.add(KeyedAppState.builder()
.setKey("ADDPRINTER")
.setSeverity(KeyedAppState.SEVERITY_ERROR)
.setMessage("Unable to retrieve the printers definition from the managed configuration data. Check logcat for more information.")
.build());
}
// Send states to feedback channel
if(states.size() > 0)
{
Log.d(TAG, "Sending state to feedback channel");
reporter.setStates(states, new KeyedAppStatesCallback() {
@Override
public void onResult(int state, @Nullable Throwable throwable) {
Log.d(TAG, "reporter.setStates result: " + state);
if(throwable != null)
{
Log.d(TAG, "Error: " + throwable.getMessage());
}
}
});
}
}
else
{
// No app restriction bundle found
// Do nothing
Log.d(TAG, "No managed configuration bundle found.");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.zebra.zebraprintservice.connection.ZebraUsbPrinter;
import com.zebra.zebraprintservice.database.PrinterDatabase;
import com.zebra.zebraprintservice.managedconfiguration.APP_Restrictions_Changed_BroadcastReceiver;
import com.zebra.zebraprintservice.managedconfiguration.ManagedConfigurationHelper;

import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void onCreate()
{
if (DEBUG) Log.d(TAG, "onCreate() ");
super.onCreate();
readConfigFromManagedConfiguration();
ManagedConfigurationHelper.readConfigFromManagedConfiguration(this);
}

/**********************************************************************************************/
Expand Down Expand Up @@ -145,98 +146,6 @@ public ZebraPrinter getPrinter(PrinterId printerId, ZebraDiscoverySession sessio
return result;
}

/**********************************************************************************************/
private void readConfigFromManagedConfiguration() {
Log.d(TAG, "Managed config");
RestrictionsManager myRestrictionsMgr =
(RestrictionsManager) getApplicationContext().getSystemService(Context.RESTRICTIONS_SERVICE);
Bundle appRestrictions = myRestrictionsMgr.getApplicationRestrictions();
Log.d(TAG, "Managed config" + appRestrictions.toString());

Parcelable[] printers_lists = appRestrictions.getParcelableArray("printers_list");
if(printers_lists != null && printers_lists.length > 0)
{
// The managed config contains a printer list
// Get the database object
PrinterDatabase mDb = new PrinterDatabase(this);

// Remove all printers from database
mDb.deleteAll();

for(int i = 0; i < printers_lists.length; i++)
{
Bundle printer = (Bundle)printers_lists[i];
// Log some data for debugging purposes
Log.d(TAG, "********************************************************************");
Log.d(TAG, "Managed config: Printer description (" + i + "/" + printers_lists.length +")");
Log.d(TAG, "Managed config, name: " + printer.getString("name"));
Log.d(TAG, "Managed config, address: " + printer.getString("address"));
Log.d(TAG, "Managed config, description: " + printer.getString("description"));
Log.d(TAG, "Managed config, type: " + printer.getString("type"));
Log.d(TAG, "Managed config, address: " + printer.getString("address"));
Log.d(TAG, "Managed config, port: " + printer.getInt("port"));
Log.d(TAG, "Managed config, dpi: " + printer.getInt("dpi"));
Log.d(TAG, "Managed config, label size unit: " + printer.getString("unit"));
Log.d(TAG, "Managed config, width: " + printer.getString("width"));
Log.d(TAG, "Managed config, height: " + printer.getString("height"));

// Create a Database Printer object
PrinterDatabase.Printer dbPrinter = new PrinterDatabase.Printer();
dbPrinter.mName = printer.getString("name");
dbPrinter.mAddress = printer.getString("address");
dbPrinter.mDescription = printer.getString("description");
dbPrinter.mType = printer.getString("type");
dbPrinter.mAddress = printer.getString("address");
if(dbPrinter.mType.equalsIgnoreCase("network"))
{
// Fill the port only for networked printers
dbPrinter.mPort = printer.getInt("port");
}
else
{
// Leave to zero for non networked printers
dbPrinter.mPort = 0;
}
dbPrinter.mDPI = printer.getInt("dpi");
String labelSizeUnit = printer.getString("unit");

// Retrieve width and height as float values
float fWidth = Float.parseFloat(printer.getString("width"));
float fHeight = Float.parseFloat(printer.getString("height"));

if(labelSizeUnit.equalsIgnoreCase("inches"))
{
dbPrinter.mWidth = (int)(fWidth * dbPrinter.mDPI);
dbPrinter.mHeight = (int)(fHeight * dbPrinter.mDPI);
}
else
{
dbPrinter.mWidth = (int)(fWidth * 0.393701f * dbPrinter.mDPI);
dbPrinter.mHeight = (int)(fHeight * 0.393701f * dbPrinter.mDPI);
}

switch(dbPrinter.mType)
{
case "network":
dbPrinter.mPrinterId = "tcp:" + dbPrinter.mAddress;
break;
case "bt":
dbPrinter.mPrinterId = "bt:" + dbPrinter.mAddress;
break;
case "usb":
dbPrinter.mPrinterId = "usb:" + dbPrinter.mName;
break;
}
dbPrinter.mPrinter = dbPrinter.mName;
dbPrinter.mTimeStamp = new Date();

// Add the printer to the database
mDb.insertPrinter(dbPrinter);
}
mDb.close();
}
}

private void registerManagedConfigurationChangeBroadcastReceiver()
{
IntentFilter restrictionFilter = new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
Expand Down

0 comments on commit 41debfd

Please sign in to comment.