From: HyungKyu Song Date: Fri, 15 Feb 2013 15:57:31 +0000 (+0900) Subject: Tizen 2.0 Release X-Git-Tag: submit/tizen_2.0/20130215.192321^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a74ad9aee7e27e42b1e56495cc5cd8332c052a47;hp=69abcabcdb292cc46bb217ed08a4bb621c1ba554;p=samples%2Fweb%2FContactsExchanger.git Tizen 2.0 Release --- diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e7736a8 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,6 @@ +Dariusz Paziewski +Pawel Sierszen +Tomasz Lukawski +Piotr Wronski +Piotr Szydelko +Tomasz Paciorek diff --git a/LICENSE.Flora b/LICENSE.Flora new file mode 100644 index 0000000..9c95663 --- /dev/null +++ b/LICENSE.Flora @@ -0,0 +1,206 @@ +Flora License + +Version 1.0, May, 2012 + +http://floralicense.org/license/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and +all other entities that control, are controlled by, or are +under common control with that entity. For the purposes of +this definition, "control" means (i) the power, direct or indirect, +to cause the direction or management of such entity, +whether by contract or otherwise, or (ii) ownership of fifty percent (50%) +or more of the outstanding shares, or (iii) beneficial ownership of +such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation source, +and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, +made available under the License, as indicated by a copyright notice +that is included in or attached to the work (an example is provided +in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, +that is based on (or derived from) the Work and for which the editorial +revisions, annotations, elaborations, or other modifications represent, +as a whole, an original work of authorship. For the purposes of this License, +Derivative Works shall not include works that remain separable from, +or merely link (or bind by name) to the interfaces of, the Work and +Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original +version of the Work and any modifications or additions to that Work or +Derivative Works thereof, that is intentionally submitted to Licensor +for inclusion in the Work by the copyright owner or by an individual or +Legal Entity authorized to submit on behalf of the copyright owner. +For the purposes of this definition, "submitted" means any form of +electronic, verbal, or written communication sent to the Licensor or +its representatives, including but not limited to communication on +electronic mailing lists, source code control systems, and issue +tracking systems that are managed by, or on behalf of, the Licensor +for the purpose of discussing and improving the Work, but excluding +communication that is conspicuously marked or otherwise designated +in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +"Tizen Certified Platform" shall mean a software platform that complies +with the standards set forth in the Compatibility Definition Document +and passes the Compatibility Test Suite as defined from time to time +by the Tizen Technical Steering Group and certified by the Tizen +Association or its designated agent. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work +solely as incorporated into a Tizen Certified Platform, where such +license applies only to those patent claims licensable by such +Contributor that are necessarily infringed by their Contribution(s) +alone or by combination of their Contribution(s) with the Work solely +as incorporated into a Tizen Certified Platform to which such +Contribution(s) was submitted. If You institute patent litigation +against any entity (including a cross-claim or counterclaim +in a lawsuit) alleging that the Work or a Contribution incorporated +within the Work constitutes direct or contributory patent infringement, +then any patent licenses granted to You under this License for that +Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof pursuant to the copyright license +above, in any medium, with or without modifications, and in Source or +Object form, provided that You meet the following conditions: + + 1. You must give any other recipients of the Work or Derivative Works + a copy of this License; and + 2. You must cause any modified files to carry prominent notices stating + that You changed the files; and + 3. You must retain, in the Source form of any Derivative Works that + You distribute, all copyright, patent, trademark, and attribution + notices from the Source form of the Work, excluding those notices + that do not pertain to any part of the Derivative Works; and + 4. If the Work includes a "NOTICE" text file as part of its distribution, + then any Derivative Works that You distribute must include a readable + copy of the attribution notices contained within such NOTICE file, + excluding those notices that do not pertain to any part of + the Derivative Works, in at least one of the following places: + within a NOTICE text file distributed as part of the Derivative Works; + within the Source form or documentation, if provided along with the + Derivative Works; or, within a display generated by the Derivative Works, + if and wherever such third-party notices normally appear. + The contents of the NOTICE file are for informational purposes only + and do not modify the License. + +You may add Your own attribution notices within Derivative Works +that You distribute, alongside or as an addendum to the NOTICE text +from the Work, provided that such additional attribution notices +cannot be construed as modifying the License. You may add Your own +copyright statement to Your modifications and may provide additional or +different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works +as a whole, provided Your use, reproduction, and distribution of +the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Flora License to your work + +To apply the Flora License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Flora License, Version 1.0 (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. + diff --git a/NOTICE.Flora b/NOTICE.Flora new file mode 100644 index 0000000..fdb699a --- /dev/null +++ b/NOTICE.Flora @@ -0,0 +1,4 @@ +Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved. +Except as noted, this software is licensed under Flora License, Version 1. +Please, see the LICENSE file for Flora License terms and conditions. + diff --git a/config.xml b/config.xml new file mode 100644 index 0000000..d0586a0 --- /dev/null +++ b/config.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + ContactsExchanger + + + + + + + + + + + + + + diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..80b16c9 --- /dev/null +++ b/css/style.css @@ -0,0 +1,87 @@ +.ui-btn-start { + margin: 0px auto; + margin-top: 10px; + padding: 40px 0px 0px 0px; + width: 90%; + height: 50px; + font-size: 16px; +} + +.defaultText { + font-size: 20px; + text-align: center; +} + +.comment { + font-size: 16px; + text-align: center; + line-height: 3px; + color: black; + padding: 0px; + margin: 0px 0px 12px 0px; +} + +#comment { + position: absolute; + top: -50px; + left: 0px; + margin: 0 auto; + right: 0px; +} + +.box { + margin: 0px; + padding: 0px; + margin-top: 50%; +} + +.gap { + width: 100%; + height: 10px; +} + +#list-choose li a { + white-space: nowrap; + overflow: hidden !important; + text-overflow: ellipsis; + width: 60%; + float: left; +} + +#list-choose li .ui-li-text-sub { + white-space: nowrap; + overflow: hidden !important; + text-overflow: ellipsis; + width: 40%; +} + +#comment-name { + white-space: nowrap; + overflow: hidden !important; + text-overflow: ellipsis; + width: 90%; + margin-left: 5%; + line-height: inherit; + font-size: 1rem; + font-weight: bold; +} + +#accept-choose { + width: auto; +} +.defaultVeryBigText { + font-size: 30px; + margin: 0px; + padding: 0px; + text-align: center; + line-height: 30px; +} + +.defaultCounterText { + margin-top: 10px; + margin-bottom: 10px; + font-size: 100px; + padding: 0px; + text-align: center; + line-height: 100px; +} \ No newline at end of file diff --git a/icon.png b/icon.png new file mode 100755 index 0000000..983c883 Binary files /dev/null and b/icon.png differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..d218962 --- /dev/null +++ b/index.html @@ -0,0 +1,75 @@ + + + + + + + + Contacts-exchanger + + + + + + + + + + + + + + + +
+
+

Contacts-exchanger

+
+ +
+ + +
+ +
+
+

Contacts list

+ Accept +
+ +
+ + +
+ +
+
+

+
+ +
+ + +
+ +
+
+

Add contact

+ Save +
+ +
+ + +
+ + + diff --git a/js/app.js b/js/app.js new file mode 100644 index 0000000..25be9fb --- /dev/null +++ b/js/app.js @@ -0,0 +1,135 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.0 (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 */ + +var App = null; + +// App instance +var app = null; + +(function () { // strict mode wrapper + 'use strict'; + + function checkCharsSize(string) { + var i, result = '', length = string.length; + for (i = 0; i < length; i += 1) { + if (string.charCodeAt(i) < 256) { + result += string[i]; + } + } + return result; + } + + /** + * Creates a new application object + * + * @class Application + */ + App = function App() {}; + + App.prototype = { + nfcAdapter: null, + addressBook: null, + started: false, + timeOutHandler: null, + nfc: null, + + init: function appInit() { + this.ui = new App.Ui(this); + this.nfc = new App.NFCControl(this); + + this.ui.defineEvents(); + this.initAddressBook(); + this.nfc.startNFC(); + }, + + saveDefaultCard: function saveDefaultCard() { + var elementSelected = $('#list-choose li.selected'); + console.log(elementSelected); + localStorage.started = true; + localStorage.id = elementSelected.attr('id'); + localStorage.firstName = elementSelected.attr('firstName'); + localStorage.lastName = elementSelected.attr('lastName'); + localStorage.phoneNumber = elementSelected.attr('phoneNumber'); + localStorage.vCard = elementSelected.attr('vCard'); + $.mobile.changePage('#start'); + }, + + initAddressBook: function initAddressBook() { + var addressBooksCB = function (addressbooks) { + console.log('addressBooksCB success'); + if (addressbooks.length > 0) { + this.addressBook = addressbooks[0]; + } else { + console.log('addressBook: failed'); + } + }, + errorCB = function (e) { + console.log('problem with getAddressBooks() method: ' + e.message); + }; + + try { + tizen.contact.getAddressBooks(addressBooksCB.bind(this), errorCB); + } catch (e) { + console.log('problem with getAddressBooks() method: ' + e.message); + } + }, + + countDown: function countDown(time, obj) { + obj.text(time); + if (time > 0) { + time -= 1; + this.timeOutHandler = setTimeout(function () { this.countDown(time, obj); }.bind(this), 1000); + } else { + this.nfc.timeExpired(); + } + }, + + saveContact: function saveContact() { + var phone, first, last, contactPage = $('#contact'), contact = null; + phone = contactPage.data('contactsData').phone; + first = contactPage.data('contactsData').first; + last = contactPage.data('contactsData').last; + + try { + contact = new tizen.Contact({name: new tizen.ContactName({firstName: first, lastName: last}), phoneNumbers: [new tizen.ContactPhoneNumber(phone)]}); + this.addressBook.add(contact); + this.app.ui.moveToStartPage('New contact added'); + } catch (err) { + this.app.ui.moveToStartPage('Problem with new contact adding'); + console.log('The following error occurred while adding: ' + err.name); + } + }, + + loadContacts: function loadContacts() { + var self = this, + errorCB = function (e) { + console.log('problem with find() method: ' + e.message); + }; + this.addressBook.find(function (contacts) { + self.ui.showContactsList(contacts); + }, errorCB); + } + + }; + +}()); + +app = new App(); + +$(document).ready(app.init.bind(app)); diff --git a/js/app.nfc.card.js b/js/app.nfc.card.js new file mode 100644 index 0000000..a7b1532 --- /dev/null +++ b/js/app.nfc.card.js @@ -0,0 +1,109 @@ +/*jslint devel: true*/ +/*global $, tizen, App, app */ + +(function () { // strict mode wrapper + 'use strict'; + + App.NFCCard = function (nfcControl) { + this.app = nfcControl.app; + console.log('NFCCard: ', nfcControl); + }; + + App.NFCCard.prototype = { + + prepareForNFC: null, + + /* card read */ + + readMessageErr: function readMessageErr(e) { + app.nfc.timeExpired('Read error!'); + }, + + sucTagReadAttach: function sucTagReadAttach(tag) { + if (tag.isSupportedNDEF) { + tag.readNDEF(this.app.nfc.readMessage.bind(this.app.nfc), this.readMessageErr.bind(this)); + } else { + console.log("This Tag doesn't support NDEF"); + } + }, + + setTagDetectRead: function setTagDetectRead() { + /* + var err, suc; + err = function (e) { + console.log('Tag Listen Error: ' + e.message); + }; + */ + var suc = { onattach: this.sucTagReadAttach.bind(this), ondetach: this.app.nfc.sucDetach.bind(this.app.nfc) }; + try { + //nfcAdapter.setTagListener(suc, err); + app.nfc.nfcAdapter.setTagListener(suc); + } catch (error) { + console.log('error: ' + error.message); + } + }, + + + /* card write */ + + sucSend: function sucSend() { + console.log('sucSend'); + app.nfc.timeExpired('Send success!'); + }, + + errSend: function errSend(e) { + console.log('errSend'); + app.nfc.timeExpired('Send problem! ' + e.message); + }, + + sucTagWriteAttach: function sucTagWriteAttach(tag) { + var newMessage = null, + separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName, + prepareForNFC; + + prepareForNFC = this.prepareForNFC || function prepareForNFC(contact) { + if (contact.length > 31) { + contact = contact.substring(0, 31); + if (contact[29] !== separator) { + contact = contact.substring(0, 30) + endOfText; + } + } + return contact; + }; + + fullContact = prepareForNFC(fullContact); + + try { + newMessage = this.app.nfc.phoneNumber2NDEF(fullContact); + } catch (err) { + console.log("NDEFMessage problem: " + err.message); + } + + if (tag.isSupportedNDEF) { + tag.writeNDEF(newMessage, this.sucSend.bind(this), this.errSend.bind(this)); + } else { + console.log("This Tag doesn't support NDEF"); + } + }, + + setTagDetectWrite: function setTagDetectWrite() { + /* + var err, suc; + err = function (e) { + console.log('Tag Listen Error: ' + e.message); + }; + */ + var suc = { onattach: this.sucTagWriteAttach.bind(this), ondetach: this.app.nfc.sucDetach }; + try { + //nfcAdapter.setTagListener(suc, err); + app.nfc.nfcAdapter.setTagListener(suc); + } catch (error) { + console.log('error: ' + error.message); + } + } + + }; + +}()); diff --git a/js/app.nfc.js b/js/app.nfc.js new file mode 100644 index 0000000..a4d151a --- /dev/null +++ b/js/app.nfc.js @@ -0,0 +1,200 @@ +/*jslint devel: true*/ +/*global $, tizen, App, app */ + +(function () { // strict mode wrapper + 'use strict'; + + App.NFCControl = function (app) { + console.log('NFCControl: ', app); + this.app = app; + this.init(); + }; + + App.NFCControl.prototype = { + nfcTarget: null, + nfcStateMemory: false, + + init: function init() { + this.card = new App.NFCCard(this); + this.peer = new App.NFCPeer(this); + }, + + timeExpired: function timeExpired(monit) { + console.log('timeExpired: ', this); + clearTimeout(this.app.timeOutHandler); + console.log(this.nfcTarget, 'this.nfcTarget'); + this.unsetTargetDetect(); + this.unsetTagDetect(); + this.app.ui.moveToStartPage(monit); + }, + + resolveContact: function resolveContact(contactsString) { + var separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + contactsArray, + phone = '', + first = '', + last = '', + resolveData; + + contactsArray = contactsString.split(separator); + + resolveData = function (value) { + if (!value) { + return ''; + } + return value.replace(endOfText, '…'); + }; + + phone = resolveData(contactsArray[0]); + first = resolveData(contactsArray[1]); + last = resolveData(contactsArray[2]); + return {phone: phone, first: first, last: last}; + }, + + fillRecordInfo: function fillRecordInfo(record) { + try { + var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload)); + this.app.ui.displayContact(contactsData); + } catch (error) { + console.log(error); + } + }, + + readMessage: function readMessage(message) { + console.log('readMessage', message); + try { + this.fillRecordInfo(message.records[0]); + } catch (e) { + console.log(e.message); + } + }, + + contact2NDEF: function contact2NDEF(contact) { + var t, a = [], i, newMessage = new tizen.NDEFMessage(); + if (typeof contact === 'string') { + t = contact; + } else { + t = contact.convertToString("VCARD_30"); + } + a.length = t.length; + for (i = 0; i < a.length; i += 1) { + a[i] = t.charCodeAt(i); + } + newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a); + return newMessage; + }, + + phoneNumber2NDEF: function phoneNumber2NDEF(contact) { + var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage(); + for (i = 0; i < length; i += 1) { + phoneNumberArray.push(contact.charCodeAt(i)); + } + newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray); + return newMessage; + }, + + convertNDEF2phoneNumber: function convertNDEF2phoneNumber(contact) { + var i, length = contact.length, phoneNumber = ''; + for (i = 0; i < length; i += 1) { + phoneNumber += String.fromCharCode(contact[i]); + } + return phoneNumber; + }, + + startNFC: function startNFC() { + var onPowerOn, onPowerOnFails; + + onPowerOn = function () { + app.started = true; + app.ui.loadStartPage(); + }; + onPowerOnFails = function (err) { + console.log('Power On error: ' + err.message); + }; + + try { + this.nfcAdapter = tizen.nfc.getDefaultAdapter(); + try { + if (this.nfcAdapter.powered) { + console.log('this.nfcAdapter.powered'); + this.nfcStateMemory = true; + onPowerOn(); + } else { + console.log('!this.nfcAdapter.powered'); + this.nfcStateMemory = false; + this.nfcAdapter.setPowered(true, onPowerOn, onPowerOnFails); + } + } catch (err) { + console.log('setPowered(true) problem: ' + err); + } + } catch (e) { + console.log('getDefaultAdapter() method problem: ' + e); + } + }, + + stopNFC: function stopNFC() { + var onPowerOff, onPowerOffFails; + + onPowerOff = function () { + tizen.application.getCurrentApplication().exit(); + }; + + onPowerOffFails = function (err) { + console.log('Power Off error: ' + err.message); + tizen.application.getCurrentApplication().exit(); + }; + + try { + if (this.nfcStateMemory) { + console.log('this.nfcStateMemory'); + onPowerOff(); + } else { + console.log('!this.nfcStateMemory'); + this.nfcAdapter.setPowered(false, onPowerOff, onPowerOffFails); + } + } catch (err) { + console.log('setPowered(false) problem: ' + err); + } + }, + + unsetTargetDetect: function unsetTargetDetect() { + console.log('unsetTargetDetect: ', this); + try { + if (this.nfcTarget) { + this.nfcTarget.unsetReceiveNDEFListener(); + this.nfcTarget = null; + } else { + console.warn("app.nfc.nfcTarget not set"); + } + } catch (error) { + console.log('error: ' + error.message); + } + try { + this.nfcAdapter.unsetPeerListener(); + } catch (e) { + console.log('error: ' + e.message); + } + }, + + unsetTagDetect: function unsetTagDetect() { + try { + this.nfcAdapter.unsetTagListener(); + } catch (error) { + console.log('error: ' + error.message); + } + }, + + displayContact: function displayContact(obj) { + clearTimeout(this.app.timeOutHandler); + this.unsetTargetDetect(); + this.unsetTagDetect(); + this.app.ui.moveToContactPage(obj); + }, + + sucDetach: function sucDetach() { + console.log('sucDetach'); + } + }; + +}()); diff --git a/js/app.nfc.peer.js b/js/app.nfc.peer.js new file mode 100644 index 0000000..07a4983 --- /dev/null +++ b/js/app.nfc.peer.js @@ -0,0 +1,72 @@ +/*jslint devel: true*/ +/*global $, tizen, App, app */ + +(function () { // strict mode wrapper + 'use strict'; + + App.NFCPeer = function (nfcControl) { + this.app = nfcControl.app; + }; + + App.NFCPeer.prototype = { + + sucSendToTarget: function sucSendToTarget() { + //timeExpired('Send success!'); + }, + + errSendToTarget: function errSendToTarget(e) { + this.app.nfc.timeExpired('Send problem! ' + e.message); + }, + + errorMessage: function errorMessage(e) { + console.log('Target Receiving Error: ' + e.message); + }, + + setReceiveFromTarget: function setReceiveFromTarget() { + try { + if (this.app.nfc.nfcTarget) { + this.app.nfc.nfcTarget.setReceiveNDEFListener(this.app.nfc.readMessage.bind(this.app.nfc), this.errorMessage.bind(this)); + } else { + console.warn("app.nfc.nfcTarget not set"); + } + } catch (error) { + console.log('setReceiveFromTarget error: ' + error.message); + } + }, + + sucTargetAttach: function sucTargetAttach(target) { + var newMessage = null, + separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName; + console.log('sucTargetAttach: ', target); + this.app.nfc.nfcTarget = target; + this.setReceiveFromTarget(); + + try { + newMessage = this.app.nfc.phoneNumber2NDEF(fullContact); + } catch (err) { + console.log("NDEFMessage problem: " + err.message); + } + + try { + target.sendNDEF(newMessage, this.sucSendToTarget.bind(this), this.errSendToTarget.bind(this)); + } catch (e) { + console.log("NDEFMessage problem: " + e.message); + } + }, + + setTargetDetect: function setTargetDetect() { + var err = function (e) { + console.log('Target Listen Error: ' + e.message); + }, + suc = { onattach: this.sucTargetAttach.bind(this), ondetach: this.app.nfc.sucDetach.bind(this.app.nfc) }; + try { + app.nfc.nfcAdapter.setPeerListener(suc, err); + } catch (error) { + console.log('error: ' + error.message); + } + } + }; + +}()); diff --git a/js/app.ui.js b/js/app.ui.js new file mode 100644 index 0000000..e51f9d1 --- /dev/null +++ b/js/app.ui.js @@ -0,0 +1,325 @@ + +/*global $, tizen, App, app */ + +App.Ui = null; + +(function () { // strict mode wrapper + 'use strict'; + + var prepareCallerName = function prepareCallerName(contact) { + var callerName, firstName, lastName; + callerName = ''; + firstName = contact.name.firstName; + lastName = contact.name.lastName; + if (firstName !== '' && firstName !== null) { + callerName = firstName; + } + if (lastName !== '' && lastName !== null) { + if (callerName === '') { + callerName += lastName; + } else { + callerName += ' '; + callerName += lastName; + } + } + if (callerName === '') { + callerName = 'no name'; + } + return callerName; + }; + + App.Ui = function App_Ui(app) { + this.app = app; + }; + + App.Ui.prototype = { + popup: null, + + showPopup: function showPopup(message, page) { + this.popup = $(''); + this.page.append(this.popup); + this.page.trigger('create'); + this.popup.data('page', page); + this.popup.popupwindow('open'); + }, + + closePopup: function closePopup() { + var page = this.popup.data('page'); + page.data('monit', ''); + this.popup.popupwindow('close'); + this.popup.remove(); + this.popup = null; + }, + + disableAccept: function disableAccept() { + $('#accept-choose').css('pointer-events', 'none').addClass('ui-disabled'); + }, + + enableAccept: function enableAccept() { + $('#accept-choose').css('pointer-events', 'auto').removeClass('ui-disabled'); + }, + + createListRecord: function createListRecord(contacts) { + $.mobile.changePage('#choose'); + }, + + prepareWaitingPage: function prepareWaitingPage(title, text) { + var waitingBox, waitingContent; + waitingBox = $('
'); + waitingContent = $('

' + text + '

'); + $('#header-transfer H1').text(title); + $('#content-transfer .ui-scrollview-view').empty(); + waitingBox.append(waitingContent); + $('#content-transfer .ui-scrollview-view').append(waitingBox); + $('#content-start').trigger('create'); + this.app.countDown(10, $('#counter')); + }, + + loadTemporaryContent: function loadTemporaryContent() { + var temporaryBox, temporaryContent, temporaryButton; + temporaryBox = $('
'); + temporaryContent = $('

Default card hasn\'t defined yet!
Do you want to define it now?

'); + temporaryButton = $('
Create default card
'); + + temporaryButton.on('tap', function (event) { + event.preventDefault(); + $.mobile.changePage('#choose'); + }); + + $('#content-start .ui-scrollview-view').empty(); + temporaryBox.append(temporaryContent).append(temporaryButton); + $('#content-start .ui-scrollview-view').append(temporaryBox); + $('#content-start').trigger('create'); + }, + + loadStartContent: function loadStartContent() { + var startBox, gap, comment, changeContact, readFromCard, writeToCard, communicateWithOtherDevice; + startBox = $('
'); + gap = $('
'); + comment = $('

Your default contact

' + (localStorage.firstName || '') + ' ' + (localStorage.lastName || '') + '

'); + changeContact = $('
Change your default contact
'); + changeContact.on('tap', function (event) { + event.preventDefault(); + $.mobile.changePage('#choose'); + }); + readFromCard = $('
Read from card
'); + readFromCard.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'read'); + $.mobile.changePage('#transfer'); + }); + writeToCard = $('
Write to card
'); + writeToCard.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'write'); + $.mobile.changePage('#transfer'); + }); + communicateWithOtherDevice = $('
Communicate with other device
'); + communicateWithOtherDevice.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'communicate'); + $.mobile.changePage('#transfer'); + }); + + $('#content-start .ui-scrollview-view').empty(); + startBox.append(changeContact).append(gap.clone()).append(readFromCard).append(gap.clone()).append(writeToCard).append(gap.clone()).append(communicateWithOtherDevice).prepend(comment); + //$('#content-start .ui-scrollview-view').append(comment); + $('#content-start .ui-scrollview-view').append(startBox); + $('#content-start').trigger('create'); + }, + + loadStartPage: function loadStartPage() { + if (localStorage.started === undefined) { + console.log('localStorage.started === undefined'); + this.loadTemporaryContent(); + } else { + console.log('localStorage.started !== undefined'); + this.loadStartContent(); + } + }, + + prepareContactsTemplate: function prepareContactsTemplate(phone, first, last) { + var i, ul; + + ul = $('
    '); + $('#contact > #content-contact > .ui-scrollview-view').empty().append(ul); + + ul.append($('
  • ' + ((first === '' || first === 'null') ? '...' : first) + 'First Name
  • ')); + ul.append($('
  • ' + ((last === '' || last === 'null') ? '...' : last) + 'Last Name
  • ')); + ul.append($('
  • ' + ((phone === '' || phone === 'null') ? '...' : phone) + 'Phone Number
  • ')); + + $('#contact > #content-contact').trigger('create'); + }, + + showContactsList: function showContactsList(contacts) { + console.log(this, 'showContactsList'); + var self = this, + ul = $('
      '), + listElementTap, + sortedContactList = [], + i, + j, + len, + length, + firstName = '', + lastName = '', + phoneNumber = '', + id, + vCard, + listElement; + + $('#choose > #content-choose > .ui-scrollview-view').empty().append(ul); + + listElementTap = function (event) { + event.preventDefault(); + console.log(self, 'listElementTap'); + self.enableAccept(); + console.log($(this), 'jQuery $(this)'); + $(this).parent().find('li').css('background-color', 'inherit').removeClass('selected'); + $(this).css('background-color', '#ccc').addClass('selected'); + }; + + for (i = 0, len = contacts.length; i < len; i += 1) { + firstName = ''; + lastName = ''; + phoneNumber = ''; + id = contacts[i].id; + vCard = contacts[i].convertToString('VCARD_30'); + if (contacts[i].name.firstName !== null) { + firstName = contacts[i].name.firstName; + } + if (contacts[i].name.lastName !== null) { + lastName = contacts[i].name.lastName; + } + if (contacts[i].phoneNumbers[0] !== undefined) { + phoneNumber = contacts[i].phoneNumbers[0].number; + } + sortedContactList.push({caller: prepareCallerName(contacts[i]), firstName: firstName, lastName: lastName, phoneNumber: phoneNumber, id: id, vCard: vCard, contact: contacts[i]}); + } + + sortedContactList.sort(function (a, b) { + if (a.caller < b.caller) { + return -1; + } + if (a.caller > b.caller) { + return 1; + } + return 0; + }); + + for (j = 0, length = sortedContactList.length; j < length; j += 1) { + listElement = $('
    • ' + sortedContactList[j].caller + '' + sortedContactList[j].phoneNumber + '
    • '); + if (localStorage.id === listElement.attr('id')) { + listElement.css('background-color', '#ccc'); + } + listElement.on('tap', listElementTap); + ul.append(listElement); + } + + $('#choose > #content-choose').trigger('create'); + }, + + moveToStartPage: function moveToStartPage(monit) { + $('#start').data('monit', monit || ''); + $.mobile.changePage('#start'); + }, + + moveToContactPage: function moveToContactPage(obj) { + $('#start').data('monit', ''); + $('#contact').data('contactsData', obj); + $.mobile.changePage('#contact'); + }, + + + defineEvents: function defineEvents() { + var self = this; + + $('#header-start .ui-btn-back').on('tap', function (event) { + event.preventDefault(); + self.app.nfc.stopNFC(); + }); + + $('#footer-transfer').on('tap', '.ui-btn-back', function (event) { + event.preventDefault(); + self.app.nfc.timeExpired(); + }); + + $('#footer-contact').on('tap', '.ui-btn-back', function (event) { + event.preventDefault(); + $.mobile.changePage('#start'); + }); + + $('#choose').on('pagebeforeshow', function () { + self.disableAccept(); + }); + + $('#choose').on('pageshow', function (event) { + console.log('#choose pageshow'); + self.app.loadContacts(); + }); + + $('#contact').on('pageshow', function (event) { + var phone, first, last; + phone = $(this).data('contactsData').phone; + first = $(this).data('contactsData').first; + last = $(this).data('contactsData').last; + self.prepareContactsTemplate(phone, first, last); + }); + + $('#accept-choose').on('tap', function (event) { + event.preventDefault(); + self.app.saveDefaultCard(); + }); + + $('#save-contact').on('tap', function (event) { + event.preventDefault(); + self.app.saveContact(); + }); + + $('#start').on('pagebeforeshow', function () { + if (self.app.started) { + self.loadStartPage(); + } + }); + + $('#start').on('pageshow', function () { + var monit, obj; + obj = $(this); + monit = obj.data('monit'); + if (monit !== '' && monit !== undefined) { + self.showPopup(obj.data('monit'), obj); + } + }); + + $('#transfer').on('pageshow', function () { + console.log($('#transfer').data('option'), 'XXXXXXXXx'); + console.log($(this).data('option'), 'ZZZZZZ'); + var option = $(this).data('option'); + if (option === 'read') { + self.prepareWaitingPage('Card to Device', 'PUT WIRELESS TAG
      CLOSE TO
      YOUR DEVICE'); + try { + self.app.nfc.card.setTagDetectRead(); + } catch (err) { + console.log(err); + } + } else if (option === 'write') { + self.prepareWaitingPage('Device to Card', 'PUT WIRELESS TAG
      CLOSE TO
      YOUR DEVICE'); + try { + self.app.nfc.card.setTagDetectWrite(); + } catch (er) { + console.log(er); + } + } else { + self.prepareWaitingPage('Device to Device', 'PUT YOUR DEVICE
      CLOSE TO
      OTHER DEVICE'); + try { + self.app.nfc.peer.setTargetDetect(); + } catch (e) { + console.log(e); + } + } + }); + } + + }; + +}()); diff --git a/js/device_to_device.js b/js/device_to_device.js new file mode 100644 index 0000000..e2bc9ab --- /dev/null +++ b/js/device_to_device.js @@ -0,0 +1,62 @@ +/*jslint devel: true*/ +/*global $, tizen, nfcAdapter, timeOutHandler, nfcTarget: true, sucDetach, timeExpired, readMessage, phoneNumber2NDEF */ + +var sucSendToTarget = function () { + "use strict"; + //timeExpired('Send success!'); +}; + +var errSendToTarget = function (e) { + "use strict"; + timeExpired('Send problem! ' + e.message); +}; + +var errorMessage = function (e) { + "use strict"; + console.log('Target Receiving Error: ' + e.message); +}; + +var setReceiveFromTarget = function () { + "use strict"; + try { + nfcTarget.setReceiveNDEFListener(readMessage, errorMessage); + } catch (error) { + console.log('setReceiveFromTarget error: ' + error.message); + } +}; + +var sucTargetAttach = function (target) { + "use strict"; + var newMessage = null, + separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName; + + nfcTarget = target; + setReceiveFromTarget(); + + try { + newMessage = phoneNumber2NDEF(fullContact); + } catch (err) { + console.log("NDEFMessage problem: " + err.message); + } + + try { + nfcTarget.sendNDEF(newMessage, sucSendToTarget, errSendToTarget); + } catch (e) { + console.log("NDEFMessage problem: " + e.message); + } +}; + +var setTargetDetect = function () { + "use strict"; + var err = function (e) { + console.log('Target Listen Error: ' + e.message); + }, + suc = { onattach: sucTargetAttach, ondetach: sucDetach }; + try { + nfcAdapter.setPeerListener(suc, err); + } catch (error) { + console.log('error: ' + error.message); + } +}; \ No newline at end of file diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..1932e73 --- /dev/null +++ b/js/main.js @@ -0,0 +1,412 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.tizenopensource.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, timeExpired, contactList: true, moveToStartPage, stopNFC, setTagDetectRead, setTagDetectWrite, setTargetDetect, startNFC */ + +var nfcAdapter = null; +var addressBook = null; +var appStarted = false; +var timeOutHandler = null; +var popup = null; +var nfcTarget = null; +var nfcStateMemory = false; + +var showPopup = function (message, page) { + "use strict"; + popup = $(''); + page.append(popup); + page.trigger('create'); + popup.data('page', page); + popup.popupwindow('open'); +}; + +var closePopup = function () { + "use strict"; + var page = popup.data('page'); + page.data('monit', ''); + popup.popupwindow('close'); + popup.remove(); +}; + +var disableAccept = function () { + "use strict"; + $('#accept-choose').css('pointer-events', 'none').addClass('ui-disabled'); +}; + +var enableAccept = function () { + "use strict"; + $('#accept-choose').css('pointer-events', 'auto').removeClass('ui-disabled'); +}; + +var checkCharsSize = function (string) { + "use strict"; + var i, result = '', length = string.length; + for (i = 0; i < length; i += 1) { + if (string.charCodeAt(i) < 256) { + result += string[i]; + } + } + return result; +}; + +var saveDefaultCard = function () { + "use strict"; + var elementSelected = $('#message-chat li.selected'); + localStorage.started = true; + localStorage.id = elementSelected.attr('id'); + localStorage.firstName = checkCharsSize(elementSelected.attr('firstName')); + localStorage.lastName = checkCharsSize(elementSelected.attr('lastName')); + localStorage.phoneNumber = checkCharsSize(elementSelected.attr('phoneNumber')); + localStorage.vCard = elementSelected.attr('vCard'); + $.mobile.changePage('#start'); +}; + +var initAddressBook = function () { + "use strict"; + var addressBooksCB = function (addressbooks) { + if (addressbooks.length > 0) { + addressBook = addressbooks[0]; + } else { + console.log('addressBook: failed'); + } + }, + errorCB = function (e) { + console.log('problem with getAddressBooks() method: ' + e.message); + }; + + try { + tizen.contact.getAddressBooks(addressBooksCB, errorCB); + } catch (e) { + console.log('problem with getAddressBooks() method: ' + e.message); + } +}; + +var createListRecord = function (contacts) { + "use strict"; + contactList = contacts; + $.mobile.changePage('#choose'); +}; + +var prepareCallerName = function (contact) { + "use strict"; + var callerName, firstName, lastName; + callerName = ''; + firstName = contact.name.firstName; + lastName = contact.name.lastName; + if (firstName !== '' && firstName !== null) { + callerName = firstName; + } + if (lastName !== '' && lastName !== null) { + if (callerName === '') { + callerName += lastName; + } else { + callerName += ' '; + callerName += lastName; + } + } + if (callerName === '') { + callerName = 'no name'; + } + return callerName; +}; + +var showContactsList = function () { + "use strict"; + var errorCB, + contactsFoundCB; + + contactsFoundCB = function (contacts) { + var ul = $('
        '), + listElementTap, + sortedContactList = [], + i, + j, + len, + length, + firstName = '', + lastName = '', + phoneNumber = '', + id, + vCard, + listElement; + + $('#choose > #content-choose > .ui-scrollview-view').empty().append(ul); + + listElementTap = function (event) { + event.preventDefault(); + enableAccept(); + $(this).parent().find('li').css('background-color', '#fff').removeClass('selected'); + $(this).css('background-color', '#ccc').addClass('selected'); + }; + + for (i = 0, len = contacts.length; i < len; i += 1) { + firstName = ''; + lastName = ''; + phoneNumber = ''; + id = contacts[i].id; + vCard = contacts[i].convertToString('VCARD_30'); + if (contacts[i].name.firstName !== null) { + firstName = contacts[i].name.firstName; + } + if (contacts[i].name.lastName !== null) { + lastName = contacts[i].name.lastName; + } + if (contacts[i].phoneNumbers[0] !== undefined) { + phoneNumber = contacts[i].phoneNumbers[0].number; + } + sortedContactList.push({caller: prepareCallerName(contacts[i]), firstName: firstName, lastName: lastName, phoneNumber: phoneNumber, id: id, vCard: vCard, contact: contacts[i]}); + } + + sortedContactList.sort(function (a, b) { + if (a.caller < b.caller) { + return -1; + } + if (a.caller > b.caller) { + return 1; + } + return 0; + }); + + for (j = 0, length = sortedContactList.length; j < length; j += 1) { + listElement = $('
      • ' + sortedContactList[j].caller + '' + sortedContactList[j].phoneNumber + '
      • '); + if (localStorage.id === listElement.attr('id')) { + listElement.css('background-color', '#ccc'); + } + listElement.on('tap', listElementTap); + ul.append(listElement); + } + + $('#choose > #content-choose').trigger('create'); + }; + errorCB = function (e) { + console.log('problem with find() method: ' + e.message); + }; + addressBook.find(contactsFoundCB, errorCB); +}; + +var countDown = function (time, obj) { + "use strict"; + obj.text(time); + if (time > 0) { + time -= 1; + timeOutHandler = setTimeout(function () { countDown(time, obj); }, 1000); + } else { + timeExpired(); + } +}; + +var prepareWaitingPage = function (title, text) { + "use strict"; + var waitingBox, waitingContent; + waitingBox = $('
        '); + waitingContent = $('

        ' + text + '

        '); + $('#header-transfer H1').text(title); + $('#content-transfer .ui-scrollview-view').empty(); + waitingBox.append(waitingContent); + $('#content-transfer .ui-scrollview-view').append(waitingBox); + $('#content-start').trigger('create'); + countDown(10, $('#counter')); +}; + +var loadTemporaryContent = function () { + "use strict"; + var temporaryBox, temporaryContent, temporaryButton; + temporaryBox = $('
        '); + temporaryContent = $('

        Default card hasn\'t defined yet!
        Do you want to define it now?

        '); + temporaryButton = $('
        Create default card
        '); + + temporaryButton.on('tap', function (event) { + event.preventDefault(); + $.mobile.changePage('#choose'); + }); + + $('#content-start .ui-scrollview-view').empty(); + temporaryBox.append(temporaryContent).append(temporaryButton); + $('#content-start .ui-scrollview-view').append(temporaryBox); + $('#content-start').trigger('create'); +}; + +var loadStartContent = function () { + "use strict"; + var startBox, gap, comment, changeContact, readFromCard, writeToCard, communicateWithOtherDevice; + startBox = $('
        '); + gap = $('
        '); + comment = $('

        Your default contact

        ( ' + (localStorage.firstName || '') + ' ' + (localStorage.lastName || '') + ' )

        '); + changeContact = $('
        Change your default contact
        '); + changeContact.on('tap', function (event) { + event.preventDefault(); + $.mobile.changePage('#choose'); + }); + readFromCard = $('
        Read from card
        '); + readFromCard.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'read'); + $.mobile.changePage('#transfer'); + }); + writeToCard = $('
        Write to card
        '); + writeToCard.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'write'); + $.mobile.changePage('#transfer'); + }); + communicateWithOtherDevice = $('
        Communicate with other device
        '); + communicateWithOtherDevice.on('tap', function (event) { + event.preventDefault(); + $('#transfer').data('option', 'communicate'); + $.mobile.changePage('#transfer'); + }); + + $('#content-start .ui-scrollview-view').empty(); + startBox.append(changeContact).append(gap.clone()).append(readFromCard).append(gap.clone()).append(writeToCard).append(gap.clone()).append(communicateWithOtherDevice); + $('#content-start .ui-scrollview-view').append(comment); + $('#content-start .ui-scrollview-view').append(startBox); + $('#content-start').trigger('create'); +}; + +var loadStartPage = function () { + "use strict"; + if (localStorage.started === undefined) { + loadTemporaryContent(); + } else { + loadStartContent(); + } +}; + +var prepareContactsTemplate = function (phone, first, last) { + "use strict"; + var i, ul; + + ul = $('
          '); + $('#contact > #content-contact > .ui-scrollview-view').empty().append(ul); + + ul.append($('
        • ' + ((first === '' || first === 'null') ? '...' : first) + 'First Name
        • ')); + ul.append($('
        • ' + ((last === '' || last === 'null') ? '...' : last) + 'Last Name
        • ')); + ul.append($('
        • ' + ((phone === '' || phone === 'null') ? '...' : phone) + 'Phone Number
        • ')); + + $('#contact > #content-contact').trigger('create'); +}; + +var saveContact = function () { + "use strict"; + var phone, first, last, contactPage = $('#contact'), contact = null; + phone = contactPage.data('contactsData').phone; + first = contactPage.data('contactsData').first; + last = contactPage.data('contactsData').last; + + try { + contact = new tizen.Contact({name: new tizen.ContactName({firstName: first, lastName: last}), phoneNumbers: [new tizen.ContactPhoneNumber(phone)]}); + addressBook.add(contact); + moveToStartPage('New contact added'); + } catch (err) { + moveToStartPage('Problem with new contact adding'); + console.log('The following error occurred while adding: ' + err.name); + } +}; + +var defineEvents = function () { + "use strict"; + $('#header-start .ui-btn-back').on('tap', function (event) { + event.preventDefault(); + stopNFC(); + }); + + $('#footer-transfer').on('tap', '.ui-btn-back', function (event) { + event.preventDefault(); + timeExpired(); + }); + + $('#footer-contact').on('tap', '.ui-btn-back', function (event) { + event.preventDefault(); + $.mobile.changePage('#start'); + }); + + $('#choose').on('pagebeforeshow', function () { + disableAccept(); + }); + + $('#choose').on('pageshow', function (event) { + showContactsList(); + }); + + $('#contact').on('pageshow', function (event) { + var phone, first, last; + phone = $(this).data('contactsData').phone; + first = $(this).data('contactsData').first; + last = $(this).data('contactsData').last; + prepareContactsTemplate(phone, first, last); + }); + + $('#accept-choose').on('tap', function (event) { + event.preventDefault(); + saveDefaultCard(); + }); + + $('#save-contact').on('tap', function (event) { + event.preventDefault(); + saveContact(); + }); + + $('#start').on('pagebeforeshow', function () { + if (appStarted) { + loadStartPage(); + } + }); + + $('#start').on('pageshow', function () { + var monit, obj; + obj = $(this); + monit = obj.data('monit'); + if (monit !== '' && monit !== undefined) { + showPopup(obj.data('monit'), obj); + } + }); + + $('#transfer').on('pageshow', function () { + var option = $(this).data('option'); + if (option === 'read') { + prepareWaitingPage('Card to Device', 'PUT WIRELESS TAG
          CLOSE TO
          YOUR DEVICE'); + try { + setTagDetectRead(); + } catch (err) { + console.log(err); + } + } else if (option === 'write') { + prepareWaitingPage('Device to Card', 'PUT WIRELESS TAG
          CLOSE TO
          YOUR DEVICE'); + try { + setTagDetectWrite(); + } catch (er) { + console.log(er); + } + } else { + prepareWaitingPage('Device to Device', 'PUT YOUR DEVICE
          CLOSE TO
          OTHER DEVICE'); + try { + setTargetDetect(); + } catch (e) { + console.log(e); + } + } + }); +}; + +var init = function () { + "use strict"; + defineEvents(); + initAddressBook(); + startNFC(); +}; + +$(document).ready(init); \ No newline at end of file diff --git a/js/nfc_common.js b/js/nfc_common.js new file mode 100644 index 0000000..29d49e3 --- /dev/null +++ b/js/nfc_common.js @@ -0,0 +1,174 @@ +/*jslint devel: true*/ +/*global $, tizen, nfcAdapter: true, appStarted: true, timeOutHandler, nfcTarget: true, nfcStateMemory: true, convertNDEF2phoneNumber, loadStartPage, resolveContact, displayContact: true */ + +var fillRecordInfo = function (record) { + "use strict"; + try { + var contactsData = resolveContact(convertNDEF2phoneNumber(record.payload)); + displayContact(contactsData); + } catch (error) { + console.log(error); + } +}; + +var readMessage = function (message) { + "use strict"; + try { + fillRecordInfo(message.records[0]); + } catch (e) { + console.log(e.message); + } +}; + +var contact2NDEF = function (contact) { + "use strict"; + var t, a = [], i, newMessage = new tizen.NDEFMessage(); + if (typeof contact === 'string') { + t = contact; + } else { + t = contact.convertToString("VCARD_30"); + } + a.length = t.length; + for (i = 0; i < a.length; i += 1) { + a[i] = t.charCodeAt(i); + } + newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a); + return newMessage; +}; + +var phoneNumber2NDEF = function (contact) { + "use strict"; + var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage(); + for (i = 0; i < length; i += 1) { + phoneNumberArray.push(contact.charCodeAt(i)); + } + newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray); + return newMessage; +}; + +var convertNDEF2phoneNumber = function (contact) { + "use strict"; + var i, length = contact.length, phoneNumber = ''; + for (i = 0; i < length; i += 1) { + phoneNumber += String.fromCharCode(contact[i]); + } + return phoneNumber; +}; + +var startNFC = function () { + "use strict"; + var onPowerOn, onPowerOnFails; + + onPowerOn = function () { + appStarted = true; + loadStartPage(); + }; + onPowerOnFails = function (err) { + console.log('Power On error: ' + err.message); + }; + + try { + nfcAdapter = tizen.nfc.getDefaultAdapter(); + try { + if (nfcAdapter.powered) { + nfcStateMemory = true; + onPowerOn(); + } else { + nfcStateMemory = false; + nfcAdapter.setPowered(true, onPowerOn, onPowerOnFails); + } + } catch (err) { + console.log('setPowered(true) problem: ' + err); + } + } catch (e) { + console.log('getDefaultAdapter() method problem: ' + e); + } +}; + +var stopNFC = function () { + "use strict"; + var onPowerOff, onPowerOffFails; + + onPowerOff = function () { + tizen.application.getCurrentApplication().exit(); + }; + + onPowerOffFails = function (err) { + console.log('Power Off error: ' + err.message); + tizen.application.getCurrentApplication().exit(); + }; + + try { + if (nfcStateMemory) { + onPowerOff(); + } else { + nfcAdapter.setPowered(false, onPowerOff, onPowerOffFails); + } + } catch (err) { + console.log('setPowered(false) problem: ' + err); + } +}; + +var unsetTargetDetect = function () { + "use strict"; + try { + nfcTarget.unsetReceiveNDEFListener(); + nfcTarget = null; + } catch (error) { + console.log('error: ' + error.message); + } + try { + nfcAdapter.unsetPeerListener(); + } catch (e) { + console.log('error: ' + e.message); + } +}; + +var unsetTagDetect = function () { + "use strict"; + try { + nfcAdapter.unsetTagListener(); + } catch (error) { + console.log('error: ' + error.message); + } +}; + +var moveToStartPage = function (monit) { + "use strict"; + $('#start').data('monit', monit || ''); + $.mobile.changePage('#start'); +}; + +var moveToContactPage = function (obj) { + "use strict"; + $('#start').data('monit', ''); + $('#contact').data('contactsData', obj); + $.mobile.changePage('#contact'); +}; + +var timeExpired = function (monit) { + "use strict"; + clearTimeout(timeOutHandler); + if (nfcTarget) { + unsetTargetDetect(); + } else { + unsetTagDetect(); + } + moveToStartPage(monit); +}; + +var displayContact = function (obj) { + "use strict"; + clearTimeout(timeOutHandler); + if (nfcTarget) { + unsetTargetDetect(); + } else { + unsetTagDetect(); + } + moveToContactPage(obj); +}; + +var sucDetach = function () { + "use strict"; + console.log('sucDetach'); +}; \ No newline at end of file diff --git a/js/read_from_card.js b/js/read_from_card.js new file mode 100644 index 0000000..559517a --- /dev/null +++ b/js/read_from_card.js @@ -0,0 +1,58 @@ +/*jslint devel: true*/ +/*global $, tizen, nfcAdapter, timeOutHandler, timeExpired, readMessage, sucDetach */ + +var resolveContact = function (contactsString) { + "use strict"; + var separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + contactsArray, + phone = '', + first = '', + last = '', + resolveData; + + contactsArray = contactsString.split(separator); + + resolveData = function (value) { + if (!value) { + return ''; + } + return value.replace(endOfText, '…'); + }; + + phone = resolveData(contactsArray[0]); + first = resolveData(contactsArray[1]); + last = resolveData(contactsArray[2]); + return {phone: phone, first: first, last: last}; +}; + +var readMessageErr = function (e) { + "use strict"; + timeExpired('Read error!'); +}; + +var sucTagReadAttach = function (tag) { + "use strict"; + if (tag.isSupportedNDEF) { + tag.readNDEF(readMessage, readMessageErr); + } else { + console.log("This Tag doesn't support NDEF"); + } +}; + +var setTagDetectRead = function () { + "use strict"; + /* + var err, suc; + err = function (e) { + console.log('Tag Listen Error: ' + e.message); + }; + */ + var suc = { onattach: sucTagReadAttach, ondetach: sucDetach }; + try { + //nfcAdapter.setTagListener(suc, err); + nfcAdapter.setTagListener(suc); + } catch (error) { + console.log('error: ' + error.message); + } +}; \ No newline at end of file diff --git a/js/write_to_card.js b/js/write_to_card.js new file mode 100644 index 0000000..f69087b --- /dev/null +++ b/js/write_to_card.js @@ -0,0 +1,66 @@ +/*jslint devel: true*/ +/*global $, tizen, nfcAdapter, timeOutHandler, timeExpired, phoneNumber2NDEF, sucDetach */ + +var sucSend = function () { + "use strict"; + console.log('sucSend'); + timeExpired('Send success!'); +}; + +var errSend = function (e) { + "use strict"; + console.log('errSend'); + timeExpired('Send problem! ' + e.message); +}; + +var prepareForNFC; + +var sucTagWriteAttach = function (tag) { + "use strict"; + var newMessage = null, + separator = String.fromCharCode(30), + endOfText = String.fromCharCode(3), + fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName, + prepareForNFC; + + prepareForNFC = prepareForNFC || function prepareForNFC(contact) { + if (contact.length > 31) { + contact = contact.substring(0, 31); + if (contact[29] !== separator) { + contact = contact.substring(0, 30) + endOfText; + } + } + return contact; + }; + + fullContact = prepareForNFC(fullContact); + + try { + newMessage = phoneNumber2NDEF(fullContact); + } catch (err) { + console.log("NDEFMessage problem: " + err.message); + } + + if (tag.isSupportedNDEF) { + tag.writeNDEF(newMessage, sucSend, errSend); + } else { + console.log("This Tag doesn't support NDEF"); + } +}; + +var setTagDetectWrite = function () { + "use strict"; + /* + var err, suc; + err = function (e) { + console.log('Tag Listen Error: ' + e.message); + }; + */ + var suc = { onattach: sucTagWriteAttach, ondetach: sucDetach }; + try { + //nfcAdapter.setTagListener(suc, err); + nfcAdapter.setTagListener(suc); + } catch (error) { + console.log('error: ' + error.message); + } +}; \ No newline at end of file diff --git a/signature1.xml b/signature1.xml new file mode 100755 index 0000000..349bf9b --- /dev/null +++ b/signature1.xml @@ -0,0 +1,92 @@ + + + + + + +O8655N55M/0g9vx6pmER5u1mOjODjaPQSBkW+6cUPYg= + + + +qU95u6MPilGevk4TxJThVweuIHj+Tq6eoF70QxT6/ek= + + + +odaI/pI419AFYI66t3/xwrN7hvISxsnf51XKCJ+vjsg= + + + +3glY615PZgnLt0NNSqlFkNZZlPB9649zWdIJdv/4wTU= + + + +wrbIrjrBHmvJiPMr07wUgIwe8qDDEFE9hWbSHcfZ1PQ= + + + +K33xbgsyFtJA0HWDjO4Poy1D5vQhfrFDwfU5+UT0QQU= + + + +wfwuPIJiNBXrBw5Nl4NE8B7X1QpT3UVtFoiOoAiJsnU= + + + +Jru8YNJAdNGjUhV8UMPXeFjxyUKYOnp4hdFIzCvaa9c= + + + +3eAKfddMvUo3Opri3yMldvBqZpQeoqxOo4o/v00KuUo= + + + +BiLxpHGjpcvnPJByV1Sb8h7UpFXESN0kQXenj5OWR+o= + + + + + + +u/jU3U4Zm5ihTMSjKGlGYbWzDfRkGphPPHx3gJIYEJ4= + + + +uYECfAv3dRcpaqXy5uE5xnw8LxXsu+NIUw9Gpyfty1kvKUwsIItqQ57PUR+5nyyN4/hvt0FAbpi8 +TtLS/Ss8dlb9pJu4fjDZEWQXevKbDPmC5r2dMuIoD962nZGTK2TqDlXEKImA9OV9TyR8VyPuPKKu +16Z5p61SKu+HTxHSFuc= + + + + +MIICmzCCAgQCCQDXI7WLdVZwiTANBgkqhkiG9w0BAQUFADCBjzELMAkGA1UEBhMCS1IxDjAMBgNV +BAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6ZW4gVGVzdCBDQTEiMCAGA1UE +CwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVzdCBDQTEkMCIGA1UEAwwbVGl6ZW4gUHVibGljIERpc3Ry +aWJ1dG9yIENBMB4XDTEyMTAyOTEzMDMwNFoXDTIyMTAyNzEzMDMwNFowgZMxCzAJBgNVBAYTAktS +MQ4wDAYDVQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAUBgNVBAoMDVRpemVuIFRlc3QgQ0Ex +IjAgBgNVBAsMGVRpemVuIERpc3RyaWJ1dG9yIFRlc3QgQ0ExKDAmBgNVBAMMH1RpemVuIFB1Ymxp +YyBEaXN0cmlidXRvciBTaWduZXIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALtMvlc5hENK +90ZdA+y66+Sy0enD1gpZDBh5T9RP0oRsptJv5jjNTseQbQi0SZOdOXb6J7iQdlBCtR343RpIEz8H +mrBy7mSY7mgwoU4EPpp4CTSUeAuKcmvrNOngTp5Hv7Ngf02TTHOLK3hZLpGayaDviyNZB5PdqQdB +hokKjzAzAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvGp1gxxAIlFfhJH1efjb9BJK/rtRkbYn9+Ez +GEbEULg1svsgnyWisFimI3uFvgI/swzr1eKVY3Sc8MQ3+Fdy3EkbDZ2+WAubhcEkorTWjzWz2fL1 +vKaYjeIsuEX6TVRUugHWudPzcEuQRLQf8ibZWjbQdBmpeQYBMg5x+xKLCJc= + + +MIICtDCCAh2gAwIBAgIJAMDbehElPNKvMA0GCSqGSIb3DQEBBQUAMIGVMQswCQYDVQQGEwJLUjEO +MAwGA1UECAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQKDA1UaXplbiBUZXN0IENBMSMw +IQYDVQQLDBpUVGl6ZW4gRGlzdHJpYnV0b3IgVGVzdCBDQTEpMCcGA1UEAwwgVGl6ZW4gUHVibGlj +IERpc3RyaWJ1dG9yIFJvb3QgQ0EwHhcNMTIxMDI5MTMwMjUwWhcNMjIxMDI3MTMwMjUwWjCBjzEL +MAkGA1UEBhMCS1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6 +ZW4gVGVzdCBDQTEiMCAGA1UECwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVzdCBDQTEkMCIGA1UEAwwb +VGl6ZW4gUHVibGljIERpc3RyaWJ1dG9yIENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDe +OTS/3nXvkDEmsFCJIvRlQ3RKDcxdWJJp625pFqHdmoJBdV+x6jl1raGK2Y1sp2Gdvpjc/z92yzAp +bE/UVLPh/tRNZPeGhzU4ejDDm7kzdr2f7Ia0U98K+OoY12ucwg7TYNItj9is7Cj4blGfuMDzd2ah +2AgnCGlwNwV/pv+uVQIDAQABoxAwDjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBACqJ +KO33YdoGudwanZIxMdXuxnnD9R6u72ltKk1S4zPfMJJv482CRGCI4FK6djhlsI4i0Lt1SVIJEed+ +yc3qckGm19dW+4xdlkekon7pViEBWuyHw8OWv3RXtTum1+PGHjBJ2eYY4ZKIpz73U/1NC16sTB/0 +VhfnkHwPltmrpYVe + + + + + \ No newline at end of file