a9e5e95f02ed8697aa4f37c4dc1d65518024896a
[apps/web/sample/ContactsExchanger.git] / project / js / app.nfc.card.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *              http://floralicense.org/license/
9  *
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.
15  */
16
17 /*jslint devel: true*/
18 /*global $, tizen, App, app, localStorage */
19
20 (function () { // strict mode wrapper
21     'use strict';
22
23     /**
24      * Constructs NFCCard
25      * @constructor
26      * @param {NFCControl} nfc
27      */
28     App.NFCCard = function nfc_NFCCard(nfc) {
29         this.nfc = nfc;
30     };
31
32     App.NFCCard.prototype = {
33
34         readMessageErr: function nfc_card_readMessageErr(e) {
35             console.error('Read error! ' + e.message, e);
36             this.nfc.timeExpired('Read error! ' + e.message);
37         },
38
39         /**
40          * Read contents from a tag
41          * @param {NFCTag} tag
42          */
43         sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
44             try {
45                 if (!tag.isSupportedNDEF) {
46                     throw {
47                         message: "This tag doesn't support NDEF"
48                     };
49                 }
50                 tag.readNDEF(
51                     this.nfc.readMessage.bind(this.nfc),
52                     this.readMessageErr.bind(this)
53                 );
54             } catch (e) {
55                 this.readMessageErr(e);
56             }
57         },
58
59         /**
60          * Set tag listener
61          */
62         setTagDetectRead: function nfc_card_setTagDetectRead() {
63             try {
64                 this.nfc.nfcAdapter.unsetTagListener();
65                 this.nfc.nfcAdapter.setTagListener({
66                     onattach: this.sucTagReadAttach.bind(this),
67                     ondetach: this.nfc.sucDetach.bind(this.nfc)
68                 });
69             } catch (error) {
70                 this.readMessageErr(error);
71             }
72         },
73
74         sucSend: function nfc_card_sucSend() {
75             this.nfc.timeExpired('Send success!');
76         },
77
78         errSend: function nfc_card_errSend(e) {
79             console.warn('errSend', e);
80             this.nfc.timeExpired('Write error! ' + e.message);
81         },
82
83         sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
84             var newMessage = null,
85                 fullContact = '';
86
87             try {
88                 fullContact = this.nfc.prepareForNFC(localStorage);
89                 newMessage = this.nfc.phoneNumber2NDEF(fullContact);
90                 if (!tag.isSupportedNDEF) {
91                     throw {
92                         message: "This tag doesn't support NDEF"
93                     };
94                 }
95                 tag.writeNDEF(
96                     newMessage,
97                     this.sucSend.bind(this),
98                     this.errSend.bind(this)
99                 );
100             } catch (e) {
101                 this.errSend(e);
102             }
103         },
104
105         setTagDetectWrite: function nfc_card_setTagDetectWrite() {
106             var suc = {
107                 onattach: this.sucTagWriteAttach.bind(this),
108                 ondetach: this.nfc.sucDetach.bind(this.nfc)
109             };
110
111             try {
112                 this.nfc.nfcAdapter.unsetTagListener();
113                 this.nfc.nfcAdapter.setTagListener(suc);
114             } catch (error) {
115                 console.error(error);
116             }
117         }
118
119     };
120
121 }());