Tizen 2.0 Release
[samples/web/ContactsExchanger.git] / js / app.nfc.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app */
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         App.NFCControl = function (app) {
8                 this.app = app;
9                 this.init();
10         };
11
12         App.NFCControl.prototype = {
13                 nfcTarget: null,
14                 nfcStateMemory: false,
15
16                 init: function init() {
17                         this.card = new App.NFCCard(this);
18                         this.peer = new App.NFCPeer(this);
19                 },
20
21                 timeExpired: function timeExpired(monit) {
22                         clearTimeout(this.app.timeOutHandler);
23                         this.unsetTargetDetect();
24                         this.unsetTagDetect();
25                         this.app.ui.moveToStartPage(monit);
26                 },
27
28                 resolveContact: function resolveContact(contactsString) {
29                         var separator = String.fromCharCode(30),
30                                 endOfText = String.fromCharCode(3),
31                                 contactsArray,
32                                 phone = '',
33                                 first = '',
34                                 last = '',
35                                 resolveData;
36
37                         contactsArray = contactsString.split(separator);
38
39                         resolveData = function (value) {
40                                 if (!value) {
41                                         return '';
42                                 }
43                                 return value.replace(endOfText, '…');
44                         };
45
46                         phone = resolveData(contactsArray[0]);
47                         first = resolveData(contactsArray[1]);
48                         last = resolveData(contactsArray[2]);
49                         return {phone: phone, first: first, last: last};
50                 },
51
52                 fillRecordInfo: function fillRecordInfo(record) {
53                         try {
54                                 var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
55                                 this.app.ui.displayContact(contactsData);
56                         } catch (error) {
57                                 console.error(error);
58                         }
59                 },
60
61                 readMessage: function readMessage(message) {
62                         try {
63                                 this.fillRecordInfo(message.records[0]);
64                         } catch (e) {
65                                 console.error(e.message);
66                         }
67                 },
68
69                 contact2NDEF: function contact2NDEF(contact) {
70                         var t, a = [], i, newMessage = new tizen.NDEFMessage();
71                         if (typeof contact === 'string') {
72                                 t = contact;
73                         } else {
74                                 t = contact.convertToString("VCARD_30");
75                         }
76                         a.length = t.length;
77                         for (i = 0; i < a.length; i += 1) {
78                                 a[i] = t.charCodeAt(i);
79                         }
80                         newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
81                         return newMessage;
82                 },
83
84                 phoneNumber2NDEF: function phoneNumber2NDEF(contact) {
85                         var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage();
86                         for (i = 0; i < length; i += 1) {
87                                 phoneNumberArray.push(contact.charCodeAt(i));
88                         }
89                         newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
90                         return newMessage;
91                 },
92
93                 convertNDEF2phoneNumber: function convertNDEF2phoneNumber(contact) {
94                         var i, length = contact.length, phoneNumber = '';
95                         for (i = 0; i < length; i += 1) {
96                                 phoneNumber += String.fromCharCode(contact[i]);
97                         }
98                         return phoneNumber;
99                 },
100
101                 startNFC: function startNFC() {
102                         var onPowerOn, onPowerOnFails;
103
104                         onPowerOn = function () {
105                                 app.started = true;
106                                 app.ui.loadStartPage();
107                         };
108                         onPowerOnFails = function (err) {
109                                 console.error('Power On error: ' + err.message);
110                         };
111
112                         try {
113                                 this.nfcAdapter = tizen.nfc.getDefaultAdapter();
114                                 try {
115                                         if (this.nfcAdapter.powered) {
116                                                 this.nfcStateMemory = true;
117                                                 onPowerOn();
118                                         } else {
119                                                 this.nfcStateMemory = false;
120                                                 this.nfcAdapter.setPowered(true, onPowerOn, onPowerOnFails);
121                                         }
122                                 } catch (err) {
123                                         console.error('setPowered(true) problem: ' + err.message);
124                                 }
125                         } catch (e) {
126                                 console.error('getDefaultAdapter() method problem: ' + e.message);
127                         }
128                 },
129
130                 stopNFC: function stopNFC() {
131                         var onPowerOff, onPowerOffFails;
132
133                         onPowerOff = function () {
134                                 tizen.application.getCurrentApplication().exit();
135                         };
136
137                         onPowerOffFails = function (err) {
138                                 console.error('Power Off error: ' + err.message);
139                                 tizen.application.getCurrentApplication().exit();
140                         };
141
142                         try {
143                                 if (this.nfcStateMemory) {
144                                         onPowerOff();
145                                 } else {
146                                         this.nfcAdapter.setPowered(false, onPowerOff, onPowerOffFails);
147                                 }
148                         } catch (err) {
149                                 console.error('setPowered(false) problem: ' + err);
150                         }
151                 },
152
153                 unsetTargetDetect: function unsetTargetDetect() {
154                         try {
155                                 if (this.nfcTarget) {
156                                         this.nfcTarget.unsetReceiveNDEFListener();
157                                         this.nfcTarget = null;
158                                 } else {
159                                         console.warn("app.nfc.nfcTarget not set");
160                                 }
161                         } catch (error) {
162                                 console.error('error: ' + error.message);
163                         }
164                         try {
165                                 this.nfcAdapter.unsetPeerListener();
166                         } catch (e) {
167                                 console.error('error: ' + e.message);
168                         }
169                 },
170
171                 unsetTagDetect: function unsetTagDetect() {
172                         try {
173                                 this.nfcAdapter.unsetTagListener();
174                         } catch (error) {
175                                 console.error('error: ' + error.message);
176                         }
177                 },
178
179                 displayContact: function displayContact(obj) {
180                         clearTimeout(this.app.timeOutHandler);
181                         this.unsetTargetDetect();
182                         this.unsetTagDetect();
183                         this.app.ui.moveToContactPage(obj);
184                 },
185
186                 sucDetach: function sucDetach() {
187                 }
188         };
189
190 }());