mass_storage_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. app->scene_manager = scene_manager_alloc(&mass_storage_scene_handlers, app);
  61. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  62. view_dispatcher_set_tick_event_callback(
  63. app->view_dispatcher, mass_storage_app_tick_event_callback, 500);
  64. view_dispatcher_set_custom_event_callback(
  65. app->view_dispatcher, mass_storage_app_custom_event_callback);
  66. view_dispatcher_set_navigation_event_callback(
  67. app->view_dispatcher, mass_storage_app_back_event_callback);
  68. app->mass_storage_view = mass_storage_alloc();
  69. view_dispatcher_add_view(
  70. app->view_dispatcher,
  71. MassStorageAppViewWork,
  72. mass_storage_get_view(app->mass_storage_view));
  73. app->text_input = text_input_alloc();
  74. view_dispatcher_add_view(
  75. app->view_dispatcher, MassStorageAppViewTextInput, text_input_get_view(app->text_input));
  76. app->loading = loading_alloc();
  77. view_dispatcher_add_view(
  78. app->view_dispatcher, MassStorageAppViewLoading, loading_get_view(app->loading));
  79. app->variable_item_list = variable_item_list_alloc();
  80. view_dispatcher_add_view(
  81. app->view_dispatcher,
  82. MassStorageAppViewStart,
  83. variable_item_list_get_view(app->variable_item_list));
  84. app->widget = widget_alloc();
  85. view_dispatcher_add_view(
  86. app->view_dispatcher, MassStorageAppViewWidget, widget_get_view(app->widget));
  87. app->popup = popup_alloc();
  88. view_dispatcher_add_view(
  89. app->view_dispatcher, MassStorageAppViewPopup, popup_get_view(app->popup));
  90. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  91. if(storage_file_exists(app->fs_api, furi_string_get_cstr(app->file_path))) {
  92. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  93. } else {
  94. scene_manager_next_scene(app->scene_manager, MassStorageSceneStart);
  95. }
  96. return app;
  97. }
  98. void mass_storage_app_free(MassStorageApp* app) {
  99. furi_assert(app);
  100. // Views
  101. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWork);
  102. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewTextInput);
  103. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewStart);
  104. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewLoading);
  105. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewWidget);
  106. view_dispatcher_remove_view(app->view_dispatcher, MassStorageAppViewPopup);
  107. mass_storage_free(app->mass_storage_view);
  108. text_input_free(app->text_input);
  109. variable_item_list_free(app->variable_item_list);
  110. loading_free(app->loading);
  111. widget_free(app->widget);
  112. popup_free(app->popup);
  113. // View dispatcher
  114. view_dispatcher_free(app->view_dispatcher);
  115. scene_manager_free(app->scene_manager);
  116. furi_string_free(app->file_path);
  117. // Close records
  118. furi_record_close(RECORD_GUI);
  119. furi_record_close(RECORD_STORAGE);
  120. furi_record_close(RECORD_DIALOGS);
  121. free(app);
  122. }
  123. int32_t mass_storage_app(void* p) {
  124. MassStorageApp* mass_storage_app = mass_storage_app_alloc((char*)p);
  125. view_dispatcher_run(mass_storage_app->view_dispatcher);
  126. mass_storage_app_free(mass_storage_app);
  127. return 0;
  128. }