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, localStorage */
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.middleName = elementSelected.data('middleName');
58 localStorage.lastName = elementSelected.data('lastName');
59 localStorage.phoneNumber = elementSelected.data('phoneNumber');
60 localStorage.vCard = elementSelected.data('vCard');
63 $.mobile.changePage('#start');
66 updateDefaultCard: function updateDefaultCard(contact) {
67 localStorage.caller = this.ui.prepareCallerName(contact);
68 localStorage.firstName = contact.name.firstName || '';
69 localStorage.middleName = contact.name.middleName || '';
70 localStorage.lastName = contact.name.lastName || '';
71 localStorage.phoneNumber = contact.phoneNumbers[0].number;
72 localStorage.id = contact.id;
73 localStorage.vCard = contact.convertToString('VCARD_30');
77 * @param {Array} addressbooks
79 getAddressBooksSuccess: function getAddressBooksSuccess(addressbooks) {
80 if (addressbooks.length > 0) {
82 resetLocalSorage = function () {
87 // Load start page with temporary content
88 self.ui.moveToStartPage();
89 self.ui.loadTemporaryContent();
91 this.addressBook = addressbooks[0];
93 if (localStorage.id) {
95 this.updateDefaultCard(
96 this.addressBook.get(localStorage.id)
99 if (err.name === 'NotFoundError') {
105 // Registers to be notified when the address book changes
106 this.addressBook.addChangeListener({
107 oncontactsadded: function (contacts) {
108 // Refresh if choose page active
109 self.ui.refreshIfActivePage('choose');
111 oncontactsremoved: function (ids) {
112 // Refresh localStorage if default contact was deleted
113 if (ids.indexOf(localStorage.id) >= 0) {
115 alert('Your default contact has been removed. ' +
116 'Please choose another one.');
118 // Refresh if choose page active
119 self.ui.refreshIfActivePage('choose');
122 oncontactsupdated: function (contacts) {
123 var index = contacts.length;
125 // check if default contact was updated
127 if (contacts[index].id === localStorage.id) {
129 self.updateDefaultCard(contacts[index]);
132 alert('That contact is no longer valid. ' +
133 'Choose another one.');
141 self.ui.refreshIfActivePage('start');
145 console.error('initAddressBook failed');
153 getAddressBooksError: function getAddressBooksError(e) {
154 console.error('getAddressBooks() error: ' + e.message);
157 initAddressBook: function initAddressBook(callback) {
159 tizen.contact.getAddressBooks(function (addressbook) {
160 this.getAddressBooksSuccess(addressbook);
163 this.getAddressBooksError.bind(this));
165 if (e.name === 'SecurityError') {
166 this.ui.showPopupWarning();
168 console.error('getAddressBooks() error: ' + e.message);
172 countDown: function countDown(time, obj) {
173 if (!this.counterState) {
174 setTimeout(function () {
175 this.countDown(time, obj);
182 if (this.nfc.isPowered()) {
184 this.timeOutHandler = setTimeout(function () {
185 this.countDown(time, obj);
188 this.nfc.timeExpired();
191 this.nfc.timeExpired();
195 saveContact: function saveContact() {
197 data = $('#contact').data('contactsData');
198 if (!(data.phone && (data.first || data.last))) {
199 this.ui.moveToStartPage('Cannot add empty contact');
200 console.error('saveContact error: empty contact');
203 contact = new tizen.Contact(data.vCard, 'VCARD_30');
204 this.addressBook.add(contact);
205 this.ui.moveToStartPage('New contact added');
207 this.ui.moveToStartPage('Problem with new contact adding');
208 console.error('saveContact error:' + err.name);
214 * Load contacts from the address book and pass the result to a callback
216 * @param {function} successCallback
217 * @param {function} errorCallback
219 loadContacts: function loadContacts(successCallback, errorCallback) {
220 this.addressBook.find(successCallback, errorCallback);
229 $(document).ready(app.init.bind(app));