2014-06-25 09:12:07 +04:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2012-11-09 02:53:44 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["PhoneNumberUtils"];
|
|
|
|
|
2012-11-30 02:50:19 +04:00
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { if(DEBUG) dump("-*- PhoneNumberutils: " + s + "\n"); }
|
2012-11-09 02:53:44 +04:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
2013-08-01 08:04:59 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2012-11-09 02:53:44 +04:00
|
|
|
|
2012-11-30 02:50:19 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2014-05-08 02:53:16 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PhoneNumberNormalizer",
|
|
|
|
"resource://gre/modules/PhoneNumberNormalizer.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "MCC_ISO3166_TABLE",
|
|
|
|
"resource://gre/modules/mcc_iso3166_table.jsm");
|
2012-11-09 02:53:44 +04:00
|
|
|
|
2012-11-30 02:50:19 +04:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-03-06 13:53:12 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "mobileConnection",
|
2014-07-02 14:10:30 +04:00
|
|
|
"@mozilla.org/mobileconnection/mobileconnectionservice;1",
|
|
|
|
"nsIMobileConnectionService");
|
2013-09-27 13:43:01 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "icc",
|
|
|
|
"@mozilla.org/ril/content-helper;1",
|
|
|
|
"nsIIccProvider");
|
2012-11-30 02:50:19 +04:00
|
|
|
#endif
|
2012-11-09 02:53:44 +04:00
|
|
|
|
|
|
|
this.PhoneNumberUtils = {
|
2013-08-01 08:04:59 +04:00
|
|
|
init: function() {
|
|
|
|
ppmm.addMessageListener(["PhoneNumberService:FuzzyMatch"], this);
|
|
|
|
},
|
2012-11-09 02:53:44 +04:00
|
|
|
// 1. See whether we have a network mcc
|
|
|
|
// 2. If we don't have that, look for the simcard mcc
|
2013-07-19 00:17:41 +04:00
|
|
|
// 3. If we don't have that or its 0 (not activated), pick up the last used mcc
|
2012-11-09 02:53:44 +04:00
|
|
|
// 4. If we don't have, default to some mcc
|
2012-11-30 02:50:19 +04:00
|
|
|
|
|
|
|
// mcc for Brasil
|
|
|
|
_mcc: '724',
|
|
|
|
|
2013-06-14 02:00:23 +04:00
|
|
|
getCountryName: function getCountryName() {
|
2012-11-09 02:53:44 +04:00
|
|
|
let mcc;
|
|
|
|
let countryName;
|
2012-11-30 02:50:19 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-10-30 08:03:54 +04:00
|
|
|
// TODO: Bug 926740 - PhoneNumberUtils for multisim
|
2013-10-16 14:15:55 +04:00
|
|
|
// In Multi-sim, there is more than one client in
|
|
|
|
// iccProvider/mobileConnectionProvider. Each client represents a
|
|
|
|
// icc/mobileConnection service. To maintain the backward compatibility with
|
|
|
|
// single sim, we always use client 0 for now. Adding support for multiple
|
|
|
|
// sim will be addressed in bug 926740, if needed.
|
|
|
|
let clientId = 0;
|
|
|
|
|
|
|
|
// Get network mcc
|
2014-09-21 11:24:43 +04:00
|
|
|
let connection = mobileConnection.getItemByServiceId(clientId);
|
|
|
|
let voice = connection && connection.voice;
|
2013-07-19 00:17:41 +04:00
|
|
|
if (voice && voice.network && voice.network.mcc) {
|
|
|
|
mcc = voice.network.mcc;
|
2012-12-20 11:17:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get SIM mcc
|
2013-10-16 14:15:55 +04:00
|
|
|
let iccInfo = icc.getIccInfo(clientId);
|
2013-08-20 13:59:08 +04:00
|
|
|
if (!mcc && iccInfo && iccInfo.mcc) {
|
2013-07-19 00:17:41 +04:00
|
|
|
mcc = iccInfo.mcc;
|
2012-12-20 11:17:37 +04:00
|
|
|
}
|
2012-11-09 02:53:44 +04:00
|
|
|
|
2013-07-19 00:17:41 +04:00
|
|
|
// Attempt to grab last known sim mcc from prefs
|
|
|
|
if (!mcc) {
|
|
|
|
try {
|
|
|
|
mcc = Services.prefs.getCharPref("ril.lastKnownSimMcc");
|
|
|
|
} catch (e) {}
|
2012-12-20 11:17:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set to default mcc
|
|
|
|
if (!mcc) {
|
|
|
|
mcc = this._mcc;
|
|
|
|
}
|
2012-11-30 02:50:19 +04:00
|
|
|
#else
|
2013-08-29 18:56:19 +04:00
|
|
|
|
|
|
|
// Attempt to grab last known sim mcc from prefs
|
|
|
|
if (!mcc) {
|
|
|
|
try {
|
|
|
|
mcc = Services.prefs.getCharPref("ril.lastKnownSimMcc");
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mcc) {
|
|
|
|
mcc = this._mcc;
|
|
|
|
}
|
2012-11-30 02:50:19 +04:00
|
|
|
#endif
|
2012-11-09 02:53:44 +04:00
|
|
|
|
|
|
|
countryName = MCC_ISO3166_TABLE[mcc];
|
2012-12-20 11:17:37 +04:00
|
|
|
if (DEBUG) debug("MCC: " + mcc + "countryName: " + countryName);
|
2012-11-09 02:53:44 +04:00
|
|
|
return countryName;
|
|
|
|
},
|
|
|
|
|
|
|
|
parse: function(aNumber) {
|
2012-12-20 11:17:37 +04:00
|
|
|
if (DEBUG) debug("call parse: " + aNumber);
|
2013-06-14 02:00:23 +04:00
|
|
|
let result = PhoneNumber.Parse(aNumber, this.getCountryName());
|
2013-08-29 18:56:19 +04:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
let countryName = result.countryName || this.getCountryName();
|
|
|
|
let number = null;
|
|
|
|
if (countryName) {
|
|
|
|
if (Services.prefs.getPrefType("dom.phonenumber.substringmatching." + countryName) == Ci.nsIPrefBranch.PREF_INT) {
|
|
|
|
let val = Services.prefs.getIntPref("dom.phonenumber.substringmatching." + countryName);
|
|
|
|
if (val) {
|
|
|
|
number = result.internationalNumber || result.nationalNumber;
|
|
|
|
if (number && number.length > val) {
|
|
|
|
number = number.slice(-val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperty(result, "nationalMatchingFormat", { value: number, enumerable: true });
|
|
|
|
if (DEBUG) {
|
2012-11-30 02:50:19 +04:00
|
|
|
debug("InternationalFormat: " + result.internationalFormat);
|
|
|
|
debug("InternationalNumber: " + result.internationalNumber);
|
|
|
|
debug("NationalNumber: " + result.nationalNumber);
|
|
|
|
debug("NationalFormat: " + result.nationalFormat);
|
2013-08-29 18:56:19 +04:00
|
|
|
debug("CountryName: " + result.countryName);
|
|
|
|
debug("NationalMatchingFormat: " + result.nationalMatchingFormat);
|
2012-11-30 02:50:19 +04:00
|
|
|
}
|
2013-08-29 18:56:19 +04:00
|
|
|
} else if (DEBUG) {
|
|
|
|
debug("NO PARSING RESULT!");
|
2012-11-30 02:50:19 +04:00
|
|
|
}
|
2012-11-09 02:53:44 +04:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
parseWithMCC: function(aNumber, aMCC) {
|
|
|
|
let countryName = MCC_ISO3166_TABLE[aMCC];
|
2012-12-20 11:17:37 +04:00
|
|
|
if (DEBUG) debug("found country name: " + countryName);
|
2012-11-09 02:53:44 +04:00
|
|
|
return PhoneNumber.Parse(aNumber, countryName);
|
2013-02-20 13:02:45 +04:00
|
|
|
},
|
|
|
|
|
2013-10-31 13:20:00 +04:00
|
|
|
parseWithCountryName: function(aNumber, countryName) {
|
|
|
|
return PhoneNumber.Parse(aNumber, countryName);
|
|
|
|
},
|
|
|
|
|
2013-03-08 19:08:55 +04:00
|
|
|
isPlainPhoneNumber: function isPlainPhoneNumber(aNumber) {
|
|
|
|
var isPlain = PhoneNumber.IsPlain(aNumber);
|
|
|
|
if (DEBUG) debug("isPlain(" + aNumber + ") " + isPlain);
|
|
|
|
return isPlain;
|
|
|
|
},
|
|
|
|
|
2013-06-14 01:52:40 +04:00
|
|
|
normalize: function Normalize(aNumber, aNumbersOnly) {
|
2013-08-01 08:04:59 +04:00
|
|
|
let normalized = PhoneNumberNormalizer.Normalize(aNumber, aNumbersOnly);
|
2013-06-14 01:52:40 +04:00
|
|
|
if (DEBUG) debug("normalize(" + aNumber + "): " + normalized + ", " + aNumbersOnly);
|
2013-03-08 19:08:55 +04:00
|
|
|
return normalized;
|
2013-08-01 08:04:59 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
fuzzyMatch: function fuzzyMatch(aNumber1, aNumber2) {
|
|
|
|
let normalized1 = this.normalize(aNumber1);
|
|
|
|
let normalized2 = this.normalize(aNumber2);
|
|
|
|
if (DEBUG) debug("Normalized Number1: " + normalized1 + ", Number2: " + normalized2);
|
|
|
|
if (normalized1 === normalized2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let parsed1 = this.parse(aNumber1);
|
|
|
|
let parsed2 = this.parse(aNumber2);
|
|
|
|
if (parsed1 && parsed2) {
|
2013-08-29 18:56:19 +04:00
|
|
|
if ((parsed1.internationalNumber && parsed1.internationalNumber === parsed2.internationalNumber)
|
|
|
|
|| (parsed1.nationalNumber && parsed1.nationalNumber === parsed2.nationalNumber)) {
|
2013-08-01 08:04:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let countryName = this.getCountryName();
|
|
|
|
let ssPref = "dom.phonenumber.substringmatching." + countryName;
|
|
|
|
if (Services.prefs.getPrefType(ssPref) == Ci.nsIPrefBranch.PREF_INT) {
|
|
|
|
let val = Services.prefs.getIntPref(ssPref);
|
2013-08-29 18:56:19 +04:00
|
|
|
if (normalized1.length > val && normalized2.length > val
|
|
|
|
&& normalized1.slice(-val) === normalized2.slice(-val)) {
|
2013-08-01 08:04:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
if (DEBUG) debug("receiveMessage " + aMessage.name);
|
|
|
|
let mm = aMessage.target;
|
|
|
|
let msg = aMessage.data;
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "PhoneNumberService:FuzzyMatch":
|
|
|
|
mm.sendAsyncMessage("PhoneNumberService:FuzzyMatch:Return:OK", {
|
|
|
|
requestID: msg.requestID,
|
|
|
|
result: this.fuzzyMatch(msg.options.number1, msg.options.number2)
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (DEBUG) debug("WRONG MESSAGE NAME: " + aMessage.name);
|
|
|
|
}
|
2012-11-30 02:50:19 +04:00
|
|
|
}
|
2012-11-09 02:53:44 +04:00
|
|
|
};
|
2013-08-01 08:04:59 +04:00
|
|
|
|
|
|
|
let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
|
|
|
|
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
|
|
|
if (inParent) {
|
2014-05-08 02:53:16 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PhoneNumber",
|
|
|
|
"resource://gre/modules/PhoneNumber.jsm");
|
2013-08-01 08:04:59 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
|
|
|
PhoneNumberUtils.init();
|
|
|
|
}
|
|
|
|
|