Updated application sources
[apps/web/sample/ContactsExchanger.git] / project / js / app.nfc.card.js
index d6ad313..a9e5e95 100644 (file)
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /*jslint devel: true*/
-/*global $, tizen, App, app */
+/*global $, tizen, App, app, localStorage */
 
 (function () { // strict mode wrapper
-       'use strict';
+    'use strict';
 
-       /**
-        * Constructs NFCCard
-        * @constructor
-        * @param {NFCControl} nfc
-        */
-       App.NFCCard = function nfc_NFCCard(nfc) {
-               this.nfc = nfc;
-       };
+    /**
+     * Constructs NFCCard
+     * @constructor
+     * @param {NFCControl} nfc
+     */
+    App.NFCCard = function nfc_NFCCard(nfc) {
+        this.nfc = nfc;
+    };
 
-       App.NFCCard.prototype = {
+    App.NFCCard.prototype = {
 
-               readMessageErr: function nfc_card_readMessageErr(e) {
-                       console.error('Read error! ' + e.message, e);
-                       this.nfc.timeExpired('Read error! ' + e.message);
-               },
+        readMessageErr: function nfc_card_readMessageErr(e) {
+            console.error('Read error! ' + e.message, e);
+            this.nfc.timeExpired('Read error! ' + e.message);
+        },
 
-               /**
-                * Read contents from a tag
-                * @param {NFCTag} tag
-                */
-               sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
-                       try {
-                               if (!tag.isSupportedNDEF) {
-                                       throw {message: "This tag doesn't support NDEF"};
-                               }
-                               tag.readNDEF(
-                                       this.nfc.readMessage.bind(this.nfc),
-                                       this.readMessageErr.bind(this)
-                               );
-                       } catch (e) {
-                               this.readMessageErr(e);
-                       }
-               },
+        /**
+         * Read contents from a tag
+         * @param {NFCTag} tag
+         */
+        sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
+            try {
+                if (!tag.isSupportedNDEF) {
+                    throw {
+                        message: "This tag doesn't support NDEF"
+                    };
+                }
+                tag.readNDEF(
+                    this.nfc.readMessage.bind(this.nfc),
+                    this.readMessageErr.bind(this)
+                );
+            } catch (e) {
+                this.readMessageErr(e);
+            }
+        },
 
-               /**
-                * Set tag listener
-                */
-               setTagDetectRead: function nfc_card_setTagDetectRead() {
-                       try {
-                               this.nfc.nfcAdapter.setTagListener({
-                                       onattach: this.sucTagReadAttach.bind(this),
-                                       ondetach: this.nfc.sucDetach.bind(this.nfc)
-                               });
-                       } catch (error) {
-                               this.readMessageErr(error);
-                       }
-               },
+        /**
+         * Set tag listener
+         */
+        setTagDetectRead: function nfc_card_setTagDetectRead() {
+            try {
+                this.nfc.nfcAdapter.unsetTagListener();
+                this.nfc.nfcAdapter.setTagListener({
+                    onattach: this.sucTagReadAttach.bind(this),
+                    ondetach: this.nfc.sucDetach.bind(this.nfc)
+                });
+            } catch (error) {
+                this.readMessageErr(error);
+            }
+        },
 
-               sucSend: function nfc_card_sucSend() {
-                       this.nfc.timeExpired('Send success!');
-               },
+        sucSend: function nfc_card_sucSend() {
+            this.nfc.timeExpired('Send success!');
+        },
 
-               errSend: function nfc_card_errSend(e) {
-                       console.warn('errSend', e);
-                       this.nfc.timeExpired('Write error! ' + e.message);
-               },
+        errSend: function nfc_card_errSend(e) {
+            console.warn('errSend', e);
+            this.nfc.timeExpired('Write error! ' + e.message);
+        },
 
-               sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
-                       var newMessage = null,
-                               fullContact = '';
+        sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
+            var newMessage = null,
+                fullContact = '';
 
-                       try {
-                               fullContact = this.nfc.prepareForNFC(localStorage);
-                               newMessage = this.nfc.phoneNumber2NDEF(fullContact);
-                               if (!tag.isSupportedNDEF) {
-                                       throw {message: "This tag doesn't support NDEF"};
-                               }
-                               tag.writeNDEF(
-                                       newMessage,
-                                       this.sucSend.bind(this),
-                                       this.errSend.bind(this)
-                               );
-                       } catch (e) {
-                               this.errSend(e);
-                       }
-               },
+            try {
+                fullContact = this.nfc.prepareForNFC(localStorage);
+                newMessage = this.nfc.phoneNumber2NDEF(fullContact);
+                if (!tag.isSupportedNDEF) {
+                    throw {
+                        message: "This tag doesn't support NDEF"
+                    };
+                }
+                tag.writeNDEF(
+                    newMessage,
+                    this.sucSend.bind(this),
+                    this.errSend.bind(this)
+                );
+            } catch (e) {
+                this.errSend(e);
+            }
+        },
 
-               setTagDetectWrite: function nfc_card_setTagDetectWrite() {
-                       var suc = {
-                               onattach: this.sucTagWriteAttach.bind(this),
-                               ondetach: this.nfc.sucDetach.bind(this.nfc)
-                       };
+        setTagDetectWrite: function nfc_card_setTagDetectWrite() {
+            var suc = {
+                onattach: this.sucTagWriteAttach.bind(this),
+                ondetach: this.nfc.sucDetach.bind(this.nfc)
+            };
 
-                       try {
-                               this.nfc.nfcAdapter.setTagListener(suc);
-                       } catch (error) {
-                               console.error(error);
-                       }
-               }
+            try {
+                this.nfc.nfcAdapter.unsetTagListener();
+                this.nfc.nfcAdapter.setTagListener(suc);
+            } catch (error) {
+                console.error(error);
+            }
+        }
 
-       };
+    };
 
-}());
+}());
\ No newline at end of file