From 682b6f0446d9a5b77683f50a8738e8c5ebf420c9 Mon Sep 17 00:00:00 2001 From: Franck ALARY Date: Wed, 27 May 2020 13:09:46 +0200 Subject: [PATCH] Refactoring library Exception + improves the management of connection states --- .../dantsu/thermalprinter/MainActivity.java | 18 +++------ .../dantsu/escposprinter/EscPosPrinter.java | 16 ++++---- .../escposprinter/EscPosPrinterCommands.java | 25 +++++------- .../connection/DeviceConnection.java | 13 +++--- .../bluetooth/BluetoothConnection.java | 40 +++++++++++-------- .../BluetoothPrintersConnections.java | 8 +++- .../connection/tcp/TcpConnection.java | 20 ++++------ .../connection/usb/UsbConnection.java | 27 +++++-------- .../connection/usb/UsbOutputStream.java | 30 ++++++++------ .../usb/UsbPrintersConnections.java | 8 +++- .../EscPosBrokenConnectionException.java | 7 ---- .../exceptions/EscPosConnectionException.java | 7 ++++ 12 files changed, 108 insertions(+), 111 deletions(-) delete mode 100644 escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosBrokenConnectionException.java create mode 100644 escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosConnectionException.java diff --git a/app/src/main/java/com/dantsu/thermalprinter/MainActivity.java b/app/src/main/java/com/dantsu/thermalprinter/MainActivity.java index ed6c293..e37edd2 100644 --- a/app/src/main/java/com/dantsu/thermalprinter/MainActivity.java +++ b/app/src/main/java/com/dantsu/thermalprinter/MainActivity.java @@ -28,7 +28,7 @@ import com.dantsu.escposprinter.connection.usb.UsbConnection; import com.dantsu.escposprinter.connection.usb.UsbPrintersConnections; import com.dantsu.escposprinter.exceptions.EscPosBarcodeException; -import com.dantsu.escposprinter.exceptions.EscPosBrokenConnectionException; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import com.dantsu.escposprinter.exceptions.EscPosEncodingException; import com.dantsu.escposprinter.exceptions.EscPosParserException; import com.dantsu.escposprinter.textparser.PrinterTextParserImg; @@ -150,9 +150,9 @@ public void run() { ==============================================================================================*/ public void printIt(DeviceConnection printerConnection) { - EscPosPrinter printer = new EscPosPrinter(printerConnection, 203, 48f, 32); try { - /*printer + EscPosPrinter printer = new EscPosPrinter(printerConnection, 203, 48f, 32); + printer .printFormattedText( "[C]" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM)) + "\n" + "[L]\n" + @@ -180,20 +180,12 @@ public void printIt(DeviceConnection printerConnection) { "[L]\n" + "[C]831254784551\n" + "[C]http://www.developpeur-web.dantsu.com/" - );*/ - printer - .printFormattedText( - "[C]0825478455\n" + - "[C]1825478455\n" + - "[C]2825478455\n" + - "[C]3825478455\n" + - "[C]4825478455\n" ); - } catch (EscPosBrokenConnectionException e) { + } catch (EscPosConnectionException e) { e.printStackTrace(); new AlertDialog.Builder(this) .setTitle("Broken connection") - .setMessage("The connection to the printer is broken. Please try again.") + .setMessage(e.getMessage()) .show(); } catch (EscPosParserException e) { e.printStackTrace(); diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinter.java b/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinter.java index bb1cf76..9c15163 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinter.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinter.java @@ -4,7 +4,7 @@ import com.dantsu.escposprinter.connection.DeviceConnection; import com.dantsu.escposprinter.exceptions.EscPosBarcodeException; -import com.dantsu.escposprinter.exceptions.EscPosBrokenConnectionException; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import com.dantsu.escposprinter.exceptions.EscPosEncodingException; import com.dantsu.escposprinter.exceptions.EscPosParserException; import com.dantsu.escposprinter.textparser.PrinterTextParser; @@ -33,7 +33,7 @@ public class EscPosPrinter { * @param printingWidthMM Printing width in millimeters * @param nbrCharactersPerLine The maximum number of characters that can be printed on a line. */ - public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float printingWidthMM, int nbrCharactersPerLine) { + public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float printingWidthMM, int nbrCharactersPerLine) throws EscPosConnectionException { this(printerConnection != null ? new EscPosPrinterCommands(printerConnection) : null, printerDpi, printingWidthMM, nbrCharactersPerLine); } @@ -46,7 +46,7 @@ public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float p * @param nbrCharactersPerLine The maximum number of characters that can be printed on a line. * @param charsetEncoding Set the charset encoding. */ - public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float printingWidthMM, int nbrCharactersPerLine, EscPosCharsetEncoding charsetEncoding) { + public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float printingWidthMM, int nbrCharactersPerLine, EscPosCharsetEncoding charsetEncoding) throws EscPosConnectionException { this(printerConnection != null ? new EscPosPrinterCommands(printerConnection, charsetEncoding) : null, printerDpi, printingWidthMM, nbrCharactersPerLine); } @@ -58,9 +58,9 @@ public EscPosPrinter(DeviceConnection printerConnection, int printerDpi, float p * @param printingWidthMM Printing width in millimeters * @param nbrCharactersPerLine The maximum number of characters that can be printed on a line. */ - public EscPosPrinter(EscPosPrinterCommands printer, int printerDpi, float printingWidthMM, int nbrCharactersPerLine) { - if (printer != null && printer.connect()) { - this.printer = printer; + public EscPosPrinter(EscPosPrinterCommands printer, int printerDpi, float printingWidthMM, int nbrCharactersPerLine) throws EscPosConnectionException { + if (printer != null) { + this.printer = printer.connect(); } this.printerDpi = printerDpi; this.printingWidthMM = printingWidthMM; @@ -144,7 +144,7 @@ public int mmToPx(float mmSize) { * @param text Formatted text to be printed. * @return Fluent interface */ - public EscPosPrinter printFormattedText(String text) throws EscPosBrokenConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException { + public EscPosPrinter printFormattedText(String text) throws EscPosConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException { if (this.printer == null || this.nbrCharactersPerLine == 0) { return this; } @@ -181,7 +181,7 @@ public EscPosPrinter printFormattedText(String text) throws EscPosBrokenConnecti * @param text Formatted text to be printed. * @return Fluent interface */ - public EscPosPrinter printFormattedTextAndCut(String text) throws EscPosBrokenConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException { + public EscPosPrinter printFormattedTextAndCut(String text) throws EscPosConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException { if (this.printer == null || this.nbrCharactersPerLine == 0) { return this; } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinterCommands.java b/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinterCommands.java index 95ec038..12f45f6 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinterCommands.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/EscPosPrinterCommands.java @@ -7,8 +7,7 @@ import com.dantsu.escposprinter.barcode.Barcode; import com.dantsu.escposprinter.connection.DeviceConnection; -import com.dantsu.escposprinter.exceptions.EscPosBarcodeException; -import com.dantsu.escposprinter.exceptions.EscPosBrokenConnectionException; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import com.dantsu.escposprinter.exceptions.EscPosEncodingException; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; @@ -200,23 +199,17 @@ public EscPosPrinterCommands(DeviceConnection printerConnection, EscPosCharsetEn /** * Start socket connection and open stream with the device. - * - * @return return true if success */ - public boolean connect() { - if (this.printerConnection.isConnected()) { - return true; - } - return this.printerConnection.connect(); + public EscPosPrinterCommands connect() throws EscPosConnectionException { + this.printerConnection.connect(); + return this; } /** * Close the socket connection and stream with the device. - * - * @return return true if success */ - public boolean disconnect() { - return this.printerConnection.disconnect(); + public void disconnect() { + this.printerConnection.disconnect(); } /** @@ -403,7 +396,7 @@ public EscPosPrinterCommands printQRCode(int qrCodeType, String text, int size) * * @return Fluent interface */ - public EscPosPrinterCommands newLine() throws EscPosBrokenConnectionException { + public EscPosPrinterCommands newLine() throws EscPosConnectionException { return this.newLine(null); } @@ -413,7 +406,7 @@ public EscPosPrinterCommands newLine() throws EscPosBrokenConnectionException { * @param align Set the alignment of text and barcodes. Use EscPosPrinterCommands.TEXT_ALIGN_... constants * @return Fluent interface */ - public EscPosPrinterCommands newLine(byte[] align) throws EscPosBrokenConnectionException { + public EscPosPrinterCommands newLine(byte[] align) throws EscPosConnectionException { if (!this.printerConnection.isConnected()) { return this; } @@ -432,7 +425,7 @@ public EscPosPrinterCommands newLine(byte[] align) throws EscPosBrokenConnection * * @return Fluent interface */ - public EscPosPrinterCommands cutPaper() throws EscPosBrokenConnectionException { + public EscPosPrinterCommands cutPaper() throws EscPosConnectionException { if (!this.printerConnection.isConnected()) { return this; } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/DeviceConnection.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/DeviceConnection.java index 827be78..3e0483b 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/DeviceConnection.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/DeviceConnection.java @@ -1,6 +1,6 @@ package com.dantsu.escposprinter.connection; -import com.dantsu.escposprinter.exceptions.EscPosBrokenConnectionException; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import java.io.IOException; import java.io.OutputStream; @@ -14,8 +14,8 @@ public DeviceConnection() { this.data = new byte[0]; } - public abstract boolean connect(); - public abstract boolean disconnect(); + public abstract DeviceConnection connect() throws EscPosConnectionException; + public abstract DeviceConnection disconnect(); /** * Check if OutputStream is open. @@ -40,7 +40,10 @@ public void write(byte[] bytes) { /** * Send data to the device. */ - public void send() throws EscPosBrokenConnectionException { + public void send() throws EscPosConnectionException { + if(!this.isConnected()) { + throw new EscPosConnectionException("Unable to send data to device."); + } try { this.stream.write(this.data); this.stream.flush(); @@ -52,7 +55,7 @@ public void send() throws EscPosBrokenConnectionException { } } catch (IOException | InterruptedException e) { e.printStackTrace(); - throw new EscPosBrokenConnectionException(e.getMessage()); + throw new EscPosConnectionException(e.getMessage()); } } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothConnection.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothConnection.java index 079c91e..e4ed1e5 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothConnection.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothConnection.java @@ -5,6 +5,7 @@ import android.os.ParcelUuid; import com.dantsu.escposprinter.connection.DeviceConnection; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import java.io.IOException; @@ -33,19 +34,31 @@ public BluetoothDevice getDevice() { } /** - * Start socket connection with the bluetooth device. + * Check if OutputStream is open. * - * @return return true if success + * @return true if is connected + */ + @Override + public boolean isConnected() { + return this.socket != null && this.socket.isConnected() && super.isConnected(); + } + + /** + * Start socket connection with the bluetooth device. */ - public boolean connect() { + public BluetoothConnection connect() throws EscPosConnectionException { if(this.isConnected()) { - return true; + return this; + } + + if(this.device == null) { + throw new EscPosConnectionException("Bluetooth device is not connected."); } ParcelUuid[] uuid = this.device.getUuids(); if(uuid == null || uuid.length == 0) { - return false; + throw new EscPosConnectionException("Unable to find bluetooth device UUID."); } try { @@ -53,42 +66,37 @@ public boolean connect() { this.socket.connect(); this.stream = this.socket.getOutputStream(); this.data = new byte[0]; - return true; } catch (IOException e) { e.printStackTrace(); - this.disconnect(); this.socket = null; this.stream = null; + throw new EscPosConnectionException("Unable to connect to bluetooth device."); } - return false; + return this; } /** * Close the socket connection with the bluetooth device. - * - * @return return true if success */ - public boolean disconnect() { + public BluetoothConnection disconnect() { this.data = new byte[0]; if(this.stream != null) { try { this.stream.close(); - this.stream = null; } catch (IOException e) { e.printStackTrace(); - return false; } + this.stream = null; } if(this.socket != null) { try { this.socket.close(); - this.socket = null; } catch (IOException e) { e.printStackTrace(); - return false; } + this.socket = null; } - return true; + return this; } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothPrintersConnections.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothPrintersConnections.java index f354662..937eb18 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothPrintersConnections.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/bluetooth/BluetoothPrintersConnections.java @@ -3,6 +3,8 @@ import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; + public class BluetoothPrintersConnections extends BluetoothConnections { /** @@ -16,8 +18,10 @@ public static BluetoothConnection selectFirstPaired() { if (bluetoothPrinters != null && bluetoothPrinters.length > 0) { for (BluetoothConnection printer : bluetoothPrinters) { - if (printer.connect()) { - return printer; + try { + return printer.connect(); + } catch (EscPosConnectionException e) { + e.printStackTrace(); } } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/tcp/TcpConnection.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/tcp/TcpConnection.java index 1215227..e6dab6a 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/tcp/TcpConnection.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/tcp/TcpConnection.java @@ -1,6 +1,7 @@ package com.dantsu.escposprinter.connection.tcp; import com.dantsu.escposprinter.connection.DeviceConnection; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import java.io.IOException; import java.net.InetAddress; @@ -35,34 +36,29 @@ public boolean isConnected() { /** * Start socket connection with the TCP device. - * - * @return return true if success */ - public boolean connect() { + public TcpConnection connect() throws EscPosConnectionException { if (this.isConnected()) { - return true; + return this; } try { this.socket = new Socket(); this.socket.connect(new InetSocketAddress(InetAddress.getByName(this.address), this.port)); this.stream = this.socket.getOutputStream(); this.data = new byte[0]; - return true; } catch (IOException e) { e.printStackTrace(); - this.disconnect(); this.socket = null; this.stream = null; + throw new EscPosConnectionException("Unable to connect to TCP device."); } - return false; + return this; } /** * Close the socket connection with the TCP device. - * - * @return return true if success */ - public boolean disconnect() { + public TcpConnection disconnect() { this.data = new byte[0]; if (this.stream != null) { try { @@ -70,7 +66,6 @@ public boolean disconnect() { this.stream = null; } catch (IOException e) { e.printStackTrace(); - return false; } } if (this.socket != null) { @@ -79,10 +74,9 @@ public boolean disconnect() { this.socket = null; } catch (IOException e) { e.printStackTrace(); - return false; } } - return true; + return this; } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbConnection.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbConnection.java index dd045f1..6c4cb60 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbConnection.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbConnection.java @@ -4,7 +4,7 @@ import android.hardware.usb.UsbManager; import com.dantsu.escposprinter.connection.DeviceConnection; -import com.dantsu.escposprinter.exceptions.EscPosBrokenConnectionException; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; import java.io.IOException; @@ -36,54 +36,49 @@ public UsbDevice getDevice() { /** * Start socket connection with the usbDevice. - * - * @return return true if success */ - public boolean connect() { + public UsbConnection connect() throws EscPosConnectionException { if(this.isConnected()) { - return true; + return this; } + try { this.stream = new UsbOutputStream(this.usbManager, this.usbDevice); this.data = new byte[0]; - return true; } catch (IOException e) { e.printStackTrace(); - this.disconnect(); this.stream = null; + throw new EscPosConnectionException("Unable to connect to USB device."); } - return false; + return this; } /** * Close the socket connection with the usbDevice. - * - * @return return true if success */ - public boolean disconnect() { + public UsbConnection disconnect() { this.data = new byte[0]; if(this.isConnected()) { try { this.stream.close(); - this.stream = null; } catch (IOException e) { e.printStackTrace(); - return false; } + this.stream = null; } - return true; + return this; } /** * Send data to the device. */ - public void send() throws EscPosBrokenConnectionException { + public void send() throws EscPosConnectionException { try { this.stream.write(this.data); this.data = new byte[0]; } catch (IOException e) { e.printStackTrace(); - throw new EscPosBrokenConnectionException(e.getMessage()); + throw new EscPosConnectionException(e.getMessage()); } } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbOutputStream.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbOutputStream.java index 0379f9d..07d0525 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbOutputStream.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbOutputStream.java @@ -36,20 +36,24 @@ public void write(@NonNull byte[] bytes) throws IOException { @Override public void write(final @NonNull byte[] bytes, final int offset, final int length) throws IOException { - if (this.usbInterface != null && this.usbEndpoint != null && this.usbConnection != null) { - if (this.usbConnection.claimInterface(this.usbInterface, true)) { - ByteBuffer buffer = ByteBuffer.wrap(bytes); - UsbRequest usbRequest = new UsbRequest(); - try { - usbRequest.initialize(this.usbConnection, this.usbEndpoint); - if (!usbRequest.queue(buffer, bytes.length)) { - throw new IOException("Error queueing request."); - } - this.usbConnection.requestWait(); - } finally { - usbRequest.close(); - } + if (this.usbInterface == null || this.usbEndpoint == null || this.usbConnection == null) { + throw new IOException("Unable to connect to USB device."); + } + + if (!this.usbConnection.claimInterface(this.usbInterface, true)) { + throw new IOException("Error during claim USB interface."); + } + + ByteBuffer buffer = ByteBuffer.wrap(bytes); + UsbRequest usbRequest = new UsbRequest(); + try { + usbRequest.initialize(this.usbConnection, this.usbEndpoint); + if (!usbRequest.queue(buffer, bytes.length)) { + throw new IOException("Error queueing USB request."); } + this.usbConnection.requestWait(); + } finally { + usbRequest.close(); } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbPrintersConnections.java b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbPrintersConnections.java index 71b7e6e..446cc3e 100644 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbPrintersConnections.java +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/connection/usb/UsbPrintersConnections.java @@ -4,6 +4,8 @@ import android.hardware.usb.UsbConstants; import android.hardware.usb.UsbDevice; +import com.dantsu.escposprinter.exceptions.EscPosConnectionException; + public class UsbPrintersConnections extends UsbConnections { /** @@ -26,8 +28,10 @@ public static UsbConnection selectFirstConnected(Context context) { if (bluetoothPrinters != null && bluetoothPrinters.length > 0) { for (UsbConnection printer : bluetoothPrinters) { - if (printer.connect()) { - return printer; + try { + return printer.connect(); + } catch (EscPosConnectionException e) { + e.printStackTrace(); } } } diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosBrokenConnectionException.java b/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosBrokenConnectionException.java deleted file mode 100644 index c33e574..0000000 --- a/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosBrokenConnectionException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.dantsu.escposprinter.exceptions; - -public class EscPosBrokenConnectionException extends Exception { - public EscPosBrokenConnectionException(String errorMessage) { - super(errorMessage); - } -} diff --git a/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosConnectionException.java b/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosConnectionException.java new file mode 100644 index 0000000..7462ad0 --- /dev/null +++ b/escposprinter/src/main/java/com/dantsu/escposprinter/exceptions/EscPosConnectionException.java @@ -0,0 +1,7 @@ +package com.dantsu.escposprinter.exceptions; + +public class EscPosConnectionException extends Exception { + public EscPosConnectionException(String errorMessage) { + super(errorMessage); + } +}