totp_app_settings.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "totp_app_settings.h"
  2. #include <math.h>
  3. #include <totp_icons.h>
  4. #include "../../ui_controls.h"
  5. #include "../../common_dialogs.h"
  6. #include "../../scene_director.h"
  7. #include "../token_menu/totp_scene_token_menu.h"
  8. #include "../../constants.h"
  9. #include "../../../services/config/config.h"
  10. #include "../../../services/convert/convert.h"
  11. #include <roll_value.h>
  12. #include "../../../types/nullable.h"
  13. #include "../../../features_config.h"
  14. #ifdef TOTP_BADBT_TYPE_ENABLED
  15. #include "../../../workers/bt_type_code/bt_type_code.h"
  16. #endif
  17. char* YES_NO_LIST[] = {"NO", "YES"};
  18. char* ON_OFF_LIST[] = {"OFF", "ON"};
  19. typedef enum {
  20. HoursInput,
  21. MinutesInput,
  22. Sound,
  23. Vibro,
  24. BadUsb,
  25. #ifdef TOTP_BADBT_TYPE_ENABLED
  26. BadBt,
  27. #endif
  28. ConfirmButton
  29. } Control;
  30. typedef struct {
  31. int8_t tz_offset_hours;
  32. uint8_t tz_offset_minutes;
  33. bool notification_sound;
  34. bool notification_vibro;
  35. bool badusb_enabled;
  36. #ifdef TOTP_BADBT_TYPE_ENABLED
  37. bool badbt_enabled;
  38. #endif
  39. uint8_t y_offset;
  40. TotpNullable_uint16_t current_token_index;
  41. Control selected_control;
  42. } SceneState;
  43. void totp_scene_app_settings_activate(
  44. PluginState* plugin_state,
  45. const AppSettingsSceneContext* context) {
  46. SceneState* scene_state = malloc(sizeof(SceneState));
  47. furi_check(scene_state != NULL);
  48. plugin_state->current_scene_state = scene_state;
  49. if(context != NULL) {
  50. TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);
  51. } else {
  52. TOTP_NULLABLE_NULL(scene_state->current_token_index);
  53. }
  54. float off_int;
  55. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  56. scene_state->tz_offset_hours = off_int;
  57. scene_state->tz_offset_minutes = 60.0f * off_dec;
  58. scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
  59. scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
  60. scene_state->badusb_enabled = plugin_state->automation_method & AutomationMethodBadUsb;
  61. #ifdef TOTP_BADBT_TYPE_ENABLED
  62. scene_state->badbt_enabled = plugin_state->automation_method & AutomationMethodBadBt;
  63. #endif
  64. }
  65. static void two_digit_to_str(int8_t num, char* str) {
  66. uint8_t index = 0;
  67. if(num < 0) {
  68. str[index++] = '-';
  69. num = -num;
  70. }
  71. uint8_t d1 = (num / 10) % 10;
  72. uint8_t d2 = num % 10;
  73. str[index++] = CONVERT_DIGIT_TO_CHAR(d1);
  74. str[index++] = CONVERT_DIGIT_TO_CHAR(d2);
  75. str[index++] = '\0';
  76. }
  77. void totp_scene_app_settings_render(Canvas* const canvas, const PluginState* plugin_state) {
  78. const SceneState* scene_state = plugin_state->current_scene_state;
  79. canvas_set_font(canvas, FontPrimary);
  80. canvas_draw_str_aligned(
  81. canvas, 0, 0 - scene_state->y_offset, AlignLeft, AlignTop, "Timezone offset");
  82. canvas_set_font(canvas, FontSecondary);
  83. char tmp_str[4];
  84. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  85. canvas_draw_str_aligned(canvas, 0, 17 - scene_state->y_offset, AlignLeft, AlignTop, "Hours:");
  86. ui_control_select_render(
  87. canvas,
  88. 36,
  89. 10 - scene_state->y_offset,
  90. SCREEN_WIDTH - 36,
  91. &tmp_str[0],
  92. scene_state->selected_control == HoursInput);
  93. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  94. canvas_draw_str_aligned(
  95. canvas, 0, 35 - scene_state->y_offset, AlignLeft, AlignTop, "Minutes:");
  96. ui_control_select_render(
  97. canvas,
  98. 36,
  99. 28 - scene_state->y_offset,
  100. SCREEN_WIDTH - 36,
  101. &tmp_str[0],
  102. scene_state->selected_control == MinutesInput);
  103. canvas_draw_icon(
  104. canvas,
  105. SCREEN_WIDTH_CENTER - 5,
  106. SCREEN_HEIGHT - 5 - scene_state->y_offset,
  107. &I_totp_arrow_bottom_10x5);
  108. canvas_set_font(canvas, FontPrimary);
  109. canvas_draw_str_aligned(
  110. canvas, 0, 64 - scene_state->y_offset, AlignLeft, AlignTop, "Notifications");
  111. canvas_set_font(canvas, FontSecondary);
  112. canvas_draw_str_aligned(canvas, 0, 81 - scene_state->y_offset, AlignLeft, AlignTop, "Sound:");
  113. ui_control_select_render(
  114. canvas,
  115. 36,
  116. 74 - scene_state->y_offset,
  117. SCREEN_WIDTH - 36,
  118. YES_NO_LIST[scene_state->notification_sound],
  119. scene_state->selected_control == Sound);
  120. canvas_draw_str_aligned(canvas, 0, 99 - scene_state->y_offset, AlignLeft, AlignTop, "Vibro:");
  121. ui_control_select_render(
  122. canvas,
  123. 36,
  124. 92 - scene_state->y_offset,
  125. SCREEN_WIDTH - 36,
  126. YES_NO_LIST[scene_state->notification_vibro],
  127. scene_state->selected_control == Vibro);
  128. canvas_draw_icon(
  129. canvas, SCREEN_WIDTH_CENTER - 5, 123 - scene_state->y_offset, &I_totp_arrow_bottom_10x5);
  130. canvas_set_font(canvas, FontPrimary);
  131. canvas_draw_str_aligned(
  132. canvas, 0, 128 - scene_state->y_offset, AlignLeft, AlignTop, "Automation");
  133. canvas_set_font(canvas, FontSecondary);
  134. canvas_draw_str_aligned(
  135. canvas, 0, 145 - scene_state->y_offset, AlignLeft, AlignTop, "BadUSB:");
  136. ui_control_select_render(
  137. canvas,
  138. 36,
  139. 138 - scene_state->y_offset,
  140. SCREEN_WIDTH - 36,
  141. ON_OFF_LIST[scene_state->badusb_enabled],
  142. scene_state->selected_control == BadUsb);
  143. #ifdef TOTP_BADBT_TYPE_ENABLED
  144. canvas_draw_str_aligned(canvas, 0, 163 - scene_state->y_offset, AlignLeft, AlignTop, "BadBT:");
  145. ui_control_select_render(
  146. canvas,
  147. 36,
  148. 156 - scene_state->y_offset,
  149. SCREEN_WIDTH - 36,
  150. ON_OFF_LIST[scene_state->badbt_enabled],
  151. scene_state->selected_control == BadBt);
  152. #endif
  153. ui_control_button_render(
  154. canvas,
  155. SCREEN_WIDTH_CENTER - 24,
  156. #ifdef TOTP_BADBT_TYPE_ENABLED
  157. 178 - scene_state->y_offset,
  158. #else
  159. 165 - scene_state->y_offset,
  160. #endif
  161. 48,
  162. 13,
  163. "Confirm",
  164. scene_state->selected_control == ConfirmButton);
  165. }
  166. bool totp_scene_app_settings_handle_event(
  167. const PluginEvent* const event,
  168. PluginState* plugin_state) {
  169. if(event->type != EventTypeKey) {
  170. return true;
  171. }
  172. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  173. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  174. return true;
  175. }
  176. switch(event->input.key) {
  177. case InputKeyUp:
  178. totp_roll_value_uint8_t(
  179. &scene_state->selected_control,
  180. -1,
  181. HoursInput,
  182. ConfirmButton,
  183. RollOverflowBehaviorStop);
  184. if(scene_state->selected_control > Vibro) {
  185. scene_state->y_offset = 128;
  186. } else if(scene_state->selected_control > MinutesInput) {
  187. scene_state->y_offset = 64;
  188. } else {
  189. scene_state->y_offset = 0;
  190. }
  191. break;
  192. case InputKeyDown:
  193. totp_roll_value_uint8_t(
  194. &scene_state->selected_control, 1, HoursInput, ConfirmButton, RollOverflowBehaviorStop);
  195. if(scene_state->selected_control > Vibro) {
  196. scene_state->y_offset = 128;
  197. } else if(scene_state->selected_control > MinutesInput) {
  198. scene_state->y_offset = 64;
  199. } else {
  200. scene_state->y_offset = 0;
  201. }
  202. break;
  203. case InputKeyRight:
  204. if(scene_state->selected_control == HoursInput) {
  205. totp_roll_value_int8_t(
  206. &scene_state->tz_offset_hours, 1, -12, 12, RollOverflowBehaviorStop);
  207. } else if(scene_state->selected_control == MinutesInput) {
  208. totp_roll_value_uint8_t(
  209. &scene_state->tz_offset_minutes, 15, 0, 45, RollOverflowBehaviorRoll);
  210. } else if(scene_state->selected_control == Sound) {
  211. scene_state->notification_sound = !scene_state->notification_sound;
  212. } else if(scene_state->selected_control == Vibro) {
  213. scene_state->notification_vibro = !scene_state->notification_vibro;
  214. } else if(scene_state->selected_control == BadUsb) {
  215. scene_state->badusb_enabled = !scene_state->badusb_enabled;
  216. }
  217. #ifdef TOTP_BADBT_TYPE_ENABLED
  218. else if(scene_state->selected_control == BadBt) {
  219. scene_state->badbt_enabled = !scene_state->badbt_enabled;
  220. }
  221. #endif
  222. break;
  223. case InputKeyLeft:
  224. if(scene_state->selected_control == HoursInput) {
  225. totp_roll_value_int8_t(
  226. &scene_state->tz_offset_hours, -1, -12, 12, RollOverflowBehaviorStop);
  227. } else if(scene_state->selected_control == MinutesInput) {
  228. totp_roll_value_uint8_t(
  229. &scene_state->tz_offset_minutes, -15, 0, 45, RollOverflowBehaviorRoll);
  230. } else if(scene_state->selected_control == Sound) {
  231. scene_state->notification_sound = !scene_state->notification_sound;
  232. } else if(scene_state->selected_control == Vibro) {
  233. scene_state->notification_vibro = !scene_state->notification_vibro;
  234. } else if(scene_state->selected_control == BadUsb) {
  235. scene_state->badusb_enabled = !scene_state->badusb_enabled;
  236. }
  237. #ifdef TOTP_BADBT_TYPE_ENABLED
  238. else if(scene_state->selected_control == BadBt) {
  239. scene_state->badbt_enabled = !scene_state->badbt_enabled;
  240. }
  241. #endif
  242. break;
  243. case InputKeyOk:
  244. if(scene_state->selected_control == ConfirmButton) {
  245. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
  246. (float)scene_state->tz_offset_minutes / 60.0f;
  247. plugin_state->notification_method =
  248. (scene_state->notification_sound ? NotificationMethodSound :
  249. NotificationMethodNone) |
  250. (scene_state->notification_vibro ? NotificationMethodVibro :
  251. NotificationMethodNone);
  252. plugin_state->automation_method =
  253. scene_state->badusb_enabled ? AutomationMethodBadUsb : AutomationMethodNone;
  254. #ifdef TOTP_BADBT_TYPE_ENABLED
  255. plugin_state->automation_method |= scene_state->badbt_enabled ? AutomationMethodBadBt :
  256. AutomationMethodNone;
  257. #endif
  258. if(totp_config_file_update_user_settings(plugin_state) !=
  259. TotpConfigFileUpdateSuccess) {
  260. totp_dialogs_config_updating_error(plugin_state);
  261. return false;
  262. }
  263. #ifdef TOTP_BADBT_TYPE_ENABLED
  264. if(!scene_state->badbt_enabled && plugin_state->bt_type_code_worker_context != NULL) {
  265. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  266. plugin_state->bt_type_code_worker_context = NULL;
  267. }
  268. #endif
  269. if(!scene_state->current_token_index.is_null) {
  270. TokenMenuSceneContext generate_scene_context = {
  271. .current_token_index = scene_state->current_token_index.value};
  272. totp_scene_director_activate_scene(
  273. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  274. } else {
  275. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  276. }
  277. }
  278. break;
  279. case InputKeyBack: {
  280. if(!scene_state->current_token_index.is_null) {
  281. TokenMenuSceneContext generate_scene_context = {
  282. .current_token_index = scene_state->current_token_index.value};
  283. totp_scene_director_activate_scene(
  284. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  285. } else {
  286. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  287. }
  288. break;
  289. }
  290. default:
  291. break;
  292. }
  293. return true;
  294. }
  295. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  296. if(plugin_state->current_scene_state == NULL) return;
  297. free(plugin_state->current_scene_state);
  298. plugin_state->current_scene_state = NULL;
  299. }