totp_app_settings.c 12 KB

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