Permalink
Browse files

[Bug 811539] [SMS] Delete PhoneNumberJS once will be available in Gec…

…ko r=julienw
  • Loading branch information...
1 parent 92ed2a2 commit 40f20ef0c5cb65bcaabe63cc61659ee635c51ae3 @borjasalguero borjasalguero committed Jan 8, 2013
Showing with 124 additions and 259 deletions.
  1. +0 −7 apps/sms/index.html
  2. +11 −23 apps/sms/js/contacts.js
  3. +0 −104 apps/sms/js/phoneNumberUtils.js
  4. +105 −120 apps/sms/js/sms.js
  5. +8 −5 apps/sms/js/utils.js
View
7 apps/sms/index.html
@@ -30,13 +30,6 @@
<script type="text/javascript" src="shared/js/custom_dialog.js"></script>
<script type="text/javascript" src="shared/js/desktop.js"></script>
<script type="text/javascript" src="shared/js/notification_helper.js"></script>
- <!-- Phone number library -->
- <script type="text/javascript" src="shared/js/phoneNumberJS/PhoneNumberMetaData.js"></script>
- <script type="text/javascript" src="shared/js/phoneNumberJS/PhoneNumber.js"></script>
- <script type="text/javascript" src="shared/js/phoneNumberJS/mcc_iso3166_table.js"></script>
- <script type="text/javascript" src="js/phoneNumberUtils.js"></script>
-
-
<!-- Specific code -->
<script type="text/javascript" src="js/blacklist.js"></script>
<script type="text/javascript" src="js/contacts.js"></script>
View
34 apps/sms/js/contacts.js
@@ -1,6 +1,8 @@
-/*
- * Contact Manager
-*/
+/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
+/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
+
+'use strict';
+
var ContactDataManager = {
contactData: {},
@@ -9,26 +11,12 @@ var ContactDataManager = {
if (!navigator.mozContacts) {
return;
}
- var numNormalized = PhoneNumberManager.getNormalizedNumber(number);
- // Based on E.164 (http://en.wikipedia.org/wiki/E.164)
- if (number.length < 8) {
- var options = {
- filterBy: ['tel'],
- filterOp: 'equals',
- filterValue: number
- };
- } else {
- // Based on E.164 (http://en.wikipedia.org/wiki/E.164)
- // Some locals added a '0' at the beggining (UK, Sweden...)
- if (numNormalized[0] == 0 || numNormalized[0] == '0') {
- var numNormalized = Number(numNormalized.toString().substr(1));
- }
- var options = {
- filterBy: ['tel'],
- filterOp: 'contains',
- filterValue: numNormalized
- };
- }
+ // Get contacts given a number
+ var options = {
+ filterBy: ['tel'],
+ filterOp: 'contains',
+ filterValue: number
+ };
var self = this;
var req = window.navigator.mozContacts.find(options);
req.onsuccess = function onsuccess() {
View
104 apps/sms/js/phoneNumberUtils.js
@@ -1,104 +0,0 @@
-/* Phone Number Manager for solving the country code format threadinf issue.
- * This Phone Number Manager required:
- * 1) PhoneNumberJS by Andreas Gal https://github.com/andreasgal/PhoneNumber.js
- * 2) mcc(Mobile Country Codes) - iso3166 country code table
- *
- * Methods in the PhoneNumberManager:
- * init - Setup mobile country code (mcc).
- *
- * getInternationalNum - Ruturn the phone number with international format.
- * If the second parameter is true, it will return original input number
- * while input number in invalid format, otherwise return null.
- *
- * getNationalNum - Ruturn the phone number with national format.
- * If the second parameter is true, it will return original input number
- * while input number in invalid format, otherwise return null.
- *
- * isValidNumber - We set a number string and it will check if the phone number
- * is valid or not.
- *
-*/
-// XXX Hack: If is the first time that we are launching SMS and there is any
-// problem with 'mozMobileConnection' we apply this as default
-// https://bugzilla.mozilla.org/show_bug.cgi?id=809057
-var PhoneNumberManager = {
- region: 'BR',
- init: function pnm_init(cb) {
- var self = this;
- // Method for retrieving the mcc
- function getLastMcc() {
- asyncStorage.getItem('mcc', function(mcc) {
- if (mcc) {
- self.region = MCC_ISO3166_TABLE[mcc];
- if (cb) {
- cb();
- }
- }
- });
- }
- // Update the MCC properly, retrieving for network
- var conn = window.navigator.mozMobileConnection;
- if (!!conn) {
- if (conn.voice.connected) {
- var currentMCC = conn.voice.network.mcc;
- // Update value of latest mcc retrieved
- asyncStorage.setItem('mcc', currentMCC);
- // Retrieve region
- self.region = MCC_ISO3166_TABLE[conn.voice.network.mcc];
- if (cb) {
- cb();
- }
- } else {
- getLastMcc();
- }
- } else {
- getLastMcc();
- }
- },
- getNormalizedNumber: function pnm_getNormalizedNumber(numInput) {
- if (!numInput) {
- return null;
- }
- try {
- var result = PhoneNumber.Parse(numInput, this.region);
- /// XXX HACK for getting smoke test working until having in Gecko
- if (result) {
- return result.nationalFormat.replace(/\s|\(|\)|-/g, '');
- } else {
- return numInput.replace(/\s|\(|\)|-/g, '');
- }
- } catch (e) {
- return numInput.replace(/\s|\(|\)|-/g, '');
- }
- },
- getNormalizedInternationalNumber: function pnm_getNormalizedNumber(numInput) {
- if (!numInput) {
- return null;
- }
- try {
- var result = PhoneNumber.Parse(numInput, this.region);
- // XXX HACK for getting smoke test working until having in Gecko
- if (result) {
- return result.internationalFormat.replace(/\s|\(|\)|-/g, '');
- } else {
- return numInput.replace(/\s|\(|\)|-/g, '');
- }
- } catch (e) {
- return numInput.replace(/\s|\(|\)|-/g, '');
- }
- },
- getOptionalNumbers: function pnm_getOptionalNumbers(numInput) {
- if (!numInput) {
- return [numInput];
- }
- try {
- /// XXX HACK for getting smoke test working until having in Gecko
- var nationalNum = this.getNormalizedNumber(numInput);
- var internationalNum = this.getNormalizedInternationalNumber(numInput);
- var internationalNumFormatted = internationalNum.replace('+', '00');
- return [nationalNum, internationalNum, internationalNumFormatted];
- } catch (e) {
- return [numInput];
- }
- }
-};
View
225 apps/sms/js/sms.js
@@ -6,8 +6,6 @@
var MessageManager = {
init: function mm_init() {
this.initialized = true;
- // Init PhoneNumberManager for solving country code issue.
- PhoneNumberManager.init();
MessageManager.getThreads(ThreadListUI.renderThreads);
// Init UI Managers
ThreadUI.init();
@@ -26,7 +24,14 @@ var MessageManager = {
onMessageSending: function mm_onMessageSending(e) {
var message = e.message;
- ThreadUI.appendMessage(message);
+ var num = message.receiver;
+ if (window.location.hash == '#new') {
+ // If we are in 'new' we go to the right thread
+ // 'num' has been internationalized by Gecko
+ window.location.hash = '#num=' + num;
+ } else {
+ ThreadUI.appendMessage(message);
+ }
MessageManager.getThreads(ThreadListUI.renderThreads);
},
@@ -129,14 +134,11 @@ var MessageManager = {
if (num) {
var filter = this.createFilter(num);
var messageInput = document.getElementById('message-to-send');
- var numNormalized =
- PhoneNumberManager.getNormalizedInternationalNumber(num);
- MessageManager.currentNum = numNormalized;
+ MessageManager.currentNum = num;
if (mainWrapper.classList.contains('edit')) {
- this.getMessages(ThreadUI.renderMessages, filter);
mainWrapper.classList.remove('edit');
} else if (threadMessages.classList.contains('new')) {
- this.getMessages(ThreadUI.renderMessages, filter, true);
+ this.getMessages(ThreadUI.renderMessages, filter, false);
threadMessages.classList.remove('new');
messageInput.focus();
@@ -148,8 +150,9 @@ var MessageManager = {
threadRead.getElementsByTagName('a')[0].classList
.remove('unread');
}
+
this.getMessages(ThreadUI.renderMessages,
- filter, true, function() {
+ filter, false, function() {
MessageManager.slide(function() {
messageInput.focus();
});
@@ -171,7 +174,7 @@ var MessageManager = {
if (num) {
var filter = this.createFilter(num);
var typedText = ThreadUI.input.value;
- this.getMessages(ThreadUI.renderMessages, filter, true,
+ this.getMessages(ThreadUI.renderMessages, filter, false,
function() {
// Restored previous typed text.
ThreadUI.input.value = typedText;
@@ -185,13 +188,7 @@ var MessageManager = {
createFilter: function mm_createFilter(num) {
var filter = new MozSmsFilter();
- if (num) {
- num = PhoneNumberManager.getNormalizedInternationalNumber(num);
- } else {
- num = '';
- }
-
- filter.numbers = [num];
+ filter.numbers = [num || ''];
return filter;
},
@@ -505,56 +502,36 @@ var ThreadListUI = {
'header');
// Edit mode available
ThreadListUI.iconEdit.classList.remove('disabled');
- var threadIds = [],
- dayHeaderIndex = 0,
- unreadThreads = [];
-
- for (var i = threads.length - 1; i >= 0; i--) {
- var thread = threads[i];
- var time = thread.timestamp.getTime();
- var num = thread.senderOrReceiver;
-
- var numNormalized =
- PhoneNumberManager.getNormalizedInternationalNumber(num);
-
- if (thread.unreadCount) {
- unreadThreads.push(numNormalized);
+ var dayHeaderIndex;
+ var appendThreads = function(threads, callback) {
+ if (threads.length === 0) {
+ if (callback) {
+ callback();
+ }
+ return;
}
-
- if (threadIds.indexOf(numNormalized) == -1) {
- var thread = {
- 'body': thread.body,
- 'name': numNormalized,
- 'num': numNormalized,
- 'timestamp': time,
- 'unreadCount': thread.unreadCount,
- 'id': numNormalized
- };
-
- if (threadIds.length == 0) {
- dayHeaderIndex = Utils.getDayDate(time);
+ var thread = threads.pop();
+ var time = thread.timestamp.getTime();
+ if (!dayHeaderIndex) {
+ dayHeaderIndex = Utils.getDayDate(time);
+ ThreadListUI.createNewHeader(time);
+ } else {
+ var tmpDayIndex = Utils.getDayDate(time);
+ if (tmpDayIndex < dayHeaderIndex) {
ThreadListUI.createNewHeader(time);
- } else {
- var tmpDayIndex = Utils.getDayDate(time);
- if (tmpDayIndex < dayHeaderIndex) {
- ThreadListUI.createNewHeader(time);
- dayHeaderIndex = tmpDayIndex;
- }
+ dayHeaderIndex = tmpDayIndex;
}
-
- threadIds.push(numNormalized);
- ThreadListUI.appendThread(thread);
}
+ setTimeout(function() {
+ ThreadListUI.appendThread(thread);
+ appendThreads(threads, callback);
+ });
}
- // Update threads with 'unread' if some was missing
- for (var i = 0; i < unreadThreads.length; i++) {
- document.getElementById('thread_' + unreadThreads[i]).
- getElementsByTagName('a')[0].classList.add('unread');
- }
-
- // Boot update of headers
- Utils.updateTimeHeaderScheduler();
+ appendThreads(threads, function at_callback() {
+ // Boot update of headers
+ Utils.updateTimeHeaderScheduler();
+ });
} else {
document.getElementById('threads-fixed-container').classList.add('hide');
@@ -584,25 +561,26 @@ var ThreadListUI = {
return;
}
// Create DOM element
+ var num = thread.senderOrReceiver;
var threadDOM = document.createElement('li');
- threadDOM.id = 'thread_' + thread.num;
+ threadDOM.id = 'thread_' + num;
// Retrieving params from thread
var bodyText = (thread.body || '').split('\n')[0];
- var formattedDate = Utils.getFormattedHour(thread.timestamp);
+ var formattedDate = Utils.getFormattedHour(thread.timestamp.getTime());
// Create HTML Structure
var structureHTML = '<label class="danger">' +
- '<input type="checkbox" value="' + thread.num + '">' +
+ '<input type="checkbox" value="' + num + '">' +
'<span></span>' +
'</label>' +
- '<a href="#num=' + thread.num +
+ '<a href="#num=' + num +
'" class="' +
(thread.unreadCount > 0 ? 'unread' : '') + '">' +
'<aside class="icon icon-unread">unread</aside>' +
'<aside class="pack-end">' +
'<img src="">' +
'</aside>' +
- '<p class="name">' + thread.num + '</p>' +
+ '<p class="name">' + num + '</p>' +
'<p><time>' + formattedDate +
'</time>' + bodyText + '</p>' +
'</a>';
@@ -614,7 +592,7 @@ var ThreadListUI = {
threadsContainer.appendChild(threadDOM);
// Update info given a number
- ThreadListUI.updateThreadWithContact(thread.num, threadDOM);
+ ThreadListUI.updateThreadWithContact(num, threadDOM);
},
// Adds a new grouping header if necessary (today, tomorrow, ...)
@@ -957,21 +935,36 @@ var ThreadUI = {
ThreadUI.timeHeaderIndex = 0;
// Init readMessages array
ThreadUI.readMessages = [];
- // Per each message I will append DOM element
- messages.forEach(ThreadUI.appendMessage);
- // Update read messages if necessary
- if (ThreadUI.readMessages.length > 0) {
- MessageManager.markMessagesRead(ThreadUI.readMessages, 'true',
- function() {
- MessageManager.getThreads(ThreadListUI.renderThreads);
+ // We append messages in a non-blocking way
+ var appendMessages = function(messages, callback) {
+ if (messages.length == 0) {
+ if (callback) {
+ callback();
+ }
+ return;
+ }
+ var message = messages.pop();
+ setTimeout(function() {
+ ThreadUI.appendMessage(message);
+ appendMessages(messages, callback);
});
- }
- // Boot update of headers
- Utils.updateTimeHeaderScheduler();
- // Callback when every message is appended
- if (callback) {
- callback();
- }
+ };
+
+ appendMessages(messages, function am_callback() {
+ // Update read messages if necessary
+ if (ThreadUI.readMessages.length > 0) {
+ MessageManager.markMessagesRead(ThreadUI.readMessages, 'true',
+ function() {
+ MessageManager.getThreads(ThreadListUI.renderThreads);
+ });
+ }
+ // Boot update of headers
+ Utils.updateTimeHeaderScheduler();
+ // Callback when every message is appended
+ if (callback) {
+ callback();
+ }
+ });
},
appendMessage: function thui_appendMessage(message) {
@@ -1236,22 +1229,12 @@ var ThreadUI = {
}
// Clean fields (this lock any repeated click in 'send' button)
this.cleanFields(true);
-
- // Retrieve normalized phone number
- // TODO Remove with
- // https://bugzilla.mozilla.org/show_bug.cgi?id=811539
- var numNormalized =
- PhoneNumberManager.getNormalizedInternationalNumber(num);
-
- MessageManager.currentNum = numNormalized;
+ // Remove when
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=825604 landed
+ MessageManager.currentNum = num;
this.updateHeaderData();
-
- MessageManager.send(numNormalized, text);
-
- if (window.location.hash == '#new') {
- // If we are in 'new' we go to the right thread
- window.location.hash = '#num=' + numNormalized;
- }
+ // Send the SMS
+ MessageManager.send(num, text);
},
onMessageSent: function thui_onMessageSent(message) {
@@ -1270,10 +1253,14 @@ var ThreadUI = {
// Remove 'sending' style and add 'error' style
var aElement = messageDOM.querySelector('a');
+ // Check if it was painted as 'error' before
+ if (!aElement.classList.contains('sending')) {
+ return;
+ }
aElement.classList.remove('sending');
aElement.classList.add('error');
- // remove only the spinner
+ // Remove only the spinner
var spinnerContainer = aElement.querySelector('aside');
spinnerContainer.innerHTML = '';
@@ -1632,33 +1619,31 @@ if (!window.location.hash.length) {
}
}
- PhoneNumberManager.init(function phoneNMReady() {
- navigator.mozApps.getSelf().onsuccess = function(evt) {
- var app = evt.target.result;
- var iconURL = NotificationHelper.getIconURI(app);
+ navigator.mozApps.getSelf().onsuccess = function(evt) {
+ var app = evt.target.result;
+ var iconURL = NotificationHelper.getIconURI(app);
- // Stashing the number at the end of the icon URL to make sure
- // we get it back even via system message
- iconURL += '?sms-received?' + number;
+ // Stashing the number at the end of the icon URL to make sure
+ // we get it back even via system message
+ iconURL += '?sms-received?' + number;
- var goToMessage = function() {
- app.launch();
- showThreadFromSystemMessage(number);
- };
+ var goToMessage = function() {
+ app.launch();
+ showThreadFromSystemMessage(number);
+ };
- ContactDataManager.getContactData(message.sender,
- function gotContact(contact) {
- var sender;
- if (contact.length && contact[0].name) {
- sender = contact[0].name;
- } else {
- sender = message.sender;
- }
+ ContactDataManager.getContactData(message.sender,
+ function gotContact(contact) {
+ var sender;
+ if (contact.length && contact[0].name) {
+ sender = contact[0].name;
+ } else {
+ sender = message.sender;
+ }
- NotificationHelper.send(sender, message.body, iconURL, goToMessage);
- });
- };
- });
+ NotificationHelper.send(sender, message.body, iconURL, goToMessage);
+ });
+ };
});
window.navigator.mozSetMessageHandler('notification',
View
13 apps/sms/js/utils.js
@@ -107,21 +107,24 @@ var Utils = {
getPhoneDetails: function ut_getPhoneDetails(number, contact, callback) {
var details = {};
-
if (contact) { // we have a contact
- //TODO what if there are more contacts?
var name = contact.name,
phone = contact.tel[0],
carrierToShow = phone.carrier;
// Check which of the contacts phone number are we using
for (var i = 0; i < contact.tel.length; i++) {
- if (PhoneNumberManager.getOptionalNumbers(
- contact.tel[i].value).indexOf(number) != -1) {
+ // Based on E.164 (http://en.wikipedia.org/wiki/E.164)
+ var tempPhoneNumber = contact.tel[i].value;
+ if (number.length > 7) {
+ var rootPhoneNumber = number.substr(-8);
+ } else {
+ var rootPhoneNumber = number;
+ }
+ if (tempPhoneNumber.indexOf(rootPhoneNumber) != -1) {
phone = contact.tel[i];
carrierToShow = phone.carrier;
break;
-
}
}

0 comments on commit 40f20ef

Please sign in to comment.