totp_app_settings.c 14 KB

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