Skip to content

Commit

Permalink
Initial work on adding a requester of USB permissions
Browse files Browse the repository at this point in the history
I was able to include the permission XML that allows any app to "respond" to a just-plugged printer, but......... not more than that.
Any Java code I included (even blatantly broken stuff) never caused an issue during compilation or runtime, not more than the current bug causes. No clue how to actually compile the plugin and get it into the debug app.

This is supposed to be a solution for auctifera-josed#58
  • Loading branch information
igorsantos07 committed Jun 6, 2022
1 parent aa835db commit 3d2cae2
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 50 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Cordova plugin for using [Star micronics printers](http://www.starmicronics.com/pages/All-Products) from a cordova, phonegap or Ionic application.

> This fork was made to solve [issue#58](https://github.com/auctifera-josed/starprnt/issues/58), where in some scenarios, Android apps would crash because of missing permissions.
## React Native Version ➜ [here](https://github.com/infoxicator/react-native-star-prnt)

**Note:** This is based on the work from the guys at [InteractiveObject](https://github.com/InteractiveObject/StarIOPlugin)
Expand Down
16 changes: 12 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-starprnt" version="2.1.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="cordova-plugin-starprnt" version="2.2.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>StarPRNT</name>
<description>Plugin to use Star printers and connected drawer</description>
<author>Jose Angarita / Ruben Casas</author>
<author>Jose Angarita / Ruben Casas / Igor Santos</author>
<keywords>print,starmicronics,star printer</keywords>
<license>MIT</license>
<engines>
Expand All @@ -23,9 +23,17 @@
</config-file>
<config-file target="AndroidManifest.xml" parent="/*" />
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" />
</config-file>
</platform>
<resource-file src="www/res/xml/device_filter.xml" target="app/src/main/res/xml/device_filter.xml" />
<resource-file src="www/res/xml/accessory_filter.xml" target="app/src/main/res/xml/accessory_filter.xml" />
</platform>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="StarPRNT">
Expand Down
2 changes: 1 addition & 1 deletion src/android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 112 additions & 45 deletions src/android/StarPRNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContentResolver;
import android.net.Uri;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.telephony.IccOpenLogicalChannelResponse;
import android.text.Layout;
Expand All @@ -52,17 +56,50 @@
import android.util.Base64;



/**
* This class echoes a string called from JavaScript.
*/
public class StarPRNT extends CordovaPlugin {


private CallbackContext _callbackContext = null;
String strInterface;
private StarIoExtManager starIoExtManager;

private static final String INTENT_ACTION_GRANT_USB = BuildConfig.APPLICATION_ID + ".GRANT_USB";
private enum UsbPermission { Unknown, Requested, Granted, Denied }
private UsbPermission usbPermission = UsbPermission.Unknown;
private final BroadcastReceiver broadcastReceiver;

public StarPRNT() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (INTENT_ACTION_GRANT_USB.equals(intent.getAction())) {
// synchronized (this) {
usbPermission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) ?
UsbPermission.Granted :
UsbPermission.Denied;

// UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// if(device != null){
// //call method to set up device communication
// }
// }
}
}
};
}

@Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB));
}

@Override
public void onPause() {
getActivity().unregisterReceiver(broadcastReceiver);
super.onPause();
}


/**
* Executes the request and returns PluginResult.
Expand All @@ -80,74 +117,99 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
String portSettings = getPortSettingsOption(portName, args.getString(1));
this.checkStatus(portName, portSettings, callbackContext);
return true;
}else if (action.equals("portDiscovery")) {
} else if (action.equals("portDiscovery")) {
String port = args.getString(0);
this.portDiscovery(port, callbackContext);
return true;
}else if (action.equals("printRasterReceipt")) {
} else if (action.equals("checkPermissions")) {
this.checkPermissions(callbackContext);
return true;
} else if (action.equals("printRasterReceipt")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
String printObj = args.getString(2);
this.printRasterReceipt(portName, portSettings, emulation, printObj, callbackContext);
return true;
}else if (action.equals("printBase64Image")) {
} else if (action.equals("printBase64Image")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
String printObj = args.getString(2);
this.printBase64Image(portName, portSettings, emulation, printObj, callbackContext);
return true;

}
else if (action.equals("printRawText")){
} else if (action.equals("printRawText")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
String printObj = args.getString(2);

this.printRawText(portName, portSettings, emulation, printObj, callbackContext);
return true;
}else if (action.equals("printRasterData")){
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
String printObj = args.getString(2);
} else if (action.equals("printRasterData")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
String printObj = args.getString(2);

try {
this.printRasterData(portName, portSettings, emulation, printObj, callbackContext);
} catch (IOException e) {
// e.printStackTrace();
// e.printStackTrace();
}
return true;
}else if (action.equals("print")){
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
JSONArray printCommands = args.getJSONArray(2);
this.print(portName, portSettings, emulation, printCommands, callbackContext);
return true;
}else if (action.equals("openCashDrawer")){
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
this.openCashDrawer(portName, portSettings, emulation, callbackContext);
return true;
} else if (action.equals("connect")){
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1)); //get port settings using emulation parameter
Boolean hasBarcodeReader = args.getBoolean(2);
_callbackContext = callbackContext;
this.connect(portName, portSettings, hasBarcodeReader, callbackContext);
return true;
}else if (action.equals("disconnect")){
this.disconnect(callbackContext);
return true;
} else if (action.equals("print")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
JSONArray printCommands = args.getJSONArray(2);
this.print(portName, portSettings, emulation, printCommands, callbackContext);
return true;
} else if (action.equals("openCashDrawer")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1));
Emulation emulation = getEmulation(args.getString(1));
this.openCashDrawer(portName, portSettings, emulation, callbackContext);
return true;
} else if (action.equals("connect")) {
String portName = args.getString(0);
String portSettings = getPortSettingsOption(portName, args.getString(1)); //get port settings using emulation parameter
Boolean hasBarcodeReader = args.getBoolean(2);
_callbackContext = callbackContext;
this.connect(portName, portSettings, hasBarcodeReader, callbackContext);
return true;
} else if (action.equals("disconnect")) {
this.disconnect(callbackContext);
return true;
}
return false;
}


public void requestPermission(CallbackContext callbackContext) {
UsbDevice device;
usbPermission = UsbPermission.Requested;
usbManager.requestPermission(device, permissionIntent);
// cordova.requestPermission(this, requestCode/*???*/, Manifest.permission.)
callbackContext.success("Permission requested");
}

public boolean checkPermissions(CallbackContext callbackContext) {
if (usbPermission == UsbPermission.Denied) {
callbackContext.error("denied");
return false;
} else if (usbPermission == UsbPermission.Unknown) {
callbackContext.error("unknown");
return false;
} else if (usbPermission == UsbPermission.Requested) {
callbackContext.error("requested");
return false;
}
callbackContext.success("granted");
}


public void checkStatus(String portName, String portSettings, CallbackContext callbackContext) {

final Context context = this.cordova.getActivity();
Expand Down Expand Up @@ -213,15 +275,17 @@ public void run() {

private void portDiscovery(String strInterface, CallbackContext callbackContext) {

Log.d(TAG, "port-disc");

final CallbackContext _callbackContext = callbackContext;
final String _strInterface = strInterface;

cordova.getThreadPool()
.execute(new Runnable() {
public void run() {
boolean shouldContinue = true;
JSONArray result = new JSONArray();
try {

if (_strInterface.equals("LAN")) {
result = getPortDiscovery("LAN");
} else if (_strInterface.equals("Bluetooth")) {
Expand All @@ -231,16 +295,19 @@ public void run() {
} else {
result = getPortDiscovery("All");
}

} catch (StarIOPortException exception) {
_callbackContext.error(exception.getMessage());

} catch (SecurityException exception) {
shouldContinue = false;
Log.d(TAG, "no-perms");
_callbackContext.error("No permission granted to access USB device");
} catch (JSONException e) {

} finally {

Log.d("Discovered ports", result.toString());
_callbackContext.success(result);
if (shouldContinue) {
Log.d("Discovered ports", result.toString());
_callbackContext.success(result);
}
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/ios/StarPRNT.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@property (nonatomic) StarIoExtManager *printerManager;

- (void)portDiscovery:(CDVInvokedUrlCommand *)command;
- (void)checkPermissions:(CDVInvokedUrlCommand *)command;
- (void)checkStatus:(CDVInvokedUrlCommand *)command;
- (void)printRawText:(CDVInvokedUrlCommand *)command;
- (void)printBase64Image:(CDVInvokedUrlCommand *)command;
Expand Down
11 changes: 11 additions & 0 deletions src/ios/StarPRNT.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ - (void)connect:(CDVInvokedUrlCommand *)command {
[self.commandDelegate sendPluginResult:result callbackId:dataCallbackId];
}];
}

//no need to ask for further permissions in iOS, only in Android - so this method is a no-op here
- (void)checkPermissions:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Success!"];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}];
}

- (void)checkStatus:(CDVInvokedUrlCommand *)command {
//NSLog(@"Checking status");
[self.commandDelegate runInBackground:^{
Expand Down Expand Up @@ -181,6 +190,7 @@ -(void)printRawText:(CDVInvokedUrlCommand *)command {
}
}];
}

-(void)printBase64Image:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{

Expand Down Expand Up @@ -239,6 +249,7 @@ -(void)printBase64Image:(CDVInvokedUrlCommand *)command {
}
}];
}

-(void)printRasterReceipt:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{

Expand Down
4 changes: 4 additions & 0 deletions www/StarPRNT.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module.exports = {
}, 'StarPRNT', 'connect', [printerPort, emulation, !!hasBarcodeReader]);
},

checkPermissions: function(success, error) {

},

// iOS only functions (Deprecated, use Super function print to access all the CommandBuilderInterface/ISCBBuilderInterface methods )

printReceipt: function (receipt, success, error, receiptId, alignment, international, font) {
Expand Down
4 changes: 4 additions & 0 deletions www/res/xml/accessory_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory model="Star TSP143IV-UE" manufacturer="STAR"/>
</resources>
21 changes: 21 additions & 0 deletions www/res/xml/device_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device class="255" subclass="66" protocol="1" />
<usb-device vendor-id="1305" product-id="0001" /> <!--IFBD-HU05/06, IFBD-HU07/08 - printerClass-->
<usb-device vendor-id="1305" product-id="0002" /> <!--IFBD-HU05/06, IFBD-HU07/08 - vendorClass-->
<usb-device vendor-id="1305" product-id="0003" /> <!--TSP100U/ECO/IIIU/IV - printerClass-->
<usb-device vendor-id="1305" product-id="0004" /> <!--TSP100U/ECO - vendorClass-->
<usb-device vendor-id="1305" product-id="0005" /> <!--TSP100GT/IIIU - printerClass-->
<usb-device vendor-id="1305" product-id="0006" /> <!--TSP100GT - vendorClass-->
<usb-device vendor-id="1305" product-id="0009" /> <!--FVP10 - printerClass-->
<usb-device vendor-id="1305" product-id="0010" /> <!--FVP10 - vendorClass-->
<usb-device vendor-id="1305" product-id="0011" /> <!--BSC10 - printerClass-->
<usb-device vendor-id="1305" product-id="0012" /> <!--BSC10 - vendorClass-->
<usb-device vendor-id="1305" product-id="0017" /> <!--BSC10BR - printerClass-->
<usb-device vendor-id="1305" product-id="0067" /> <!--SM-S210i/230i - mobile printer-->
<usb-device vendor-id="1305" product-id="0023" /> <!--mPOP - printerClass-->
<usb-device vendor-id="1305" product-id="0071" /> <!--mC-Print3 - printerClass-->
<usb-device vendor-id="1305" product-id="0073" /> <!--mC-Print2 - printerClass-->
<usb-device vendor-id="1305" product-id="0075" /> <!--SK1-211/221/V211 - printerClass-->
<usb-device vendor-id="1305" product-id="0077" /> <!--SK1-311/321/V311 - printerClass-->
</resources>

0 comments on commit 3d2cae2

Please sign in to comment.