Skip to content

Commit

Permalink
Fixed some USB problems in printer discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrudu committed Sep 29, 2023
1 parent a86ec9e commit 4edb491
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 73 deletions.
6 changes: 4 additions & 2 deletions ZebraPrintService/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
compileSdk 34
applicationId "com.zebra.zebraprintservice"
minSdkVersion 30
versionCode 32
versionName "1.3.0"
versionCode 35
versionName "1.4.0"
externalNativeBuild {
cmake {
cppFlags "-std=c++14"
Expand Down Expand Up @@ -55,4 +55,6 @@ dependencies {
//implementation 'com.github.librepdf:openpdf:1.3.30'
implementation 'com.tom-roush:pdfbox-android:2.0.27.0'
implementation 'com.github.ltrudu:CriticalPermissionsHelper:+'
implementation 'com.github.ltrudu:UsbPopupRemovalHelper:V0.2.2'

}
13 changes: 11 additions & 2 deletions ZebraPrintService/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="com.symbol.emdk.permission.EMDK" />


<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" android:minSdkVersion="31"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:minSdkVersion="31"/>

Expand All @@ -14,6 +17,9 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
tools:ignore="ProtectedPermissions" />

<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
Expand All @@ -31,6 +37,10 @@
android:name="android.hardware.usb.host"
android:required="false" />

<queries>
<package android:name="com.symbol.emdk.emdkservice" />
</queries>

<application
android:allowBackup="true"
android:icon="@drawable/ic_printservice"
Expand Down Expand Up @@ -81,7 +91,6 @@
android:name=".activities.AddActivity"
android:autoRemoveFromRecents="true"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light"
android:label="@string/add_printer" />

<!-- Settings Activity -->
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.zebra.criticalpermissionshelper.CriticalPermissionsHelper;
import com.zebra.criticalpermissionshelper.EPermissionType;
import com.zebra.criticalpermissionshelper.IResultCallbacks;
import com.zebra.zebraprintservice.BuildConfig;
import com.zebra.zebraprintservice.database.PrinterDatabase;
import com.zebra.zebraprintservice.R;
import com.zebra.zebraprintservice.service.ZebraPrintService;
Expand All @@ -30,6 +31,8 @@

public class SettingsActivity extends Activity
{
private static final boolean DEBUG = BuildConfig.DEBUG & true;

private ZebraPrintService printService;
private TextView mVersion;
private TextView mServiceVersion;
Expand Down Expand Up @@ -139,6 +142,12 @@ public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item)
return true;
}

case R.id.addPrinters:
{
startActivity(new Intent(this, AddActivity.class));
return true;
}

case R.id.importPrinters:
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Expand All @@ -164,24 +173,30 @@ protected void onResume()
super.onResume();
String deviceManufacturer = android.os.Build.MANUFACTURER;
if(deviceManufacturer.contains("Zebra")||deviceManufacturer.contains("ZEBRA")) {
CriticalPermissionsHelper.grantPermission(this, EPermissionType.ALL_DANGEROUS_PERMISSIONS, new IResultCallbacks() {
@Override
public void onSuccess(String message, String resultXML) {
Log.d("CriticPermHelp", EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + " granted with success.");
checkAndroidPermissions();
}

@Override
public void onError(String message, String resultXML) {
Log.d("CriticPermHelp", "Error granting " + EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + " permission.\n" + message);
checkAndroidPermissions();
}
if(DEBUG) {
CriticalPermissionsHelper.grantPermission(this, EPermissionType.ALL_DANGEROUS_PERMISSIONS, new IResultCallbacks() {
@Override
public void onSuccess(String message, String resultXML) {
Log.d("CriticPermHelp", EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + " granted with success.");
checkAndroidPermissions();
}

@Override
public void onDebugStatus(String message) {
Log.d("CriticPermHelp", "Debug Grant Permission " + EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + ": " + message);
}
});
@Override
public void onError(String message, String resultXML) {
Log.d("CriticPermHelp", "Error granting " + EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + " permission.\n" + message);
checkAndroidPermissions();
}

@Override
public void onDebugStatus(String message) {
Log.d("CriticPermHelp", "Debug Grant Permission " + EPermissionType.ALL_DANGEROUS_PERMISSIONS.toString() + ": " + message);
}
});
}
else {
checkAndroidPermissions();
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public interface WriteDataCallback
}

public abstract boolean isAvailable();

public abstract void updateDeviceAvailability();

public abstract void connect(ConnectionCallback callback);
public abstract void disconnect();
public abstract void destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public boolean isAvailable()
return true;
}

@Override
public void updateDeviceAvailability() {
}

/*********************************************************************************************/
@Override
public void connect(ConnectionCallback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public boolean isAvailable()
return true;
}

@Override
public void updateDeviceAvailability() {

}

/*********************************************************************************************/
@Override
public void connect(ConnectionCallback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public boolean isAvailable()
{
return true;
}

@Override
public void updateDeviceAvailability() {

}

/*********************************************************************************************/
@Override
public void connect(ConnectionCallback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Handler;
import android.util.Log;

import com.zebra.usbpopupremovalhelper.EDeviceClass;
import com.zebra.usbpopupremovalhelper.IResultCallbacks;
import com.zebra.usbpopupremovalhelper.PackageManagementHelper;
import com.zebra.usbpopupremovalhelper.UsbPopupRemovalHelper;
import com.zebra.zebraprintservice.BuildConfig;
import com.zebra.zebraprintservice.database.PrinterDatabase;
import com.zebra.zebraprintservice.service.ZebraPrintService;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -56,6 +62,10 @@ public ZebraUsbPrinter(Context ctx, PrinterDatabase.Printer printer)

mCtx.registerReceiver(UsbReciever,new IntentFilter(ACTION_USB_PERMISSION));

updateDeviceAvailability();
}

public void updateDeviceAvailability() {
if (mUsbManager != null)
{
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Expand All @@ -67,7 +77,7 @@ public ZebraUsbPrinter(Context ctx, PrinterDatabase.Printer printer)
if (device.getInterface(0).getInterfaceClass() == 0x07)
{
if (DEBUG) Log.v(TAG, "Found USB Printer :" + device.getProductName() + " VID:" + device.getVendorId() + " PID:" + device.getProductId() + " -> " + deviceName);
if (printer.mAddress.equals(device.getDeviceName())) mDevice = device;
if (getPrinter().mAddress.equals(device.getDeviceName())) mDevice = device;
}
}
}
Expand Down Expand Up @@ -140,6 +150,8 @@ public void destroy()
@Override
public boolean isAvailable()
{
//return true;
updateDeviceAvailability();
return mDevice != null;
}
/*********************************************************************************************/
Expand All @@ -153,15 +165,8 @@ public void connect(final ConnectionCallback callback)
if (DEBUG) Log.d(TAG, "connect()");
if (!mUsbManager.hasPermission(mDevice))
{
mHandler.post(new Runnable()
{
@Override
public void run()
{
mUsbManager.requestPermission(mDevice, mPermissionIntent);
}
});
return;
autograntUSBPermissionIfPossible();
return;
}

if (DEBUG) Log.i(TAG,"Setting up USB");
Expand All @@ -187,6 +192,78 @@ public void run()
callback.onConnected();
}

private void autograntUSBPermissionIfPossible() {
mHandler.post(new Runnable()
{
@Override
public void run()
{
String deviceManufacturer = Build.MANUFACTURER;
if(ZebraPrintService.ZEBRA_EXTENSIONS && (deviceManufacturer.contains("Zebra")||deviceManufacturer.contains("ZEBRA")) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
{

String certificate = PackageManagementHelper.getSignature(mCtx, null);
if(certificate == null) {
if (DEBUG)
Log.d(TAG, "getSignature error, couldn't retrieve app certificate");
mUsbManager.requestPermission(mDevice, mPermissionIntent);
return;
}

String packageName = PackageManagementHelper.getPackageName(mCtx, null);
if(packageName == null) {
if (DEBUG)
Log.d(TAG, "getSignature error, couldn't retrieve app certificate");
mUsbManager.requestPermission(mDevice, mPermissionIntent);
return;
}

String controlRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<popupsuppress>\n" +
" <rule>\n" +
" <pkg>" + packageName + "</pkg>\n" +
" <cert>" + certificate + "</cert> \n" +
" <vid vendorid=\""+ mDevice.getVendorId() + "\">\n" +
" <pid>" + mDevice.getProductId() + "</pid></vid>\n" +
" </rule>\n" +
"</popupsuppress>\n" +
"\n" +
"<usbconfig mode=\"whitelist\">\n" +
" <class>USB_CLASS_PRINTER</class>\n" +
"</usbconfig>";
UsbPopupRemovalHelper.processRawControlRuleXML(mCtx, controlRule, new IResultCallbacks() {
@Override
public void onSuccess(String message, String ResultXML) {
if (DEBUG)
Log.d(TAG, "USBPermission success message: " + message + "\nResultXML: " + ResultXML);

}

@Override
public void onError(String message, String ResultXML) {
if (DEBUG)
Log.d(TAG, "USBPermission error message: " + message + "\nResultXML: " + ResultXML);
mUsbManager.requestPermission(mDevice, mPermissionIntent);
}

@Override
public void onDebugStatus(String s) {
if (DEBUG)
Log.d(TAG, "USBPermission: " + s);
}
});
}
else
{
if (DEBUG)
Log.d(TAG, "USBPermission: not a Zebra device");
mUsbManager.requestPermission(mDevice, mPermissionIntent);
}
}
});
}

/*********************************************************************************************/
@Override
public void disconnect()
Expand Down
Loading

0 comments on commit 4edb491

Please sign in to comment.