This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
143 lines (122 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2013-2015 BlackBerry Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var barcodescanner,
resultObjs = {},
readCallback,
_utils = require("../../lib/utils");
module.exports = {
// methods to start and stop scanning
startRead: function (success, fail, args, env) {
var result = new PluginResult(args, env);
resultObjs[result.callbackId] = result;
readCallback = result.callbackId;
var views = qnx.webplatform.getWebViews();
var handle = null;
var group = null;
var z = -1;
for (var i = 0; i < views.length; i++) {
if (views[i].visible && views[i].zOrder > z){
z = views[i].zOrder;
group = views[i].windowGroup;
handle = views[i].jsScreenWindowHandle;
}
}
if (handle !== null) {
var values = { group: group, handle: handle };
result.ok(barcodescanner.getInstance().startRead(result.callbackId, values), true);
} else {
result.error("Failed to find window handle", false);
}
},
stopRead: function (success, fail, args, env) {
var result = new PluginResult(args, env);
resultObjs[result.callbackId] = result;
result.ok(barcodescanner.getInstance().stopRead(result.callbackId), true);
},
add: function (success, fail) {
console.log('Frame Available event listening');
},
remove: function (success, fail) {
console.log('End listening to frames');
}
};
JNEXT.BarcodeScanner = function () {
var self = this,
hasInstance = false;
self.getId = function () {
return self.m_id;
};
self.init = function () {
if (!JNEXT.require("libBarcodeScanner")) {
return false;
}
self.m_id = JNEXT.createObject("libBarcodeScanner.BarcodeScannerJS");
if (self.m_id === "") {
return false;
}
JNEXT.registerEvents(self);
};
// ************************
// Enter your methods here
// ************************
// Fired by the Event framework (used by asynchronous callbacks)
self.onEvent = function (strData) {
var arData = strData.split(" "),
callbackId = arData[0],
receivedEvent = arData[1],
data = receivedEvent + " " + arData[2],
result = resultObjs[callbackId],
events = ["community.barcodescanner.codefound.native",
"community.barcodescanner.errorfound.native",
"community.barcodescanner.started.native",
"community.barcodescanner.ended.native"];
// Restructures results when codefound has spaces
if(arData.length > 3){
var i;
for(i=3; i<arData.length; i++) {
data += " " + arData[i];
}
}
if (events.indexOf(receivedEvent) != -1) {
console.log(callbackId);
console.log(data);
result.callbackOk(data, true);
}
if(receivedEvent == "community.barcodescanner.ended.native") {
delete resultObjs[readCallback];
readCallback = null
}
};
// Thread methods
self.startRead = function (callbackId, handle) {
return JNEXT.invoke(self.m_id, "startRead " + callbackId + " " + JSON.stringify(handle));
};
self.stopRead = function (callbackId) {
return JNEXT.invoke(self.m_id, "stopRead " + callbackId);
};
// ************************
// End of methods to edit
// ************************
self.m_id = "";
self.getInstance = function () {
if (!hasInstance) {
hasInstance = true;
self.init();
}
return self;
};
};
barcodescanner = new JNEXT.BarcodeScanner();