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, Config */
25 (function () { // strict mode wrapper
29 * Creates a new application object
33 App = function App() {};
43 init: function appInit() {
44 this.config = new Config();
45 this.ui = new App.Ui(this);
46 this.nfc = new App.NFCControl(this);
47 this.ui.defineEvents();
48 this.initAddressBook(this.nfc.startNFC.bind(this.nfc));
51 saveDefaultCard: function saveDefaultCard() {
52 var elementSelected = $('#list-choose li.selected');
53 localStorage.started = true;
54 localStorage.id = elementSelected.data('id');
55 localStorage.caller = elementSelected.data('caller');
56 localStorage.firstName = elementSelected.data('firstName');
57 localStorage.lastName = elementSelected.data('lastName');
58 localStorage.phoneNumber = elementSelected.data('phoneNumber');
59 localStorage.vCard = elementSelected.data('vCard');
61 $.mobile.changePage('#start');
64 updateDefaultCard: function updateDefaultCard (contact) {
65 localStorage.caller = this.ui.prepareCallerName(contact);
66 localStorage.firstName = contact.name.firstName || '';
67 localStorage.lastName = contact.name.lastName || '';
68 localStorage.phoneNumber = contact.phoneNumbers[0].number;
69 localStorage.id = contact.id;
70 localStorage.vCard = contact.convertToString('VCARD_30');
74 * @param {Array} addressbooks
76 getAddressBooksSuccess: function getAddressBooksSuccess(addressbooks) {
77 if (addressbooks.length > 0) {
79 resetLocalSorage = function () {
84 // Load start page with temporary content
85 self.ui.moveToStartPage();
86 self.ui.loadTemporaryContent();
88 this.addressBook = addressbooks[0];
90 if (localStorage.id) {
92 this.updateDefaultCard(
93 this.addressBook.get(localStorage.id)
96 if (err.name === "NotFoundError") {
102 // Registers to be notified when the address book changes
103 this.addressBook.addChangeListener({
104 oncontactsadded: function(contacts) {
105 // Refresh if choose page active
106 self.ui.refreshIfActivePage("choose");
108 oncontactsremoved: function(ids) {
109 // Refresh localStorage if default contact was deleted
110 if (ids.indexOf(localStorage.id) >= 0) {
112 alert("Your default contact has been removed. Please choose another one.");
114 // Refresh if choose page active
115 self.ui.refreshIfActivePage("choose");
118 oncontactsupdated: function (contacts) {
119 var index = contacts.length;
121 // check if default contact was updated
123 if (contacts[index].id === localStorage.id) {
124 self.updateDefaultCard(contacts[index]);
129 self.ui.refreshIfActivePage("start");
133 console.error('initAddressBook failed');
141 getAddressBooksError: function getAddressBooksError(e) {
142 console.error('getAddressBooks() error: ' + e.message);
145 initAddressBook: function initAddressBook(callback) {
147 tizen.contact.getAddressBooks(
148 function (addressbook) {
149 this.getAddressBooksSuccess(addressbook)
152 this.getAddressBooksError.bind(this)
155 if (e.name === "SecurityError") {
156 this.ui.showPopupWarning();
158 console.error('getAddressBooks() error: ' + e.message);
162 countDown: function countDown(time, obj) {
163 if (!this.counterState) {
164 setTimeout(function () {
165 this.countDown(time, obj);
172 if (this.nfc.isPowered()) {
174 this.timeOutHandler = setTimeout(function () {
175 this.countDown(time, obj);
178 this.nfc.timeExpired();
181 this.nfc.timeExpired();
185 saveContact: function saveContact() {
186 var contact = null, i, empty = true,
187 data = $('#contact').data('contactsData');
190 if (data.hasOwnProperty(i) && empty) {
191 if (data[i] !== '') {
198 this.ui.moveToStartPage('Cannot add empty contact');
199 console.error('saveContact error: empty contact');
202 contact = new tizen.Contact({
203 name: new tizen.ContactName({
205 firstName: data.first,
208 phoneNumbers: [new tizen.ContactPhoneNumber(data.phone)]
210 this.addressBook.add(contact);
211 this.ui.moveToStartPage('New contact added');
213 this.ui.moveToStartPage('Problem with new contact adding');
214 console.error('saveContact error:' + err.name);
220 * Load contacts from the address book and pass the result to a callback
222 * @param {function} successCallback
223 * @param {function} errorCallback
225 loadContacts: function loadContacts(successCallback, errorCallback) {
226 this.addressBook.find(successCallback, errorCallback);
235 $(document).ready(app.init.bind(app));