mass_storage_app.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "mass_storage_app_i.h"
  2. #include <furi.h>
  3. #include <storage/storage.h>
  4. #include <lib/toolbox/path.h>
  5. static bool mass_storage_app_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. MassStorageApp* app = context;
  8. return scene_manager_handle_custom_event(app->scene_manager, event);
  9. }
  10. static bool mass_storage_app_back_event_callback(void* context) {
  11. furi_assert(context);
  12. MassStorageApp* app = context;
  13. return scene_manager_handle_back_event(app->scene_manager);
  14. }
  15. static void mass_storage_app_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. MassStorageApp* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. void mass_storage_app_show_loading_popup(MassStorageApp* app, bool show) {
  21. if(show) {
  22. // Raise timer priority so that animations can play
  23. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  24. view_dispatcher_switch_to_view(app->view_dispatcher, MassStorageAppViewLoading);
  25. } else {
  26. // Restore default timer priority
  27. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  28. }
  29. }
  30. MassStorageApp* mass_storage_app_alloc(char* arg) {
  31. MassStorageApp* app = malloc(sizeof(MassStorageApp));
  32. app->file_path = furi_string_alloc();
  33. if(arg != NULL) {
  34. furi_string_set_str(app->file_path, arg);
  35. } else {
  36. furi_string_set_str(app->file_path, MASS_STORAGE_APP_PATH_FOLDER);
  37. }
  38. app->gui = furi_record_open(RECORD_GUI);
  39. app->fs_api = furi_record_open(RECORD_STORAGE);
  40. app->dialogs = furi_record_open(RECORD_DIALOGS);
  41. app->view_dispatcher = view_dispatcher_alloc();
  42. view_dispatcher_enable_queue(app->view_dispatcher);
  43. app->scene_manager = scene_manager_alloc(&mass_storage_scene_handlers, app);
  44. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  45. view_dispatcher_set_tick_event_callback(
  46. app->view_dispatcher, mass_storage_app_tick_event_callback, 500);
  47. view_dispatcher_set_custom_event_callback(
  48. app->view_dispatcher, mass_storage_app_custom_event_callback);
  49. view_dispatcher_set_navigation_event_callback(
  50. app->view_dispatcher, mass_storage_app_back_event_callback);
  51. app->mass_storage_view = mass_storage_alloc();
  52. view_dispatcher_add_view(
  53. app->view_dispatcher,
  54. MassStorageAppViewWork,
  55. mass_storage_get_view(app->mass_storage_view));
  56. app->text_input = text_input_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher, MassStorageAppViewTextInput, text_input_get_view(app->text_input));
  59. app->loading = loading_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher, MassStorageAppViewLoading, loading_get_view(app->loading));
  62. app->variable_item_list = variable_item_list_alloc();
  63. view_dispatcher_add_view(
  64. app->view_dispatcher,
  65. MassStorageAppViewStart,
  66. variable_item_list_get_view(app->variable_item_list));
  67. app->widget = widget_alloc();
  68. view_dispatcher_add_view(
  69. app->view_dispatcher, MassStorageAppViewWidget, widget_get_view(app->widget));
  70. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  71. if(storage_file_exists(app->fs_api, furi_string_get_cstr(app->file_path))) {
  72. if(!furi_hal_usb_is_locked()) {
  73. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  74. } else {
  75. scene_manager_next_scene(app->scene_manager, MassStorageSceneUsbLocked);
  76. }
  77. } else {
  78. scene_manager_next_scene(app->scene_manager, MassStorageSceneStart);
  79. }
  80. return app;
  81. }
  82. void mass_storage_app_free(MassStorageApp* app) {
  83. furi_assert(app);
  84. // Views
  85. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWork);
  86. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewTextInput);
  87. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewStart);
  88. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewLoading);
  89. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWidget);
  90. mass_storage_free(app->mass_storage_view);
  91. text_input_free(app->text_input);
  92. variable_item_list_free(app->variable_item_list);
  93. loading_free(app->loading);
  94. widget_free(app->widget);
  95. // View dispatcher
  96. view_dispatcher_free(app->view_dispatcher);
  97. scene_manager_free(app->scene_manager);
  98. furi_string_free(app->file_path);
  99. // Close records
  100. furi_record_close(RECORD_GUI);
  101. furi_record_close(RECORD_STORAGE);
  102. furi_record_close(RECORD_DIALOGS);
  103. free(app);
  104. }
  105. int32_t mass_storage_app(void* p) {
  106. MassStorageApp* mass_storage_app = mass_storage_app_alloc((char*)p);
  107. view_dispatcher_run(mass_storage_app->view_dispatcher);
  108. mass_storage_app_free(mass_storage_app);
  109. return 0;
  110. }