Skip to content

Commit

Permalink
Merge pull request #46 from TristanGodal/master
Browse files Browse the repository at this point in the history
Adding customFilePath parameter.
  • Loading branch information
arcadius authored Jan 7, 2022
2 parents 2ed054b + a544d11 commit 627465f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ PT-E800W, PT-D800W, PT-E850TKW
PT-P900W, PT-P950NW
```

__Tested models:__ `QL-720NW`, `QL-820NWB`
__Tested models:__ `QL-720NW`, `QL-820NWB`, `TD-2120N`

(if you have tried this with other models, please update this list and send a pull request)

Expand Down
2 changes: 1 addition & 1 deletion src/android/BasePrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private BasePrintResult setCustomPaper() {
if (manualCustomPaperSettingsEnabled) {
result = setManualCustomPaper(mPrinterInfo.printerModel);
} else {
mPrinterInfo.customPaper = Common.CUSTOM_PAPER_FOLDER + customSetting;
mPrinterInfo.customPaper = customSetting;
result = setManualCustomPaper(null);
}
break;
Expand Down
53 changes: 47 additions & 6 deletions src/android/BrotherPrinter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.threescreens.cordova.plugin.brotherprinter;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -33,12 +32,11 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.telecom.Call;
import android.util.Base64;
import android.util.Log;

Expand All @@ -64,6 +62,7 @@ public class BrotherPrinter extends CordovaPlugin {
PrinterInfo.Model.QL_720NW,
PrinterInfo.Model.QL_820NWB,
PrinterInfo.Model.QL_1110NWB,
PrinterInfo.Model.TD_2120N
};

private MsgHandle mHandle;
Expand Down Expand Up @@ -152,6 +151,7 @@ private class DiscoveredPrinter {
public String orientation;
public String numberOfCopies;
public String includeBatteryStatus;
public String customPaperFilePath;

public DiscoveredPrinter(BluetoothDevice device) {
port = PrinterInfo.Port.BLUETOOTH;
Expand Down Expand Up @@ -231,6 +231,10 @@ public DiscoveredPrinter(JSONObject object) throws JSONException {
numberOfCopies = object.getString("numberOfCopies");
}

if(object.has("customPaperFilePath")) {
customPaperFilePath = object.getString("customPaperFilePath");
}

if (object.has(INCLUDE_BATTERY_STATUS)) {
includeBatteryStatus = object.getString(INCLUDE_BATTERY_STATUS);
} else {
Expand All @@ -250,6 +254,9 @@ public JSONObject toJSON() throws JSONException {
result.put("nodeName", nodeName);
result.put("location", location);
result.put("paperLabelName", paperLabelName);
result.put("orientation", orientation);
result.put("numberOfCopies", numberOfCopies);
result.put("customPaperFilePath", customPaperFilePath);
return result;
}
}
Expand Down Expand Up @@ -387,6 +394,13 @@ private void setPrinter(JSONArray args, final CallbackContext callbackctx) {
editor.putString("orientation", printer.orientation != null ? printer.orientation : PrinterInfo.Orientation.LANDSCAPE.toString());
editor.putString("numberOfCopies", printer.numberOfCopies);

if(!printer.customPaperFilePath.isEmpty()) {
String targetBinFolder = cordova.getActivity()
.getExternalFilesDir("customPaperFileSet/").toString();
copyBinFile("www/" + printer.customPaperFilePath, targetBinFolder);
editor.putString("customSetting", targetBinFolder + new File(printer.customPaperFilePath).getName());
}

editor.putString(INCLUDE_BATTERY_STATUS, printer.includeBatteryStatus);

editor.commit();
Expand Down Expand Up @@ -554,7 +568,6 @@ public void onReceive(Context context, Intent intent) {
//label info must be set after setPrinterInfo, it's not in the docs
myPrinter.setLabelInfo(myLabelInfo);


try {
File outputDir = context.getCacheDir();
File outputFile = new File(outputDir.getPath() + "configure.prn");
Expand All @@ -581,4 +594,32 @@ public void onReceive(Context context, Intent intent) {
});
}

private void copyBinFile(String filename, String targetPath) {
AssetManager assetManager = cordova.getActivity().getAssets();
InputStream in = null;
OutputStream out = null;
String newFileName = null;
try {
in = assetManager.open(filename);

newFileName = targetPath + filename.substring(filename.lastIndexOf("/")+1);

out = new FileOutputStream(newFileName);

byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("Brother/SDKEvent", "Exception in copyBinFile() " + e.toString());
}

}

}
3 changes: 0 additions & 3 deletions src/android/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ public class Common {
// Activity_Settings
public static final int FOLDER_SELECT = 10016;

public static final String CUSTOM_PAPER_FOLDER = Environment
.getExternalStorageDirectory().toString() + "/customPaperFileSet/";

public static int mUsbRequest;

public enum BatteryStatus {
Expand Down
3 changes: 2 additions & 1 deletion src/ios/BrotherPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#import <BRLMPrinterKit/BRPtouchPrinterKit.h>
#import "BRBluetoothPrintOperation.h"
#import "BRWLANPrintOperation.h"
#define kPaperLabelName @"paperLabelName"
#define kPaperLabelName @"paperLabelName"
#define kPrintNumberOfPaperKey @"numberOfCopies"
#define kPrintOrientationKey @"orientation"
#define kCustomPaperFilePath @"customPaperFilePath"

static NSString *const IS_FINISHED_FOR_WLAN = @"isFinishedForWLAN";

Expand Down
23 changes: 18 additions & 5 deletions src/ios/BrotherPrinter.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ @implementation BrotherPrinter {
NSMutableArray *_brotherDeviceList;
BRPtouchNetworkManager *_networkManager;
BRPtouchPrinter *_ptp;
NSString *_customPaperFilePath;
UIImage *_image;
NSString *_printCallbackId;

Expand Down Expand Up @@ -50,7 +51,8 @@ - (void)pluginInitialize {
@"Brother QL-710W",
@"Brother QL-720NW",
@"Brother QL-810W",
@"Brother QL-820NWB"];
@"Brother QL-820NWB",
@"Brother TD-2120N"];


}
Expand Down Expand Up @@ -267,6 +269,7 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command {
NSString *paperLabelName = obj[@"paperLabelName"];
NSString *numberOfCopies = obj[@"numberOfCopies"];
NSString *orientation = obj[@"orientation"];
NSString *customPaperFilePath = obj[@"customPaperFilePath"];

if (!modelName) {
[self.commandDelegate
Expand Down Expand Up @@ -335,6 +338,11 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command {
forKey:kPrintOrientationKey];
}
}

if(customPaperFilePath) {
NSString *absoluteCustomPaperFilePath = [NSString stringWithFormat:@"%@/%@", @"www", [customPaperFilePath stringByDeletingPathExtension]];
_customPaperFilePath = [[NSBundle mainBundle] pathForResource:absoluteCustomPaperFilePath ofType:@"bin"];
}

[userDefaults synchronize];

Expand Down Expand Up @@ -430,10 +438,6 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command {
printInfo.bSpecialTape = (int) [self integerValueFromDefaults:userDefaults forKey:kPrintSpecialTapeKey withFallback:SpecialTapeOff]; // Item 23
printInfo.bRotate180 = (int) [self integerValueFromDefaults:userDefaults forKey:kRotateKey withFallback:RotateOff]; // Item 24
printInfo.bPeel = (int) [self integerValueFromDefaults:userDefaults forKey:kPeelKey withFallback:PeelOff]; // Item 25

NSString *customPaper = [self stringValueFromDefaults:userDefaults forKey:kPrintCustomPaperKey withFallback:@""]; // Item 26
NSString *customPaperFilePath = nil;

printInfo.bCutMark = (int) [self integerValueFromDefaults:userDefaults forKey:kPrintCutMarkKey withFallback:CutMarkOff]; // Item 27
printInfo.nLabelMargine = (int) [self integerValueFromDefaults:userDefaults forKey:kPrintLabelMargineKey withFallback:0]; // Item 28

Expand Down Expand Up @@ -497,14 +501,23 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command {
finalDeviceName = [NSString stringWithFormat:@"Brother %@", printerName];
}
}];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (isBluetooth == 1) {

_ptp = [[BRPtouchPrinter alloc] initWithPrinterName:finalDeviceName interface:CONNECTION_TYPE_BLUETOOTH];
if ([fileManager fileExistsAtPath:_customPaperFilePath]){
[_ptp setCustomPaperFile:_customPaperFilePath];
}
// [_ptp setupForBluetoothDeviceWithSerialNumber:serialNumber];

} else if (isWifi == 1) {
_ptp = [[BRPtouchPrinter alloc] initWithPrinterName:finalDeviceName interface:CONNECTION_TYPE_WLAN];

if ([fileManager fileExistsAtPath:_customPaperFilePath]){
[_ptp setCustomPaperFile:_customPaperFilePath];
}
// [_ptp setIPAddress:ipAddress];
} else {
_ptp = nil;
Expand Down

0 comments on commit 627465f

Please sign in to comment.