From 7cceab434cf381b5b21678193ee90e2c8d2faee7 Mon Sep 17 00:00:00 2001 From: JumpMaster Date: Sat, 20 Sep 2014 12:50:59 +0100 Subject: [PATCH] Added idle timeout --- appinfo.json | 104 ++++++++++++++++++++-------------------- src/js/pebble-js-app.js | 25 ++++++++-- src/main.c | 42 +++++++++++++++- src/main.h | 4 +- 4 files changed, 116 insertions(+), 59 deletions(-) diff --git a/appinfo.json b/appinfo.json index 4bd1563..436fe81 100644 --- a/appinfo.json +++ b/appinfo.json @@ -1,82 +1,84 @@ { + "appKeys": { + "delete_all": 8, + "delete_key": 6, + "font_style": 7, + "idle_timeout": 9, + "key_count": 0, + "request_key": 1, + "show_message": 4, + "theme": 5, + "timezone": 3, + "transmit_key": 2 + }, + "capabilities": [ + "configurable" + ], + "companyName": "JumpMaster", + "longName": "PebbleAuth", + "projectType": "native", "resources": { "media": [ { - "name": "IMAGE_ICON", - "menuIcon": true, - "type": "png", - "file": "images/auth_icon.png" + "characterRegex": "[0-9]", + "file": "fonts/BD_Cartoon_Shout.ttf", + "name": "FONT_BD_CARTOON_28", + "type": "font" }, { - "name": "FONT_BITWISE_32", - "type": "font", - "file": "fonts/BITWISE_32.ttf" + "file": "fonts/BD_Cartoon_Shout.ttf", + "name": "FONT_BD_CARTOON_20", + "type": "font" }, { - "name": "FONT_ORBITRON_28", - "type": "font", - "file": "fonts/ORBITRON_28.ttf" + "characterRegex": "[0-9]", + "file": "fonts/ANDROID_24.ttf", + "name": "FONT_DIGITAL_38", + "type": "font" }, { - "name": "IMAGE_ICON_NO", - "type": "png", - "file": "images/No.png" + "file": "fonts/ANDROID_24.ttf", + "name": "FONT_DIGITAL_28", + "type": "font" }, { - "name": "IMAGE_ICON_YES", - "type": "png", - "file": "images/Yes.png" + "file": "fonts/UNISPACE_20.ttf", + "name": "FONT_UNISPACE_20", + "type": "font" }, { - "name": "FONT_UNISPACE_20", - "type": "font", - "file": "fonts/UNISPACE_20.ttf" + "file": "images/Yes.png", + "name": "IMAGE_ICON_YES", + "type": "png" }, { - "name": "FONT_DIGITAL_28", - "type": "font", - "file": "fonts/ANDROID_24.ttf" + "file": "images/No.png", + "name": "IMAGE_ICON_NO", + "type": "png" }, { - "name": "FONT_DIGITAL_38", - "characterRegex": "[0-9]", - "type": "font", - "file": "fonts/ANDROID_24.ttf" + "file": "fonts/ORBITRON_28.ttf", + "name": "FONT_ORBITRON_28", + "type": "font" }, { - "name": "FONT_BD_CARTOON_20", - "type": "font", - "file": "fonts/BD_Cartoon_Shout.ttf" + "file": "fonts/BITWISE_32.ttf", + "name": "FONT_BITWISE_32", + "type": "font" }, { - "name": "FONT_BD_CARTOON_28", - "characterRegex": "[0-9]", - "type": "font", - "file": "fonts/BD_Cartoon_Shout.ttf" + "file": "images/auth_icon.png", + "menuIcon": true, + "name": "IMAGE_ICON", + "type": "png" } ] }, - "companyName": "JumpMaster", "shortName": "PebbleAuth", "uuid": "1f4d9835-3b9a-4ddd-907e-41a25d06f19c", "versionCode": 5, - "capabilities": [ - "configurable" - ], - "longName": "PebbleAuth", + "versionLabel": "1.4", "watchapp": { "watchface": false - }, - "appKeys": { - "delete_all": 8, - "timezone": 3, - "font_style": 7, - "show_message": 4, - "delete_key": 6, - "transmit_key": 2, - "request_key": 1, - "key_count": 0, - "theme": 5 - }, - "versionLabel": "1.4" + } } diff --git a/src/js/pebble-js-app.js b/src/js/pebble-js-app.js index 2fbc02f..2827465 100644 --- a/src/js/pebble-js-app.js +++ b/src/js/pebble-js-app.js @@ -6,10 +6,11 @@ var otp_count = 0; var theme = 0; var font_style = 0; var timezoneOffset = 0; +var idle_timeout = 0; var message_send_retries = 0; var message_send_max_retries = 5; var app_version = 6; -var debug = true; +var debug = false; function loadLocalVariables() { @@ -24,11 +25,12 @@ function loadLocalVariables() { theme = parseInt(localStorage.getItem("theme")); font_style = parseInt(localStorage.getItem("font_style")); - + idle_timeout = localStorage.getItem("idle_timeout"); timezoneOffset = new Date().getTimezoneOffset(); theme = !theme ? 0 : theme; font_style = !font_style ? 0 : font_style; + idle_timeout = idle_timeout === null ? 300 : parseInt(idle_timeout); } function sendAppMessage(data) { @@ -66,7 +68,8 @@ Pebble.addEventListener("ready", "key_count":otp_count, "theme":theme, "timezone":timezoneOffset, - "font_style":font_style + "font_style":font_style, + "idle_timeout":idle_timeout }); if (debug) { @@ -74,6 +77,7 @@ Pebble.addEventListener("ready", console.log("INFO: theme="+theme); console.log("INFO: timezoneOffset="+timezoneOffset); console.log("INFO: font_style="+font_style); + console.log("INFO: idle_timeout="+idle_timeout); } // ####### CLEAN APP ############## @@ -83,6 +87,7 @@ Pebble.addEventListener("ready", // } // localStorage.removeItem("theme"); // localStorage.removeItem("font_style"); +// localStorage.removeItem("idle_timeout"); // ####### /CLEAN APP ############## } ); @@ -136,7 +141,8 @@ Pebble.addEventListener('showConfiguration', function(e) { app_version+'/'+ '?otp_count='+otp_count+ '&theme='+theme+ - '&font_style='+font_style; + '&font_style='+font_style+ + '&idle_timeout='+idle_timeout; if (debug) console.log("INFO: "+url); @@ -179,7 +185,16 @@ Pebble.addEventListener("webviewclosed", localStorage.setItem("font_style",font_style); config.font_style = font_style; } - + + if(!isNaN(configuration.idle_timeout) && configuration.idle_timeout != idle_timeout) { + if (debug) + console.log("INFO: Idle timeout changed"); + + idle_timeout = configuration.idle_timeout; + localStorage.setItem("idle_timeout",idle_timeout); + config.idle_timeout = idle_timeout; + } + if(configuration.label && configuration.secret) { var secret = configuration.secret .replace(/0/g,"O") diff --git a/src/main.c b/src/main.c index b50db47..e834729 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ #define MAX_LABEL_LENGTH 21 // 20 + termination #define MAX_KEY_LENGTH 65 // 64 + termination #define MAX_COMBINED_LENGTH MAX_LABEL_LENGTH+MAX_KEY_LENGTH -#define DEBUG true +#define DEBUG false // Main Window Window *main_window; @@ -62,6 +62,8 @@ unsigned int theme = 0; // 0 = Dark, 1 = Light unsigned int phone_otp_count = 0; unsigned int watch_otp_count = 0; +unsigned int idle_second_count = 0; +unsigned int idle_timeout = 300; unsigned int otp_selected = 0; unsigned int otp_default = 0; unsigned int otp_update_tick = 0; @@ -83,6 +85,10 @@ void refresh_screen_data(int direction) { start_refreshing(); } +void resetIdleTime() { + idle_second_count = 0; +} + void update_screen_fonts() { fonts_changed = true; refresh_screen_data(DOWN); @@ -240,7 +246,7 @@ void finish_refreshing() { if (watch_otp_count) strcpy(label_text, otp_labels[otp_selected]); else - strcpy(label_text, "TEST"); + strcpy(label_text, "EMPTY"); if (fonts_changed) set_fonts(); @@ -300,6 +306,18 @@ static void handle_second_tick(struct tm *tick_time, TimeUnits units_changed) { int seconds = tick_time->tm_sec; + if (idle_timeout > 0) { + // If app is idle after X minutes then exit + if (idle_second_count >= idle_timeout) { + if (DEBUG) + APP_LOG(APP_LOG_LEVEL_DEBUG, "INFO: Timer reached %d, exiting", idle_second_count); + window_stack_pop_all(true); + return; + } + else + idle_second_count += 1; + } + if (seconds % 30 == 0) otp_update_tick++; @@ -327,6 +345,7 @@ static void handle_second_tick(struct tm *tick_time, TimeUnits units_changed) { } void up_single_click_handler(ClickRecognizerRef recognizer, void *context) { + resetIdleTime(); if (watch_otp_count) { if (otp_selected == 0) otp_selected = (watch_otp_count-1); @@ -338,6 +357,7 @@ void up_single_click_handler(ClickRecognizerRef recognizer, void *context) { } void down_single_click_handler(ClickRecognizerRef recognizer, void *context) { + resetIdleTime(); if (watch_otp_count) { if (otp_selected == (watch_otp_count-1)) otp_selected = 0; @@ -377,6 +397,7 @@ void request_key(int code_id) { } void details_actionbar_up_click_handler(ClickRecognizerRef recognizer, void *context) { + resetIdleTime(); otp_default = details_selected_key; persist_write_int(PS_DEFAULT_KEY, otp_default); @@ -390,6 +411,7 @@ void details_actionbar_up_click_handler(ClickRecognizerRef recognizer, void *con } void details_actionbar_down_click_handler(ClickRecognizerRef recognizer, void *context) { + resetIdleTime(); request_delete(otp_keys[details_selected_key]); window_stack_remove(select_window, false); @@ -458,6 +480,7 @@ void details_window_unload(Window *window) { } static void key_menu_select_callback(int index, void *ctx) { + resetIdleTime(); details_selected_key = index; details_window = window_create(); @@ -511,6 +534,7 @@ void select_window_unload(Window *window) { } void select_single_click_handler(ClickRecognizerRef recognizer, void *context) { + resetIdleTime(); if (watch_otp_count) { select_window = window_create(); window_set_window_handlers(select_window, (WindowHandlers) { @@ -625,6 +649,7 @@ static void in_received_handler(DictionaryIterator *iter, void *context) { if (DEBUG) APP_LOG(APP_LOG_LEVEL_DEBUG, "INFO: Message Recieved"); + resetIdleTime(); Tuple *key_count_tuple = dict_find(iter, JS_KEY_COUNT); Tuple *key_tuple = dict_find(iter, JS_TRANSMIT_KEY); Tuple *key_delete_tuple = dict_find(iter, JS_DELETE_KEY); @@ -632,6 +657,7 @@ static void in_received_handler(DictionaryIterator *iter, void *context) { Tuple *theme_tuple = dict_find(iter, JS_THEME); Tuple *font_style_tuple = dict_find(iter, JS_FONT_STYLE); Tuple *delete_all_tuple = dict_find(iter, JS_DELETE_ALL); + Tuple *idle_timeout_tuple = dict_find(iter, JS_IDLE_TIMEOUT); // Act on the found fields received if (delete_all_tuple) { @@ -744,6 +770,17 @@ static void in_received_handler(DictionaryIterator *iter, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "INFO: Font style: %d", font_style); } } // font_style_tuple + + if (idle_timeout_tuple) { + unsigned int idle_timeout_value = idle_timeout_tuple->value->int16; + if (idle_timeout != idle_timeout_value) { + idle_timeout = idle_timeout_value; + persist_write_int(PS_IDLE_TIMEOUT, idle_timeout); + + if (DEBUG) + APP_LOG(APP_LOG_LEVEL_DEBUG, "INFO: Idle Timeout: %d", idle_timeout); + } + } // idle_timeout_tuple } void in_dropped_handler(AppMessageResult reason, void *context) { @@ -757,6 +794,7 @@ void load_persistent_data() { theme = persist_exists(PS_THEME) ? persist_read_int(PS_THEME) : 0; otp_default = persist_exists(PS_DEFAULT_KEY) ? persist_read_int(PS_DEFAULT_KEY) : 0; font_style = persist_exists(PS_FONT_STYLE) ? persist_read_int(PS_FONT_STYLE) : 0; + idle_timeout = persist_exists(PS_IDLE_TIMEOUT) ? persist_read_int(PS_IDLE_TIMEOUT) : 300; if (persist_exists(PS_SECRET)) { for(int i = 0; i < MAX_OTP; i++) { diff --git a/src/main.h b/src/main.h index 94a7c59..ee055a7 100644 --- a/src/main.h +++ b/src/main.h @@ -30,6 +30,7 @@ enum { PS_THEME, PS_DEFAULT_KEY, PS_FONT_STYLE, + PS_IDLE_TIMEOUT, PS_SECRET = 0x40 // Needs 16 spaces, should always be last }; @@ -43,7 +44,8 @@ enum { JS_THEME, JS_DELETE_KEY, JS_FONT_STYLE, - JS_DELETE_ALL + JS_DELETE_ALL, + JS_IDLE_TIMEOUT }; enum {