Merge branch 'master' into dev
This commit is contained in:
350
js/app.js
350
js/app.js
@ -11,10 +11,18 @@
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
var events;
|
||||
|
||||
/* 'downloads' for debugging,
|
||||
* 'wgt-private' for any release */
|
||||
var STORAGE_SPACE = "wgt-private";
|
||||
var SHORT_WAIT = 10000;
|
||||
var LONG_WAIT = 600000;
|
||||
var wait_time = LONG_WAIT;
|
||||
var events = null;
|
||||
var deviceFingerprint = null;
|
||||
var eventsTimeStamp = 0;
|
||||
|
||||
(function() {
|
||||
@ -24,85 +32,207 @@ var eventsTimeStamp = 0;
|
||||
ctxContent,
|
||||
center,
|
||||
watchRadius;
|
||||
|
||||
function readJSON(path) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', path, true);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.onload = function(e) {
|
||||
if (this.status == 0) {
|
||||
var file = new File([this.response], 'temp');
|
||||
var fileReader = new FileReader();
|
||||
fileReader.addEventListener('load', function(){
|
||||
events = JSON.parse(fileReader.result);
|
||||
|
||||
var onError = function(e) {
|
||||
console.log('Error!' + e.message);
|
||||
};
|
||||
|
||||
var onResolveSuccess = function(dir) {
|
||||
var onListFilesSuccess = function(files) {
|
||||
files.forEach(function(file) {
|
||||
if (!file.isDirectory) {
|
||||
dir.deleteFile(file.fullPath, onDeleteSuccess, onError);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
dir.listFiles(onListFilesSuccess, onError);
|
||||
};
|
||||
|
||||
var onDeleteSuccess = function() {};
|
||||
|
||||
tizen.filesystem.resolve('/opt/usr/media/Downloads', onResolveSuccess, onError);
|
||||
|
||||
|
||||
|
||||
});
|
||||
fileReader.readAsText(file);
|
||||
}
|
||||
|
||||
|
||||
function deleteFile(name, callback) {
|
||||
|
||||
/* successful resolution of wgt-private */
|
||||
function onsuccess(dir){
|
||||
var dirfile = null;
|
||||
try {
|
||||
dirfile = dir.resolve(name);
|
||||
} catch (exc) {
|
||||
console.log(exc.message)
|
||||
}
|
||||
|
||||
// if file not existed, call callback with null
|
||||
if(dirfile == null) {
|
||||
callback();
|
||||
return;
|
||||
} else {
|
||||
|
||||
// if file was found, delete it
|
||||
dir.deleteFile(dirfile.fullPath, function() {
|
||||
console.log("deleted");
|
||||
callback();
|
||||
},
|
||||
function(e) {console.log(e)});
|
||||
}
|
||||
}
|
||||
|
||||
function onerror(e){
|
||||
console.log("error"+e);
|
||||
}
|
||||
xhr.send();
|
||||
|
||||
function onsuccessPermission(){
|
||||
tizen.filesystem.resolve(STORAGE_SPACE, onsuccess, onerror, "rw");
|
||||
}
|
||||
|
||||
function onErrorPermission(e){
|
||||
console.log("error "+ JSON.stringify(e));
|
||||
}
|
||||
|
||||
tizen.ppm.requestPermission("http://tizen.org/privilege/mediastorage", onsuccessPermission, onErrorPermission);
|
||||
}
|
||||
|
||||
function clearDownloads() {
|
||||
var onError = function(e) {
|
||||
console.log('Error!' + e.message);
|
||||
};
|
||||
|
||||
var onResolveSuccess = function(dir) {
|
||||
var onListFilesSuccess = function(files) {
|
||||
files.forEach(function(file) {
|
||||
if (!file.isDirectory) {
|
||||
dir.deleteFile(file.fullPath, onDeleteSuccess, onError);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
dir.listFiles(onListFilesSuccess, onError);
|
||||
};
|
||||
|
||||
var onDeleteSuccess = function() {};
|
||||
|
||||
tizen.filesystem.resolve('/opt/usr/media/Downloads', onResolveSuccess, onError);
|
||||
|
||||
}
|
||||
|
||||
function getJSON() {
|
||||
var downloadRequest = new tizen.DownloadRequest('https://longitudecalendar.com/userinfo/107971745944668140075/calendarevents.json', 'downloads');
|
||||
tizen.systeminfo.getPropertyValue('NETWORK', function(networkInfo) {
|
||||
if (networkInfo.networkType === 'NONE') {
|
||||
console.log('Network connection is not available.Download is not possible.');
|
||||
downloadRequest = null;
|
||||
}
|
||||
});
|
||||
function getJsonFile(name, callback) {
|
||||
console.log("searching for File");
|
||||
/* successful resolution of wgt-private */
|
||||
function onsuccess(dir){
|
||||
var dirfile = null;
|
||||
try {
|
||||
dirfile = dir.resolve(name);
|
||||
} catch (exc) {
|
||||
console.log(exc.message)
|
||||
}
|
||||
|
||||
var listener = {
|
||||
/* When the download progresses (interval is platform-dependent) */
|
||||
onprogress: function(id, receivedSize, totalSize) {
|
||||
console.log('progress for id: ' + id);
|
||||
console.log('received: ' + receivedSize + ", total: " + totalSize);
|
||||
},
|
||||
// if file not existed, call callback with null
|
||||
if(dirfile == null) {
|
||||
callback(null)
|
||||
return;
|
||||
} else {
|
||||
|
||||
// if file was found, call callback with json
|
||||
dirfile.openStream("r", function(fs) {
|
||||
callback(JSON.parse(fs.read(dirfile.fileSize)));
|
||||
fs.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function onerror(e){
|
||||
console.log("error"+e);
|
||||
}
|
||||
|
||||
function onsuccessPermission(){
|
||||
tizen.filesystem.resolve(STORAGE_SPACE, onsuccess, onerror, "rw");
|
||||
}
|
||||
|
||||
function onErrorPermission(e){
|
||||
console.log("error "+ JSON.stringify(e));
|
||||
}
|
||||
|
||||
tizen.ppm.requestPermission("http://tizen.org/privilege/mediastorage", onsuccessPermission, onErrorPermission);
|
||||
}
|
||||
|
||||
function readJSON() {
|
||||
/* - requests permission to view media storage
|
||||
* - resolves the file 'calendarevents'
|
||||
* - opens stream and reads entire file as json
|
||||
*/
|
||||
|
||||
getJsonFile("calendarevents", function(eventlist) {events = eventlist;});
|
||||
}
|
||||
|
||||
function getNewFingerprintFromServer() {
|
||||
getFileFromServer("/devicefingerprint.json", function() {
|
||||
console.log("getting device fingerprint from server")
|
||||
getJsonFile("devicefingerprint", function(df) {devicefingerprint = df; console.log(devicefingerprint)});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/* When the user pauses the download */
|
||||
onpaused: function(id) {
|
||||
console.log('Paused with id: ' + id);
|
||||
},
|
||||
function getDeviceFingerprint() {
|
||||
// check if device id set already as global, if it is return
|
||||
if(deviceFingerprint != null)
|
||||
return;
|
||||
|
||||
// otherwise, check if there is a devicefingerprint file in wgt-private
|
||||
// if there is, open it up and get your device ID from it
|
||||
getJsonFile("devicefingerprint", function(df) {
|
||||
if(df != null) {
|
||||
deviceFingerprint = df;
|
||||
} else {
|
||||
// otherwise, ask the longitude server for a device fingerprint file
|
||||
getNewFingerprintFromServer();
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function getFileFromServer(route, callback) {
|
||||
console.log("getting file");
|
||||
var server = "https://longitudecalendar.com/"
|
||||
var downloadRequest = new tizen.DownloadRequest(server + route, STORAGE_SPACE);
|
||||
tizen.systeminfo.getPropertyValue('NETWORK', function(networkInfo) {
|
||||
if (networkInfo.networkType === 'NONE') {
|
||||
console.log('Network connection is not available.Download is not possible.');
|
||||
downloadRequest = null;
|
||||
}
|
||||
});
|
||||
|
||||
var listener = {
|
||||
/* When the download progresses (interval is platform-dependent) */
|
||||
onprogress: function(id, receivedSize, totalSize) {
|
||||
console.log('progress for id: ' + id);
|
||||
},
|
||||
|
||||
/* When the user cancels the download */
|
||||
oncanceled: function(id) {
|
||||
console.log('Canceled with id: ' + id);
|
||||
},
|
||||
/* When the user pauses the download */
|
||||
onpaused: function(id) {
|
||||
console.log('Paused with id: ' + id);
|
||||
},
|
||||
|
||||
/* When the download is completed */
|
||||
oncompleted: function(id, fullPath) {
|
||||
console.log('Completed with id: ' + id + ', full path: ' + fullPath);
|
||||
readJSON(fullPath);
|
||||
},
|
||||
/* When the user cancels the download */
|
||||
oncanceled: function(id) {
|
||||
console.log('Canceled with id: ' + id);
|
||||
},
|
||||
|
||||
/* When the download fails */
|
||||
onfailed: function(id, error) {
|
||||
console.log('Failed with id: ' + id + ', error name: ' + error.name);
|
||||
}
|
||||
};
|
||||
downloadId = tizen.download.start(downloadRequest, listener);
|
||||
/* When the download is completed */
|
||||
oncompleted: function(id, fullPath) {
|
||||
console.log('Completed with id: ' + id + ', full path: ' + fullPath);
|
||||
callback();
|
||||
},
|
||||
|
||||
/* When the download fails */
|
||||
onfailed: function(id, error) {
|
||||
console.log('Failed with id: ' + id + ', error name: ' + error.name);
|
||||
}
|
||||
};
|
||||
tizen.download.start(downloadRequest, listener);
|
||||
}
|
||||
|
||||
function updateCalendar() {
|
||||
|
||||
if(deviceFingerprint == null) {
|
||||
console.log("no fingerprint, loading from file or server");
|
||||
getDeviceFingerprint();
|
||||
wait_time = SHORT_WAIT;
|
||||
return false;
|
||||
}
|
||||
|
||||
function deleteCallback() {
|
||||
console.log("done");
|
||||
console.log("getting new calendar events");
|
||||
getFileFromServer("device/" + deviceFingerprint.deviceName + "/calendarevents.json", readJSON);
|
||||
}
|
||||
|
||||
console.log("deleting calendarevents");
|
||||
deleteFile("calendarevents", deleteCallback);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -258,8 +388,8 @@ var eventsTimeStamp = 0;
|
||||
|
||||
function renderEarth(date, minute, second) {
|
||||
|
||||
earthColor = "#0077BE";
|
||||
earthDistance = document.body.clientWidth / 2 - 120;
|
||||
var earthColor = "#0077BE";
|
||||
var earthDistance = document.body.clientWidth / 2 - 120;
|
||||
renderCircle(ctxContent, polToCart(earthDistance, minuteToAngle(minute + second / 60)), 10, earthColor);
|
||||
}
|
||||
|
||||
@ -291,9 +421,7 @@ var eventsTimeStamp = 0;
|
||||
* @private
|
||||
*/
|
||||
function drawWatchLayout() {
|
||||
var grd,
|
||||
angle,
|
||||
i,
|
||||
var i,
|
||||
j;
|
||||
|
||||
// Clear canvas
|
||||
@ -347,32 +475,35 @@ var eventsTimeStamp = 0;
|
||||
// Draw the minute needle
|
||||
|
||||
renderEarth(ctxContent, minute, second);
|
||||
// renderNeedle(ctxContent, Math.PI * (((minute + second / 60) / 30) - 0.5), 0, 0.70, 3, "#454545");
|
||||
|
||||
// Draw the minute/hour circle
|
||||
// renderCircle(ctxContent, center, 8, "#454545");
|
||||
|
||||
// Draw the second needle
|
||||
// ctxContent.shadowOffsetX = 4;
|
||||
// ctxContent.shadowOffsetY = 4;
|
||||
// renderNeedle(ctxContent, Math.PI * ((second / 30) - 0.5), -0.10, 0.85, 1, "#c4c4c4");
|
||||
|
||||
// Draw the second circle
|
||||
// ctxContent.shadowOffsetX = 0;
|
||||
// ctxContent.shadowOffsetY = 0;
|
||||
// renderCircle(ctxContent, center, 5, "#c4c4c4");
|
||||
|
||||
// Draw the center circle
|
||||
// renderCircle(ctxContent, center, 2, "#454545");
|
||||
|
||||
// Draw the text for date
|
||||
// renderText(ctxContent, date, center.x, center.y + (watchRadius * 0.5), 25, "#999999");
|
||||
|
||||
|
||||
/* if no return from server yet */
|
||||
if(events == null)
|
||||
return;
|
||||
|
||||
thickness = 18;
|
||||
edge = document.body.clientWidth / 2 - thickness / 2 - 2;
|
||||
|
||||
/* if device not on server anymore */
|
||||
if(events.kind == "not found") {
|
||||
deviceFingerprint = null;
|
||||
events = null;
|
||||
deleteFile("devicefingerprint", function() {});
|
||||
wait_time = SHORT_WAIT;
|
||||
return;
|
||||
}
|
||||
/* if device not registered */
|
||||
if(events.kind == "unregistered") {
|
||||
wait_time = SHORT_WAIT;
|
||||
if(deviceFingerprint === null) {
|
||||
} else {
|
||||
renderText(ctxContent, deviceFingerprint.deviceName, center.x, center.y, 20, "FF0000");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("switched to long wait");
|
||||
wait_time = LONG_WAIT;
|
||||
|
||||
/* else: device registered and all events saved */
|
||||
var thickness = 18;
|
||||
var edge = document.body.clientWidth / 2 - thickness / 2 - 2;
|
||||
|
||||
for(var event in events.events){
|
||||
var startedBeforeToday = false;
|
||||
@ -426,15 +557,13 @@ var eventsTimeStamp = 0;
|
||||
|
||||
}
|
||||
|
||||
function updateCalendar(offset_ms) {
|
||||
function loopCalendar(offset_ms) {
|
||||
var d = new Date();
|
||||
var currentTime = d.getTime();
|
||||
console.log("current Time: " + currentTime);
|
||||
if(eventsTimeStamp + offset_ms < currentTime){
|
||||
console.log("updating Events");
|
||||
getJSON();
|
||||
eventsTimeStamp = currentTime;
|
||||
}
|
||||
updateCalendar();
|
||||
eventsTimeStamp = currentTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -482,6 +611,7 @@ var eventsTimeStamp = 0;
|
||||
* @private
|
||||
*/
|
||||
function init() {
|
||||
// clearDownloads();
|
||||
setDefaultVariables();
|
||||
setDefaultEvents();
|
||||
|
||||
@ -492,7 +622,7 @@ var eventsTimeStamp = 0;
|
||||
// Update the content of the watch every second
|
||||
setInterval(function() {
|
||||
drawWatchContent();
|
||||
updateCalendar(60000);
|
||||
loopCalendar(wait_time);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
@ -520,4 +650,4 @@ document.addEventListener('timetick', function(ev) {
|
||||
/* Update the UI */
|
||||
|
||||
drawWatchContent();
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user