totp_app_settings.c 15 KB

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