-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Franck ALARY
committed
Jun 23, 2020
1 parent
18ccd95
commit d03c1cf
Showing
24 changed files
with
508 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/dantsu/thermalprinter/async/AsyncBluetoothEscPosPrint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.dantsu.thermalprinter.async; | ||
|
||
import android.content.Context; | ||
|
||
import com.dantsu.escposprinter.connection.DeviceConnection; | ||
import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections; | ||
|
||
public class AsyncBluetoothEscPosPrint extends AsyncEscPosPrint { | ||
public AsyncBluetoothEscPosPrint(Context context) { | ||
super(context); | ||
} | ||
|
||
protected Integer doInBackground(AsyncEscPosPrinter... printersData) { | ||
if (printersData.length == 0) { | ||
return AsyncEscPosPrint.FINISH_NO_PRINTER; | ||
} | ||
|
||
AsyncEscPosPrinter printerData = printersData[0]; | ||
|
||
DeviceConnection deviceConnection = printerData.getPrinterConnection(); | ||
|
||
if(deviceConnection == null) { | ||
this.publishProgress(AsyncEscPosPrint.PROGRESS_CONNECTING); | ||
|
||
printersData[0] = new AsyncEscPosPrinter( | ||
BluetoothPrintersConnections.selectFirstPaired(), | ||
printerData.getPrinterDpi(), | ||
printerData.getPrinterWidthMM(), | ||
printerData.getPrinterNbrCharactersPerLine() | ||
); | ||
printersData[0].setTextToPrint(printerData.getTextToPrint()); | ||
} | ||
|
||
return super.doInBackground(printersData); | ||
} | ||
} |
171 changes: 171 additions & 0 deletions
171
app/src/main/java/com/dantsu/thermalprinter/async/AsyncEscPosPrint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package com.dantsu.thermalprinter.async; | ||
|
||
import android.app.AlertDialog; | ||
import android.app.ProgressDialog; | ||
import android.content.Context; | ||
import android.os.AsyncTask; | ||
|
||
import com.dantsu.escposprinter.EscPosPrinter; | ||
import com.dantsu.escposprinter.connection.DeviceConnection; | ||
import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections; | ||
import com.dantsu.escposprinter.exceptions.EscPosBarcodeException; | ||
import com.dantsu.escposprinter.exceptions.EscPosConnectionException; | ||
import com.dantsu.escposprinter.exceptions.EscPosEncodingException; | ||
import com.dantsu.escposprinter.exceptions.EscPosParserException; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
public abstract class AsyncEscPosPrint extends AsyncTask<AsyncEscPosPrinter, Integer, Integer> { | ||
protected final static int FINISH_SUCCESS = 1; | ||
protected final static int FINISH_NO_PRINTER = 2; | ||
protected final static int FINISH_PRINTER_DISCONNECTED = 3; | ||
protected final static int FINISH_PARSER_ERROR = 4; | ||
protected final static int FINISH_ENCODING_ERROR = 5; | ||
protected final static int FINISH_BARCODE_ERROR = 6; | ||
|
||
protected final static int PROGRESS_CONNECTING = 1; | ||
protected final static int PROGRESS_CONNECTED = 2; | ||
protected final static int PROGRESS_PRINTING = 3; | ||
protected final static int PROGRESS_PRINTED = 4; | ||
|
||
protected ProgressDialog dialog; | ||
protected WeakReference<Context> weakContext; | ||
|
||
|
||
public AsyncEscPosPrint(Context context) { | ||
this.weakContext = new WeakReference<>(context); | ||
} | ||
|
||
protected Integer doInBackground(AsyncEscPosPrinter... printersData) { | ||
if (printersData.length == 0) { | ||
return AsyncEscPosPrint.FINISH_NO_PRINTER; | ||
} | ||
|
||
this.publishProgress(AsyncEscPosPrint.PROGRESS_CONNECTING); | ||
|
||
AsyncEscPosPrinter printerData = printersData[0]; | ||
|
||
try { | ||
DeviceConnection deviceConnection = printerData.getPrinterConnection(); | ||
|
||
if(deviceConnection == null) { | ||
deviceConnection = BluetoothPrintersConnections.selectFirstPaired(); | ||
} | ||
|
||
if(deviceConnection == null) { | ||
return AsyncEscPosPrint.FINISH_NO_PRINTER; | ||
} | ||
|
||
EscPosPrinter printer = new EscPosPrinter(deviceConnection, printerData.getPrinterDpi(), printerData.getPrinterWidthMM(), printerData.getPrinterNbrCharactersPerLine()); | ||
|
||
this.publishProgress(AsyncEscPosPrint.PROGRESS_PRINTING); | ||
|
||
printer.printFormattedText(printerData.getTextToPrint()); | ||
|
||
this.publishProgress(AsyncEscPosPrint.PROGRESS_PRINTED); | ||
|
||
} catch (EscPosConnectionException e) { | ||
e.printStackTrace(); | ||
return AsyncEscPosPrint.FINISH_PRINTER_DISCONNECTED; | ||
} catch (EscPosParserException e) { | ||
e.printStackTrace(); | ||
return AsyncEscPosPrint.FINISH_PARSER_ERROR; | ||
} catch (EscPosEncodingException e) { | ||
e.printStackTrace(); | ||
return AsyncEscPosPrint.FINISH_ENCODING_ERROR; | ||
} catch (EscPosBarcodeException e) { | ||
e.printStackTrace(); | ||
return AsyncEscPosPrint.FINISH_BARCODE_ERROR; | ||
} | ||
|
||
return AsyncEscPosPrint.FINISH_SUCCESS; | ||
} | ||
|
||
protected void onPreExecute() { | ||
if (this.dialog == null) { | ||
Context context = weakContext.get(); | ||
|
||
if (context == null) { | ||
return; | ||
} | ||
|
||
this.dialog = new ProgressDialog(context); | ||
this.dialog.setTitle("Printing in progress..."); | ||
this.dialog.setMessage("..."); | ||
this.dialog.setProgressNumberFormat("%1d / %2d"); | ||
this.dialog.setCancelable(false); | ||
this.dialog.setIndeterminate(false); | ||
this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); | ||
this.dialog.show(); | ||
} | ||
} | ||
|
||
protected void onProgressUpdate(Integer... progress) { | ||
switch (progress[0]) { | ||
case AsyncEscPosPrint.PROGRESS_CONNECTING: | ||
this.dialog.setMessage("Connecting printer..."); | ||
break; | ||
case AsyncEscPosPrint.PROGRESS_CONNECTED: | ||
this.dialog.setMessage("Printer is connected..."); | ||
break; | ||
case AsyncEscPosPrint.PROGRESS_PRINTING: | ||
this.dialog.setMessage("Printer is printing..."); | ||
break; | ||
case AsyncEscPosPrint.PROGRESS_PRINTED: | ||
this.dialog.setMessage("Printer has finished..."); | ||
break; | ||
} | ||
this.dialog.setProgress(progress[0]); | ||
this.dialog.setMax(4); | ||
} | ||
|
||
protected void onPostExecute(Integer result) { | ||
this.dialog.dismiss(); | ||
this.dialog = null; | ||
|
||
Context context = weakContext.get(); | ||
|
||
if (context == null) { | ||
return; | ||
} | ||
|
||
switch (result) { | ||
case AsyncEscPosPrint.FINISH_SUCCESS: | ||
new AlertDialog.Builder(context) | ||
.setTitle("Success") | ||
.setMessage("Congratulation ! The text is printed !") | ||
.show(); | ||
break; | ||
case AsyncEscPosPrint.FINISH_NO_PRINTER: | ||
new AlertDialog.Builder(context) | ||
.setTitle("No printer") | ||
.setMessage("The application can't find any printer connected.") | ||
.show(); | ||
break; | ||
case AsyncEscPosPrint.FINISH_PRINTER_DISCONNECTED: | ||
new AlertDialog.Builder(context) | ||
.setTitle("Broken connection") | ||
.setMessage("Unable to connect the printer.") | ||
.show(); | ||
break; | ||
case AsyncEscPosPrint.FINISH_PARSER_ERROR: | ||
new AlertDialog.Builder(context) | ||
.setTitle("Invalid formatted text") | ||
.setMessage("It seems to be an invalid syntax problem.") | ||
.show(); | ||
break; | ||
case AsyncEscPosPrint.FINISH_ENCODING_ERROR: | ||
new AlertDialog.Builder(context) | ||
.setTitle("Bad selected encoding") | ||
.setMessage("The selected encoding character returning an error.") | ||
.show(); | ||
break; | ||
case AsyncEscPosPrint.FINISH_BARCODE_ERROR: | ||
new AlertDialog.Builder(context) | ||
.setTitle("Invalid barcode") | ||
.setMessage("Data send to be converted to barcode or QR code seems to be invalid.") | ||
.show(); | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.