totp_app_settings.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "totp_app_settings.h"
  2. #include <math.h>
  3. #include <totp_icons.h>
  4. #include "../../../services/fonts/font_provider.h"
  5. #include "../../canvas_extensions.h"
  6. #include "../../ui_controls.h"
  7. #include "../../common_dialogs.h"
  8. #include "../../scene_director.h"
  9. #include "../../constants.h"
  10. #include "../../../services/config/config.h"
  11. #include "../../../services/convert/convert.h"
  12. #include "../../../services/kb_layouts/kb_layout_provider.h"
  13. #include <roll_value.h>
  14. #include "../../../config/app/config.h"
  15. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  16. #include "../../../workers/bt_type_code/bt_type_code.h"
  17. #endif
  18. #include <assets_icons.h>
  19. #define FONT_TEST_STR_LENGTH (7)
  20. static const char* YES_NO_LIST[] = {"NO", "YES"};
  21. static const char* AUTOMATION_LIST[] = {
  22. "None",
  23. "USB"
  24. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  25. ,
  26. "Bluetooth",
  27. "BT and USB"
  28. #endif
  29. };
  30. static const char* FONT_TEST_STR = "0123BCD";
  31. typedef enum {
  32. HoursInput,
  33. MinutesInput,
  34. FontSelect,
  35. SoundSwitch,
  36. VibroSwitch,
  37. AutomationSwitch,
  38. BadKeyboardLayoutSelect,
  39. AutomationDelaySelect,
  40. ConfirmButton
  41. } Control;
  42. typedef struct {
  43. int8_t tz_offset_hours;
  44. uint8_t tz_offset_minutes;
  45. bool notification_sound;
  46. bool notification_vibro;
  47. AutomationMethod automation_method;
  48. uint16_t y_offset;
  49. AutomationKeyboardLayout automation_kb_layout;
  50. uint8_t automation_kb_layout_count;
  51. char automation_kb_layout_name[TOTP_KB_LAYOUT_NAME_MAX_LENGTH + 1];
  52. Control selected_control;
  53. uint8_t active_font_index;
  54. FontInfo* active_font;
  55. uint8_t total_fonts_count;
  56. uint16_t automation_initial_delay;
  57. char automation_initial_delay_formatted[10];
  58. } SceneState;
  59. static void two_digit_to_str(int8_t num, char* str) {
  60. char* s = str;
  61. if(num < 0) {
  62. *(s++) = '-';
  63. num = -num;
  64. }
  65. uint8_t d1 = (num / 10) % 10;
  66. uint8_t d2 = num % 10;
  67. *(s++) = CONVERT_DIGIT_TO_CHAR(d1);
  68. *(s++) = CONVERT_DIGIT_TO_CHAR(d2);
  69. *(s++) = '\0';
  70. }
  71. static void update_formatted_automation_initial_delay(SceneState* scene_state) {
  72. snprintf(
  73. &scene_state->automation_initial_delay_formatted[0],
  74. sizeof(scene_state->automation_initial_delay_formatted),
  75. "%.1f sec.",
  76. (double)(scene_state->automation_initial_delay / 1000.0f));
  77. }
  78. static void update_formatted_automation_kb_layout_name(SceneState* scene_state) {
  79. totp_kb_layout_provider_get_layout_name(
  80. scene_state->automation_kb_layout,
  81. &scene_state->automation_kb_layout_name[0],
  82. sizeof(scene_state->automation_kb_layout_name));
  83. }
  84. void totp_scene_app_settings_activate(PluginState* plugin_state) {
  85. SceneState* scene_state = malloc(sizeof(SceneState));
  86. furi_check(scene_state != NULL);
  87. plugin_state->current_scene_state = scene_state;
  88. float off_int;
  89. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  90. scene_state->tz_offset_hours = off_int;
  91. scene_state->tz_offset_minutes = 60.0f * off_dec;
  92. scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
  93. scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
  94. scene_state->automation_method =
  95. MIN(plugin_state->automation_method, COUNT_OF(AUTOMATION_LIST) - 1);
  96. scene_state->automation_kb_layout_count = totp_kb_layout_provider_get_layouts_count();
  97. scene_state->automation_kb_layout =
  98. MIN(plugin_state->automation_kb_layout, scene_state->automation_kb_layout_count - 1);
  99. scene_state->automation_initial_delay = plugin_state->automation_initial_delay;
  100. scene_state->total_fonts_count = totp_font_provider_get_fonts_count();
  101. scene_state->active_font_index = plugin_state->active_font_index;
  102. scene_state->active_font = totp_font_info_alloc();
  103. if(!totp_font_provider_get_font(scene_state->active_font_index, scene_state->active_font)) {
  104. scene_state->active_font_index = 0;
  105. totp_font_provider_get_font(scene_state->active_font_index, scene_state->active_font);
  106. }
  107. update_formatted_automation_initial_delay(scene_state);
  108. update_formatted_automation_kb_layout_name(scene_state);
  109. }
  110. void totp_scene_app_settings_render(Canvas* const canvas, const PluginState* plugin_state) {
  111. const SceneState* scene_state = plugin_state->current_scene_state;
  112. if(scene_state->selected_control < FontSelect) {
  113. canvas_set_font(canvas, FontPrimary);
  114. canvas_draw_str_aligned(
  115. canvas, 0, 0 - scene_state->y_offset, AlignLeft, AlignTop, "Timezone offset");
  116. canvas_set_font(canvas, FontSecondary);
  117. char tmp_str[4];
  118. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  119. canvas_draw_str_aligned(
  120. canvas, 0, 17 - scene_state->y_offset, AlignLeft, AlignTop, "Hours:");
  121. ui_control_select_render(
  122. canvas,
  123. 36,
  124. 10 - scene_state->y_offset,
  125. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  126. &tmp_str[0],
  127. scene_state->selected_control == HoursInput);
  128. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  129. canvas_draw_str_aligned(
  130. canvas, 0, 35 - scene_state->y_offset, AlignLeft, AlignTop, "Minutes:");
  131. ui_control_select_render(
  132. canvas,
  133. 36,
  134. 28 - scene_state->y_offset,
  135. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  136. &tmp_str[0],
  137. scene_state->selected_control == MinutesInput);
  138. } else if(scene_state->selected_control < SoundSwitch) {
  139. canvas_set_font(canvas, FontPrimary);
  140. canvas_draw_str_aligned(
  141. canvas, 0, 64 - scene_state->y_offset, AlignLeft, AlignTop, "Font");
  142. canvas_set_font(canvas, FontSecondary);
  143. const FontInfo* const font = scene_state->active_font;
  144. ui_control_select_render(
  145. canvas,
  146. 0,
  147. 74 - scene_state->y_offset,
  148. SCREEN_WIDTH - UI_CONTROL_VSCROLL_WIDTH,
  149. font->name,
  150. scene_state->selected_control == FontSelect);
  151. uint8_t font_x_offset =
  152. SCREEN_WIDTH_CENTER -
  153. (((font->char_info[0].width + font->space_width) * FONT_TEST_STR_LENGTH) >> 1);
  154. uint8_t font_y_offset = 108 - scene_state->y_offset - (font->height >> 1);
  155. canvas_draw_str_ex(
  156. canvas, font_x_offset, font_y_offset, FONT_TEST_STR, FONT_TEST_STR_LENGTH, font);
  157. } else if(scene_state->selected_control < AutomationSwitch) {
  158. canvas_set_font(canvas, FontPrimary);
  159. canvas_draw_str_aligned(
  160. canvas, 0, 128 - scene_state->y_offset, AlignLeft, AlignTop, "Notifications");
  161. canvas_set_font(canvas, FontSecondary);
  162. canvas_draw_str_aligned(
  163. canvas, 0, 145 - scene_state->y_offset, AlignLeft, AlignTop, "Sound:");
  164. ui_control_select_render(
  165. canvas,
  166. 36,
  167. 138 - scene_state->y_offset,
  168. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  169. YES_NO_LIST[scene_state->notification_sound],
  170. scene_state->selected_control == SoundSwitch);
  171. canvas_draw_str_aligned(
  172. canvas, 0, 163 - scene_state->y_offset, AlignLeft, AlignTop, "Vibro:");
  173. ui_control_select_render(
  174. canvas,
  175. 36,
  176. 156 - scene_state->y_offset,
  177. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  178. YES_NO_LIST[scene_state->notification_vibro],
  179. scene_state->selected_control == VibroSwitch);
  180. } else {
  181. canvas_set_font(canvas, FontPrimary);
  182. canvas_draw_str_aligned(
  183. canvas, 0, 192 - scene_state->y_offset, AlignLeft, AlignTop, "Automation");
  184. canvas_set_font(canvas, FontSecondary);
  185. int group_offset = 0;
  186. if(scene_state->selected_control <= AutomationSwitch) {
  187. canvas_draw_str_aligned(
  188. canvas,
  189. 0,
  190. 209 - scene_state->y_offset - group_offset,
  191. AlignLeft,
  192. AlignTop,
  193. "Method:");
  194. ui_control_select_render(
  195. canvas,
  196. 36,
  197. 202 - scene_state->y_offset - group_offset,
  198. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  199. AUTOMATION_LIST[scene_state->automation_method],
  200. scene_state->selected_control == AutomationSwitch);
  201. } else {
  202. group_offset += 18;
  203. }
  204. canvas_draw_str_aligned(
  205. canvas, 0, 227 - scene_state->y_offset - group_offset, AlignLeft, AlignTop, "Layout:");
  206. ui_control_select_render(
  207. canvas,
  208. 36,
  209. 220 - scene_state->y_offset - group_offset,
  210. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  211. scene_state->automation_kb_layout_name,
  212. scene_state->selected_control == BadKeyboardLayoutSelect);
  213. canvas_draw_str_aligned(
  214. canvas, 0, 245 - scene_state->y_offset - group_offset, AlignLeft, AlignTop, "Delay:");
  215. ui_control_select_render(
  216. canvas,
  217. 36,
  218. 238 - scene_state->y_offset - group_offset,
  219. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  220. &scene_state->automation_initial_delay_formatted[0],
  221. scene_state->selected_control == AutomationDelaySelect);
  222. ui_control_button_render(
  223. canvas,
  224. SCREEN_WIDTH_CENTER - 24,
  225. 260 - scene_state->y_offset - group_offset,
  226. 48,
  227. 13,
  228. "Confirm",
  229. scene_state->selected_control == ConfirmButton);
  230. }
  231. ui_control_vscroll_render(
  232. canvas, SCREEN_WIDTH - 3, 0, SCREEN_HEIGHT, scene_state->selected_control, ConfirmButton);
  233. }
  234. bool totp_scene_app_settings_handle_event(
  235. const PluginEvent* const event,
  236. PluginState* plugin_state) {
  237. if(event->type != EventTypeKey) {
  238. return true;
  239. }
  240. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  241. if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  242. switch(event->input.key) {
  243. case InputKeyUp:
  244. totp_roll_value_uint8_t(
  245. &scene_state->selected_control,
  246. -1,
  247. HoursInput,
  248. ConfirmButton,
  249. RollOverflowBehaviorStop);
  250. if(scene_state->selected_control > VibroSwitch) {
  251. scene_state->y_offset = SCREEN_HEIGHT * 3;
  252. } else if(scene_state->selected_control > FontSelect) {
  253. scene_state->y_offset = SCREEN_HEIGHT * 2;
  254. } else if(scene_state->selected_control > MinutesInput) {
  255. scene_state->y_offset = SCREEN_HEIGHT;
  256. } else {
  257. scene_state->y_offset = 0;
  258. }
  259. break;
  260. case InputKeyDown:
  261. totp_roll_value_uint8_t(
  262. &scene_state->selected_control,
  263. 1,
  264. HoursInput,
  265. ConfirmButton,
  266. RollOverflowBehaviorStop);
  267. if(scene_state->selected_control > VibroSwitch) {
  268. scene_state->y_offset = SCREEN_HEIGHT * 3;
  269. } else if(scene_state->selected_control > FontSelect) {
  270. scene_state->y_offset = SCREEN_HEIGHT * 2;
  271. } else if(scene_state->selected_control > MinutesInput) {
  272. scene_state->y_offset = SCREEN_HEIGHT;
  273. } else {
  274. scene_state->y_offset = 0;
  275. }
  276. break;
  277. case InputKeyRight:
  278. if(scene_state->selected_control == HoursInput) {
  279. totp_roll_value_int8_t(
  280. &scene_state->tz_offset_hours, 1, -12, 12, RollOverflowBehaviorStop);
  281. } else if(scene_state->selected_control == MinutesInput) {
  282. totp_roll_value_uint8_t(
  283. &scene_state->tz_offset_minutes, 15, 0, 45, RollOverflowBehaviorRoll);
  284. } else if(scene_state->selected_control == FontSelect) {
  285. totp_roll_value_uint8_t(
  286. &scene_state->active_font_index,
  287. 1,
  288. 0,
  289. scene_state->total_fonts_count - 1,
  290. RollOverflowBehaviorRoll);
  291. totp_font_provider_get_font(
  292. scene_state->active_font_index, scene_state->active_font);
  293. } else if(scene_state->selected_control == SoundSwitch) {
  294. scene_state->notification_sound = !scene_state->notification_sound;
  295. } else if(scene_state->selected_control == VibroSwitch) {
  296. scene_state->notification_vibro = !scene_state->notification_vibro;
  297. } else if(scene_state->selected_control == AutomationSwitch) {
  298. totp_roll_value_uint8_t(
  299. &scene_state->automation_method,
  300. 1,
  301. 0,
  302. COUNT_OF(AUTOMATION_LIST) - 1,
  303. RollOverflowBehaviorRoll);
  304. } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
  305. totp_roll_value_uint8_t(
  306. &scene_state->automation_kb_layout,
  307. 1,
  308. 0,
  309. scene_state->automation_kb_layout_count - 1,
  310. RollOverflowBehaviorRoll);
  311. update_formatted_automation_kb_layout_name(scene_state);
  312. } else if(scene_state->selected_control == AutomationDelaySelect) {
  313. totp_roll_value_uint16_t(
  314. &scene_state->automation_initial_delay,
  315. 500,
  316. 0,
  317. 60000,
  318. RollOverflowBehaviorStop);
  319. update_formatted_automation_initial_delay(scene_state);
  320. }
  321. break;
  322. case InputKeyLeft:
  323. if(scene_state->selected_control == HoursInput) {
  324. totp_roll_value_int8_t(
  325. &scene_state->tz_offset_hours, -1, -12, 12, RollOverflowBehaviorStop);
  326. } else if(scene_state->selected_control == MinutesInput) {
  327. totp_roll_value_uint8_t(
  328. &scene_state->tz_offset_minutes, -15, 0, 45, RollOverflowBehaviorRoll);
  329. } else if(scene_state->selected_control == FontSelect) {
  330. totp_roll_value_uint8_t(
  331. &scene_state->active_font_index,
  332. -1,
  333. 0,
  334. scene_state->total_fonts_count - 1,
  335. RollOverflowBehaviorRoll);
  336. totp_font_provider_get_font(
  337. scene_state->active_font_index, scene_state->active_font);
  338. } else if(scene_state->selected_control == SoundSwitch) {
  339. scene_state->notification_sound = !scene_state->notification_sound;
  340. } else if(scene_state->selected_control == VibroSwitch) {
  341. scene_state->notification_vibro = !scene_state->notification_vibro;
  342. } else if(scene_state->selected_control == AutomationSwitch) {
  343. totp_roll_value_uint8_t(
  344. &scene_state->automation_method,
  345. -1,
  346. 0,
  347. COUNT_OF(AUTOMATION_LIST) - 1,
  348. RollOverflowBehaviorRoll);
  349. } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
  350. totp_roll_value_uint8_t(
  351. &scene_state->automation_kb_layout,
  352. -1,
  353. 0,
  354. scene_state->automation_kb_layout_count - 1,
  355. RollOverflowBehaviorRoll);
  356. update_formatted_automation_kb_layout_name(scene_state);
  357. } else if(scene_state->selected_control == AutomationDelaySelect) {
  358. totp_roll_value_uint16_t(
  359. &scene_state->automation_initial_delay,
  360. -500,
  361. 0,
  362. 60000,
  363. RollOverflowBehaviorStop);
  364. update_formatted_automation_initial_delay(scene_state);
  365. }
  366. break;
  367. case InputKeyOk:
  368. if(scene_state->selected_control == ConfirmButton) {
  369. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
  370. (float)scene_state->tz_offset_minutes / 60.0f;
  371. plugin_state->notification_method =
  372. (scene_state->notification_sound ? NotificationMethodSound :
  373. NotificationMethodNone) |
  374. (scene_state->notification_vibro ? NotificationMethodVibro :
  375. NotificationMethodNone);
  376. plugin_state->automation_method = scene_state->automation_method;
  377. plugin_state->active_font_index = scene_state->active_font_index;
  378. plugin_state->automation_kb_layout = scene_state->automation_kb_layout;
  379. plugin_state->automation_initial_delay = scene_state->automation_initial_delay;
  380. if(!totp_config_file_update_user_settings(plugin_state)) {
  381. totp_dialogs_config_updating_error(plugin_state);
  382. return false;
  383. }
  384. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  385. if((scene_state->automation_method & AutomationMethodBadBt) == 0 &&
  386. plugin_state->bt_type_code_worker_context != NULL) {
  387. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  388. plugin_state->bt_type_code_worker_context = NULL;
  389. }
  390. #endif
  391. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  392. }
  393. break;
  394. case InputKeyBack: {
  395. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  396. break;
  397. }
  398. default:
  399. break;
  400. }
  401. }
  402. return true;
  403. }
  404. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  405. if(plugin_state->current_scene_state == NULL) return;
  406. SceneState* scene_state = plugin_state->current_scene_state;
  407. totp_font_info_free(scene_state->active_font);
  408. free(scene_state);
  409. plugin_state->current_scene_state = NULL;
  410. }