mass_storage_app.c 4.7 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. app->scene_manager = scene_manager_alloc(&mass_storage_scene_handlers, app);
  43. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  44. view_dispatcher_set_tick_event_callback(
  45. app->view_dispatcher, mass_storage_app_tick_event_callback, 500);
  46. view_dispatcher_set_custom_event_callback(
  47. app->view_dispatcher, mass_storage_app_custom_event_callback);
  48. view_dispatcher_set_navigation_event_callback(
  49. app->view_dispatcher, mass_storage_app_back_event_callback);
  50. app->mass_storage_view = mass_storage_alloc();
  51. view_dispatcher_add_view(
  52. app->view_dispatcher,
  53. MassStorageAppViewWork,
  54. mass_storage_get_view(app->mass_storage_view));
  55. app->text_input = text_input_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher, MassStorageAppViewTextInput, text_input_get_view(app->text_input));
  58. app->loading = loading_alloc();
  59. view_dispatcher_add_view(
  60. app->view_dispatcher, MassStorageAppViewLoading, loading_get_view(app->loading));
  61. app->variable_item_list = variable_item_list_alloc();
  62. view_dispatcher_add_view(
  63. app->view_dispatcher,
  64. MassStorageAppViewStart,
  65. variable_item_list_get_view(app->variable_item_list));
  66. app->widget = widget_alloc();
  67. view_dispatcher_add_view(
  68. app->view_dispatcher, MassStorageAppViewWidget, widget_get_view(app->widget));
  69. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  70. if(storage_file_exists(app->fs_api, furi_string_get_cstr(app->file_path))) {
  71. if(!furi_hal_usb_is_locked()) {
  72. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  73. } else {
  74. scene_manager_next_scene(app->scene_manager, MassStorageSceneUsbLocked);
  75. }
  76. } else {
  77. scene_manager_next_scene(app->scene_manager, MassStorageSceneStart);
  78. }
  79. return app;
  80. }
  81. void mass_storage_app_free(MassStorageApp* app) {
  82. furi_assert(app);
  83. // Views
  84. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWork);
  85. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewTextInput);
  86. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewStart);
  87. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewLoading);
  88. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWidget);
  89. mass_storage_free(app->mass_storage_view);
  90. text_input_free(app->text_input);
  91. variable_item_list_free(app->variable_item_list);
  92. loading_free(app->loading);
  93. widget_free(app->widget);
  94. // View dispatcher
  95. view_dispatcher_free(app->view_dispatcher);
  96. scene_manager_free(app->scene_manager);
  97. furi_string_free(app->file_path);
  98. // Close records
  99. furi_record_close(RECORD_GUI);
  100. furi_record_close(RECORD_STORAGE);
  101. furi_record_close(RECORD_DIALOGS);
  102. free(app);
  103. }
  104. int32_t mass_storage_app(void* p) {
  105. MassStorageApp* mass_storage_app = mass_storage_app_alloc((char*)p);
  106. view_dispatcher_run(mass_storage_app->view_dispatcher);
  107. mass_storage_app_free(mass_storage_app);
  108. return 0;
  109. }