2012-11-09 02:53:44 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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;
|
|
|
|
|
2012-11-30 02:50:19 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2012-11-09 02:53:44 +04:00
|
|
|
Cu.import("resource://gre/modules/PhoneNumber.jsm");
|
|
|
|
Cu.import("resource://gre/modules/mcc_iso3166_table.jsm");
|
|
|
|
|
2012-11-30 02:50:19 +04:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-03-06 13:53:12 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "mobileConnection",
|
2012-11-09 02:53:44 +04:00
|
|
|
"@mozilla.org/ril/content-helper;1",
|
2013-03-06 13:53:12 +04:00
|
|
|
"nsIMobileConnectionProvider");
|
2012-11-30 02:50:19 +04:00
|
|
|
#endif
|
2012-11-09 02:53:44 +04:00
|
|
|
|
|
|
|
this.PhoneNumberUtils = {
|
|
|
|
// 1. See whether we have a network mcc
|
|
|
|
// 2. If we don't have that, look for the simcard mcc
|
|
|
|
// 3. TODO: If we don't have that or its 0 (not activated), pick up the last used mcc
|
|
|
|
// 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
|
2012-12-20 11:17:37 +04:00
|
|
|
// Get network mcc
|
2013-03-06 13:53:12 +04:00
|
|
|
if (mobileConnection.voiceConnectionInfo &&
|
|
|
|
mobileConnection.voiceConnectionInfo.network) {
|
|
|
|
mcc = mobileConnection.voiceConnectionInfo.network.mcc;
|
2012-12-20 11:17:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get SIM mcc
|
|
|
|
if (!mcc) {
|
2013-03-06 13:53:12 +04:00
|
|
|
mcc = mobileConnection.iccInfo.mcc;
|
2012-12-20 11:17:37 +04:00
|
|
|
}
|
2012-11-09 02:53:44 +04:00
|
|
|
|
2012-12-20 11:17:37 +04:00
|
|
|
// Get previous mcc
|
2013-03-06 13:53:12 +04:00
|
|
|
if (!mcc && mobileConnection.voiceConnectionInfo) {
|
|
|
|
mcc = mobileConnection.voiceConnectionInfo.lastKnownMcc;
|
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
|
|
|
|
mcc = this._mcc;
|
|
|
|
#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());
|
2012-11-30 02:50:19 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
if (result) {
|
|
|
|
debug("InternationalFormat: " + result.internationalFormat);
|
|
|
|
debug("InternationalNumber: " + result.internationalNumber);
|
|
|
|
debug("NationalNumber: " + result.nationalNumber);
|
|
|
|
debug("NationalFormat: " + result.nationalFormat);
|
|
|
|
} else {
|
|
|
|
debug("No result!\n");
|
|
|
|
}
|
|
|
|
}
|
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-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) {
|
|
|
|
var normalized = PhoneNumber.Normalize(aNumber, aNumbersOnly);
|
|
|
|
if (DEBUG) debug("normalize(" + aNumber + "): " + normalized + ", " + aNumbersOnly);
|
2013-03-08 19:08:55 +04:00
|
|
|
return normalized;
|
2012-11-30 02:50:19 +04:00
|
|
|
}
|
2012-11-09 02:53:44 +04:00
|
|
|
};
|