2022-02-10 18:17:41 +00:00
|
|
|
#include <projdefs.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include <gui/elements.h>
|
|
|
|
#include <gui/icon.h>
|
|
|
|
#include <gui/view.h>
|
2022-10-28 14:08:50 +00:00
|
|
|
#include <assets_icons.h>
|
2022-02-10 18:17:41 +00:00
|
|
|
#include <portmacro.h>
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
#include <desktop/desktop_settings.h>
|
2022-02-10 18:17:41 +00:00
|
|
|
#include "../desktop_i.h"
|
|
|
|
#include "desktop_view_locked.h"
|
|
|
|
|
|
|
|
#define DOOR_MOVING_INTERVAL_MS (1000 / 16)
|
2022-04-14 12:20:41 +00:00
|
|
|
#define LOCKED_HINT_TIMEOUT_MS (1000)
|
2022-02-10 18:17:41 +00:00
|
|
|
#define UNLOCKED_HINT_TIMEOUT_MS (2000)
|
|
|
|
|
|
|
|
#define DOOR_OFFSET_START -55
|
|
|
|
#define DOOR_OFFSET_END 0
|
|
|
|
|
|
|
|
#define DOOR_L_FINAL_POS 0
|
|
|
|
#define DOOR_R_FINAL_POS 60
|
|
|
|
|
|
|
|
#define UNLOCK_CNT 3
|
|
|
|
#define UNLOCK_RST_TIMEOUT 600
|
|
|
|
|
|
|
|
struct DesktopViewLocked {
|
|
|
|
View* view;
|
|
|
|
DesktopViewLockedCallback callback;
|
|
|
|
void* context;
|
|
|
|
|
|
|
|
TimerHandle_t timer;
|
|
|
|
uint8_t lock_count;
|
|
|
|
uint32_t lock_lastpress;
|
|
|
|
};
|
|
|
|
|
2022-04-14 12:20:41 +00:00
|
|
|
typedef enum {
|
|
|
|
DesktopViewLockedStateUnlocked,
|
|
|
|
DesktopViewLockedStateLocked,
|
|
|
|
DesktopViewLockedStateDoorsClosing,
|
|
|
|
DesktopViewLockedStateLockedHintShown,
|
|
|
|
DesktopViewLockedStateUnlockedHintShown
|
|
|
|
} DesktopViewLockedState;
|
|
|
|
|
2022-02-10 18:17:41 +00:00
|
|
|
typedef struct {
|
|
|
|
bool pin_locked;
|
|
|
|
int8_t door_offset;
|
2022-04-14 12:20:41 +00:00
|
|
|
DesktopViewLockedState view_state;
|
2022-02-10 18:17:41 +00:00
|
|
|
} DesktopViewLockedModel;
|
|
|
|
|
|
|
|
void desktop_view_locked_set_callback(
|
|
|
|
DesktopViewLocked* locked_view,
|
|
|
|
DesktopViewLockedCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(locked_view);
|
|
|
|
furi_assert(callback);
|
|
|
|
locked_view->callback = callback;
|
|
|
|
locked_view->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void locked_view_timer_callback(TimerHandle_t timer) {
|
|
|
|
DesktopViewLocked* locked_view = pvTimerGetTimerID(timer);
|
|
|
|
locked_view->callback(DesktopLockedEventUpdate, locked_view->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void desktop_view_locked_doors_draw(Canvas* canvas, DesktopViewLockedModel* model) {
|
|
|
|
int8_t offset = model->door_offset;
|
|
|
|
uint8_t door_left_x = DOOR_L_FINAL_POS + offset;
|
|
|
|
uint8_t door_right_x = DOOR_R_FINAL_POS - offset;
|
|
|
|
uint8_t height = icon_get_height(&I_DoorLeft_70x55);
|
|
|
|
canvas_draw_icon(canvas, door_left_x, canvas_height(canvas) - height, &I_DoorLeft_70x55);
|
|
|
|
canvas_draw_icon(canvas, door_right_x, canvas_height(canvas) - height, &I_DoorRight_70x55);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool desktop_view_locked_doors_move(DesktopViewLockedModel* model) {
|
|
|
|
bool stop = false;
|
|
|
|
if(model->door_offset < DOOR_OFFSET_END) {
|
|
|
|
model->door_offset = CLAMP(model->door_offset + 5, DOOR_OFFSET_END, DOOR_OFFSET_START);
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void desktop_view_locked_update_hint_icon_timeout(DesktopViewLocked* locked_view) {
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
2022-04-14 12:20:41 +00:00
|
|
|
const bool change_state = (model->view_state == DesktopViewLockedStateLocked) &&
|
|
|
|
!model->pin_locked;
|
|
|
|
if(change_state) {
|
|
|
|
model->view_state = DesktopViewLockedStateLockedHintShown;
|
|
|
|
}
|
|
|
|
view_commit_model(locked_view->view, change_state);
|
|
|
|
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(LOCKED_HINT_TIMEOUT_MS), portMAX_DELAY);
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void desktop_view_locked_update(DesktopViewLocked* locked_view) {
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
2022-04-14 12:20:41 +00:00
|
|
|
DesktopViewLockedState view_state = model->view_state;
|
|
|
|
|
|
|
|
if(view_state == DesktopViewLockedStateDoorsClosing &&
|
|
|
|
!desktop_view_locked_doors_move(model)) {
|
2023-08-17 10:04:40 +00:00
|
|
|
locked_view->callback(DesktopLockedEventDoorsClosed, locked_view->context);
|
2022-04-14 12:20:41 +00:00
|
|
|
model->view_state = DesktopViewLockedStateLocked;
|
|
|
|
} else if(view_state == DesktopViewLockedStateLockedHintShown) {
|
|
|
|
model->view_state = DesktopViewLockedStateLocked;
|
|
|
|
} else if(view_state == DesktopViewLockedStateUnlockedHintShown) {
|
|
|
|
model->view_state = DesktopViewLockedStateUnlocked;
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
2022-04-14 12:20:41 +00:00
|
|
|
|
2022-02-10 18:17:41 +00:00
|
|
|
view_commit_model(locked_view->view, true);
|
|
|
|
|
2022-04-14 12:20:41 +00:00
|
|
|
if(view_state != DesktopViewLockedStateDoorsClosing) {
|
2022-02-10 18:17:41 +00:00
|
|
|
xTimerStop(locked_view->timer, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void desktop_view_locked_draw(Canvas* canvas, void* model) {
|
|
|
|
DesktopViewLockedModel* m = model;
|
2022-04-14 12:20:41 +00:00
|
|
|
DesktopViewLockedState view_state = m->view_state;
|
2022-02-10 18:17:41 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
2022-04-14 12:20:41 +00:00
|
|
|
if(view_state == DesktopViewLockedStateDoorsClosing) {
|
|
|
|
desktop_view_locked_doors_draw(canvas, m);
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Locked");
|
|
|
|
} else if(view_state == DesktopViewLockedStateLockedHintShown) {
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
elements_bold_rounded_frame(canvas, 14, 2 + STATUS_BAR_Y_SHIFT, 99, 48);
|
|
|
|
elements_multiline_text(canvas, 65, 20 + STATUS_BAR_Y_SHIFT, "To unlock\npress:");
|
2022-05-19 16:07:45 +00:00
|
|
|
canvas_draw_icon(canvas, 65, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
|
|
|
|
canvas_draw_icon(canvas, 80, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
|
|
|
|
canvas_draw_icon(canvas, 95, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
|
2022-04-14 12:20:41 +00:00
|
|
|
canvas_draw_icon(canvas, 16, 7 + STATUS_BAR_Y_SHIFT, &I_WarningDolphin_45x42);
|
|
|
|
canvas_draw_dot(canvas, 17, 61);
|
|
|
|
} else if(view_state == DesktopViewLockedStateUnlockedHintShown) {
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Unlocked");
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
View* desktop_view_locked_get_view(DesktopViewLocked* locked_view) {
|
|
|
|
furi_assert(locked_view);
|
|
|
|
return locked_view->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool desktop_view_locked_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(event);
|
|
|
|
furi_assert(context);
|
|
|
|
|
2022-04-14 12:20:41 +00:00
|
|
|
bool is_changed = false;
|
|
|
|
const uint32_t press_time = xTaskGetTickCount();
|
|
|
|
DesktopViewLocked* locked_view = context;
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
|
|
|
if(model->view_state == DesktopViewLockedStateUnlockedHintShown &&
|
|
|
|
event->type == InputTypePress) {
|
|
|
|
model->view_state = DesktopViewLockedStateUnlocked;
|
|
|
|
is_changed = true;
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
2022-04-14 12:20:41 +00:00
|
|
|
const DesktopViewLockedState view_state = model->view_state;
|
|
|
|
const bool pin_locked = model->pin_locked;
|
|
|
|
view_commit_model(locked_view->view, is_changed);
|
2022-02-10 18:17:41 +00:00
|
|
|
|
2022-07-06 13:54:08 +00:00
|
|
|
if(view_state == DesktopViewLockedStateUnlocked) {
|
2022-11-04 06:08:51 +00:00
|
|
|
return false;
|
2022-04-14 12:20:41 +00:00
|
|
|
} else if(view_state == DesktopViewLockedStateLocked && pin_locked) {
|
2022-02-10 18:17:41 +00:00
|
|
|
locked_view->callback(DesktopLockedEventShowPinInput, locked_view->context);
|
2022-04-14 12:20:41 +00:00
|
|
|
} else if(
|
|
|
|
view_state == DesktopViewLockedStateLocked ||
|
|
|
|
view_state == DesktopViewLockedStateLockedHintShown) {
|
2022-02-10 18:17:41 +00:00
|
|
|
if(press_time - locked_view->lock_lastpress > UNLOCK_RST_TIMEOUT) {
|
|
|
|
locked_view->lock_lastpress = press_time;
|
|
|
|
locked_view->lock_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
desktop_view_locked_update_hint_icon_timeout(locked_view);
|
2022-04-14 12:20:41 +00:00
|
|
|
|
2022-02-10 18:17:41 +00:00
|
|
|
if(event->key == InputKeyBack) {
|
2022-07-06 13:54:08 +00:00
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
locked_view->lock_lastpress = press_time;
|
|
|
|
locked_view->lock_count++;
|
|
|
|
if(locked_view->lock_count == UNLOCK_CNT) {
|
|
|
|
locked_view->callback(DesktopLockedEventUnlocked, locked_view->context);
|
|
|
|
}
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
locked_view->lock_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
locked_view->lock_lastpress = press_time;
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:20:41 +00:00
|
|
|
return true;
|
2022-02-10 18:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DesktopViewLocked* desktop_view_locked_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
DesktopViewLocked* locked_view = malloc(sizeof(DesktopViewLocked));
|
2022-02-10 18:17:41 +00:00
|
|
|
locked_view->view = view_alloc();
|
|
|
|
locked_view->timer =
|
|
|
|
xTimerCreate(NULL, 1000 / 16, pdTRUE, locked_view, locked_view_timer_callback);
|
|
|
|
|
|
|
|
view_allocate_model(locked_view->view, ViewModelTypeLocking, sizeof(DesktopViewLockedModel));
|
|
|
|
view_set_context(locked_view->view, locked_view);
|
|
|
|
view_set_draw_callback(locked_view->view, desktop_view_locked_draw);
|
|
|
|
view_set_input_callback(locked_view->view, desktop_view_locked_input);
|
|
|
|
|
|
|
|
return locked_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void desktop_view_locked_free(DesktopViewLocked* locked_view) {
|
|
|
|
furi_assert(locked_view);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_free(locked_view->timer);
|
2022-02-10 18:17:41 +00:00
|
|
|
view_free(locked_view->view);
|
|
|
|
free(locked_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void desktop_view_locked_close_doors(DesktopViewLocked* locked_view) {
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
2022-04-14 12:20:41 +00:00
|
|
|
furi_assert(model->view_state == DesktopViewLockedStateLocked);
|
|
|
|
model->view_state = DesktopViewLockedStateDoorsClosing;
|
2022-02-10 18:17:41 +00:00
|
|
|
model->door_offset = DOOR_OFFSET_START;
|
|
|
|
view_commit_model(locked_view->view, true);
|
|
|
|
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(DOOR_MOVING_INTERVAL_MS), portMAX_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void desktop_view_locked_lock(DesktopViewLocked* locked_view, bool pin_locked) {
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
2023-08-18 08:27:12 +00:00
|
|
|
furi_assert(model->view_state == DesktopViewLockedStateUnlocked);
|
2022-04-14 12:20:41 +00:00
|
|
|
model->view_state = DesktopViewLockedStateLocked;
|
2022-02-10 18:17:41 +00:00
|
|
|
model->pin_locked = pin_locked;
|
|
|
|
view_commit_model(locked_view->view, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void desktop_view_locked_unlock(DesktopViewLocked* locked_view) {
|
|
|
|
locked_view->lock_count = 0;
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
2022-04-14 12:20:41 +00:00
|
|
|
model->view_state = DesktopViewLockedStateUnlockedHintShown;
|
2022-02-10 18:17:41 +00:00
|
|
|
model->pin_locked = false;
|
|
|
|
view_commit_model(locked_view->view, true);
|
|
|
|
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(UNLOCKED_HINT_TIMEOUT_MS), portMAX_DELAY);
|
|
|
|
}
|
2022-04-14 12:20:41 +00:00
|
|
|
|
|
|
|
bool desktop_view_locked_is_locked_hint_visible(DesktopViewLocked* locked_view) {
|
|
|
|
DesktopViewLockedModel* model = view_get_model(locked_view->view);
|
|
|
|
const DesktopViewLockedState view_state = model->view_state;
|
|
|
|
view_commit_model(locked_view->view, false);
|
2023-05-01 23:38:42 +00:00
|
|
|
return view_state == DesktopViewLockedStateLockedHintShown ||
|
|
|
|
view_state == DesktopViewLockedStateLocked;
|
2022-04-14 12:20:41 +00:00
|
|
|
}
|