@captureid/capacitor-cidprint 0.2.65 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CaptureidCapacitor3Cidprint.podspec +21 -0
  2. package/README.md +809 -20
  3. package/android/.project +2 -2
  4. package/android/.settings/org.eclipse.buildship.core.prefs +1 -1
  5. package/android/app/captureid/cidprinterlibrary/1.0.5/cidprinterlibrary-1.0.5.aar +0 -0
  6. package/android/app/captureid/cidprinterlibrary/1.0.5/cidprinterlibrary-1.0.5.pom +20 -0
  7. package/android/app/captureid/cidprinterlibrary/1.0.5/cidprinterlibrary-1.0.5.pom.md5 +1 -0
  8. package/android/app/captureid/cidprinterlibrary/1.0.5/cidprinterlibrary-1.0.5.pom.sha1 +1 -0
  9. package/android/app/captureid/cidprinterlibrary/maven-metadata.xml +15 -0
  10. package/android/app/captureid/cidprinterlibrary/maven-metadata.xml.md5 +1 -0
  11. package/android/app/captureid/cidprinterlibrary/maven-metadata.xml.sha1 +1 -0
  12. package/android/app/captureid/cidprinterlibrary/maven-metadata.xml.sha256 +1 -0
  13. package/android/app/captureid/cidprinterlibrary/maven-metadata.xml.sha512 +1 -0
  14. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.aar +0 -0
  15. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.pom +9 -0
  16. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.pom.md5 +1 -0
  17. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.pom.sha1 +1 -0
  18. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.pom.sha256 +1 -0
  19. package/android/app/captureid/supportv1/1.0.1/supportv1-1.0.1.pom.sha512 +1 -0
  20. package/android/app/captureid/supportv1/maven-metadata-local.xml +12 -0
  21. package/android/app/captureid/supportv1/maven-metadata.xml +13 -0
  22. package/android/app/captureid/supportv1/maven-metadata.xml.md5 +1 -0
  23. package/android/app/captureid/supportv1/maven-metadata.xml.sha1 +1 -0
  24. package/android/app/captureid/supportv1/maven-metadata.xml.sha256 +1 -0
  25. package/android/app/captureid/supportv1/maven-metadata.xml.sha512 +1 -0
  26. package/android/build.gradle +20 -5
  27. package/android/src/main/java/app/captureid/plugins/cidprint/CIDPrint.java +686 -175
  28. package/dist/docs.json +1292 -0
  29. package/dist/esm/configuration.d.ts +26 -0
  30. package/dist/esm/configuration.js +20 -0
  31. package/dist/esm/configuration.js.map +1 -0
  32. package/dist/esm/definitions.d.ts +92 -21
  33. package/dist/esm/definitions.js +2 -0
  34. package/dist/esm/definitions.js.map +1 -1
  35. package/dist/esm/enums.d.ts +58 -3
  36. package/dist/esm/enums.js +88 -0
  37. package/dist/esm/enums.js.map +1 -1
  38. package/dist/esm/index.d.ts +5 -0
  39. package/dist/esm/index.js +8 -0
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/printer.d.ts +45 -0
  42. package/dist/esm/printer.js +1 -0
  43. package/dist/esm/printer.js.map +1 -0
  44. package/dist/esm/web.d.ts +77 -5
  45. package/dist/esm/web.js +102 -49
  46. package/dist/esm/web.js.map +1 -1
  47. package/dist/plugin.js +234 -48
  48. package/dist/plugin.js.map +1 -1
  49. package/ios/Plugin/BluetoothManager.h +46 -0
  50. package/ios/Plugin/CIDPrint.h +13 -0
  51. package/ios/Plugin/CIDPrint.m +518 -0
  52. package/ios/Plugin/{Plugin.h → CIDPrintPlugin.h} +11 -2
  53. package/ios/Plugin/CIDPrintPlugin.m +31 -0
  54. package/ios/Plugin/CIDPrinter.h +43 -0
  55. package/ios/Plugin/Device.h +57 -0
  56. package/ios/Plugin/Info.plist +8 -0
  57. package/ios/Plugin/InitResult.h +20 -0
  58. package/ios/Plugin/PrinterLibraryEvent.h +59 -0
  59. package/ios/Plugin/ResultClass.h +18 -0
  60. package/ios/Plugin/libCIDPrinterLibrary.a +0 -0
  61. package/package.json +19 -16
  62. package/android/.DS_Store +0 -0
  63. package/android/.npmignore +0 -1
  64. package/android/libs/.DS_Store +0 -0
  65. package/android/libs/cidprinterlibrary.aar +0 -0
  66. package/ios/Plugin/Plugin.m +0 -8
  67. package/ios/Plugin/Plugin.swift +0 -17
@@ -1,243 +1,583 @@
1
1
  package app.captureid.plugins.cidprint;
2
2
 
3
+ import android.Manifest;
3
4
  import android.content.Intent;
5
+ import android.content.pm.ApplicationInfo;
4
6
  import android.util.Log;
7
+ import android.util.Size;
8
+ import android.view.Window;
9
+ import android.view.WindowManager;
10
+
5
11
  import com.getcapacitor.JSObject;
6
- import com.getcapacitor.NativePlugin;
12
+ import com.getcapacitor.PermissionState;
7
13
  import com.getcapacitor.Plugin;
8
14
  import com.getcapacitor.PluginCall;
9
15
  import com.getcapacitor.PluginMethod;
10
16
  import com.getcapacitor.PluginResult;
17
+ import com.getcapacitor.annotation.ActivityCallback;
18
+ import com.getcapacitor.annotation.CapacitorPlugin;
19
+ import com.getcapacitor.annotation.Permission;
20
+ import com.getcapacitor.annotation.PermissionCallback;
21
+ import com.getcapacitor.JSArray;
22
+
11
23
  import org.json.JSONException;
12
24
  import org.json.JSONObject;
13
25
  import java.util.HashMap;
14
26
  import java.util.Iterator;
27
+ import java.util.List;
28
+
29
+ import androidx.activity.result.ActivityResult;
15
30
  import androidx.appcompat.app.AppCompatActivity;
31
+ import androidx.core.view.WindowCompat;
32
+ import androidx.core.view.WindowInsetsCompat;
33
+ import androidx.core.view.WindowInsetsControllerCompat;
34
+
16
35
  import app.captureid.cidprinterlibrary.CIDPrinter;
17
- import app.captureid.cidprinterlibrary.connectors.BluetoothConnector;
18
36
  import app.captureid.cidprinterlibrary.labels.ReferenceLabel;
19
37
  import app.captureid.cidprinterlibrary.notifications.InitResult;
20
38
  import app.captureid.cidprinterlibrary.notifications.PrinterLibraryEvent;
21
39
  import app.captureid.cidprinterlibrary.notifications.PrinterLibraryListener;
22
-
23
- @NativePlugin(
24
- requestCodes={BluetoothConnector.REQUEST_ENABLE_BT}
40
+ import app.captureid.cidprinterlibrary.printers.PrinterInformation;
41
+ import app.captureid.cidprinterlibrary.utils.PrinterUtils;
42
+
43
+ @CapacitorPlugin(
44
+ permissions = {
45
+ @Permission(strings = { Manifest.permission.ACCESS_COARSE_LOCATION }, alias = "android.permission.ACCESS_COARSE_LOCATION"),
46
+ @Permission(strings = { Manifest.permission.BLUETOOTH_ADMIN }, alias = "android.permission.BLUETOOTH_ADMIN"),
47
+ @Permission(strings = { Manifest.permission.BLUETOOTH }, alias = "android.permission.BLUETOOTH"),
48
+ // @Permission(strings = { Manifest.permission.BLUETOOTH_CONNECT }, alias = "bluetooth_connect"),
49
+ // @Permission(strings = { Manifest.permission.BLUETOOTH_SCAN }, alias = "bluetooth_scan"),
50
+ @Permission(strings = { Manifest.permission.ACCESS_FINE_LOCATION }, alias = "android.permission.ACCESS_FINE_LOCATION"),
51
+ @Permission(strings = { Manifest.permission.INTERNET }, alias = "Manifest.permission.INTERNET"),
52
+ @Permission(strings = { Manifest.permission.WRITE_EXTERNAL_STORAGE }, alias = "Manifest.permission.WRITE_EXTERNAL_STORAGE"),
53
+ @Permission(strings = { Manifest.permission.READ_EXTERNAL_STORAGE }, alias = "Manifest.permission.READ_EXTERNAL_STORAGE"),
54
+ @Permission(strings = { Manifest.permission.READ_CONTACTS }, alias = "android.permission.READ_CONTACTS"),
55
+ @Permission(strings = { Manifest.permission.READ_CALL_LOG }, alias = "android.permission.READ_CALL_LOG")
56
+ }
25
57
  )
26
58
 
27
59
  public class CIDPrint extends Plugin {
28
60
  private static final String TAG = CIDPrint.class.getSimpleName();
29
61
 
30
62
  private static final String LIBRARY_EVENT = "printerLibraryEvent";
63
+ private static final String LIBRARY_NOT_INITIALIZED = "library not initialized";
64
+ private static final String LIBRARY_ALREADY_INITIALIZED = "library already initialized";
31
65
 
66
+ private boolean isDebug = false;
32
67
  private CIDPrinter _printerlibrary;
68
+ // list to handle different callback methods
33
69
  private HashMap<String, PluginCall> _callbacks = new HashMap<String, PluginCall>();
34
70
  private AppCompatActivity _activity;
35
71
  private int _enableBluetoothRequestID;
72
+ // initialization state of the printer library
73
+ private boolean _isInitialized = false;
74
+ private final Object _permissionLock = new Object();
36
75
 
76
+ private String _activateLicenseCallbackID = "";
77
+ private String _enableBluetoothCallbackID = "";
37
78
  public CIDPrint() {
38
79
  super();
39
80
  }
40
81
 
41
- @Override
42
- protected void handleOnResume() {
43
- super.handleOnResume();
44
- _activity = getActivity();
82
+ // provides the ability to Log messages form the release library when the building app works in debug mode
83
+ private void LOG(String message) {
84
+ if(isDebug) {
85
+ Log.i(TAG, message);
86
+ }
45
87
  }
46
88
 
47
- private PrinterLibraryListener _library_listener = new PrinterLibraryListener() {
48
- @Override
49
- public void onPrinterLibraryEvent(PrinterLibraryEvent event) {
50
- switch(event.getEventType()) {
51
- case SUCCESS:
52
- handleSuccess(event);
53
- break;
54
- case FAILED:
55
- handleError(event);
56
- break;
57
- case NOTIFY:
58
- handleNotification(event);
59
- break;
60
- }
89
+ @PluginMethod
90
+ public void showNavigationBar(PluginCall call) {
91
+ boolean bshow = call.getBoolean("show");
92
+ try{
93
+ final Window window = getActivity().getWindow();
94
+ getActivity().runOnUiThread(new Runnable() {
95
+ @Override
96
+ public void run() {
97
+ WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView());
98
+ if(bshow) {
99
+ windowInsetsControllerCompat.show(WindowInsetsCompat.Type.navigationBars());
100
+ } else {
101
+ windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.navigationBars());
102
+ }
103
+
104
+ window.clearFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN);
105
+ window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
106
+ notifyListeners("onHide", new JSObject());
107
+ call.resolve();
108
+ }
109
+ });
110
+ } catch (Exception e){
111
+ Log.d(TAG, "showNavigationBar: " + e.toString());
112
+ call.reject(e.toString());
61
113
  }
114
+ }
62
115
 
63
- @Override
64
- public void requestForStartIntent(Intent intent, int requestID) {
65
- _enableBluetoothRequestID = requestID;
66
- startActivityForResult(savedLastCall, intent, requestID);
116
+ /**
117
+ * activate License has to be called once after initialization and before any other method call
118
+ * @param call Capacitor PluginCall instance
119
+ */
120
+ @PluginMethod
121
+ public void activateLicense(PluginCall call) {
122
+ // library initialization is required
123
+ if(_isInitialized) {
124
+ getBridge().saveCall(call);
125
+ _activateLicenseCallbackID = call.getCallbackId();
126
+ call.setKeepAlive(true);
127
+ PrinterLibraryEvent evt = _printerlibrary.activateLicense(call.getString("licenseKey"), call.getString("customerID"));
128
+ call.resolve(fromJSONObject(evt.toJSONObject()));
129
+ } else {
130
+ call.reject("Library not initialized");
67
131
  }
68
- };
132
+ }
69
133
 
70
- private void handleSuccess(PrinterLibraryEvent evt) {
71
- JSObject obj = null;
72
- try {
73
- obj = fromJSONObject(evt.getJSONObject());
74
- } catch (JSONException e) {
75
- e.printStackTrace();
134
+ /**
135
+ * called to release used resources should be called for a clean shutdown
136
+ * @param call Capacitor PluginCall instance
137
+ */
138
+ @PluginMethod
139
+ public void closeCIDPrinterLib(PluginCall call) {
140
+ if(_isInitialized) {
141
+ _printerlibrary.closeSharedlibrary();
142
+ _isInitialized = false;
76
143
  }
77
- try {
78
- switch(evt.getAction()) {
79
- case LICENSE_ACTIVATION:
80
- savedLastCall.success(obj);
81
- default:
82
- notifyListeners(LIBRARY_EVENT, obj);
83
- }
84
- // switch (evt.getAction()) {
85
- // case DISCOVER:
86
- // notifyListeners(LIBRARY_EVENT, obj);
87
- //// _callbacks.get("discoverDevices").successCallback(result);
88
- // break;
89
- // case CONNECT:
90
- //// _callbacks.get("connectToPreferredPrinter").successCallback(result);
91
- // break;
92
- // case PRINT:
93
- // notifyListeners(LIBRARY_EVENT, obj);
94
- // break;
95
- // }
96
- } catch(Exception ex) {
97
- Log.d(TAG, "handleSuccess: " + ex.getMessage());
144
+ }
145
+
146
+ /**
147
+ * establish a connection to the printer with the given bluetooth mac address
148
+ * or the latest connected printer when mc is set to null
149
+ * parameters:
150
+ * mac = bluetooth mac address of the printer or null
151
+ * autoreconnect = try to reconnect printer when device resumes from sleep mode
152
+ * @param call Capacitor PluginCall instance
153
+ */
154
+ @PluginMethod
155
+ public void connectToPreferredPrinter(PluginCall call) {
156
+ if(_isInitialized) {
157
+ // store the PluginCall instance to notify the caller in a later step
158
+ addCallback("connectToPreferredPrinter", call);
159
+ _printerlibrary.connectToPreferredPrinter(call.getString("mac"), call.getBoolean("autoreconnect") == null? false:call.getBoolean("autoreconnect"));
98
160
  }
99
161
  }
100
162
 
101
- private void handleError(PrinterLibraryEvent evt) {
102
- JSObject obj = new JSObject();
103
- try {
104
- obj = fromJSONObject((evt.getJSONObject()));
105
- } catch(JSONException ex) {
106
- Log.d(TAG, "handleError: " + ex.getMessage());
163
+ /**
164
+ * connect to a network printer with the given address or hostname and port
165
+ * parameters:
166
+ * address = ipaddress or hostname of the printer
167
+ * port = the configured port of the printer to communicate with (default 9100)
168
+ * @param call Capacitor PluginCall instance
169
+ */
170
+ @PluginMethod
171
+ public void connectToPrinter(PluginCall call) {
172
+ if(_isInitialized) {
173
+ boolean reconnect = call.getBoolean("autoreconnect") == null?false:call.getBoolean("autoreconnect");
174
+ _printerlibrary.connectToPrinter(call.getString("address"), reconnect);
107
175
  }
108
- notifyListeners(LIBRARY_EVENT, obj);
109
176
  }
110
177
 
111
- private void handleNotification(PrinterLibraryEvent evt) {
112
- JSObject obj = new JSObject();
113
- try {
114
- obj = fromJSONObject(evt.getJSONObject());
115
- } catch (JSONException ex) {
116
- Log.d(TAG, "handleNotification: " + ex.getMessage());
178
+ /**
179
+ * converts a label file into the connected printers command language
180
+ * parameters:
181
+ * label = name of the labelfile (needs to be placed in the assets folder)
182
+ * @param call
183
+ */
184
+ @PluginMethod
185
+ public void convert(PluginCall call) {
186
+ if(_isInitialized) {
187
+ _printerlibrary.convert(call.getString("label"));
117
188
  }
118
- notifyListeners(LIBRARY_EVENT, obj);
119
189
  }
120
190
 
121
- private void addCallback(String functionName, PluginCall callback) {
122
- if(_callbacks.containsKey(functionName)) {
123
- if(_callbacks.get(functionName).isReleased()) {
124
- _callbacks.remove(functionName);
125
- _callbacks.put(functionName, callback);
191
+ /**
192
+ * provides debug output and helps to debug the library without sending the data to a printer
193
+ * parameters:
194
+ * label = labelfilename (needs to be placed in the assets folder)
195
+ * @param call Capacitor PluginCall instance
196
+ */
197
+ @PluginMethod
198
+ public void debugPrint(PluginCall call) {
199
+ if(_isInitialized) _printerlibrary.debugPrint(call.getString("label"));
200
+ }
201
+
202
+ /**
203
+ * close connection and disconnect from printer
204
+ * @param call Capacitor PluginCall instance
205
+ */
206
+ @PluginMethod
207
+ public void disconnectFromPrinter(PluginCall call) {
208
+ if(_isInitialized) {
209
+ _printerlibrary.disconnectPrinter();
210
+ }
211
+ }
212
+
213
+ /**
214
+ * run the discover process to find bluetooth printers
215
+ * @param call Capacitor PluginCall instance
216
+ */
217
+ @PluginMethod
218
+ public void discoverDevices(PluginCall call) {
219
+ if(_isInitialized) {
220
+ addCallback("discoverDevices", call);
221
+ int timeout = call.getInt("timeout")==null?0:call.getInt("timeout");
222
+ _printerlibrary.discoverDevices(timeout);
223
+ }
224
+ }
225
+
226
+ /**
227
+ * enable/disable the bluetooth adapter
228
+ * parameters:
229
+ * enable = true(enable) or false(disable)
230
+ * @param call Capacitor PluginCall instance
231
+ */
232
+ @PluginMethod
233
+ public void enableBluetoothAdapter(PluginCall call) {
234
+ getBridge().saveCall(call);
235
+ _enableBluetoothCallbackID = call.getCallbackId();
236
+ call.setKeepAlive(true);
237
+
238
+ if(_isInitialized) call.resolve(fromJSONObject(_printerlibrary.enableBluetoothAdapter(call.getBoolean("enable")).toJSONObject()));
239
+ }
240
+
241
+ /**
242
+ * method to activate/deactivate bluetooth communication abilities in the library (required to be
243
+ * called once before calling any other bluetooth method from the library)
244
+ * parameters:
245
+ * enable = true(enable) false(disable)
246
+ *
247
+ * note:
248
+ * when called to enable bluetooth capabilities the method tries to enable the bluetooth adapter if not already enabled
249
+ * on call to disable bluetooth printing the bluetooth adapter will not be disabled.
250
+ * to enable/disable the bluetooth adapter use the method enableBluetoothAdapter.
251
+ *
252
+ * @param call Capacitor PluginCall instance
253
+ */
254
+ @PluginMethod
255
+ public void enableBluetoothPrinting (PluginCall call) {
256
+ if(_isInitialized) {
257
+ getBridge().saveCall(call);
258
+ _enableBluetoothCallbackID = call.getCallbackId();
259
+ call.setKeepAlive(true);
260
+ addCallback("enableBluetoothPrinting", call);
261
+ _printerlibrary.enableBluetoothPrinting(call.getBoolean("enable"));
262
+ }
263
+ }
264
+
265
+ /**
266
+ * enable/disable the dispensing mode of the printer (not all printer models are supported)
267
+ * parameters:
268
+ * enable = ture(enable) false(disable)
269
+ * @param call Capacitor PluginCall instance
270
+ */
271
+ @PluginMethod
272
+ public void enableDispendingMode(PluginCall call) {
273
+ if(_isInitialized) {
274
+ _printerlibrary.enableDispensingMode(call.getBoolean("enable"));
275
+ }
276
+ }
277
+
278
+ /**
279
+ * enable/disable the possibilities to work with network printers
280
+ * parameters:
281
+ * enable = true(enable) false(disable)
282
+ * @param call
283
+ */
284
+ @PluginMethod
285
+ public void enableNetworkPrinting (PluginCall call) {
286
+ if(_isInitialized) _printerlibrary.enableNetworkPrinting(call.getBoolean("enable"));
287
+ }
288
+
289
+ /**
290
+ * reports if a printer actually was connected before device goes to suspend mode
291
+ * can be used when device resumes and the app need to do some work in that case
292
+ * @param call Capacitor PluginCall instance
293
+ */
294
+ @PluginMethod
295
+ public void getLatestConnectState(PluginCall call) {
296
+ if(_isInitialized) {
297
+ PrinterLibraryEvent evt = _printerlibrary.getLatestConnectState();
298
+ call.successCallback(new PluginResult(fromJSONObject(evt.toJSONObject())));
299
+ }
300
+ }
301
+
302
+ /**
303
+ * retrieve the media size settings from the printer
304
+ * return:
305
+ * JSONObject with the width and height of the media size set in printer configuration
306
+ * @param call Capacitor PluginCall instance
307
+ *
308
+ */
309
+ @PluginMethod
310
+ public void getMediaSize(PluginCall call) {
311
+ if(_isInitialized) {
312
+ Size sz =_printerlibrary.getMediaSize();
313
+ if(sz == null) call.reject("failed to retrieve media size");
314
+ else {
315
+ JSObject obj = new JSObject();
316
+ obj.put("width", sz.getWidth());
317
+ obj.put("height", sz.getHeight());
318
+ call.resolve(obj);
126
319
  }
127
- } else {
128
- _callbacks.put(functionName, callback);
129
320
  }
130
321
  }
131
322
 
132
- private boolean hasRequiredPermissions(String[] permissions) {
133
- for(String permission: permissions) {
134
- if(!hasPermission(permission)) {
135
- return false;
323
+ /**
324
+ * notifies the app about the devices actually paired with the printer
325
+ * @param call Capacitor PluginCall instance
326
+ */
327
+ @PluginMethod
328
+ public void getPairedDevices(PluginCall call) {
329
+ if(_isInitialized) {
330
+ addCallback("getPairedDevices", call);
331
+ _printerlibrary.getPairedDevices();
332
+ }
333
+ }
334
+
335
+ /**
336
+ * retreive information about the printer
337
+ * the provided data depends on printer model
338
+ * @param call Capacitor PluginCall instance
339
+ */
340
+ @PluginMethod
341
+ public void getPrinterInformation(PluginCall call) {
342
+ if(_isInitialized) {
343
+ PrinterInformation info = _printerlibrary.getPrinterInformation();
344
+ try {
345
+ call.successCallback(new PluginResult(JSObject.fromJSONObject(info.toJSONObject())));
346
+ } catch (JSONException e) {
347
+ Log.e(TAG, "getPrinterInformation error: " + e.getMessage());
348
+ call.errorCallback(e.getMessage());
349
+ } catch(NullPointerException ex) {
350
+ Log.e(TAG, "getPrinterInformation error: " + ex.getMessage());
351
+ call.errorCallback(ex.getMessage());
136
352
  }
137
353
  }
138
- return true;
139
354
  }
140
355
 
356
+ /**
357
+ * returns the library version number as string
358
+ * @param call Capacitor PluginCall instance
359
+ */
141
360
  @PluginMethod
142
- public void debugPrint(PluginCall call) {
143
- _printerlibrary.debugPrint(call.getString("label"));
361
+ public void getPrinterLibraryVersion(PluginCall call) {
362
+ call.resolve(new JSObject().put("version", CIDPrinter.getVersion()));
144
363
  }
145
364
 
365
+ @PluginMethod
366
+ public void getPrinterStatus(PluginCall call) {
367
+ if(_isInitialized) {
368
+ _printerlibrary.getStatus();
369
+ }
370
+ }
371
+
372
+ /**
373
+ * initialize the printer library
374
+ * @param call Capacitor PluginCall instance
375
+ */
146
376
  @PluginMethod
147
377
  public void initCIDPrinterLib(PluginCall call) {
378
+ if(_isInitialized) {
379
+ call.reject(LIBRARY_ALREADY_INITIALIZED);
380
+ return;
381
+ }
382
+ // request the library instance. we are working with a single instance
148
383
  _printerlibrary = CIDPrinter.getSharedLibrary(_activity);
149
- PrinterLibraryEvent evt = null;
384
+ // test for a valid instance
150
385
  if(_printerlibrary != null) {
386
+ _printerlibrary.enableDebugToFile(false);
387
+ _isInitialized = true;
388
+ // set state and add our global listener to receive notifications from the library
389
+ // this is done before any other call that we doesn't miss any notification messages
151
390
  _printerlibrary.addListener(_library_listener);
152
- evt = _printerlibrary.initialize();
153
- if (hasRequiredPermissions(CIDPrinter.getRequiredPermissions())) {
391
+ // now run initialization routine
392
+ PrinterLibraryEvent evt = _printerlibrary.initialize();
393
+ // check if the running app is debug mode to enable information messages from the library
394
+ isDebug = (this.getActivity().getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE;
395
+ _printerlibrary.setDebug(isDebug);
396
+ // check for permissions
397
+ if(hasRequiredPermissions(CIDPrinter.getRequiredPermissions())) {
398
+ // permssion are set. no further action required
399
+ // we use the event data returned from initialize method
400
+ // and set the type to notification
401
+ // send the result to the app and exit method
402
+ call.successCallback(new PluginResult(fromJSONObject(evt.toJSONObject())));
403
+ return;
404
+ }
405
+ // permssion not set. send the result form initialization and add the list of the required permissions
406
+ evt = new PrinterLibraryEvent(this, PrinterLibraryEvent.EventType.NOTIFY, PrinterLibraryEvent.LibraryActionType.INITIALIZE, new InitResult(CIDPrinter.getRequiredPermissions()));
407
+ call.successCallback(new PluginResult(fromJSONObject(evt.toJSONObject())));
408
+ // .success(fromJSONObject(evt.getJSONObject()));
409
+ } else {
410
+ call.reject("library initialization failed");
411
+ }
412
+ }
413
+
414
+ @PluginMethod
415
+ public void requestAllPermissions(PluginCall call) {
416
+ JSArray permissions = call.getArray("permissions");
417
+ if(_isInitialized) {
418
+ requestAllPermissions(call, "permissionCallback");
419
+ synchronized (_permissionLock) {
154
420
  try {
155
- call.success(fromJSONObject(evt.getJSONObject()));
156
- } catch (JSONException e) {
157
- call.error("initCIDPrinterLib encountered an error: " + e.getMessage());
421
+ _permissionLock.wait();
422
+ } catch (InterruptedException ex) {
423
+ Log.e(TAG, "Permission request interrupted");
424
+ call.reject("Permission request interrupted");
425
+ return;
158
426
  }
427
+ // call.resolve(new JSObject().put("result", true));
159
428
  return;
160
429
  }
161
- evt = new PrinterLibraryEvent(PrinterLibraryEvent.EventType.SUCCESS, PrinterLibraryEvent.LibraryActionType.INITIALIZE, new InitResult(CIDPrinter.getRequiredPermissions()));
162
- try {
163
- call.success(fromJSONObject(evt.getJSONObject()));
164
- } catch (JSONException e) {
165
- call.error("initCIDPrinterLib encountered an error: " + e.getMessage());
166
- }
167
- } else {
168
- call.error("Failed to initialize PrinterPlugin");
169
430
  }
431
+ call.reject(LIBRARY_NOT_INITIALIZED);
170
432
  }
171
433
 
434
+ /**
435
+ * intergrated method to request the permissions required for this library. to avoid typo mistakes
436
+ * you can use the strings returned from the call to initCIDPrinterLib .
437
+ * paramters:
438
+ * permission = string containing the requested permission in android notation (i.e. android.permission.BLUETOOTH)
439
+ * @param call Capacitor PluginCall instance
440
+ */
172
441
  @PluginMethod
173
- public void closeCIDPrinterLib(PluginCall call) {
174
- if(_printerlibrary != null) {
175
- _printerlibrary.closeSharedlibrary();
442
+ public void requestPermission(PluginCall call) {
443
+ if(_isInitialized) {
444
+ // use the integrated capacitor plugin method to request the permission
445
+ requestPermissionForAlias(call.getString("permission"), call, "permissionCallback");
446
+ // add a lock to wait for the result
447
+ synchronized (_permissionLock) {
448
+ try {
449
+ _permissionLock.wait();
450
+ } catch (InterruptedException e) {
451
+ Log.e(TAG, "Permission request interrupted");
452
+ call.reject("Permission request interrupted");
453
+ }
454
+ return;
455
+ }
176
456
  }
457
+ call.reject(LIBRARY_NOT_INITIALIZED);
177
458
  }
178
459
 
460
+ /**
461
+ * method to enable/disable the ability to log the raw data sent to printer to a file
462
+ * @param enable
463
+ */
179
464
  @PluginMethod
180
- public void activateLicense(PluginCall call) {
181
- if(_printerlibrary != null) {
182
- saveCall(call);
183
- _printerlibrary.activateLicense(call.getString("licenseKey"), call.getString("customerID"));
465
+ public void enableDebugToFile(PluginCall call) {
466
+ if(_isInitialized) {
467
+ _printerlibrary.enableDebugToFile(call.getBoolean("enable"));
468
+ }
469
+ }
470
+
471
+ /**
472
+ * method to receive the result of the user interaction while permission request
473
+ * @param call Capacitor PluginCall instance
474
+ */
475
+ @PermissionCallback
476
+ private void permissionCallback(PluginCall call) {
477
+ JSArray map = call.getArray("permissions");
478
+ try {
479
+ for(Object perm: map.toList()) {
480
+ // check if permission has not been granted
481
+ if(getPermissionState(perm.toString()) != PermissionState.GRANTED) {
482
+ call.reject("permission denied: " + perm);
483
+ return;
484
+ }
485
+ }
486
+ } catch (JSONException e) {
487
+ call.reject("permission exception: " + e.getMessage());
488
+ }
489
+ call.resolve(new JSObject().put("result", true));
490
+ synchronized (_permissionLock) {
491
+ _permissionLock.notify();
184
492
  }
185
493
  }
186
494
 
187
495
  @PluginMethod
188
- public void enableBluetoothPrinting (PluginCall call) {
189
- if(_printerlibrary != null) {
190
- saveCall(call);
191
- addCallback("enableBluetoothPrinting", call);
192
- _printerlibrary.enableBluetoothPrinting(call.getBoolean("enable"));
496
+ public void isLibraryInitialized(PluginCall call) {
497
+ call.resolve(new JSObject().put("result", _isInitialized));
498
+ }
499
+
500
+ /**
501
+ * return the state of the bluetooth adapter
502
+ * @param call Capacitor PluginCall instance
503
+ */
504
+ @PluginMethod
505
+ public void isBluetoothAdapterEnabled(PluginCall call) {
506
+ if(_isInitialized) {
507
+ call.resolve(new JSObject().put("result", _printerlibrary.isBluetoothAdapterEnabled()));
193
508
  }
194
509
  }
195
510
 
196
511
  @PluginMethod
197
- public void getPairedDevices(PluginCall call) {
198
- if(_printerlibrary != null) {
199
- addCallback("getPairedDevices", call);
200
- _printerlibrary.getPairedDevices();
512
+ public void enableClipping(PluginCall call) {
513
+ if(_isInitialized) {
514
+ _printerlibrary.enableClipping(call.getBoolean("enable"));
201
515
  }
202
516
  }
203
517
 
518
+ /**
519
+ * send calibration request to the printer
520
+ * @param call Capacitor PluginCall instance
521
+ */
204
522
  @PluginMethod
205
- public void discoverDevices(PluginCall call) {
206
- if(_printerlibrary != null) {
207
- addCallback("discoverDevices", call);
208
- _printerlibrary.discoverDevices();
523
+ public void enableCalibration(PluginCall call) {
524
+ if(_isInitialized) {
525
+ _printerlibrary.enableCalibration(call.getBoolean("enable"));
209
526
  }
210
527
  }
211
528
 
529
+ /**
530
+ * send a formfeed request to the printer
531
+ * @param call
532
+ */
212
533
  @PluginMethod
213
- public void printReferenceLabel(PluginCall call) {
214
- if(_printerlibrary != null) {
215
- _printerlibrary.printReferenceLabel(ReferenceLabel.ReferenceLabelType.values()[call.getInt("labeltype", 0)]);
534
+ public void sendFormFeed(PluginCall call) {
535
+ if(_isInitialized) {
536
+ _printerlibrary.sendFormFeed();
216
537
  }
217
538
  }
218
539
 
540
+ /**
541
+ * print out the given data to the connected printer
542
+ * parameters:
543
+ * data = string representing the data that should be printed
544
+ * @param call Capacitor PluginCall instance
545
+ */
219
546
  @PluginMethod
220
547
  public void printData(PluginCall call) {
221
- if(_printerlibrary != null) {
548
+ if(_isInitialized) {
222
549
  addCallback("printData", call);
223
550
  _printerlibrary.print(call.getString("data").getBytes(), false);
224
551
  }
225
552
  }
226
553
 
554
+ /**
555
+ * print out the content of the given label file
556
+ * parameters:
557
+ * label = name of the labelfile that should be printed (the file has to be available in the assets folder)
558
+ * @param call Capacitor PluginCall instance
559
+ */
227
560
  @PluginMethod
228
- public void printNativeData(PluginCall call) {
229
- if(_printerlibrary != null) {
230
- addCallback("printNativeData", call);
231
- _printerlibrary.print(call.getString("data").getBytes(), true);
561
+ public void printLabel(PluginCall call) {
562
+ if(_isInitialized) {
563
+ addCallback("printData", call);
564
+ _printerlibrary.print(call.getString("label"));
232
565
  }
233
566
  }
234
567
 
568
+ /**
569
+ * prints out the given label and replaces the variable placeholders with the values in the list
570
+ * parameters:
571
+ * label = label file template provided in the assets folder
572
+ * data = list with the values that should be places into the template
573
+ * @param call Capacitor PluginCall instance
574
+ */
235
575
  @PluginMethod
236
- public void printLabelWithObject(PluginCall call) {
576
+ public void printLabelWithData(PluginCall call) {
237
577
  try {
238
- if (_printerlibrary != null) {
239
- addCallback("printLabelWithObject", call);
240
- _printerlibrary.print(call.getString("label"), call.getData(). getJSONObject("data").getJSONObject("variables"));
578
+ if (_isInitialized) {
579
+ addCallback("printLabelWithData", call);
580
+ _printerlibrary.print(call.getString("label"), call.getArray("data").toList().toArray(new String[call.getArray("data").toList().size()]));
241
581
  }
242
582
  } catch(JSONException ex) {
243
583
  Log.d(TAG, "printLabelWithData: " + ex.getMessage());
@@ -248,12 +588,22 @@ public class CIDPrint extends Plugin {
248
588
  }
249
589
  }
250
590
 
591
+ /**
592
+ * prints the given label template and replaces the variable content with the values provided by the Json object
593
+ * parameters:
594
+ * label = name of the label file provided in the assets folder
595
+ * data = json object with the data to be used in the label template
596
+ * @param call Capacitor PluginCall instance
597
+ */
251
598
  @PluginMethod
252
- public void printLabelWithData(PluginCall call) {
599
+ public void printLabelWithObject(PluginCall call) {
253
600
  try {
254
- if (_printerlibrary != null) {
255
- addCallback("printLabelWithData", call);
256
- _printerlibrary.print(call.getString("label"), call.getArray("data").toList().toArray(new String[call.getArray("data").toList().size()]));
601
+ if (_isInitialized) {
602
+ LOG("start print command received");
603
+ addCallback("printLabelWithObject", call);
604
+ LOG("callback registered");
605
+ _printerlibrary.print(call.getString("label"), call.getData(). getJSONObject("data").getJSONObject("variables"));
606
+ LOG("print command sent to library");
257
607
  }
258
608
  } catch(JSONException ex) {
259
609
  Log.d(TAG, "printLabelWithData: " + ex.getMessage());
@@ -264,83 +614,206 @@ public class CIDPrint extends Plugin {
264
614
  }
265
615
  }
266
616
 
617
+ /**
618
+ * print the label data content without converting it. used to print data to the connected printer
619
+ * in its native printer command language
620
+ * @param call Capacitor PluginCall instance
621
+ */
267
622
  @PluginMethod
268
- public void getPrinterStatus(PluginCall call) {
269
- if(_printerlibrary != null) {
270
- _printerlibrary.getStatus();
623
+ public void printNativeData(PluginCall call) {
624
+ if(_isInitialized) {
625
+ addCallback("printNativeData", call);
626
+ _printerlibrary.print(call.getString("data").getBytes(), true);
271
627
  }
272
628
  }
273
629
 
630
+ /**
631
+ * print out a receipt or journal with variable length
632
+ * @param call Capacitor PluginCall instance
633
+ */
274
634
  @PluginMethod
275
- public void setupMediaSize(PluginCall call) {
276
- if(_printerlibrary != null) {
277
- _printerlibrary.setupMediaSize(call.getInt("width"), call.getInt("height"));
635
+ public void printReceiptWithObject(PluginCall call) {
636
+ try {
637
+ if (_isInitialized) {
638
+ addCallback("printReceiptWithObject", call);
639
+ _printerlibrary.printReceipt(call.getString("label"), call.getData().getJSONObject("data"));
640
+ }
641
+ } catch(JSONException ex) {
642
+ Log.d(TAG, "printReceiptWithObject: " + ex.getMessage());
643
+ } catch(ClassCastException ex) {
644
+ Log.d(TAG, "printReceiptWithObject: " + ex.getMessage());
645
+ } catch(Exception ex) {
646
+ Log.d(TAG, "printReceiptWithObject: " + ex.getMessage());
278
647
  }
279
648
  }
280
649
 
650
+ /**
651
+ * print a dotmatrix to help positioning elements on the label
652
+ * for internal use only
653
+ * @param call Capacitor PluginCall instance
654
+ */
281
655
  @PluginMethod
282
- public void enableDispendingMode(PluginCall call) {
283
- if(_printerlibrary != null) {
284
- _printerlibrary.enableDispensingMode(call.getBoolean("enable"));
656
+ public void printReferenceLabel(PluginCall call) {
657
+ if(_isInitialized) {
658
+ _printerlibrary.printReferenceLabel(ReferenceLabel.ReferenceLabelType.values()[call.getInt("labeltype", 0)]);
285
659
  }
286
660
  }
287
661
 
662
+ /**
663
+ * enable/disable autoreconnect after device resume
664
+ * parameters:
665
+ * autoreconnect = true(enable) false(disable)
666
+ * @param call Capacitor PluginCall instance
667
+ */
288
668
  @PluginMethod
289
- public void printLabel(PluginCall call) {
290
- if(_printerlibrary != null) {
291
- addCallback("printData", call);
292
- _printerlibrary.print(call.getString("label"));
669
+ public void setAutoReconnect(PluginCall call) {
670
+ if(_isInitialized) {
671
+ _printerlibrary.setAutoReconnect(call.getBoolean("autoreconnect"));
293
672
  }
294
673
  }
295
674
 
675
+ /**
676
+ * set the library and printer configuration with the values given by the json object data
677
+ * @param call Capacitor PluginCall instance
678
+ */
296
679
  @PluginMethod
297
- public void disconnectFromPrinter(PluginCall call) {
298
- if(_printerlibrary != null) {
299
- _printerlibrary.disconnectPrinter();
680
+ public void setConfiguration(PluginCall call) {
681
+ if(_isInitialized) {
682
+ _printerlibrary.setConfiguration(call.getData());
300
683
  }
301
684
  }
302
685
 
686
+ /**
687
+ * set the media width and height
688
+ * parameters:
689
+ * width = the width of the label
690
+ * height = the height of the label
691
+ * @param call Capacitor PluginCall instance
692
+ */
303
693
  @PluginMethod
304
- public void connectToPreferredPrinter(PluginCall call) {
305
- if(_printerlibrary != null) {
306
- addCallback("connectToPreferredPrinter", call);
307
- _printerlibrary.connectToPreferredPrinter(call.getString("mac"));
694
+ public void setupMediaSize(PluginCall call) {
695
+ if(_isInitialized) {
696
+ _printerlibrary.setupMediaSize(call.getInt("width"), call.getInt("height"));
308
697
  }
309
698
  }
310
699
 
311
- @Override
312
- protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
313
- super.handleOnActivityResult(requestCode, resultCode, data);
314
- if(_printerlibrary != null) {
315
- _printerlibrary.handleOnActivityResult(requestCode, resultCode, data);
700
+ /**
701
+ * upload a file to the printer(i.e. font file)
702
+ * prmeters:
703
+ * file = filename to be uploaded to the printer (must be provided in the assets folder)
704
+ * filetype = type of the file to be uploaded
705
+ * @param call Capacitor PluginCall instance
706
+ */
707
+ @PluginMethod
708
+ public void uploadFile(PluginCall call) {
709
+ if(_isInitialized) {
710
+ _printerlibrary.uploadFile(call.getString("file"), PrinterUtils.PrinterFileType.values()[call.getInt("filetype")]);
316
711
  }
317
712
  }
318
713
 
319
- @Override
320
- protected void handleOnRestart() {
321
- super.handleOnRestart();
322
- if(_printerlibrary != null) {
323
- _printerlibrary.onResume();
714
+ private PrinterLibraryListener _library_listener = new PrinterLibraryListener() {
715
+ @Override
716
+ public void onPrinterLibraryEvent(PrinterLibraryEvent event) {
717
+ switch(event.getEventType()) {
718
+ case SUCCESS:
719
+ handleSuccess(event);
720
+ break;
721
+ case FAILED:
722
+ handleError(event);
723
+ break;
724
+ case NOTIFY:
725
+ handleNotification(event);
726
+ break;
727
+ }
728
+ }
729
+
730
+ @Override
731
+ public void requestForStartIntent(Intent intent, int requestID) {
732
+ _enableBluetoothRequestID = requestID;
733
+ PluginCall call = getBridge().getSavedCall(_enableBluetoothCallbackID);
734
+ startActivityForResult(call, intent, "enableBTCallback");
735
+ // startActivityForResult(savedLastCall, intent, requestID);
736
+ }
737
+ };
738
+
739
+ @ActivityCallback
740
+ private void enableBTCallback(PluginCall call, ActivityResult result) {
741
+ if (call == null) {
742
+ return;
743
+ }
744
+ if(_isInitialized) {
745
+ _printerlibrary.handleOnActivityResult(CIDPrinter.REQUEST_ENABLE_BT, result.getResultCode(), null);
324
746
  }
325
747
  }
326
748
 
327
- @Override
328
- protected void handleOnPause() {
329
- if(_printerlibrary != null) {
330
- _printerlibrary.onPause();
749
+ private void handleSuccess(PrinterLibraryEvent evt) {
750
+ JSObject obj = null;
751
+ obj = fromJSONObject(evt.toJSONObject());
752
+ try {
753
+ switch(evt.getAction()) {
754
+ case LICENSE_ACTIVATION:
755
+ PluginCall call = getBridge().getSavedCall(_activateLicenseCallbackID);
756
+ call.successCallback(new PluginResult(obj));
757
+ getBridge().releaseCall(call);
758
+ break;
759
+ default:
760
+ }
761
+ notifyListeners(LIBRARY_EVENT, obj);
762
+ // switch (evt.getAction()) {
763
+ // case DISCOVER:
764
+ // notifyListeners(LIBRARY_EVENT, obj);
765
+ //// _callbacks.get("discoverDevices").successCallback(result);
766
+ // break;
767
+ // case CONNECT:
768
+ //// _callbacks.get("connectToPreferredPrinter").successCallback(result);
769
+ // break;
770
+ // case PRINT:
771
+ // notifyListeners(LIBRARY_EVENT, obj);
772
+ // break;
773
+ // }
774
+ } catch(Exception ex) {
775
+ Log.d(TAG, "handleSuccess: " + ex.getMessage());
331
776
  }
332
- super.handleOnPause();
333
777
  }
334
778
 
335
- @Override
336
- protected void handleOnDestroy() {
337
- if(_printerlibrary != null) {
338
- _printerlibrary.onDestroy();
779
+ private void handleError(PrinterLibraryEvent evt) {
780
+ JSObject obj = new JSObject();
781
+ obj = fromJSONObject((evt.toJSONObject()));
782
+ notifyListeners(LIBRARY_EVENT, obj);
783
+ }
784
+
785
+ private void handleNotification(PrinterLibraryEvent evt) {
786
+ JSObject obj = new JSObject();
787
+ obj = fromJSONObject(evt.toJSONObject());
788
+ notifyListeners(LIBRARY_EVENT, obj);
789
+ }
790
+
791
+ private void addCallback(String functionName, PluginCall callback) {
792
+ try {
793
+ if (_callbacks.containsKey(functionName)) {
794
+ if (_callbacks.get(functionName).isReleased()) {
795
+ _callbacks.remove(functionName);
796
+ _callbacks.put(functionName, callback);
797
+ }
798
+ } else {
799
+ _callbacks.put(functionName, callback);
800
+ }
801
+ } catch(NullPointerException ex) {
802
+ Log.d(TAG, "addCallback throws error: " + ex.getMessage());
803
+ }
804
+ }
805
+
806
+ private boolean hasRequiredPermissions(String[] permissions) {
807
+ for(String permission: permissions) {
808
+ if(getPermissionState(permission) == PermissionState.DENIED ||
809
+ getPermissionState(permission) == PermissionState.PROMPT) {
810
+ return false;
811
+ }
339
812
  }
340
- super.handleOnDestroy();
813
+ return true;
341
814
  }
342
815
 
343
- private JSObject fromJSONObject(JSONObject jobj) throws JSONException {
816
+ private JSObject fromJSONObject(JSONObject jobj) {
344
817
  Iterator<String> keys = jobj.keys();
345
818
  while(keys.hasNext()) {
346
819
  String key = keys.next();
@@ -352,6 +825,44 @@ public class CIDPrint extends Plugin {
352
825
  Log.d(TAG, "fromJSONObject encounters an error in " + key + " error: " + e.getMessage());
353
826
  }
354
827
  }
355
- return JSObject.fromJSONObject(jobj);
828
+ try {
829
+ return JSObject.fromJSONObject(jobj);
830
+ } catch (JSONException e) {
831
+ Log.d(TAG, "fromJSONObject encounters an error: " + e.getMessage());
832
+ }
833
+ return new JSObject();
834
+ }
835
+
836
+ @Override
837
+ protected void handleOnResume() {
838
+ super.handleOnResume();
839
+ _activity = getActivity();
840
+ if(_isInitialized) {
841
+ _printerlibrary.onResume(false);
842
+ }
843
+ }
844
+
845
+ @Override
846
+ protected void handleOnRestart() {
847
+ super.handleOnRestart();
848
+ if(_isInitialized) {
849
+ _printerlibrary.onResume(true);
850
+ }
851
+ }
852
+
853
+ @Override
854
+ protected void handleOnPause() {
855
+ if(_isInitialized) {
856
+ _printerlibrary.onPause();
857
+ }
858
+ super.handleOnPause();
859
+ }
860
+
861
+ @Override
862
+ protected void handleOnDestroy() {
863
+ if(_isInitialized) {
864
+ _printerlibrary.onDestroy();
865
+ }
866
+ super.handleOnDestroy();
356
867
  }
357
868
  }