Revert "Updated application sources"
[apps/web/sample/ContactsExchanger.git] / project / js / app.nfc.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app */
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         /**
8          * Constructs NFCControl
9          * @constructor
10          * @param {App} app
11          */
12         App.NFCControl = function NFCControl(app) {
13                 this.app = app;
14                 this.init();
15         };
16
17         App.NFCControl.prototype = {
18                 nfcTarget: null,
19                 nfcStateMemory: false,
20
21                 /**
22                  * Initialize NFC application module
23                  */
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);
29                 },
30                 /**
31                  * Disable tag/target detection and move to start page
32                  *
33                  * @param {string} monit message text to be displayed
34                  */
35                 timeExpired: function nfc_timeExpired(monit) {
36                         clearTimeout(this.app.timeOutHandler);
37                         this.unsetTargetDetect();
38                         this.unsetTagDetect();
39                         this.app.ui.moveToStartPage(monit);
40                 },
41
42                 /**
43                  * Get the field separator
44                  *
45                  * @returns {string}
46                  */
47                 getSeparator: function nfc_getSeparator() {
48                         return this.separator;
49                 },
50
51                 /**
52                  * Get the end of text marker
53                  *
54                  * @returns {string}
55                  */
56                 getEndOfText: function nfc_getEndOfText() {
57                         return this.endOfText;
58                 },
59
60                 createContactString: function createContactString(contact) {
61                         var contactString;
62                         contactString = contact.phoneNumber + this.getSeparator();
63                         contactString += contact.firstName + this.getSeparator();
64                         contactString += contact.lastName;
65                         return contactString;
66                 },
67
68                 /**
69                  *
70                  * @param {string|object} contact
71                  * @returns {string}
72                  */
73                 prepareForNFC: function nfc_prepareForNFC(contact) {
74                         if ($.type(contact) !== 'string') {
75                                 contact = this.createContactString(contact);
76                         }
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();
81                                 }
82                         }
83                         return contact;
84                 },
85
86                 resolveData: function nfc_resolveData(value, endOfText) {
87                         if (!value) {
88                                 return '';
89                         }
90
91                         endOfText = endOfText || this.getEndOfText();
92
93                         return value.replace(endOfText, '…');
94                 },
95
96                 resolveContact: function nfc_resolveContact(contactsString) {
97                         var separator = this.getSeparator(),
98                                 contactsArray = contactsString.split(separator);
99
100                         return {
101                                 phone: this.resolveData(contactsArray[0]),
102                                 first: this.resolveData(contactsArray[1]),
103                                 last: this.resolveData(contactsArray[2])
104                         };
105                 },
106
107                 /**
108                  *
109                  * @param {type} record
110                  * @returns {undefined}
111                  */
112                 fillRecordInfo: function nfc_fillRecordInfo(record) {
113                         try {
114                                 var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
115                                 this.app.nfc.displayContact(contactsData);
116                         } catch (error) {
117                                 console.error(error);
118                         }
119                 },
120
121                 /**
122                  *
123                  * @param {NDEFMessage} message
124                  */
125                 readMessage: function nfc_readMessage(message) {
126                         try {
127                                 this.fillRecordInfo(message.records[0]);
128                         } catch (e) {
129                                 console.error(e.message);
130                         }
131                 },
132
133                 /**
134                  *
135                  * @param {string|object} contact
136                  * @returns {NDEFMessage}
137                  */
138                 contact2NDEF: function nfc_contact2NDEF(contact) {
139                         var t, a = [], len, i, newMessage = new tizen.NDEFMessage();
140                         if (typeof contact === 'string') {
141                                 t = contact;
142                         } else {
143                                 t = contact.convertToString("VCARD_30");
144                         }
145                         len = t.length;
146                         for (i = 0; i < len; i += 1) {
147                                 a[i] = t.charCodeAt(i);
148                         }
149                         newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
150                         return newMessage;
151                 },
152
153                 /**
154                  *
155                  * @param {string} contact
156                  * @returns {NDEFMessage}
157                  */
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));
162                         }
163                         newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
164                         return newMessage;
165                 },
166
167                 /**
168                  *
169                  * @param {string} contact
170                  * @returns {string}
171                  */
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]);
176                         }
177                         return phoneNumber;
178                 },
179
180                 /**
181                  * NFC setPowered success callback
182                  * @returns {undefined}
183                  */
184                 onPowerOn: function nfc_onPowerOn() {
185                         console.log('Power On succeeded.');
186                         this.app.started = true;
187                         this.app.ui.loadStartPage();
188                 },
189
190                 /**
191                  * NFC setPowered error callback
192                  * @param {Error} err
193                  * @returns {undefined}
194                  */
195                 onPowerOnFails: function nfc_onPowerOnFails(err) {
196                         console.error('Power On error: ' + err.message);
197                         this.app.ui.showPopupWarning();
198                 },
199
200                 /**
201                  * NFC setPowered success callback
202                  * @returns {undefined}
203                  */
204                 onPowerOff: function nfc_onPowerOff() {
205                         tizen.application.getCurrentApplication().exit();
206                 },
207
208                 /**
209                  * NFC setPowered error callback
210                  * @param {Error} err
211                  * @returns {undefined}
212                  */
213                 onPowerOffFails: function nfc_onPowerOffFails(err) {
214                         console.error('Power Off error', err);
215                         tizen.application.getCurrentApplication().exit();
216                 },
217
218                 isPowered: function nfc_isPowered() {
219                         return this.nfcAdapter.powered;
220                 },
221
222                 startNFC: function nfc_startNFC() {
223                         try {
224                                 this.nfcAdapter = tizen.nfc.getDefaultAdapter();
225                                 if (this.nfcAdapter.powered) {
226                                         this.nfcStateMemory = true;
227                                         this.onPowerOn();
228                                         return;
229                                 }
230                                 this.nfcStateMemory = false;
231                                 console.log('Turning NFC adapter On...');
232                                 this.nfcAdapter.setPowered(true, this.onPowerOn.bind(this), this.onPowerOnFails.bind(this));
233                         } catch (e) {
234                                 console.error('startNFC problem', e);
235                                 this.app.ui.showPopupWarning();
236                         }
237                 },
238
239                 stopNFC: function nfc_stopNFC() {
240                         try {
241                                 if (this.nfcStateMemory) {
242                                         this.onPowerOff();
243                                 } else {
244                                         this.nfcAdapter.setPowered(false, this.onPowerOff.bind(this), this.onPowerOffFails.bind(this));
245                                 }
246                         } catch (err) {
247                                 console.error('setPowered(false) problem', err);
248                         }
249                 },
250
251                 unsetTargetDetect: function nfc_unsetTargetDetect() {
252                         try {
253                                 if (this.nfcTarget) {
254                                         this.nfcTarget.unsetReceiveNDEFListener();
255                                         this.nfcTarget = null;
256                                 } else {
257                                         //console.warn("app.nfc.nfcTarget not set");
258                                 }
259                         } catch (error) {
260                                 console.error('error: ' + error.message);
261                         }
262                         try {
263                                 this.nfcAdapter.unsetPeerListener();
264                         } catch (e) {
265                                 console.error('error: ' + e.message);
266                         }
267                 },
268
269                 unsetTagDetect: function nfc_unsetTagDetect() {
270                         try {
271                                 this.nfcAdapter.unsetTagListener();
272                         } catch (error) {
273                                 console.error('error: ' + error.message);
274                         }
275                 },
276
277                 displayContact: function nfc_displayContact(obj) {
278                         clearTimeout(this.app.timeOutHandler);
279                         this.unsetTargetDetect();
280                         this.unsetTagDetect();
281                         this.app.ui.moveToContactPage(obj);
282                 },
283
284                 sucDetach: function nfc_sucDetach() {
285                 }
286         };
287
288 }());