2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*jslint devel: true*/
18 /*global $, tizen, App, app */
19 (function () { // strict mode wrapper
23 * Constructs NFCControl
27 App.NFCControl = function NFCControl(app) {
32 App.NFCControl.prototype = {
34 nfcStateMemory: false,
37 * Initialize NFC application module
39 init: function nfc_init() {
40 this.card = new App.NFCCard(this);
41 this.peer = new App.NFCPeer(this);
42 this.endOfText = String.fromCharCode(3);
45 * Disable tag/target detection and move to start page
47 * @param {string} monit message text to be displayed
49 timeExpired: function nfc_timeExpired(monit) {
50 clearTimeout(this.app.timeOutHandler);
51 if (tizen.nfc.getDefaultAdapter().powered) {
52 this.unsetTargetDetect();
53 this.unsetTagDetect();
55 this.app.ui.moveToStartPage(monit);
59 * Get the end of text marker
63 getEndOfText: function nfc_getEndOfText() {
64 return this.endOfText;
69 * @param {string|object} contact
72 prepareForNFC: function nfc_prepareForNFC(contact) {
76 resolveData: function nfc_resolveData(value, endOfText) {
80 endOfText = endOfText || this.getEndOfText();
81 return value.replace(endOfText, '…');
85 resolveContact: function nfc_resolveContact(contactsString) {
86 var receivedContact = new tizen.Contact(contactsString, 'VCARD_30');
89 phone: receivedContact.phoneNumbers[0].number,
90 phone_type: receivedContact.phoneNumbers[0].types[0],
91 first: receivedContact.name.firstName,
92 middle: receivedContact.name.middleName,
93 last: receivedContact.name.lastName,
94 vCard: receivedContact.convertToString('VCARD_30')
100 * @param {type} record
101 * @returns {undefined}
103 fillRecordInfo: function nfc_fillRecordInfo(record) {
105 var contactsData = this.resolveContact(
106 this.convertNDEF2phoneNumber(record.payload)
108 this.app.nfc.displayContact(contactsData);
110 console.error(error);
116 * @param {NDEFMessage} message
118 readMessage: function nfc_readMessage(message) {
120 this.fillRecordInfo(message.records[0]);
122 console.error(e.message);
128 * @param {string|object} contact
129 * @returns {NDEFMessage}
131 contact2NDEF: function nfc_contact2NDEF(contact) {
133 len, i, newMessage = new tizen.NDEFMessage();
134 if (typeof contact === 'string') {
137 t = contact.convertToString('VCARD_30');
140 for (i = 0; i < len; i += 1) {
141 a[i] = t.charCodeAt(i);
143 newMessage.records[0] =
144 new tizen.NDEFRecordMedia('text/x-vcard', a);
150 * @param {string} contact
151 * @returns {NDEFMessage}
153 phoneNumber2NDEF: function nfc_phoneNumber2NDEF(contact) {
154 var phoneNumberArray = [],
156 length = contact.length,
157 newMessage = new tizen.NDEFMessage();
158 for (i = 0; i < length; i += 1) {
159 phoneNumberArray.push(contact.charCodeAt(i));
161 newMessage.records[0] =
162 new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
168 * @param {string} contact
171 convertNDEF2phoneNumber: function nfc_convertNDEF2phoneNumber(contact) {
172 var i, length = contact.length,
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(
234 this.onPowerOn.bind(this),
235 this.onPowerOnFails.bind(this)
238 console.error('startNFC problem', e);
239 this.app.ui.showPopupWarning();
243 stopNFC: function nfc_stopNFC() {
245 if (this.nfcStateMemory) {
248 this.nfcAdapter.setPowered(
250 this.onPowerOff.bind(this),
251 this.onPowerOffFails.bind(this)
255 console.error('setPowered(false) problem', err);
259 unsetTargetDetect: function nfc_unsetTargetDetect() {
261 if (this.nfcTarget) {
262 this.nfcTarget.unsetReceiveNDEFListener();
263 this.nfcTarget = null;
265 //console.info('app.nfc.nfcTarget not set');
268 console.error('error: ' + error.message);
271 this.nfcAdapter.unsetPeerListener();
273 console.error('error: ' + e.message);
277 unsetTagDetect: function nfc_unsetTagDetect() {
279 this.nfcAdapter.unsetTagListener();
281 console.error('error: ' + error.message);
285 displayContact: function nfc_displayContact(obj) {
286 clearTimeout(this.app.timeOutHandler);
287 if (tizen.nfc.getDefaultAdapter().powered) {
288 this.unsetTargetDetect();
289 this.unsetTagDetect();
291 this.app.ui.moveToContactPage(obj);
294 sucDetach: function nfc_sucDetach() {}