mirror of
https://github.com/inventree/inventree-app.git
synced 2026-08-18 17:26:28 +00:00
Add intent wedge scanner, MDM support for Android (#863)
Add Android intent wedge scanner support with auto configuration for supported Zebra and Datalogic devices. Intent wedge scans can be done without pressing the scan button first. Adds the ability for an MDM server to push settings on Android.
This commit is contained in:
@@ -94,4 +94,5 @@ dependencies {
|
||||
androidTestImplementation 'com.android.support:multidex:2.0.1'
|
||||
implementation "androidx.core:core:1.9.0"
|
||||
implementation 'androidx.appcompat:appcompat:1.6.0'
|
||||
compileOnly 'com.datalogic:datalogic-android-sdk:1.50'
|
||||
}
|
||||
|
||||
@@ -15,11 +15,22 @@
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<data android:scheme="https" />
|
||||
</intent>
|
||||
|
||||
<!-- For intent wedge detection -->
|
||||
<package android:name="com.symbol.datawedge" />
|
||||
<package android:name="com.datalogic.decode" />
|
||||
<package android:name="com.datalogic.decodewedge" />
|
||||
<package android:name="com.datalogic.systemsettings" />
|
||||
</queries>
|
||||
|
||||
<application
|
||||
android:label="InvenTree"
|
||||
android:icon="@mipmap/ic_launcher">
|
||||
|
||||
<uses-library
|
||||
android:name="com.datalogic.device"
|
||||
android:required="false" />
|
||||
|
||||
<meta-data
|
||||
android:name="flutterEmbedding"
|
||||
android:value="2" />
|
||||
|
||||
@@ -1,6 +1,172 @@
|
||||
package inventree.inventree_app;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.RestrictionsManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.flutter.embedding.android.FlutterActivity;
|
||||
import io.flutter.embedding.engine.FlutterEngine;
|
||||
import io.flutter.plugin.common.EventChannel;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
|
||||
public class MainActivity extends FlutterActivity {
|
||||
|
||||
static final String ACTION_CUSTOM_SCAN = "inventree.inventree_app.SCAN";
|
||||
|
||||
// Datalogic default wedge settings
|
||||
static final String ACTION_DATALOGIC_DECODE = "com.datalogic.decodewedge.decode_action";
|
||||
static final String CATEGORY_DATALOGIC_DECODE = "com.datalogic.decodewedge.decode_category";
|
||||
|
||||
private static final String EVENT_CHANNEL = "inventree/datawedge_scans";
|
||||
private static final String MANAGED_CONFIG_CHANNEL = "inventree/managed_config";
|
||||
private static final String WEDGE_CHANNEL = "inventree/wedge";
|
||||
|
||||
private EventChannel.EventSink eventSink;
|
||||
|
||||
private final BroadcastReceiver scanReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null) return;
|
||||
|
||||
String action = intent.getAction();
|
||||
if (action == null) return;
|
||||
|
||||
// Check if it matches one of the strings
|
||||
if (!ACTION_CUSTOM_SCAN.equals(action) && !ACTION_DATALOGIC_DECODE.equals(action)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String data = intent.getStringExtra("com.symbol.datawedge.data_string");
|
||||
if (data == null || data.isEmpty()) {
|
||||
data = intent.getStringExtra("com.datalogic.decode.intentwedge.barcode_string");
|
||||
}
|
||||
if (data == null || data.isEmpty()) return;
|
||||
if (eventSink == null) return;
|
||||
|
||||
Map<String, Object> payload = new HashMap<>();
|
||||
payload.put("data", data);
|
||||
payload.put("action", action);
|
||||
eventSink.success(payload);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Auto-configure the datawedge for Zebra/Datalogic
|
||||
WedgeConfigurator.configure(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine);
|
||||
|
||||
new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), EVENT_CHANNEL)
|
||||
.setStreamHandler(new EventChannel.StreamHandler() {
|
||||
@Override
|
||||
public void onListen(Object arguments, EventChannel.EventSink events) {
|
||||
eventSink = events;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(Object arguments) {
|
||||
eventSink = null;
|
||||
}
|
||||
});
|
||||
|
||||
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), MANAGED_CONFIG_CHANNEL)
|
||||
.setMethodCallHandler((call, result) -> {
|
||||
if ("getManagedConfiguration".equals(call.method)) {
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
|
||||
RestrictionsManager rm =
|
||||
(RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
|
||||
|
||||
if (rm != null) {
|
||||
Bundle restrictions = rm.getApplicationRestrictions();
|
||||
|
||||
if (restrictions != null) {
|
||||
for (String key : restrictions.keySet()) {
|
||||
@SuppressWarnings("deprecation")
|
||||
Object value = restrictions.get(key);
|
||||
|
||||
if (value instanceof Boolean
|
||||
|| value instanceof Integer
|
||||
|| value instanceof Long
|
||||
|| value instanceof Double
|
||||
|| value instanceof String) {
|
||||
config.put(key, value);
|
||||
} else if (value instanceof String[]) {
|
||||
config.put(key, java.util.Arrays.asList((String[]) value));
|
||||
} else if (value != null) {
|
||||
config.put(key, value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.success(config);
|
||||
} else {
|
||||
result.notImplemented();
|
||||
}
|
||||
});
|
||||
|
||||
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), WEDGE_CHANNEL)
|
||||
.setMethodCallHandler((call, result) -> {
|
||||
if ("detectWedge".equals(call.method)) {
|
||||
String vendor = WedgeConfigurator.detectVendor(this);
|
||||
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("vendor", vendor);
|
||||
info.put("supported", !WedgeConfigurator.VENDOR_NONE.equals(vendor));
|
||||
|
||||
result.success(info);
|
||||
} else {
|
||||
result.notImplemented();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
||||
eventSink = null;
|
||||
super.cleanUpFlutterEngine(flutterEngine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ACTION_CUSTOM_SCAN);
|
||||
filter.addAction(ACTION_DATALOGIC_DECODE);
|
||||
filter.addCategory(CATEGORY_DATALOGIC_DECODE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
registerReceiver(scanReceiver, filter, Context.RECEIVER_EXPORTED);
|
||||
} else {
|
||||
registerReceiver(scanReceiver, filter);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
try {
|
||||
unregisterReceiver(scanReceiver);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// Receiver was never registered
|
||||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
package inventree.inventree_app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.datalogic.decode.BarcodeManager;
|
||||
import com.datalogic.decode.configuration.IntentDeliveryMode;
|
||||
import com.datalogic.decode.configuration.ScannerProperties;
|
||||
import com.datalogic.device.ErrorManager;
|
||||
import com.datalogic.device.configuration.ConfigException;
|
||||
|
||||
public class WedgeConfigurator {
|
||||
|
||||
// Zebra specific settings
|
||||
private static final String DATAWEDGE_PROFILE = "InvenTree";
|
||||
public static final String ZEBRA_BROADCAST_INTENT = "2";
|
||||
private static final String DATAWEDGE_API_ACTION = "com.symbol.datawedge.api.ACTION";
|
||||
private static final String DATAWEDGE_SET_CONFIG = "com.symbol.datawedge.api.SET_CONFIG";
|
||||
|
||||
// Datalogic retry settings
|
||||
private static final int DATALOGIC_MAX_ATTEMPTS = 5;
|
||||
private static final long DATALOGIC_RETRY_DELAY_MS = 2000;
|
||||
|
||||
// Vendor identifiers
|
||||
public static final String VENDOR_ZEBRA = "zebra";
|
||||
public static final String VENDOR_DATALOGIC = "datalogic";
|
||||
public static final String VENDOR_NONE = "none";
|
||||
|
||||
private static final Map<String, String[]> VENDOR_PACKAGES = buildVendorPackages();
|
||||
|
||||
// Spawn a new thread for Datalogic calls
|
||||
private static final ExecutorService DATALOGIC_EXECUTOR = Executors.newSingleThreadExecutor();
|
||||
|
||||
private static Map<String, String[]> buildVendorPackages() {
|
||||
Map<String, String[]> map = new LinkedHashMap<>();
|
||||
|
||||
map.put(VENDOR_ZEBRA, new String[] {"com.symbol.datawedge"});
|
||||
|
||||
map.put(
|
||||
VENDOR_DATALOGIC,
|
||||
new String[] {
|
||||
"com.datalogic.decode",
|
||||
"com.datalogic.decodewedge",
|
||||
"com.datalogic.systemsettings"
|
||||
});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String detectVendor(Context context) {
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
if (pm == null) {
|
||||
return VENDOR_NONE;
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String[]> entry : VENDOR_PACKAGES.entrySet()) {
|
||||
for (String packageName : entry.getValue()) {
|
||||
if (isPackageInstalled(pm, packageName)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// PackageManager lookup failed
|
||||
}
|
||||
|
||||
return detectVendorByManufacturer();
|
||||
}
|
||||
|
||||
private static String detectVendorByManufacturer() {
|
||||
String manufacturer = Build.MANUFACTURER == null ? "" : Build.MANUFACTURER.toLowerCase();
|
||||
|
||||
if (manufacturer.contains("zebra") || manufacturer.contains("symbol")) {
|
||||
return VENDOR_ZEBRA;
|
||||
}
|
||||
|
||||
if (manufacturer.contains("datalogic")) {
|
||||
return VENDOR_DATALOGIC;
|
||||
}
|
||||
|
||||
return VENDOR_NONE;
|
||||
}
|
||||
|
||||
private static boolean isPackageInstalled(PackageManager pm, String packageName) {
|
||||
try {
|
||||
pm.getPackageInfo(packageName, 0);
|
||||
return true;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
} catch (Throwable e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void configure(Context context) {
|
||||
try {
|
||||
String vendor = detectVendor(context);
|
||||
|
||||
if (VENDOR_ZEBRA.equals(vendor)) {
|
||||
configureZebra(context);
|
||||
} else if (VENDOR_DATALOGIC.equals(vendor)) {
|
||||
scheduleDatalogicConfig(1);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// Vendor detection was unsuccessful
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule the Datalogic wedge configuration.
|
||||
* @param attempt Current attempt number
|
||||
*/
|
||||
private static void scheduleDatalogicConfig(int attempt) {
|
||||
new Handler(Looper.getMainLooper())
|
||||
.postDelayed(
|
||||
() -> DATALOGIC_EXECUTOR.execute(() -> {
|
||||
try {
|
||||
configureDatalogic();
|
||||
} catch (Throwable e) {
|
||||
if (attempt < DATALOGIC_MAX_ATTEMPTS) {
|
||||
scheduleDatalogicConfig(attempt + 1);
|
||||
}
|
||||
}
|
||||
}),
|
||||
DATALOGIC_RETRY_DELAY_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure Zebra datawedge by creating a custom profile for the app.
|
||||
* @param context MainActivity instance passed in as context
|
||||
*/
|
||||
private static void configureZebra(Context context) {
|
||||
Bundle profileConfig = new Bundle();
|
||||
profileConfig.putString("PROFILE_NAME", DATAWEDGE_PROFILE);
|
||||
profileConfig.putString("PROFILE_ENABLED", "true");
|
||||
profileConfig.putString("CONFIG_MODE", "CREATE_IF_NOT_EXIST");
|
||||
|
||||
Bundle appConfig = new Bundle();
|
||||
appConfig.putString("PACKAGE_NAME", context.getPackageName());
|
||||
appConfig.putStringArray("ACTIVITY_LIST", new String[] {"*"});
|
||||
profileConfig.putParcelableArray("APP_LIST", new Bundle[] {appConfig});
|
||||
|
||||
ArrayList<Bundle> plugins = new ArrayList<>();
|
||||
|
||||
Bundle barcodePlugin = new Bundle();
|
||||
barcodePlugin.putString("PLUGIN_NAME", "BARCODE");
|
||||
barcodePlugin.putString("RESET_CONFIG", "true");
|
||||
|
||||
Bundle barcodeProps = new Bundle();
|
||||
barcodeProps.putString("scanner_input_enabled", "true");
|
||||
barcodeProps.putString("scanner_selection", "auto");
|
||||
barcodePlugin.putBundle("PARAM_LIST", barcodeProps);
|
||||
plugins.add(barcodePlugin);
|
||||
|
||||
Bundle intentPlugin = new Bundle();
|
||||
intentPlugin.putString("PLUGIN_NAME", "INTENT");
|
||||
intentPlugin.putString("RESET_CONFIG", "true");
|
||||
|
||||
Bundle intentProps = new Bundle();
|
||||
intentProps.putString("intent_output_enabled", "true");
|
||||
intentProps.putString("intent_action", MainActivity.ACTION_CUSTOM_SCAN);
|
||||
intentProps.putString("intent_delivery", ZEBRA_BROADCAST_INTENT);
|
||||
intentPlugin.putBundle("PARAM_LIST", intentProps);
|
||||
plugins.add(intentPlugin);
|
||||
|
||||
Bundle keystrokePlugin = new Bundle();
|
||||
keystrokePlugin.putString("PLUGIN_NAME", "KEYSTROKE");
|
||||
keystrokePlugin.putString("RESET_CONFIG", "true");
|
||||
|
||||
Bundle keystrokeProps = new Bundle();
|
||||
keystrokeProps.putString("keystroke_output_enabled", "false");
|
||||
keystrokePlugin.putBundle("PARAM_LIST", keystrokeProps);
|
||||
plugins.add(keystrokePlugin);
|
||||
|
||||
profileConfig.putParcelableArrayList("PLUGIN_CONFIG", plugins);
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(DATAWEDGE_API_ACTION);
|
||||
intent.putExtra(DATAWEDGE_SET_CONFIG, profileConfig);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to apply Datalogic wedge configuration.
|
||||
*/
|
||||
private static void configureDatalogic() {
|
||||
ErrorManager.enableExceptions(false);
|
||||
|
||||
BarcodeManager manager = new BarcodeManager();
|
||||
|
||||
ScannerProperties configuration = ScannerProperties.edit(manager);
|
||||
|
||||
if (configuration == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
// Keyboard wedge only on focus of a text field
|
||||
configuration.keyboardWedge.onlyOnFocus.set(true);
|
||||
configuration.intentWedge.enable.set(true);
|
||||
// Default action, category
|
||||
configuration.intentWedge.action.set(MainActivity.ACTION_DATALOGIC_DECODE);
|
||||
configuration.intentWedge.category.set(MainActivity.CATEGORY_DATALOGIC_DECODE);
|
||||
// Enable broadcast
|
||||
configuration.intentWedge.deliveryMode.set(IntentDeliveryMode.BROADCAST);
|
||||
|
||||
int result = configuration.store(manager, true);
|
||||
|
||||
if (result != ConfigException.SUCCESS) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user