totp_app_settings.c 15 KB

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