Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #22329 from Moz-TedC/bug-1026098-fix-attempt-2
Browse files Browse the repository at this point in the history
Bug 1026098 - Skip time and timezone setting when network time available. r=fcampo
  • Loading branch information
rvandermeulen committed Aug 4, 2014
2 parents 0e0131f + e5494c1 commit c35e153
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 51 deletions.
9 changes: 9 additions & 0 deletions apps/ftu/js/navigation.js
Expand Up @@ -373,5 +373,14 @@ var Navigation = {
var skipUnlockScreens = this.currentStep < this.previousStep;
SimManager.handleCardState(check_cardState, skipUnlockScreens);
}

// Only show the Date & Time screen if we couldn't determine the
// timezone from the network. (We assume that if we can
// determine the timezone, we can determine the time too.)
if (steps[self.currentStep].hash === '#date_and_time') {
if (!UIManager.timeZoneNeedsConfirmation) {
self.skipStep();
}
}
}
};
8 changes: 6 additions & 2 deletions apps/ftu/js/ui.js
Expand Up @@ -111,6 +111,7 @@ var UIManager = {
],

dataConnectionChangedByUsr: false,
timeZoneNeedsConfirmation: true,

init: function ui_init() {
_ = navigator.mozL10n.get;
Expand Down Expand Up @@ -307,7 +308,8 @@ var UIManager = {
// Initialize the timezone selector, see /shared/js/tz_select.js
var tzRegion = document.getElementById('tz-region');
var tzCity = document.getElementById('tz-city');
tzSelect(tzRegion, tzCity, this.setTimeZone, this.setTimeZone);
tzSelect(tzRegion, tzCity,
this.setTimeZone.bind(this), this.setTimeZone.bind(this));
},

handleEvent: function ui_handleEvent(event) {
Expand Down Expand Up @@ -503,7 +505,9 @@ var UIManager = {
timeLabel.innerHTML = f.localeFormat(timeToSet, format);
},

setTimeZone: function ui_stz(timezone) {
setTimeZone: function ui_stz(timezone, needsConfirmation) {
this.timeZoneNeedsConfirmation = !!needsConfirmation;

var utcOffset = timezone.utcOffset;
document.getElementById('time_zone_overlay').className =
'UTC' + utcOffset.replace(/[+:]/g, '');
Expand Down
1 change: 1 addition & 0 deletions apps/ftu/test/unit/mock_ui_manager.js
Expand Up @@ -119,6 +119,7 @@ var MockUIManager = {
}.bind(this));
},

timeZoneNeedsConfirmation: true,
sendNewsletter: function(callback) {return callback(true);},
updateDataConnectionStatus: function(status) {return DataMobile.getStatus();},
displayOfflineDialog: function() {},
Expand Down
18 changes: 18 additions & 0 deletions apps/ftu/test/unit/navigation_test.js
Expand Up @@ -146,6 +146,24 @@ suite('navigation >', function() {
}
});

test('skips date and time when network time is available', function() {
var oldTimeZoneNeedsConfirmation = UIManager.timeZoneNeedsConfirmation;
UIManager.timeZoneNeedsConfirmation = false;

MockIccHelper.setProperty('cardState', 'ready');
Navigation.simMandatory = true;
Navigation.totalSteps = numSteps; // explicitly set the total steps

setStepState(3);
Navigation.forward();
assert.equal(Navigation.getProgressBarState(), 5);
assert.equal(Navigation.previousStep, 3);
assert.equal(Navigation.currentStep, 5);
assert.equal(window.location.hash, steps[5].hash);

UIManager.timeZoneNeedsConfirmation = oldTimeZoneNeedsConfirmation;
});

test('progress bar start and end positions', function() {
Navigation.simMandatory = true;
Navigation.totalSteps = numSteps; // explicitly set the total steps
Expand Down
29 changes: 19 additions & 10 deletions apps/ftu/test/unit/timezone_test.js
Expand Up @@ -163,10 +163,11 @@ suite('timezones >', function() {
});

test('we use DEFAULT value', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'America');
assert.equal(currentValues.city, 'New York');
assert.isTrue(needsConfirmation);
done();
}

Expand All @@ -181,8 +182,8 @@ suite('timezones >', function() {
// we need a live connection for this test (network)
conn = new window.MockMobileconnection();
conn.voice = {
connected: true,
network: {
connected: true,
mcc: 466, // Asia / Taipei
mnc: 2
}
Expand All @@ -197,10 +198,13 @@ suite('timezones >', function() {
});

test('get timezone from the connection', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'Australia');
assert.equal(currentValues.city, 'Sydney');
// When the timezone (and presumably the time) is loaded from
// network, it doesn't need to be confirmed by the user.
assert.isFalse(needsConfirmation);
done();
}

Expand All @@ -209,11 +213,12 @@ suite('timezones >', function() {
});

test('if unknown MCC/MNC, use default', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
// DEFAULT is America/New_York
assert.equal(currentValues.region, 'America');
assert.equal(currentValues.city, 'New York');
assert.isTrue(needsConfirmation);
done();
}

Expand Down Expand Up @@ -242,10 +247,11 @@ suite('timezones >', function() {
});

test('get timezone from SIM', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'Europe');
assert.equal(currentValues.city, 'London');
assert.isTrue(needsConfirmation);
done();
}

Expand All @@ -258,10 +264,11 @@ suite('timezones >', function() {
});

test('if unknown MCC/MNC, use default', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'America');
assert.equal(currentValues.city, 'New York');
assert.isTrue(needsConfirmation);
done();
}

Expand Down Expand Up @@ -292,12 +299,12 @@ suite('timezones >', function() {
});

test('> default is used while waiting for access', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.lengthOf(IccHelper.mEventListeners.iccinfochange, 1);
assert.equal(currentValues.region, 'America');
assert.equal(currentValues.city, 'New York');

assert.isTrue(needsConfirmation);
done();
}

Expand All @@ -306,12 +313,13 @@ suite('timezones >', function() {

test('> reload when granted access to SIM info', function(done) {
var calls = 0;
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
calls++;
IccHelper.mTriggerEventListeners('iccinfochange', {});
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'Europe');
assert.equal(currentValues.city, 'London');
assert.isTrue(needsConfirmation);
// we expect 2 calls before finishing
if (calls === 2) {
done();
Expand Down Expand Up @@ -343,10 +351,11 @@ suite('timezones >', function() {
// });

test('> previous selection prevails', function(done) {
function tzLoaded() {
function tzLoaded(timezone, needsConfirmation) {
var currentValues = getTextFromSelectors();
assert.equal(currentValues.region, 'Europe');
assert.equal(currentValues.city, 'Madrid');
assert.isTrue(needsConfirmation);
done();
}

Expand Down
7 changes: 7 additions & 0 deletions apps/ftu/test/unit/ui_manager_test.js
Expand Up @@ -114,6 +114,13 @@ suite('UI Manager > ', function() {
UIManager.setTimeZone(FAKE_TIMEZONE);
});

test('should set/clear timeZoneNeedsConfirmation flag', function() {
UIManager.setTimeZone(FAKE_TIMEZONE, true);
assert.isTrue(UIManager.timeZoneNeedsConfirmation);
UIManager.setTimeZone(FAKE_TIMEZONE, false);
assert.isFalse(UIManager.timeZoneNeedsConfirmation);
});

test('should localize title', function() {
assert.isTrue(localizeSpy.calledOnce);
assert.isTrue(localizeSpy.calledWith(timezoneTitle,
Expand Down
80 changes: 41 additions & 39 deletions shared/js/tz_select.js
Expand Up @@ -3,8 +3,14 @@
'use strict';

function tzSelect(regionSelector, citySelector, onchange, onload) {
var TIMEZONE_FILE = '/shared/resources/tz.json';
var APN_TZ_FILE = '/shared/resources/apn_tz.json';
const TIMEZONE_FILE = '/shared/resources/tz.json';
const APN_TZ_FILE = '/shared/resources/apn_tz.json';

const TIMEZONE_NONE = 'NONE';
const TIMEZONE_NETWORK = 'NETWORK';
const TIMEZONE_SIM = 'SIM';
const TIMEZONE_USER = 'USER';
const TIMEZONE_PREVIOUS_SETTING = 'PREVIOUS_SETTING';

function loadJSON(href, callback) {
var xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -33,6 +39,8 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
// Worst case scenario: default to New York (which is just as good or
// bad as anything else -- we used to default to Pago Pago)
var tzDefault = 'America/New_York';
var source = TIMEZONE_NONE;

// retrieve MCC/MNC: use the current network codes when available,
// default to the SIM codes if necessary.
var mcc, mnc;
Expand All @@ -42,14 +50,16 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
window.navigator.mozMobileConnections &&
window.navigator.mozMobileConnections[0];
if (conn && IccHelper) {
if (conn.voice && conn.voice.network && conn.voice.network.connected) {
if (conn.voice && conn.voice.network && conn.voice.connected) {
// we have connection available, so we use it
mcc = conn.voice.network.mcc;
mnc = conn.voice.network.mnc;
source = TIMEZONE_NETWORK;
} else if (IccHelper.iccInfo) {
// we don't have connection available, we rely on the SIM
mcc = IccHelper.iccInfo.mcc;
mnc = IccHelper.iccInfo.mnc;
source = TIMEZONE_SIM;
// if SIM is not available, mcc and mnc are null,
// so we wait for a future event where we have access to the SIM.
if (IccHelper.cardState !== 'ready') {
Expand All @@ -64,7 +74,7 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
}

if (!mcc) {
callback(tzDefault);
callback(tzDefault, TIMEZONE_NONE);
return;
}

Expand All @@ -74,27 +84,27 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
if (response) {
var tz = response[mcc];
if (typeof(tz) === 'string') {
tzDefault = tz;
callback(tz, source);
return;
} else if (tz && (mnc in tz)) {
tzDefault = tz[mnc];
callback(tz[mnc], source);
return;
}
}
callback(tzDefault);
callback(tzDefault, TIMEZONE_NONE);
});
}


/**
* Activate a timezone selector UI
*/
function newTZSelector(onchangeTZ, currentID, alreadyDefined) {
function newTZSelector(onchangeTZ, currentID, source) {
// for region, we use whatever is BEFORE the /
var gRegion = currentID.replace(/\/.*/, '');
// for city, we use whatever is AFTER the /
var gCity = currentID.replace(/.*?\//, '');
var gTZ = null;
var loaded = false;
var userSelected = false;

function fillSelectElement(selector, options) {
selector.innerHTML = '';
Expand Down Expand Up @@ -141,19 +151,14 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
});
}
fillSelectElement(citySelector, options);

if (loaded) {
setTimezone();
} else {
if (onload) {
onload(getTZInfo());
}
loaded = true;
}
setTimezone();
}

function setTimezone() {
onchangeTZ(getTZInfo(), {'changedByUser': userSelected});
onchangeTZ(getTZInfo(), {
'changedByUser': source === TIMEZONE_USER,
'needsConfirmation': source !== TIMEZONE_USER &&
source !== TIMEZONE_NETWORK});
}

function getTZInfo() {
Expand All @@ -171,20 +176,17 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
}

regionSelector.onchange = function() {
userSelected = true;
source = TIMEZONE_USER;
fillCities();
};
citySelector.onchange = function() {
userSelected = true;
source = TIMEZONE_USER;
setTimezone();
};

loadJSON(TIMEZONE_FILE, function loadTZ(response) {
gTZ = response;
fillRegions();
if (!alreadyDefined) { // no timezone defined: `currentID' is a new value
setTimezone();
}
});
}

Expand All @@ -202,26 +204,26 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
setTimezoneDescription(event.settingValue);
});

function initSelector(initialValue, alreadyDefined) {
function initSelector(initialValue, source) {
// initialize the timezone selector with the initial TZ setting
newTZSelector(function updateTZ(tz, options) {
var req = settings.createLock().set({'time.timezone': tz.id});
if (onchange) {
req.onsuccess = function updateTZ_callback() {
// Store the user manually selected timezone separately
if (options.changedByUser) {
settings.createLock().set({'time.timezone.user-selected': tz.id});
}

req.onsuccess = function updateTZ_callback() {
// Store the user manually selected timezone separately
if (options.changedByUser) {
settings.createLock().set({'time.timezone.user-selected': tz.id});
// Wait until the timezone is actually set
// before calling the callback.
window.addEventListener('moztimechange', function timeChanged() {
onchange && window.addEventListener('moztimechange',
function timeChanged() {
window.removeEventListener('moztimechange', timeChanged);
onchange(tz);
onchange(tz, options.needsConfirmation);
});
};
}
}, initialValue, alreadyDefined);
} else {
onload && onload(tz, options.needsConfirmation);
}
};
}, initialValue, source);
}

var reqTimezone = settings.createLock().get('time.timezone');
Expand All @@ -232,7 +234,7 @@ function tzSelect(regionSelector, citySelector, onchange, onload) {
reqUserTZ.onsuccess = function dt_getUserTimezoneSuccess() {
var userSelTimezone = reqUserTZ.result['time.timezone.user-selected'];
if (userSelTimezone) {
initSelector(userSelTimezone, true);
initSelector(userSelTimezone, TIMEZONE_PREVIOUS_SETTING);
} else {
getDefaultTimezoneID(initSelector);
}
Expand Down

0 comments on commit c35e153

Please sign in to comment.