totp_app_settings.c 14 KB

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