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 #246 from crdlc/bug-1067614-v2
Browse files Browse the repository at this point in the history
Bug 1067614 - [User Story] Ensure users are always on the latest loop mo...(cherry picked from commit 6face3a)
  • Loading branch information
Cristian Rodriguez committed Oct 29, 2014
1 parent 7fc9307 commit a8e1120
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 33 deletions.
6 changes: 5 additions & 1 deletion app/js/boot.js
Expand Up @@ -2,7 +2,11 @@

window.addEventListener('load', function load() {
window.removeEventListener('load', load);
CompatibilityChecker.check();
CompatibilityChecker.check().then(() => {
LazyLoader.load('js/update_checker.js', () => {
UpdateChecker.check();
});
});
// TODO Add LazyLoading
// TODO If it's an incoming call, I launch it before rendering the app
Controller.init();
Expand Down
67 changes: 36 additions & 31 deletions app/js/compatibility_checker.js
Expand Up @@ -50,8 +50,9 @@
}
}

function onConfirmed() {
function onConfirmed(callback) {
document.cookie = 'compatibility=confirmed';
callback && callback();
}

function isConfirmed() {
Expand All @@ -60,40 +61,44 @@

exports.CompatibilityChecker = {
check: function() {
if (isConfirmed()) {
return;
}

LazyLoader.getJSON('compatibility.json').then((conf) => {
// Check device
var matching = false;
return new Promise((resolve, reject) => {
if (isConfirmed()) {
resolve();
return;
}

var deviceNames = conf.device.names;
var length = deviceNames.length;
for (var i = 0; i < length; i++) {
matching = userAgent.contains(deviceNames[i]);
if (matching) {
break;
LazyLoader.getJSON('compatibility.json').then((conf) => {
// Check device
var matching = false;

var deviceNames = conf.device && conf.device.names;
deviceNames = deviceNames || [];
var length = deviceNames.length;
for (var i = 0; i < length; i++) {
matching = userAgent.contains(deviceNames[i]);
if (matching) {
break;
}
}
}

if (!matching) {
handleError('notCompatibleDevice');
return;
}
if (!matching) {
handleError('notCompatibleDevice');
return;
}

// Check operating system version
var majorVersion = getVersion().major;
// No message if we aren't able to know the version
if (majorVersion !== null &&
majorVersion < conf.os.minimumMajorVersion) {
handleError('oldOSVersion');
return;
}
onConfirmed();
}, (error) => {
onConfirmed();
console.error('Error parsing compatibility JSON file', error);
// Check operating system version
var majorVersion = getVersion().major;
// No message if we aren't able to know the version
if (majorVersion !== null &&
majorVersion < conf.os.minimumMajorVersion) {
handleError('oldOSVersion');
return;
}
onConfirmed(resolve);
}, (error) => {
onConfirmed(resolve);
console.error('Error parsing compatibility JSON file', error);
});
});
}
};
Expand Down
3 changes: 2 additions & 1 deletion app/js/config.js
Expand Up @@ -25,7 +25,8 @@ Config = {
offline: {
signInDelay: 60 * 1000, // 1 min
maxSignInAttempts: 3 // Max number of sign in attempts before logging out.
}
},
maxVersionCheckAttempts: 3 // Max number of retries checking version.
};

window.OTProperties = {
Expand Down
83 changes: 83 additions & 0 deletions app/js/update_checker.js
@@ -0,0 +1,83 @@
'use strict';

(function(exports) {

/*
* This variable indicates the number of retries when there was a connection
* problem.
*/
var retries = Config.maxVersionCheckAttempts;

function closeApp() {
window.close();
}

function showUpdateDialog(app) {
var onMozL10nReady = function() {
(new OptionMenu({
section: Branding.getTranslation('newUpdateAvailable'),
type: 'confirm',
items: [{
name: 'Cancel',
l10nId: 'cancel',
method: closeApp
}, {
name: 'Download',
l10nId:'download',
class: 'recommend',
method: function onDownload() {
app.download();
closeApp();
}
}]
})).show();
};

if (navigator.mozL10n.readyState === 'complete') {
onMozL10nReady();
} else {
navigator.mozL10n.ready(onMozL10nReady);
}
}

function connected() {
return new Promise(function(resolve, reject) {
if (navigator.onLine) {
resolve();
} else {
window.addEventListener('online', function onConnect() {
window.removeEventListener('online', onConnect);
resolve();
});
}
});
}

function checkForUpdate(app) {
if (--retries < 0) {
return;
}

var req = app.checkForUpdate();

req.onsuccess = function(event) {
app.downloadAvailable && showUpdateDialog(app);
};

req.onerror = function(error) {
if (error.name === 'NETWORK_ERROR') {
connected().then(checkForUpdate.bind(null, app));
}
};
}

exports.UpdateChecker = {
check: function() {
navigator.mozApps.getSelf().onsuccess = function(evt) {
var app = evt.target.result;
connected().then(checkForUpdate.bind(null, app));
};
}
};

})(window);

0 comments on commit a8e1120

Please sign in to comment.