Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android USB permissions #59

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**.idea/
**.idea/
.DS_Store
Binary file removed frameworks/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-starprnt",
"version": "2.2.0",
"version": "2.3.0",
"description": "StarPRNT Cordova Plugin for Star Micronics Bluetooth/LAN printers",
"types": "./types/index.d.ts",
"cordova": {
Expand Down Expand Up @@ -28,4 +28,4 @@
},
"author": "Jose Angarita / Ruben Casas"

}
}
18 changes: 15 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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.3.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>
Expand All @@ -23,9 +23,21 @@
</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" />
</config-file>
</platform>
<config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
<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>

<!-- app/src/main/ get prepended to target -->
<resource-file src="src/android/xml/device_filter.xml" target="res/xml/device_filter.xml" />
<resource-file src="src/android/xml/accessory_filter.xml" target="res/xml/accessory_filter.xml" />
</platform>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="StarPRNT">
Expand Down
Binary file removed src/.DS_Store
Binary file not shown.
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.

185 changes: 131 additions & 54 deletions src/android/StarPRNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;


import java.io.IOException;
Expand Down Expand Up @@ -33,16 +35,22 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.app.PendingIntent;
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.UsbDevice;
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 +60,74 @@
import android.util.Base64;



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

private static String TAG = "cordova.starprnt";
private static int STAR_VENDOR_ID = 1305;

private CordovaInterface cordova;
private CallbackContext _callbackContext = null;
String strInterface;
private String _action = null;
private JSONArray _args = null;

private StarIoExtManager starIoExtManager;

private String INTENT_ACTION_GRANT_USB;
private enum UsbPermission { Unknown, Requested, Granted, Denied }
private UsbPermission usbPermission = UsbPermission.Unknown;
private UsbDevice usbDevice;
private BroadcastReceiver broadcastReceiver;
private UsbManager usbManager;
private IntentFilter intentFilter;
private PendingIntent pendingIntent;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
this.cordova = cordova;
this.INTENT_ACTION_GRANT_USB = cordova.getActivity().getPackageName() + ".GRANT_USB";
this.broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast onreceive" + intent.toString());

if (INTENT_ACTION_GRANT_USB.equals(intent.getAction())) {
synchronized (this) {
usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
usbPermission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) ?
UsbPermission.Granted :
UsbPermission.Denied;
if (usbPermission == UsbPermission.Granted && usbDevice != null) {
try {
execute(_action, _args, _callbackContext);
} catch (JSONException e) {
_callbackContext.error(e.getMessage());
}
} else {
_callbackContext.error("No permssion granted to access USB device.");
}
}
}
}
};
this.usbManager = (UsbManager) cordova.getActivity().getSystemService(Context.USB_SERVICE);
this.pendingIntent = PendingIntent.getBroadcast(cordova.getContext(), 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
intentFilter = new IntentFilter(INTENT_ACTION_GRANT_USB);
cordova.getActivity().registerReceiver(this.broadcastReceiver, intentFilter);
}

@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
this.cordova.getActivity().registerReceiver(broadcastReceiver, intentFilter);
}

@Override
public void onPause(boolean multitasking) {
this.cordova.getActivity().unregisterReceiver(broadcastReceiver);
super.onPause(multitasking);
}


/**
* Executes the request and returns PluginResult.
Expand All @@ -74,80 +139,87 @@ public class StarPRNT extends CordovaPlugin {
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
for (UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getVendorId() == STAR_VENDOR_ID && !usbManager.hasPermission(device)) {
this._action = action;
this._args = args;
this._callbackContext = callbackContext;
usbManager.requestPermission(device, pendingIntent);
return true;
}
}

if (action.equals("checkStatus")) {
String portName = args.getString(0);
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("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 checkStatus(String portName, String portSettings, CallbackContext callbackContext) {

final Context context = this.cordova.getActivity();
Expand Down Expand Up @@ -213,15 +285,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 +305,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 Expand Up @@ -331,13 +408,13 @@ private String getPortSettingsOption(String portName, String emulation) { // gen

String portSettings = "";

if (emulation.equals("EscPosMobile")) portSettings += "mini";
else if (emulation.equals("EscPos")) portSettings += "escpos";
else //StarLine, StarGraphic, StarDotImpact
if (emulation.equals("StarPRNT") || emulation.equals("StarPRNTL")) {
portSettings += "Portable";
portSettings += ";l"; //retry on
}else portSettings += "";
if (emulation.equals("EscPosMobile")) portSettings += "mini";
else if (emulation.equals("EscPos")) portSettings += "escpos";
else //StarLine, StarGraphic, StarDotImpact
if (emulation.equals("StarPRNT") || emulation.equals("StarPRNTL")) {
portSettings += "Portable";
portSettings += ";l"; //retry on
} else portSettings += "";
return portSettings;
}

Expand Down
5 changes: 5 additions & 0 deletions src/android/xml/accessory_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://www.star-m.jp/products/s_print/sdk/starprnt_sdk/manual/android_java/en/configure_application.html#device-filter-xmlaccessory-filter-xml -->
<resources>
<usb-accessory model="Star TSP143IV-UE" manufacturer="STAR"/>
</resources>
22 changes: 22 additions & 0 deletions src/android/xml/device_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://www.star-m.jp/products/s_print/sdk/starprnt_sdk/manual/android_java/en/configure_application.html#device-filter-xmlaccessory-filter-xml -->
<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>