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 #20414 from EverythingMe/1024330-ctx
Browse files Browse the repository at this point in the history
Bug 1024330 - [Everything.me] Device context for Partners API
  • Loading branch information
amirnissim committed Jun 12, 2014
2 parents 080027d + 37a330d commit a73d0a6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 84 deletions.
1 change: 0 additions & 1 deletion apps/collection/create.html
Expand Up @@ -14,7 +14,6 @@

<script defer src="shared/js/l10n.js"></script>
<script defer src="shared/js/settings_listener.js"></script>
<script defer src="shared/js/mobile_operator.js"></script>
<script defer src="shared/js/async_storage.js"></script>
<script defer src="shared/js/collections_database.js"></script>

Expand Down
1 change: 0 additions & 1 deletion apps/collection/view.html
Expand Up @@ -6,7 +6,6 @@

<script defer src="shared/js/l10n.js"></script>
<script defer src="shared/js/settings_listener.js"></script>
<script defer src="shared/js/mobile_operator.js"></script>
<script defer src="shared/js/lazy_loader.js"></script>
<script defer src="shared/js/component_utils.js"></script>
<script defer src="shared/js/collections_database.js"></script>
Expand Down
1 change: 0 additions & 1 deletion apps/search/index.html
Expand Up @@ -25,7 +25,6 @@
<script defer src="shared/js/url_helper.js"></script>
<script defer src="shared/js/utilities.js"></script>
<script defer src="shared/js/settings_listener.js"></script>
<script defer src="shared/js/mobile_operator.js"></script>
<script defer src="shared/js/everythingme/eme.js"></script>
<script defer src="shared/js/everythingme/cache.js"></script>
<script defer src="shared/js/everythingme/device.js"></script>
Expand Down
66 changes: 43 additions & 23 deletions shared/js/everythingme/api.js
Expand Up @@ -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.
Expand All @@ -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) + '&';
}
}

Expand Down
97 changes: 39 additions & 58 deletions shared/js/everythingme/device.js
@@ -1,6 +1,5 @@
'use strict';
/* global Promise */
/* global MobileOperator */

(function(eme) {

Expand All @@ -9,7 +8,7 @@
var mozMobileConnections = navigator.mozMobileConnections;

// geolocation
var lastPosition = null;
var position = null;
var positionPromise = null;

const positionTTL = 10 * 60 * 1000;
Expand All @@ -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
Expand All @@ -36,76 +35,47 @@
// 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);
});

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('*');
Expand All @@ -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);
Expand All @@ -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;
}
};

Expand Down

0 comments on commit a73d0a6

Please sign in to comment.