diff --git a/CalendarWatch.wgt b/CalendarWatch.wgt index 42370e3..e5ee681 100644 Binary files a/CalendarWatch.wgt and b/CalendarWatch.wgt differ diff --git a/js/app.js b/js/app.js index 3d9197d..92d67d0 100644 --- a/js/app.js +++ b/js/app.js @@ -17,7 +17,10 @@ /* 'downloads' for debugging, * 'wgt-private' for any release */ -var STORAGE_SPACE = "downloads"; +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; @@ -30,9 +33,73 @@ var eventsTimeStamp = 0; center, watchRadius; + + 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); + } + + 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 getJsonFile(name, callback) { - + console.log("searching for File"); /* successful resolution of wgt-private */ function onsuccess(dir){ var dirfile = null; @@ -78,20 +145,17 @@ var eventsTimeStamp = 0; */ getJsonFile("calendarevents", function(eventlist) {events = eventlist;}); - console.log(events); } function getNewFingerprintFromServer() { - console.log("fingerprintfromserver"); getFileFromServer("/devicefingerprint.json", function() { - console.log("1"); + console.log("getting device fingerprint from server") getJsonFile("devicefingerprint", function(df) {devicefingerprint = df; console.log(devicefingerprint)}); }); } function getDeviceFingerprint() { - console.log("getfingerprint"); // check if device id set already as global, if it is return if(deviceFingerprint != null) return; @@ -110,6 +174,7 @@ var eventsTimeStamp = 0; } 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) { @@ -146,58 +211,28 @@ var eventsTimeStamp = 0; console.log('Failed with id: ' + id + ', error name: ' + error.name); } }; - downloadId = tizen.download.start(downloadRequest, listener); + tizen.download.start(downloadRequest, listener); } - function getJSON() { + + 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("userinfo/" + deviceFingerprint.deviceId + "/calendarevents.json", readJSON); + } - - console.log(deviceFingerprint); - console.log(deviceFingerprint.deviceId); - getFileFromServer("userinfo/" + deviceFingerprint.deviceId + "/calendarevents.json", readJSON); + console.log("deleting calendarevents"); + deleteFile("calendarevents", deleteCallback); + return true; - - var downloadRequest = new tizen.DownloadRequest('https://longitudecalendar.com/userinfo/107971745944668140075/calendarevents.json', 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); - console.log('received: ' + receivedSize + ", total: " + totalSize); - }, - - /* When the user pauses the download */ - onpaused: function(id) { - console.log('Paused with id: ' + id); - }, - - /* When the user cancels the download */ - oncanceled: function(id) { - console.log('Canceled with id: ' + id); - }, - - /* When the download is completed */ - oncompleted: function(id, fullPath) { - console.log('Completed with id: ' + id + ', full path: ' + fullPath); - readJSON(); - }, - - /* When the download fails */ - onfailed: function(id, error) { - console.log('Failed with id: ' + id + ', error name: ' + error.name); - } - }; - downloadId = tizen.download.start(downloadRequest, listener); } /** @@ -353,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); } @@ -386,9 +421,7 @@ var eventsTimeStamp = 0; * @private */ function drawWatchLayout() { - var grd, - angle, - i, + var i, j; // Clear canvas @@ -442,32 +475,29 @@ 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 registered */ + if(events.kind == "unregistered") { + wait_time = SHORT_WAIT; + if(deviceFingerprint === null) { + console.log("no fingerprint to render"); + } else { + console.log("rendering text"); + renderText(ctxContent, deviceFingerprint.deviceId, 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; @@ -521,11 +551,11 @@ var eventsTimeStamp = 0; } - function updateCalendar(offset_ms) { + function loopCalendar(offset_ms) { var d = new Date(); var currentTime = d.getTime(); if(eventsTimeStamp + offset_ms < currentTime){ - getJSON(); + updateCalendar(); eventsTimeStamp = currentTime; } @@ -575,7 +605,7 @@ var eventsTimeStamp = 0; * @private */ function init() { - + // clearDownloads(); setDefaultVariables(); setDefaultEvents(); @@ -586,7 +616,7 @@ var eventsTimeStamp = 0; // Update the content of the watch every second setInterval(function() { drawWatchContent(); - updateCalendar(5000); //60000 -> 10 minutes + loopCalendar(wait_time); }, 1000); }