删 删 删
This commit is contained in:
parent
1a45602b31
commit
4b52c32260
@ -13,10 +13,6 @@
|
||||
|
||||
<source-file src="src/android/cordova/plugin/jcprinter/JcPrinter.java"
|
||||
target-dir="cordova/plugin/jcprinter"/>
|
||||
<source-file src="src/android/cordova/plugin/jcprinter/Util.java"
|
||||
target-dir="cordova/plugin/jcprinter"/>
|
||||
<source-file src="src/android/cordova/plugin/jcprinter/MyApplication.java"
|
||||
target-dir="cordova/plugin/jcprinter"/>
|
||||
|
||||
<lib-file src="src/android/3.1.8-release.aar" />
|
||||
<lib-file src="src/android/image-2.0.10-release.aar" />
|
||||
|
@ -3,36 +3,30 @@ package cordova.plugin.jcprinter;
|
||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import com.gengcon.www.jcprintersdk.bean.ImageDataInfo;
|
||||
import com.gengcon.www.jcprintersdk.JCPrintApi;
|
||||
import com.gengcon.www.jcprintersdk.callback.Callback;
|
||||
import com.gengcon.www.jcprintersdk.callback.PrintCallback;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -42,42 +36,41 @@ public class JcPrinter extends CordovaPlugin {
|
||||
|
||||
private static final String TAG = "JcPrinter";
|
||||
|
||||
private Method[] methods = Util.getInstance().getClass().getDeclaredMethods();
|
||||
private static JCPrintApi api;
|
||||
|
||||
/**
|
||||
* 页打印份数
|
||||
*/
|
||||
private int quantity;
|
||||
/**
|
||||
* 是否打印错误
|
||||
*/
|
||||
private boolean isError;
|
||||
/**
|
||||
* 是否取消打印
|
||||
*/
|
||||
private boolean isCancel;
|
||||
private final Method[] methods = JCPrintApi.class.getDeclaredMethods();
|
||||
|
||||
/**
|
||||
* 打印模式
|
||||
*/
|
||||
private int printMode;
|
||||
|
||||
/**
|
||||
* 打印浓度
|
||||
*/
|
||||
private int printDensity;
|
||||
|
||||
/**
|
||||
* 打印倍率(分辨率)
|
||||
*/
|
||||
private Float printMultiple;
|
||||
|
||||
private final static int START_PRINT = 1;
|
||||
|
||||
|
||||
@Override
|
||||
protected void pluginInitialize() {
|
||||
super.pluginInitialize();
|
||||
if(api == null){
|
||||
api = JCPrintApi.getInstance(CALLBACK);
|
||||
api.init(cordova.getActivity().getApplication());
|
||||
api.initImageProcessingDefault("", "");
|
||||
}
|
||||
}
|
||||
|
||||
public JCPrintApi getInstance() {
|
||||
if (api == null) {
|
||||
this.pluginInitialize();
|
||||
}
|
||||
|
||||
return api;
|
||||
|
||||
}
|
||||
|
||||
public int openPrinter(String address) {
|
||||
return getInstance().openPrinterByAddress(address);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
getInstance().close();
|
||||
}
|
||||
|
||||
public int isConnection() {
|
||||
return getInstance().isConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,7 +95,6 @@ public class JcPrinter extends CordovaPlugin {
|
||||
String methodName = args.optString(0);
|
||||
Log.d(TAG,"action-method:"+methodName);
|
||||
for(Method m:this.methods){
|
||||
String name = m.getName();
|
||||
if(m.getName().equals(methodName)){
|
||||
try {
|
||||
JSONArray methodJsonArgs = args.getJSONArray(1);
|
||||
@ -110,7 +102,7 @@ public class JcPrinter extends CordovaPlugin {
|
||||
for(int i=0;i<methodJsonArgs.length();i++){
|
||||
Object obj = methodJsonArgs.opt(i);
|
||||
if(obj instanceof JSONArray){
|
||||
Class a = m.getParameterTypes()[i].getComponentType();
|
||||
Class<?> a = m.getParameterTypes()[i].getComponentType();
|
||||
Object array = Array.newInstance(a,((JSONArray) obj).length());
|
||||
for(int j=0;j<((JSONArray) obj).length();j++){
|
||||
Array.set(array,j,((JSONArray) obj).opt(j));
|
||||
@ -120,7 +112,7 @@ public class JcPrinter extends CordovaPlugin {
|
||||
methodArgs[i] = obj;
|
||||
}
|
||||
}
|
||||
m.invoke(Util.getInstance(),methodArgs);
|
||||
m.invoke(getInstance(),methodArgs);
|
||||
callbackContext.success();
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
callbackContext.error(e.getMessage());
|
||||
@ -131,7 +123,7 @@ public class JcPrinter extends CordovaPlugin {
|
||||
|
||||
|
||||
case "generateLabelJson":
|
||||
callbackContext.success(new String(Util.getInstance().generateLabelJson()));
|
||||
callbackContext.success(new String(getInstance().generateLabelJson()));
|
||||
return true;
|
||||
}
|
||||
}catch (Exception e){
|
||||
@ -141,6 +133,13 @@ public class JcPrinter extends CordovaPlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
//重置错误状态变量
|
||||
private boolean isError = false;
|
||||
//重置取消打印状态变量
|
||||
private boolean isCancel = false;
|
||||
|
||||
private int generatedPrintDataPageCount = 0;
|
||||
|
||||
/**
|
||||
* 打印标签
|
||||
*
|
||||
@ -148,34 +147,37 @@ public class JcPrinter extends CordovaPlugin {
|
||||
private void print(JSONArray args, CallbackContext callbackContext) {
|
||||
JSONArray data = args.optJSONArray(0);
|
||||
String infoStr = args.optString(1);
|
||||
List<String> info = new ArrayList<>();
|
||||
info.add(infoStr);
|
||||
//重置错误状态变量
|
||||
isError = false;
|
||||
//重置取消打印状态变量
|
||||
isCancel = false;
|
||||
JSONObject cfg = args.optJSONObject(2);
|
||||
int printDensity = cfg.optInt("printDensity",8);
|
||||
int printMode = cfg.optInt("printMode",2);
|
||||
int paperType = cfg.optInt("paperType",2);
|
||||
int quantity = cfg.optInt("quantity",1);
|
||||
generatedPrintDataPageCount = 0;
|
||||
//总打印份数
|
||||
|
||||
final int[] generatedPrintDataPageCount = {0};
|
||||
int pageCount = data.length();
|
||||
quantity = 1;
|
||||
int totalQuantity = pageCount * quantity;
|
||||
//总打印份数,表示所有页面的打印份数之和。例如,如果你有3页需要打印,第一页打印3份,第二页打印2份,第三页打印5份,那么count的值应为10(3+2+5)。
|
||||
Util.getInstance().setTotalQuantityOfPrints(totalQuantity);
|
||||
printDensity = 8;
|
||||
printMode = 2;
|
||||
getInstance().setTotalQuantityOfPrints(totalQuantity);
|
||||
Log.d(TAG, "测试:参数设置-打印浓度: " + printDensity + ",打印模式:" + printMode);
|
||||
|
||||
List<String>pageData = new ArrayList<>();
|
||||
List<String>pageInfo = new ArrayList<>();
|
||||
for(int i=0;i<data.length();i++){
|
||||
pageData.add(data.optString(i));
|
||||
pageInfo.add(infoStr);
|
||||
}
|
||||
|
||||
/*
|
||||
* 参数1:打印浓度 ,参数2:纸张类型 参数3:打印模式
|
||||
* 打印浓度 B50/B50W/T6/T7/T8 建议设置6或8,Z401/B32建议设置8,B3S/B21/B203/B1建议设置3
|
||||
*/
|
||||
Util.getInstance().startPrintJob(printDensity, 2, printMode, new PrintCallback() {
|
||||
getInstance().startPrintJob(printDensity, paperType, printMode, new PrintCallback() {
|
||||
@Override
|
||||
public void onProgress(int pageIndex, int quantityIndex, HashMap<String, Object> hashMap) {
|
||||
Log.d(TAG, "测试:打印进度:已打印到第" + pageIndex + "页,第" + quantityIndex + "份");
|
||||
//打印进度回调
|
||||
if (pageIndex == pageCount && quantityIndex == quantity) {
|
||||
if (Util.getInstance().endJob()) {
|
||||
if (getInstance().endJob()) {
|
||||
Log.d(TAG, "结束打印成功");
|
||||
callbackContext.success("结束打印成功");
|
||||
} else {
|
||||
@ -317,69 +319,77 @@ public class JcPrinter extends CordovaPlugin {
|
||||
if (pageIndex > pageCount) {
|
||||
return;
|
||||
}
|
||||
List<String>pageData = new ArrayList<>();
|
||||
String dataStr= data.optString(pageIndex-1);
|
||||
pageData.add(dataStr);
|
||||
Util.getInstance().commitData(pageData,info);
|
||||
|
||||
if(generatedPrintDataPageCount < pageCount){
|
||||
if(pageCount - generatedPrintDataPageCount <bufferSize){
|
||||
getInstance().commitData(pageData.subList(generatedPrintDataPageCount,pageCount),pageInfo.subList(generatedPrintDataPageCount,pageCount));
|
||||
generatedPrintDataPageCount += pageCount;
|
||||
} else {
|
||||
getInstance().commitData(
|
||||
pageData.subList(generatedPrintDataPageCount,
|
||||
generatedPrintDataPageCount + bufferSize),
|
||||
pageInfo.subList(generatedPrintDataPageCount,
|
||||
generatedPrintDataPageCount + bufferSize));
|
||||
generatedPrintDataPageCount += bufferSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void isConnection(CallbackContext callbackContext) throws JSONException {
|
||||
int connectFlag = Util.isConnection();
|
||||
private void isConnection(CallbackContext callbackContext) {
|
||||
int connectFlag = isConnection();
|
||||
cordova.getActivity().runOnUiThread(() -> {
|
||||
String hint = "";
|
||||
switch (connectFlag) {
|
||||
case 0:
|
||||
hint = "已连接";
|
||||
callbackContext.success(hint);
|
||||
callbackContext.success(this.msg(connectFlag,"已连接"));
|
||||
break;
|
||||
case -1:
|
||||
hint = "未连接";
|
||||
callbackContext.error(hint);
|
||||
callbackContext.error(this.msg(connectFlag,"未连接"));
|
||||
break;
|
||||
case -3:
|
||||
hint = "不支持的机型";
|
||||
callbackContext.error(hint);
|
||||
callbackContext.error(this.msg(connectFlag,"不支持的机型"));
|
||||
break;
|
||||
default:
|
||||
callbackContext.error(this.msg(connectFlag,"未知状态"));
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private JSONObject msg(int code,String msg) {
|
||||
try {
|
||||
return new JSONObject().put("code",code).put("msg",msg);
|
||||
}catch (JSONException e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
JSONObject obj = args.getJSONObject(0);
|
||||
String address = obj.getString("address");
|
||||
int connectResult = Util.openPrinter(address);
|
||||
int connectFlag = openPrinter(address);
|
||||
cordova.getActivity().runOnUiThread(() -> {
|
||||
String hint = "";
|
||||
switch (connectResult) {
|
||||
switch (connectFlag) {
|
||||
case 0:
|
||||
hint = "连接成功";
|
||||
callbackContext.success(hint);
|
||||
callbackContext.success(this.msg(connectFlag,"连接成功"));
|
||||
break;
|
||||
case -1:
|
||||
hint = "连接失败";
|
||||
callbackContext.error(hint);
|
||||
callbackContext.error(this.msg(connectFlag,"连接失败"));
|
||||
break;
|
||||
case -2:
|
||||
hint = "不支持的机型";
|
||||
callbackContext.error(hint);
|
||||
case -3:
|
||||
callbackContext.error(this.msg(connectFlag,"不支持的机型"));
|
||||
break;
|
||||
default:
|
||||
callbackContext.error(this.msg(connectFlag,"未知状态"));
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/***
|
||||
* Find Zebra printers we can connect to
|
||||
* @return
|
||||
*/
|
||||
@SuppressLint("MissingPermission")
|
||||
private JSONArray getList(CallbackContext callbackContext) {
|
||||
private void getList(CallbackContext callbackContext) {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
int blueTooth_permission = ContextCompat.checkSelfPermission(cordova.getContext(), Manifest.permission.BLUETOOTH);
|
||||
@ -424,8 +434,53 @@ public class JcPrinter extends CordovaPlugin {
|
||||
}
|
||||
Log.d(TAG,"printers = " + printers);
|
||||
callbackContext.success(printers);
|
||||
return printers;
|
||||
}
|
||||
|
||||
|
||||
private static final Callback CALLBACK = new Callback() {
|
||||
@Override
|
||||
public void onConnectSuccess(String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisConnect() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onElectricityChange(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCoverStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaperStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRfidReadStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrinterIsFree(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeartDisConnect() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirmErrors() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
package cordova.plugin.jcprinter;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* 自定义Application
|
||||
*
|
||||
* @author zhangbin
|
||||
*/
|
||||
public class MyApplication extends Application {
|
||||
private static MyApplication app;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
app = this;
|
||||
}
|
||||
|
||||
public static MyApplication getInstance() {
|
||||
return app;
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
package cordova.plugin.jcprinter;
|
||||
|
||||
import com.gengcon.www.jcprintersdk.JCPrintApi;
|
||||
import com.gengcon.www.jcprintersdk.callback.Callback;
|
||||
import cordova.plugin.jcprinter.MyApplication;
|
||||
|
||||
public class Util {
|
||||
|
||||
|
||||
private static final Callback CALLBACK = new Callback() {
|
||||
@Override
|
||||
public void onConnectSuccess(String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisConnect() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onElectricityChange(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCoverStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaperStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRfidReadStatus(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrinterIsFree(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeartDisConnect() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirmErrors() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private static JCPrintApi api;
|
||||
|
||||
|
||||
public static JCPrintApi getInstance() {
|
||||
if (api == null) {
|
||||
api = JCPrintApi.getInstance(CALLBACK);
|
||||
api.init(MyApplication.getInstance());
|
||||
api.initImageProcessingDefault("", "");
|
||||
|
||||
|
||||
// Log.d(TAG, "api.init"+api.init(MyApplication.getInstance()));
|
||||
// Log.d(TAG, "api.initImageProcessingDefault"+ api.initImageProcessingDefault("", ""));
|
||||
}
|
||||
|
||||
return api;
|
||||
|
||||
}
|
||||
|
||||
private static final String TAG = "PrintUtil";
|
||||
|
||||
public static int openPrinter(String address) {
|
||||
getInstance();
|
||||
return api.openPrinterByAddress(address);
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
getInstance();
|
||||
api.close();
|
||||
}
|
||||
|
||||
public static int isConnection() {
|
||||
getInstance();
|
||||
return api.isConnection();
|
||||
}
|
||||
|
||||
}
|
@ -26,12 +26,12 @@ var exec = require('cordova/exec');
|
||||
* @param {*} success
|
||||
* @param {*} error
|
||||
*/
|
||||
exports.print = async function (data,info) {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'print', [data,info]));
|
||||
exports.print = async function (data,info,cfg) {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'print', [data,info,cfg||{}]));
|
||||
};
|
||||
|
||||
exports.getList = function (arg0) {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'getList', [arg0]));
|
||||
exports.getList = function () {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'getList', []));
|
||||
};
|
||||
|
||||
exports.connectPrinter = function (arg0) {
|
||||
@ -39,8 +39,8 @@ exports.connectPrinter = function (arg0) {
|
||||
}
|
||||
|
||||
|
||||
exports.isConnection = function (arg0) {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject,'JcPrinter', 'isConnection', [arg0]));
|
||||
exports.isConnection = function () {
|
||||
return new Promise((resolve, reject) => exec(resolve,reject,'JcPrinter', 'isConnection', []));
|
||||
}
|
||||
|
||||
exports.Instance = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user