2012-11-08 14:53:44 -08: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-29 14:50:19 -08:00
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { if(DEBUG) dump("-*- PhoneNumberutils: " + s + "\n"); }
|
2012-11-08 14:53:44 -08:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2012-11-29 14:50:19 -08:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2012-11-08 14:53:44 -08:00
|
|
|
Cu.import("resource://gre/modules/PhoneNumber.jsm");
|
|
|
|
Cu.import("resource://gre/modules/mcc_iso3166_table.jsm");
|
|
|
|
|
2012-11-29 14:50:19 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-03-06 01:53:12 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "mobileConnection",
|
2012-11-08 14:53:44 -08:00
|
|
|
"@mozilla.org/ril/content-helper;1",
|
2013-03-06 01:53:12 -08:00
|
|
|
"nsIMobileConnectionProvider");
|
2012-11-29 14:50:19 -08:00
|
|
|
#endif
|
2012-11-08 14:53:44 -08: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-29 14:50:19 -08:00
|
|
|
|
|
|
|
// mcc for Brasil
|
|
|
|
_mcc: '724',
|
|
|
|
|
2012-11-08 14:53:44 -08:00
|
|
|
_getCountryName: function() {
|
|
|
|
let mcc;
|
|
|
|
let countryName;
|
2012-11-29 14:50:19 -08:00
|
|
|
|
|
|
|
#ifdef MOZ_B2G_RIL
|
2012-12-19 23:17:37 -08:00
|
|
|
// Get network mcc
|
2013-03-06 01:53:12 -08:00
|
|
|
if (mobileConnection.voiceConnectionInfo &&
|
|
|
|
mobileConnection.voiceConnectionInfo.network) {
|
|
|
|
mcc = mobileConnection.voiceConnectionInfo.network.mcc;
|
2012-12-19 23:17:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get SIM mcc
|
|
|
|
if (!mcc) {
|
2013-03-06 01:53:12 -08:00
|
|
|
mcc = mobileConnection.iccInfo.mcc;
|
2012-12-19 23:17:37 -08:00
|
|
|
}
|
2012-11-08 14:53:44 -08:00
|
|
|
|
2012-12-19 23:17:37 -08:00
|
|
|
// Get previous mcc
|
2013-03-06 01:53:12 -08:00
|
|
|
if (!mcc && mobileConnection.voiceConnectionInfo) {
|
|
|
|
mcc = mobileConnection.voiceConnectionInfo.lastKnownMcc;
|
2012-12-19 23:17:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set to default mcc
|
|
|
|
if (!mcc) {
|
|
|
|
mcc = this._mcc;
|
|
|
|
}
|
2012-11-29 14:50:19 -08:00
|
|
|
#else
|
|
|
|
mcc = this._mcc;
|
|
|
|
#endif
|
2012-11-08 14:53:44 -08:00
|
|
|
|
|
|
|
countryName = MCC_ISO3166_TABLE[mcc];
|
2012-12-19 23:17:37 -08:00
|
|
|
if (DEBUG) debug("MCC: " + mcc + "countryName: " + countryName);
|
2012-11-08 14:53:44 -08:00
|
|
|
return countryName;
|
|
|
|
},
|
|
|
|
|
|
|
|
parse: function(aNumber) {
|
2012-12-19 23:17:37 -08:00
|
|
|
if (DEBUG) debug("call parse: " + aNumber);
|
2012-11-08 14:53:44 -08:00
|
|
|
let result = PhoneNumber.Parse(aNumber, this._getCountryName());
|
2012-11-29 14:50:19 -08: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-08 14:53:44 -08:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
parseWithMCC: function(aNumber, aMCC) {
|
|
|
|
let countryName = MCC_ISO3166_TABLE[aMCC];
|
2012-12-19 23:17:37 -08:00
|
|
|
if (DEBUG) debug("found country name: " + countryName);
|
2012-11-08 14:53:44 -08:00
|
|
|
return PhoneNumber.Parse(aNumber, countryName);
|
2013-02-20 01:02:45 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
isViablePhoneNumber: function IsViablePhoneNumber(aNumber) {
|
|
|
|
let viable = PhoneNumber.IsViable(aNumber);
|
|
|
|
if (DEBUG) debug("IsViable(" + aNumber + "): " + viable);
|
|
|
|
return viable;
|
2012-11-29 14:50:19 -08:00
|
|
|
}
|
2012-11-08 14:53:44 -08:00
|
|
|
};
|