This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
device.js
173 lines (140 loc) · 4.77 KB
/
device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
/* global Promise */
/* global MobileOperator */
(function(eme) {
var geolocation = navigator.geolocation;
var mozSettings = navigator.mozSettings;
var mozMobileConnections = navigator.mozMobileConnections;
// geolocation
var lastPosition = null;
var positionPromise = null;
const positionTTL = 10 * 60 * 1000;
const geoOptions = {
timeout: 4000,
maximumAge: positionTTL
};
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
// even though we are passing a maximumAge > 0
// I would assume that on second launch of the collection app there should
// already be a cached location and that a new position request will not
// be fired.
// 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) {
positionPromise = null;
if (position && position.coords) {
eme.log('new position', position.coords.latitude,
position.coords.longitude,
position.timestamp);
lastPosition = position;
}
resolve(lastPosition);
},
function error(positionError) {
positionPromise = null;
eme.log('position error', positionError.code, positionError.message);
resolve(lastPosition);
},
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;
this.osVersion = null;
this.deviceId = null;
this.deviceType = null;
this.carrierName = 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()]);
},
updateDeviceInfo: function updateDeviceInfo() {
return new Promise(function ready(resolve, reject) {
var lock = mozSettings.createLock();
var request = lock.get('*');
request.onsuccess = function onsuccess() {
var settings = request.result;
var deviceId = settings['search.deviceId'];
if (!deviceId) {
deviceId = this.generateDeviceId();
mozSettings.createLock().set({
'search.deviceId': deviceId
});
}
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);
resolve();
}.bind(this);
request.onerror = function onerror() {
eme.log('fatal error accessing device settings', request.error);
reject();
};
}.bind(this));
},
// see duplicate in homescreen/everything.me.js
generateDeviceId: function generateDeviceId() {
var url = window.URL.createObjectURL(new Blob());
var id = url.replace('blob:', '');
window.URL.revokeObjectURL(url);
return 'fxos-' + id;
},
get mobileConnection() {
if (mozMobileConnections && mozMobileConnections.length) {
return mozMobileConnections[0];
}
return null;
},
// returns last known position
get position() {
if (!lastPosition ||
(Date.now() - lastPosition.timestamp > positionTTL)) {
updatePosition();
}
return lastPosition;
}
};
eme.device = new Device();
})(window.eme);