totp_app_settings.c 18 KB

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