Skip to content

Commit

Permalink
feat: adding voucher print-outs (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 authored Jun 5, 2024
1 parent 455b25f commit 21a8053
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 18 deletions.
130 changes: 112 additions & 18 deletions app/src/main/java/com/blink/pos/companion/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onServiceConnected(ComponentName name, IBinder service) {
printerService = IPrinterService.Stub.asInterface(service);
isPrinterServiceBound = true;
if (pendingPrintData != null) {
PrintReceipt(pendingPrintData[0], pendingPrintData[1], pendingPrintData[2], pendingPrintData[3], pendingPrintData[4], pendingPrintData[5]);
dispatchPrintRequest(pendingPrintData);
pendingPrintData = null;
}
}
Expand All @@ -55,46 +55,122 @@ protected void onNewIntent(Intent intent) {
handleSendText(intent);
}

private void handleSendText(Intent intent) {
private void handleSendText(Intent intent) {
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_VIEW.equals(action)) {
Uri data = intent.getData();
if (data != null) {
String username = data.getQueryParameter("username");
String amount = data.getQueryParameter("amount");
String paymentHash = data.getQueryParameter("paymentHash");
String transactionId = data.getQueryParameter("id");
String date = data.getQueryParameter("date");
String time = data.getQueryParameter("time");
String[] printData = extractPrintData(data);
if (isPrinterServiceBound) {
PrintReceipt(username, amount, paymentHash, transactionId, date , time );
dispatchPrintRequest(printData);
} else {
pendingPrintData = new String[]{username, amount, paymentHash, transactionId, date ,time};
pendingPrintData = printData;
}
finish();
}
}
}

private void bindService() {
Intent intent = new Intent();
intent.setPackage("net.nyx.printerservice");
intent.setAction("net.nyx.printerservice.IPrinterService");
bindService(intent, connService, Context.BIND_AUTO_CREATE);
private String[] extractPrintData(Uri data) {
String app = data.getQueryParameter("app");
if (app == null) {
return extractPayData(data);
}

switch (app) {
case "voucher":
return extractVoucherData(data);
default: //default is blink pos/pay
return extractPayData(data);
}
}

private void paperOut() {
private String[] extractVoucherData(Uri data) {
String lnurl = data.getQueryParameter("lnurl");
String voucherPrice = data.getQueryParameter("voucherPrice");
String voucherAmount = data.getQueryParameter("voucherAmount");
String voucherSecret = data.getQueryParameter("voucherSecret");
return new String[]{"voucher", lnurl, voucherPrice, voucherAmount, voucherSecret};
}

private String[] extractPayData(Uri data) {
String username = data.getQueryParameter("username");
String amount = data.getQueryParameter("amount");
String paymentHash = data.getQueryParameter("paymentHash");
String transactionId = data.getQueryParameter("id");
String date = data.getQueryParameter("date");
String time = data.getQueryParameter("time");
return new String[]{"pay", username, amount, paymentHash, transactionId, date, time};
}

private void dispatchPrintRequest(String[] printData) {
switch (printData[0]) {
case "voucher":
printVoucherReceipt(printData[1], printData[2], printData[3], printData[4]);
break;
default:
printPayReceipt(printData[1], printData[2], printData[3], printData[4], printData[5], printData[6]);
break;
}
}

private void printVoucherReceipt(String lnurl, String voucherPrice, String voucherAmount, String voucherSecret) {
singleThreadExecutor.submit(() -> {
try {
printerService.paperOut(80);
PrintTextFormat dashedFormat = new PrintTextFormat();
dashedFormat.setStyle(0);
dashedFormat.setTextSize(27);
dashedFormat.setAli(1);
dashedFormat.setStyle(1);

//Blink Logo
Bitmap originalBitmap = BitmapFactory.decodeStream(getAssets().open("blink-logo.png"));
int maxWidthPixels = 200;
double aspectRatio = (double) originalBitmap.getWidth() / originalBitmap.getHeight();
int newHeight = (int) (maxWidthPixels / aspectRatio);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, maxWidthPixels, newHeight, true);
printerService.printBitmap(resizedBitmap, 1, 1);

//dashed line
String dashedLine = new String(new char[32]).replace("\0", "-");
printerService.printText( dashedLine, dashedFormat);

//transaction data
printDynamicKeyValue("Price:" ," ", voucherPrice);
printDynamicKeyValue("Amount:"," ", voucherAmount);
printDynamicKeyValue("Secret:"," ", voucherSecret);

//dashed line
printerService.printText( dashedLine , dashedFormat);

//QR
printerService.printQrCode(lnurl, 300, 300, 1);

// dashed line
printerService.printText( dashedLine, dashedFormat);

//
PrintTextFormat appLink = new PrintTextFormat();
appLink.setAli(1);
appLink.setTextSize(23);
appLink.setStyle(1);
printerService.printText("voucher.blink.sv", appLink);
printerService.printText("\n", appLink);

//stop printing
paperOut();
} catch (RemoteException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private void PrintReceipt(String username, String amount, String paymentHash, String transactionId, String date, String time) {


private void printPayReceipt(String username, String amount, String paymentHash, String transactionId, String date, String time) {
singleThreadExecutor.submit(() -> {
try {
PrintTextFormat dashedFormat = new PrintTextFormat();
Expand Down Expand Up @@ -182,6 +258,24 @@ private void printDynamicKeyValue(String key, String space ,String value) throws
}


//bind service-------------------------------------------------
private void bindService() {
Intent intent = new Intent();
intent.setPackage("net.nyx.printerservice");
intent.setAction("net.nyx.printerservice.IPrinterService");
bindService(intent, connService, Context.BIND_AUTO_CREATE);
}

private void paperOut() {
singleThreadExecutor.submit(() -> {
try {
printerService.paperOut(80);
} catch (RemoteException e) {
e.printStackTrace();
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
\"namespace\": \"web\",
\"site\": \"https://pay.mainnet.galoy.io\"
}
},
{
\"relation\": [\"delegate_permission/common.handle_all_urls\"],
\"target\": {
\"namespace\": \"web\",
\"site\": \"https://voucher.staging.blink.sv\"
}
},
{
\"relation\": [\"delegate_permission/common.handle_all_urls\"],
\"target\": {
\"namespace\": \"web\",
\"site\": \"https://voucher.blink.sv\"
}
}
]
</string>
Expand Down

0 comments on commit 21a8053

Please sign in to comment.