mass_storage_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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->create_image_size = (uint8_t)-1;
  42. SDInfo sd_info;
  43. if(storage_sd_info(app->fs_api, &sd_info) == FSE_OK) {
  44. switch(sd_info.fs_type) {
  45. case FST_FAT12:
  46. app->create_image_max = 16LL * 1024 * 1024;
  47. break;
  48. case FST_FAT16:
  49. app->create_image_max = 2LL * 1024 * 1024 * 1024;
  50. break;
  51. case FST_FAT32:
  52. app->create_image_max = 4LL * 1024 * 1024 * 1024;
  53. break;
  54. default:
  55. app->create_image_max = 0;
  56. break;
  57. }
  58. }
  59. app->view_dispatcher = view_dispatcher_alloc();
  60. view_dispatcher_enable_queue(app->view_dispatcher);
  61. app->scene_manager = scene_manager_alloc(&mass_storage_scene_handlers, app);
  62. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  63. view_dispatcher_set_tick_event_callback(
  64. app->view_dispatcher, mass_storage_app_tick_event_callback, 500);
  65. view_dispatcher_set_custom_event_callback(
  66. app->view_dispatcher, mass_storage_app_custom_event_callback);
  67. view_dispatcher_set_navigation_event_callback(
  68. app->view_dispatcher, mass_storage_app_back_event_callback);
  69. app->mass_storage_view = mass_storage_alloc();
  70. view_dispatcher_add_view(
  71. app->view_dispatcher,
  72. MassStorageAppViewWork,
  73. mass_storage_get_view(app->mass_storage_view));
  74. app->text_input = text_input_alloc();
  75. view_dispatcher_add_view(
  76. app->view_dispatcher, MassStorageAppViewTextInput, text_input_get_view(app->text_input));
  77. app->loading = loading_alloc();
  78. view_dispatcher_add_view(
  79. app->view_dispatcher, MassStorageAppViewLoading, loading_get_view(app->loading));
  80. app->variable_item_list = variable_item_list_alloc();
  81. view_dispatcher_add_view(
  82. app->view_dispatcher,
  83. MassStorageAppViewStart,
  84. variable_item_list_get_view(app->variable_item_list));
  85. app->widget = widget_alloc();
  86. view_dispatcher_add_view(
  87. app->view_dispatcher, MassStorageAppViewWidget, widget_get_view(app->widget));
  88. app->popup = popup_alloc();
  89. view_dispatcher_add_view(
  90. app->view_dispatcher, MassStorageAppViewPopup, popup_get_view(app->popup));
  91. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  92. if(storage_file_exists(app->fs_api, furi_string_get_cstr(app->file_path))) {
  93. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  94. } else {
  95. scene_manager_next_scene(app->scene_manager, MassStorageSceneStart);
  96. }
  97. return app;
  98. }
  99. void mass_storage_app_free(MassStorageApp* app) {
  100. furi_assert(app);
  101. // Views
  102. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWork);
  103. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewTextInput);
  104. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewStart);
  105. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewLoading);
  106. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWidget);
  107. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewPopup);
  108. mass_storage_free(app->mass_storage_view);
  109. text_input_free(app->text_input);
  110. variable_item_list_free(app->variable_item_list);
  111. loading_free(app->loading);
  112. widget_free(app->widget);
  113. popup_free(app->popup);
  114. // View dispatcher
  115. view_dispatcher_free(app->view_dispatcher);
  116. scene_manager_free(app->scene_manager);
  117. furi_string_free(app->file_path);
  118. // Close records
  119. furi_record_close(RECORD_GUI);
  120. furi_record_close(RECORD_STORAGE);
  121. furi_record_close(RECORD_DIALOGS);
  122. free(app);
  123. }
  124. int32_t mass_storage_app(void* p) {
  125. MassStorageApp* mass_storage_app = mass_storage_app_alloc((char*)p);
  126. view_dispatcher_run(mass_storage_app->view_dispatcher);
  127. mass_storage_app_free(mass_storage_app);
  128. return 0;
  129. }