package com.spd.print; import android.content.ContentResolver; import android.content.ContentUris; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import com.alibaba.fastjson.JSONObject; import com.printer.sdk.Barcode; import com.printer.sdk.PrinterConstants; import com.printer.sdk.Table; import com.spd.print.jx.impl.PrintImpl; import com.spd.print.jx.inter.IConnectCallback; import com.spd.print.jx.utils.PictureUtils; import io.dcloud.feature.uniapp.annotation.UniJSMethod; import io.dcloud.feature.uniapp.common.UniModule; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /* loaded from: print-release.aar:classes.jar:com/spd/print/PrintModule.class */ public class PrintModule extends UniModule { private final String LOG_TAG = "PRINT"; private static PrintImpl sPrinterImpl; public static Boolean isConnection = false; public static String deviceName = ""; public static String deviceAddress = ""; public static String model; @UniJSMethod(uiThread = false) public JSONObject connectPrinter() { Log.d("PRINT", "==connectPrinter=="); final JSONObject data = new JSONObject(); final JSONObject send = new JSONObject(); if (sPrinterImpl == null) { sPrinterImpl = new PrintImpl(); } deviceName = "未连接"; deviceAddress = "未连接"; if (sPrinterImpl == null) { data.put("code", "999"); return data; } if (!isConnection.booleanValue()) { sPrinterImpl.connectPrinter(new IConnectCallback() { // from class: com.spd.print.PrintModule.1 public void onPrinterConnectSuccess() { PrintModule.isConnection = true; PrintModule.deviceName = "Serial device"; PrintModule.deviceAddress = "/dev/ttyMT0"; send.put("isConnection", "true"); send.put("deviceName", PrintModule.deviceName); send.put("deviceAddress", PrintModule.deviceAddress); send.put("result", "connect_success"); PrintModule.this.sendMessage(send, "initPrint"); data.put("code", "0"); } public void onPrinterConnectFailed(int errorCode) { PrintModule.isConnection = false; PrintModule.deviceName = "未连接"; PrintModule.deviceAddress = "未连接"; switch (errorCode) { case 103: send.put("isConnection", "false"); send.put("deviceName", PrintModule.deviceName); send.put("deviceAddress", PrintModule.deviceAddress); send.put("result", "connect_closed"); PrintModule.this.sendMessage(send, "initPrint"); return; default: send.put("isConnection", "false"); send.put("deviceName", PrintModule.deviceName); send.put("deviceAddress", PrintModule.deviceAddress); send.put("result", "connect_fail"); PrintModule.this.sendMessage(send, "initPrint"); return; } } }); } data.put("code", "0"); return data; } @UniJSMethod(uiThread = false) public JSONObject closeConnect() { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } sPrinterImpl.closeConnect(); isConnection = false; data.put("code", "0"); return data; } @UniJSMethod(uiThread = false) public JSONObject sendBytesData(String hexString) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int result = sPrinterImpl.sendBytesData(string2bytes(hexString)); data.put("code", String.valueOf(result)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject read() { JSONObject data = new JSONObject(); JSONObject send = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } byte[] readData = new byte[100]; try { int resultCode = sPrinterImpl.read(readData); Log.d("LOG_TAG", "read====" + Arrays.toString(readData)); if (resultCode > 0) { send.put("readData", Arrays.toString(readData)); sendMessage(send, "read"); } data.put("code", String.valueOf(resultCode)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject initPrint() { Log.d("PRINT", "==initPrint=="); JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.initPrinter(); data.put("code", "0"); } catch (RuntimeException e) { e.printStackTrace(); data.put("code", e.getCause().toString()); } return data; } @UniJSMethod(uiThread = false) public JSONObject setFont(int mCharacterType, int mWidth, int mHeight, int mBold, int mUnderline) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.setFont(mCharacterType, mWidth, mHeight, mBold, mUnderline); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setPrinter(int command, int value) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.setPrinter(command, value); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setPaperType(int paperType) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int resultCode = sPrinterImpl.setPaperType(paperType); data.put("code", String.valueOf(resultCode)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setDensity(int density) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int resultCode = sPrinterImpl.setDensity(density); data.put("code", String.valueOf(resultCode)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setPaperFeed(int line) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.setPaperFeed(line); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setPaperBack(int line) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.setPaperBack(line); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printSelfCheck() { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.printSelfCheck(); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printText(String text) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.printText(text); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printBarCode(Barcode barcode) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int resultCode = sPrinterImpl.printBarCode(barcode); data.put("code", String.valueOf(resultCode)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printBarCode(byte barcodeType, int param1, int param2, int param3, String content) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int resultCode = sPrinterImpl.printBarCode(barcodeType, param1, param2, param3, content); data.put("code", String.valueOf(resultCode)); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printImage(Bitmap bitmap, int alignType, int left, boolean isCompressed) { Log.d("PRINT", "printImage==bitmap"); JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.printImage(BitmapUtils.compressBitmapWidth(bitmap, 360), alignType, left, isCompressed); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printBigImage(Bitmap bitmap, int alignType, int left, boolean isCompressed) { Log.d("PRINT", "printBigImage=="); JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.printBigImage(bitmap, alignType, left, isCompressed); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printImage(String imgPath, int alignType, int left, boolean isCompressed) { Log.d("PRINT", "printImage==" + imgPath); JSONObject data = new JSONObject(); if ("".equals(imgPath) || imgPath == null) { data.put("code", "1"); return data; } BitmapFactory.Options bfoOptions = new BitmapFactory.Options(); bfoOptions.inScaled = false; File tempFile = new File(Uri.parse(imgPath).getPath()); String absolutePath = tempFile.getAbsolutePath(); Log.d("PRINT", "getAbsolutePath=" + absolutePath); Bitmap bitmap = BitmapFactory.decodeFile(absolutePath, bfoOptions); Matrix matrix = new Matrix(); matrix.setScale(1.0f, 1.0f); Log.d("PRINT", "==宽度==" + bitmap.getWidth() + "==高度==" + bitmap.getHeight()); Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); Log.d("PRINT", "==resize宽度==" + resizeBmp.getWidth() + "==resize高度==" + resizeBmp.getHeight()); if (alignType == 0) { PictureUtils.printBitmapImage(sPrinterImpl.getPrinter(), resizeBmp, PrinterConstants.PAlign.START, left, isCompressed); } else if (alignType == 1) { PictureUtils.printBitmapImage(sPrinterImpl.getPrinter(), resizeBmp, PrinterConstants.PAlign.CENTER, left, isCompressed); } else if (alignType == 2) { PictureUtils.printBitmapImage(sPrinterImpl.getPrinter(), resizeBmp, PrinterConstants.PAlign.END, left, isCompressed); } else if (alignType == 3) { PictureUtils.printBitmapImage(sPrinterImpl.getPrinter(), resizeBmp, PrinterConstants.PAlign.NONE, left, isCompressed); } deleteImage(Uri.parse(imgPath).getPath()); data.put("code", "0"); return data; } @UniJSMethod(uiThread = false) public JSONObject printTable(Table table) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.printTable(table); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject printTable(String column, String regularExpression, String columnWidth, ArrayList rows) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { String[] arrStr = columnWidth.split(","); int[] temp = new int[arrStr.length]; for (int i = 0; i < arrStr.length; i++) { temp[i] = Integer.parseInt(arrStr[i]); } sPrinterImpl.printTable(column, regularExpression, temp, rows); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject searchGap() { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.searchGap(); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject getPrinterStatus() { JSONObject data = new JSONObject(); JSONObject send = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { int resultCode = sPrinterImpl.getPrinterStatus(); send.put("code", String.valueOf(resultCode)); sendMessage(send, "getPrinterStatus"); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = false) public JSONObject setSensitivity(int sen) { JSONObject data = new JSONObject(); if (sPrinterImpl == null) { data.put("code", "999"); return data; } try { sPrinterImpl.setSensitivity(sen); data.put("code", "0"); } catch (RuntimeException e) { data.put("code", "-999"); } return data; } @UniJSMethod(uiThread = true) public void sendMessage(JSONObject msg, String tag) { Map params = new HashMap<>(); params.put("msg", msg); params.put("tag", tag); this.mUniSDKInstance.fireGlobalEventCallback("sendMessage", params); } public byte[] string2bytes(String content) { char[] charArray = content.toCharArray(); byte[] tempByte = new byte[512]; tempByte[0] = 52; int count = 0; for (int i = 0; i < charArray.length; i++) { if (charArray[i] == 'x') { int i2 = count; count++; tempByte[i2] = (byte) ((char2Int(charArray[i + 1]) * 16) + char2Int(charArray[i + 2])); } } byte[] retByte = new byte[count]; System.arraycopy(tempByte, 0, retByte, 0, count); return tempByte; } private int char2Int(char data) { if (data >= '0' && data <= '9') { data = (char) (data - '0'); } else if (data >= 'A' && data <= 'F') { data = (char) (data - '7'); } else if (data >= 'a' && data <= 'f') { data = (char) (data - 'W'); } return Integer.valueOf(data).intValue(); } private boolean deleteImage(String imgPath) { boolean result; ContentResolver resolver = this.mUniSDKInstance.getContext().getContentResolver(); Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id"}, "_data=?", new String[]{imgPath}, null); if (null != cursor && cursor.moveToFirst()) { long id = cursor.getLong(0); Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Uri uri = ContentUris.withAppendedId(contentUri, id); int count = this.mUniSDKInstance.getContext().getContentResolver().delete(uri, null, null); result = count == 1; } else { File file = new File(imgPath); result = file.delete(); } return result; } }