2 /*global $, tizen, App, app */
4 (function () { // strict mode wrapper
8 * Constructs NFCControl
12 App.NFCControl = function NFCControl(app) {
17 App.NFCControl.prototype = {
19 nfcStateMemory: false,
22 * Initialize NFC application module
24 init: function nfc_init() {
25 this.card = new App.NFCCard(this);
26 this.peer = new App.NFCPeer(this);
27 this.separator = String.fromCharCode(30);
28 this.endOfText = String.fromCharCode(3);
31 * Disable tag/target detection and move to start page
33 * @param {string} monit message text to be displayed
35 timeExpired: function nfc_timeExpired(monit) {
36 clearTimeout(this.app.timeOutHandler);
37 this.unsetTargetDetect();
38 this.unsetTagDetect();
39 this.app.ui.moveToStartPage(monit);
43 * Get the field separator
47 getSeparator: function nfc_getSeparator() {
48 return this.separator;
52 * Get the end of text marker
56 getEndOfText: function nfc_getEndOfText() {
57 return this.endOfText;
60 createContactString: function createContactString(contact) {
62 contactString = contact.phoneNumber + this.getSeparator();
63 contactString += contact.firstName + this.getSeparator();
64 contactString += contact.lastName;
70 * @param {string|object} contact
73 prepareForNFC: function nfc_prepareForNFC(contact) {
74 if ($.type(contact) !== 'string') {
75 contact = this.createContactString(contact);
77 if (contact.length > 31) {
78 contact = contact.substring(0, 31);
79 if (contact[29] !== this.getSeparator()) {
80 contact = contact.substring(0, 30) + this.getEndOfText();
86 resolveData: function nfc_resolveData(value, endOfText) {
91 endOfText = endOfText || this.getEndOfText();
93 return value.replace(endOfText, '…');
96 resolveContact: function nfc_resolveContact(contactsString) {
97 var separator = this.getSeparator(),
98 contactsArray = contactsString.split(separator);
101 phone: this.resolveData(contactsArray[0]),
102 first: this.resolveData(contactsArray[1]),
103 last: this.resolveData(contactsArray[2])
109 * @param {type} record
110 * @returns {undefined}
112 fillRecordInfo: function nfc_fillRecordInfo(record) {
114 var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
115 this.app.nfc.displayContact(contactsData);
117 console.error(error);
123 * @param {NDEFMessage} message
125 readMessage: function nfc_readMessage(message) {
127 this.fillRecordInfo(message.records[0]);
129 console.error(e.message);
135 * @param {string|object} contact
136 * @returns {NDEFMessage}
138 contact2NDEF: function nfc_contact2NDEF(contact) {
139 var t, a = [], len, i, newMessage = new tizen.NDEFMessage();
140 if (typeof contact === 'string') {
143 t = contact.convertToString("VCARD_30");
146 for (i = 0; i < len; i += 1) {
147 a[i] = t.charCodeAt(i);
149 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
155 * @param {string} contact
156 * @returns {NDEFMessage}
158 phoneNumber2NDEF: function nfc_phoneNumber2NDEF(contact) {
159 var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage();
160 for (i = 0; i < length; i += 1) {
161 phoneNumberArray.push(contact.charCodeAt(i));
163 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
169 * @param {string} contact
172 convertNDEF2phoneNumber: function nfc_convertNDEF2phoneNumber(contact) {
173 var i, length = contact.length, phoneNumber = '';
174 for (i = 0; i < length; i += 1) {
175 phoneNumber += String.fromCharCode(contact[i]);
181 * NFC setPowered success callback
182 * @returns {undefined}
184 onPowerOn: function nfc_onPowerOn() {
185 console.log('Power On succeeded.');
186 this.app.started = true;
187 this.app.ui.loadStartPage();
191 * NFC setPowered error callback
193 * @returns {undefined}
195 onPowerOnFails: function nfc_onPowerOnFails(err) {
196 console.error('Power On error: ' + err.message);
197 this.app.ui.showPopupWarning();
201 * NFC setPowered success callback
202 * @returns {undefined}
204 onPowerOff: function nfc_onPowerOff() {
205 tizen.application.getCurrentApplication().exit();
209 * NFC setPowered error callback
211 * @returns {undefined}
213 onPowerOffFails: function nfc_onPowerOffFails(err) {
214 console.error('Power Off error', err);
215 tizen.application.getCurrentApplication().exit();
218 isPowered: function nfc_isPowered() {
219 return this.nfcAdapter.powered;
222 startNFC: function nfc_startNFC() {
224 this.nfcAdapter = tizen.nfc.getDefaultAdapter();
225 if (this.nfcAdapter.powered) {
226 this.nfcStateMemory = true;
230 this.nfcStateMemory = false;
231 console.log('Turning NFC adapter On...');
232 this.nfcAdapter.setPowered(true, this.onPowerOn.bind(this), this.onPowerOnFails.bind(this));
234 console.error('startNFC problem', e);
235 this.app.ui.showPopupWarning();
239 stopNFC: function nfc_stopNFC() {
241 if (this.nfcStateMemory) {
244 this.nfcAdapter.setPowered(false, this.onPowerOff.bind(this), this.onPowerOffFails.bind(this));
247 console.error('setPowered(false) problem', err);
251 unsetTargetDetect: function nfc_unsetTargetDetect() {
253 if (this.nfcTarget) {
254 this.nfcTarget.unsetReceiveNDEFListener();
255 this.nfcTarget = null;
257 //console.warn("app.nfc.nfcTarget not set");
260 console.error('error: ' + error.message);
263 this.nfcAdapter.unsetPeerListener();
265 console.error('error: ' + e.message);
269 unsetTagDetect: function nfc_unsetTagDetect() {
271 this.nfcAdapter.unsetTagListener();
273 console.error('error: ' + error.message);
277 displayContact: function nfc_displayContact(obj) {
278 clearTimeout(this.app.timeOutHandler);
279 this.unsetTargetDetect();
280 this.unsetTagDetect();
281 this.app.ui.moveToContactPage(obj);
284 sucDetach: function nfc_sucDetach() {