From 37a330d0f57205ee479e59446df6499a7d280a3f Mon Sep 17 00:00:00 2001 From: Amir Nissim Date: Tue, 10 Jun 2014 13:30:16 +0300 Subject: [PATCH] Bug 1024330 - [Everything.me] Device context for Partners API: * send ctx param to API * device.js simpler with getters * observe window.languagechange and use navigator.language instead of settings["language.current"] --- apps/collection/create.html | 1 - apps/collection/view.html | 1 - apps/search/index.html | 1 - shared/js/everythingme/api.js | 66 ++++++++++++++-------- shared/js/everythingme/device.js | 97 +++++++++++++------------------- 5 files changed, 82 insertions(+), 84 deletions(-) diff --git a/apps/collection/create.html b/apps/collection/create.html index 1915e88c42ad..c5035552d204 100644 --- a/apps/collection/create.html +++ b/apps/collection/create.html @@ -14,7 +14,6 @@ - diff --git a/apps/collection/view.html b/apps/collection/view.html index baa29dab3e96..604a34000cf5 100644 --- a/apps/collection/view.html +++ b/apps/collection/view.html @@ -6,7 +6,6 @@ - diff --git a/apps/search/index.html b/apps/search/index.html index 0af143d683e1..999a9bea3e24 100644 --- a/apps/search/index.html +++ b/apps/search/index.html @@ -25,7 +25,6 @@ - diff --git a/shared/js/everythingme/api.js b/shared/js/everythingme/api.js index b289a9e46cb4..be92cbe81604 100644 --- a/shared/js/everythingme/api.js +++ b/shared/js/everythingme/api.js @@ -3,11 +3,41 @@ (function(eme) { - var OK = 1; - var NETWORK_ERROR = 'network error'; + const OK = 1; + const NETWORK_ERROR = 'network error'; - var API_URL = 'https://api.everything.me/partners/1.0/{resource}/'; - var API_KEY = '79011a035b40ef3d7baeabc8f85b862f'; + const API_URL = 'https://api.everything.me/partners/1.0/{resource}/'; + const API_KEY = '79011a035b40ef3d7baeabc8f85b862f'; + + var device = eme.device; + + function getCtx() { + var w = device.screen.width; + var h = device.screen.height; + + var lat; + var lon; + var position = device.position; + + if (position && position.coords) { + lat = position.coords.latitude; + lon = position.coords.longitude; + } + + // default to undefined's so that JSON.stringify will drop them + return { + lc: device.language || undefined, + tz: device.timezone || undefined, + v: device.osVersion || undefined, + dn: device.deviceName || undefined, + cr: device.carrier || undefined, + sr: (w && h) ? [w, h].join('x') : undefined, + ll: (lat && lon) ? [lat,lon].join(',') : undefined + // TODO hc: home country + // TODO ct: connection type - wifi, 2g, 3g, 4g + }; + + } /** * Make an async httpRequest to resource with given options. @@ -21,29 +51,19 @@ options = options ? options : {}; - // must send API key options.apiKey = API_KEY; - - // device info - options.lc = eme.device.lc; - options.tz = eme.device.tz; - options.osVersion = eme.device.osVersion; options.deviceId = eme.device.deviceId; - options.deviceType = eme.device.deviceType; - options.carrierName = eme.device.carrierName; - // user location - var position = eme.device.position; - if (position && position.coords) { - var lat = position.coords.latitude; - var lon = position.coords.longitude; - options.latlon = (lat && lon) ? [lat,lon].join(',') : null; - } + options.ctx = getCtx(); + + for (var opt in options) { + var value = options[opt]; + if (value !== null && value !== undefined) { + if (typeof value === 'object') { + value = JSON.stringify(value); + } - for (var k in options) { - var v = options[k]; - if (v !== null && v !== undefined) { - payload += k + '=' + encodeURIComponent(options[k]) + '&'; + payload += opt + '=' + encodeURIComponent(value) + '&'; } } diff --git a/shared/js/everythingme/device.js b/shared/js/everythingme/device.js index f50df9cb62dd..5527b85fbb66 100644 --- a/shared/js/everythingme/device.js +++ b/shared/js/everythingme/device.js @@ -1,6 +1,5 @@ 'use strict'; /* global Promise */ -/* global MobileOperator */ (function(eme) { @@ -9,7 +8,7 @@ var mozMobileConnections = navigator.mozMobileConnections; // geolocation - var lastPosition = null; + var position = null; var positionPromise = null; const positionTTL = 10 * 60 * 1000; @@ -19,13 +18,13 @@ }; + // returns a promise always resolved with current position or null + // caller should check the resolved value function updatePosition() { if (positionPromise) { return positionPromise; } - // always resolves - // caller should check the resolved value is not null positionPromise = new Promise(function ready(resolve) { // TODO // it looks like getCurrentPosition never returns cached position @@ -36,22 +35,22 @@ // this does not seem to work on an unagi. // for now, we defer the init() until the position request is done. geolocation.getCurrentPosition( - function success(position) { + function success(newPosition) { positionPromise = null; - if (position && position.coords) { - eme.log('new position', position.coords.latitude, - position.coords.longitude, - position.timestamp); + if (newPosition && newPosition.coords) { + eme.log('new position', newPosition.coords.latitude, + newPosition.coords.longitude, + newPosition.timestamp); - lastPosition = position; + position = newPosition; } - resolve(lastPosition); + resolve(position); }, function error(positionError) { positionPromise = null; eme.log('position error', positionError.code, positionError.message); - resolve(lastPosition); + resolve(position); }, geoOptions); }); @@ -59,53 +58,24 @@ return positionPromise; } - function getTimezoneOffset() { - return (new Date().getTimezoneOffset() / -60).toString(); - } - - function getCarrier(mobileConnection) { - if (MobileOperator && mobileConnection) { - var info = MobileOperator.userFacingInfo(mobileConnection); - return info.operator || null; - } - - return null; - } - function Device() { - // device info - // set async. when calling init() - this.lc = null; - this.tz = null; + // set async. from settings when calling init() this.osVersion = null; this.deviceId = null; - this.deviceType = null; - this.carrierName = null; + this.deviceName = null; + this.screen = { width: window.screen.width * window.devicePixelRatio, height: window.screen.height * window.devicePixelRatio }; - - // observe info that may change - mozSettings.addObserver('language.current', function setLocale(e) { - var current = this.lc; - this.lc = e.settingValue || navigator.language || ''; - eme.log('lc changed', current, '->', this.lc); - }.bind(this)); - - mozSettings.addObserver('time.timezone', function setTimezone(e) { - var current = this.tz; - this.tz = getTimezoneOffset(); - eme.log('tz changed', current, '->', this.tz); - }.bind(this)); } Device.prototype = { init: function init() { - return Promise.all([this.updateDeviceInfo(), updatePosition()]); + return Promise.all([this.readSettings(), updatePosition()]); }, - updateDeviceInfo: function updateDeviceInfo() { + readSettings: function readSettings() { return new Promise(function ready(resolve, reject) { var lock = mozSettings.createLock(); var request = lock.get('*'); @@ -121,12 +91,9 @@ }); } - this.lc = settings['language.current']; - this.tz = getTimezoneOffset(); - this.osVersion = settings['deviceinfo.os']; this.deviceId = deviceId; - this.deviceType = settings['deviceinfo.product_model']; - this.carrierName = getCarrier(this.mobileConnection); + this.deviceName = settings['deviceinfo.product_model']; + this.osVersion = settings['deviceinfo.os']; resolve(); }.bind(this); @@ -148,23 +115,37 @@ return 'fxos-' + id; }, - get mobileConnection() { - if (mozMobileConnections && mozMobileConnections.length) { - return mozMobileConnections[0]; + get language() { + return navigator.language; + }, + + get timezone() { + return (new Date().getTimezoneOffset() / -60).toString(); + }, + + get carrier() { + var network; + var carrier; + var connection = mozMobileConnections && + mozMobileConnections.length && mozMobileConnections[0]; + + if (connection && connection.voice) { + network = connection.voice.network; + carrier = network ? (network.shortName || network.longName) : null; } - return null; + return carrier; }, // returns last known position get position() { - if (!lastPosition || - (Date.now() - lastPosition.timestamp > positionTTL)) { + if (!position || + (Date.now() - position.timestamp > positionTTL)) { updatePosition(); } - return lastPosition; + return position; } };