application sources from tizen_2.2
[apps/web/sample/ContactsExchanger.git] / project / js / app.nfc.card.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app */
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         /**
8          * Constructs NFCCard
9          * @constructor
10          * @param {NFCControl} nfc
11          */
12         App.NFCCard = function nfc_NFCCard(nfc) {
13                 this.nfc = nfc;
14         };
15
16         App.NFCCard.prototype = {
17
18                 readMessageErr: function nfc_card_readMessageErr(e) {
19                         console.error('Read error! ' + e.message, e);
20                         this.nfc.timeExpired('Read error! ' + e.message);
21                 },
22
23                 /**
24                  * Read contents from a tag
25                  * @param {NFCTag} tag
26                  */
27                 sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
28                         try {
29                                 if (!tag.isSupportedNDEF) {
30                                         throw {message: "This tag doesn't support NDEF"};
31                                 }
32                                 tag.readNDEF(
33                                         this.nfc.readMessage.bind(this.nfc),
34                                         this.readMessageErr.bind(this)
35                                 );
36                         } catch (e) {
37                                 this.readMessageErr(e);
38                         }
39                 },
40
41                 /**
42                  * Set tag listener
43                  */
44                 setTagDetectRead: function nfc_card_setTagDetectRead() {
45                         try {
46                                 this.nfc.nfcAdapter.setTagListener({
47                                         onattach: this.sucTagReadAttach.bind(this),
48                                         ondetach: this.nfc.sucDetach.bind(this.nfc)
49                                 });
50                         } catch (error) {
51                                 this.readMessageErr(error);
52                         }
53                 },
54
55                 sucSend: function nfc_card_sucSend() {
56                         this.nfc.timeExpired('Send success!');
57                 },
58
59                 errSend: function nfc_card_errSend(e) {
60                         console.warn('errSend', e);
61                         this.nfc.timeExpired('Write error! ' + e.message);
62                 },
63
64                 sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
65                         var newMessage = null,
66                                 fullContact = '';
67
68                         try {
69                                 fullContact = this.nfc.prepareForNFC(localStorage);
70                                 newMessage = this.nfc.phoneNumber2NDEF(fullContact);
71                                 if (!tag.isSupportedNDEF) {
72                                         throw {message: "This tag doesn't support NDEF"};
73                                 }
74                                 tag.writeNDEF(
75                                         newMessage,
76                                         this.sucSend.bind(this),
77                                         this.errSend.bind(this)
78                                 );
79                         } catch (e) {
80                                 this.errSend(e);
81                         }
82                 },
83
84                 setTagDetectWrite: function nfc_card_setTagDetectWrite() {
85                         var suc = {
86                                 onattach: this.sucTagWriteAttach.bind(this),
87                                 ondetach: this.nfc.sucDetach.bind(this.nfc)
88                         };
89
90                         try {
91                                 this.nfc.nfcAdapter.setTagListener(suc);
92                         } catch (error) {
93                                 console.error(error);
94                         }
95                 }
96
97         };
98
99 }());