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