totp_app_settings.c 14 KB

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