totp_app_settings.c 15 KB

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