diff --git a/README.md b/README.md index 90a4513..32536d6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/android/BasePrint.java b/src/android/BasePrint.java index 9099c15..5333f56 100644 --- a/src/android/BasePrint.java +++ b/src/android/BasePrint.java @@ -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; diff --git a/src/android/BrotherPrinter.java b/src/android/BrotherPrinter.java index bcd4e3e..2ebc073 100644 --- a/src/android/BrotherPrinter.java +++ b/src/android/BrotherPrinter.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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 { @@ -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; } } @@ -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(); @@ -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"); @@ -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 copyFile() " + e.toString()); + } + + } + } diff --git a/src/android/Common.java b/src/android/Common.java index 20c95fc..663618a 100644 --- a/src/android/Common.java +++ b/src/android/Common.java @@ -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 { diff --git a/src/ios/BrotherPrinter.h b/src/ios/BrotherPrinter.h index 99cdca0..0bca9ed 100644 --- a/src/ios/BrotherPrinter.h +++ b/src/ios/BrotherPrinter.h @@ -5,9 +5,10 @@ #import #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"; diff --git a/src/ios/BrotherPrinter.m b/src/ios/BrotherPrinter.m index 394a1e6..13c8102 100644 --- a/src/ios/BrotherPrinter.m +++ b/src/ios/BrotherPrinter.m @@ -4,6 +4,7 @@ @implementation BrotherPrinter { NSMutableArray *_brotherDeviceList; BRPtouchNetworkManager *_networkManager; BRPtouchPrinter *_ptp; + NSString *_customPaperFilePath; UIImage *_image; NSString *_printCallbackId; @@ -50,7 +51,8 @@ - (void)pluginInitialize { @"Brother QL-710W", @"Brother QL-720NW", @"Brother QL-810W", - @"Brother QL-820NWB"]; + @"Brother QL-820NWB", + @"Brother TD-2120N"]; } @@ -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 @@ -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]; @@ -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 @@ -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;