totp_app_settings.c 15 KB

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