Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Added idle timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpMaster committed Sep 20, 2014
1 parent 095af56 commit 7cceab4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 59 deletions.
104 changes: 53 additions & 51 deletions appinfo.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
25 changes: 20 additions & 5 deletions src/js/pebble-js-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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) {
Expand Down Expand Up @@ -66,14 +68,16 @@ 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) {
console.log("INFO: otp_count="+otp_count);
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 ##############
Expand All @@ -83,6 +87,7 @@ Pebble.addEventListener("ready",
// }
// localStorage.removeItem("theme");
// localStorage.removeItem("font_style");
// localStorage.removeItem("idle_timeout");
// ####### /CLEAN APP ##############
}
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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")
Expand Down
42 changes: 40 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -625,13 +649,15 @@ 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);
Tuple *timezone_tuple = dict_find(iter, JS_TIMEZONE);
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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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++) {
Expand Down
4 changes: 3 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -43,7 +44,8 @@ enum {
JS_THEME,
JS_DELETE_KEY,
JS_FONT_STYLE,
JS_DELETE_ALL
JS_DELETE_ALL,
JS_IDLE_TIMEOUT
};

enum {
Expand Down

0 comments on commit 7cceab4

Please sign in to comment.