animation_manager.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "animation_manager.h"
  2. #include "furi_hal_delay.h"
  3. #include "portmacro.h"
  4. #include "views/bubble_animation_view.h"
  5. #include "animation_storage.h"
  6. #include <cmsis_os2.h>
  7. #include <dolphin/dolphin.h>
  8. #include <furi/check.h>
  9. #include <furi/pubsub.h>
  10. #include <furi/record.h>
  11. #include <m-string.h>
  12. #include <power/power_service/power.h>
  13. #include <stdint.h>
  14. #include <storage/storage.h>
  15. #include <dolphin/dolphin_i.h>
  16. #include <storage/filesystem_api_defines.h>
  17. #define TAG "AnimationManager"
  18. typedef enum {
  19. AnimationManagerStateIdle,
  20. AnimationManagerStateBlocked,
  21. AnimationManagerStateFreezedIdle,
  22. AnimationManagerStateFreezedBlocked,
  23. } AnimationManagerState;
  24. struct AnimationManager {
  25. bool sd_show_url;
  26. bool sd_shown_no_db;
  27. bool sd_shown_sd_ok;
  28. AnimationManagerState state;
  29. FuriPubSubSubscription* pubsub_subscription_storage;
  30. FuriPubSubSubscription* pubsub_subscription_dolphin;
  31. BubbleAnimationView* animation_view;
  32. osTimerId_t idle_animation_timer;
  33. StorageAnimation* current_animation;
  34. AnimationManagerInteractCallback interact_callback;
  35. AnimationManagerSetNewIdleAnimationCallback new_idle_callback;
  36. AnimationManagerSetNewIdleAnimationCallback check_blocking_callback;
  37. void* context;
  38. string_t freezed_animation_name;
  39. int32_t freezed_animation_time_left;
  40. };
  41. static StorageAnimation*
  42. animation_manager_select_idle_animation(AnimationManager* animation_manager);
  43. static void animation_manager_replace_current_animation(
  44. AnimationManager* animation_manager,
  45. StorageAnimation* storage_animation);
  46. static void animation_manager_start_new_idle(AnimationManager* animation_manager);
  47. static bool animation_manager_check_blocking(AnimationManager* animation_manager);
  48. void animation_manager_set_context(AnimationManager* animation_manager, void* context) {
  49. furi_assert(animation_manager);
  50. animation_manager->context = context;
  51. }
  52. void animation_manager_set_new_idle_callback(
  53. AnimationManager* animation_manager,
  54. AnimationManagerSetNewIdleAnimationCallback callback) {
  55. furi_assert(animation_manager);
  56. animation_manager->new_idle_callback = callback;
  57. }
  58. void animation_manager_set_check_callback(
  59. AnimationManager* animation_manager,
  60. AnimationManagerCheckBlockingCallback callback) {
  61. furi_assert(animation_manager);
  62. animation_manager->check_blocking_callback = callback;
  63. }
  64. void animation_manager_set_interact_callback(
  65. AnimationManager* animation_manager,
  66. AnimationManagerInteractCallback callback) {
  67. furi_assert(animation_manager);
  68. animation_manager->interact_callback = callback;
  69. }
  70. static void animation_manager_check_blocking_callback(const void* message, void* context) {
  71. furi_assert(context);
  72. AnimationManager* animation_manager = context;
  73. if(animation_manager->check_blocking_callback) {
  74. animation_manager->check_blocking_callback(animation_manager->context);
  75. }
  76. }
  77. static void animation_manager_timer_callback(void* context) {
  78. furi_assert(context);
  79. AnimationManager* animation_manager = context;
  80. if(animation_manager->new_idle_callback) {
  81. animation_manager->new_idle_callback(animation_manager->context);
  82. }
  83. }
  84. static void animation_manager_interact_callback(void* context) {
  85. furi_assert(context);
  86. AnimationManager* animation_manager = context;
  87. if(animation_manager->interact_callback) {
  88. animation_manager->interact_callback(animation_manager->context);
  89. }
  90. }
  91. /* reaction to animation_manager->interact_callback() */
  92. void animation_manager_check_blocking_process(AnimationManager* animation_manager) {
  93. furi_assert(animation_manager);
  94. if(animation_manager->state == AnimationManagerStateIdle) {
  95. animation_manager_check_blocking(animation_manager);
  96. }
  97. }
  98. /* reaction to animation_manager->new_idle_callback() */
  99. void animation_manager_new_idle_process(AnimationManager* animation_manager) {
  100. furi_assert(animation_manager);
  101. if(animation_manager->state == AnimationManagerStateIdle) {
  102. animation_manager_start_new_idle(animation_manager);
  103. }
  104. }
  105. /* reaction to animation_manager->check_blocking_callback() */
  106. void animation_manager_interact_process(AnimationManager* animation_manager) {
  107. furi_assert(animation_manager);
  108. if(animation_manager->state == AnimationManagerStateBlocked) {
  109. /* check if new blocking animation has to be displayed */
  110. bool blocked = animation_manager_check_blocking(animation_manager);
  111. if(!blocked) {
  112. animation_manager_start_new_idle(animation_manager);
  113. }
  114. }
  115. }
  116. static void animation_manager_start_new_idle(AnimationManager* animation_manager) {
  117. furi_assert(animation_manager);
  118. StorageAnimation* new_animation = animation_manager_select_idle_animation(animation_manager);
  119. animation_manager_replace_current_animation(animation_manager, new_animation);
  120. const BubbleAnimation* bubble_animation =
  121. animation_storage_get_bubble_animation(animation_manager->current_animation);
  122. animation_manager->state = AnimationManagerStateIdle;
  123. osTimerStart(animation_manager->idle_animation_timer, bubble_animation->duration * 1000);
  124. }
  125. static bool animation_manager_check_blocking(AnimationManager* animation_manager) {
  126. furi_assert(animation_manager);
  127. StorageAnimation* blocking_animation = NULL;
  128. Storage* storage = furi_record_open("storage");
  129. FS_Error sd_status = storage_sd_status(storage);
  130. if(sd_status == FSE_INTERNAL) {
  131. blocking_animation = animation_storage_find_animation(BAD_SD_ANIMATION_NAME);
  132. } else if(sd_status == FSE_NOT_READY) {
  133. animation_manager->sd_shown_sd_ok = false;
  134. animation_manager->sd_shown_no_db = false;
  135. } else if(sd_status == FSE_OK) {
  136. if(!animation_manager->sd_shown_sd_ok) {
  137. blocking_animation = animation_storage_find_animation(SD_OK_ANIMATION_NAME);
  138. animation_manager->sd_shown_sd_ok = true;
  139. } else if(!animation_manager->sd_shown_no_db) {
  140. bool db_exists = storage_common_stat(storage, "/ext/Manifest", NULL) == FSE_OK;
  141. if(!db_exists) {
  142. blocking_animation = animation_storage_find_animation(NO_DB_ANIMATION_NAME);
  143. animation_manager->sd_shown_no_db = true;
  144. animation_manager->sd_show_url = true;
  145. }
  146. } else if(animation_manager->sd_show_url) {
  147. blocking_animation = animation_storage_find_animation(URL_ANIMATION_NAME);
  148. animation_manager->sd_show_url = false;
  149. }
  150. }
  151. Dolphin* dolphin = furi_record_open("dolphin");
  152. DolphinStats stats = dolphin_stats(dolphin);
  153. furi_record_close("dolphin");
  154. if(!blocking_animation && stats.level_up_is_pending) {
  155. blocking_animation = animation_storage_find_animation(LEVELUP_ANIMATION_NAME);
  156. }
  157. if(blocking_animation) {
  158. osTimerStop(animation_manager->idle_animation_timer);
  159. animation_manager_replace_current_animation(animation_manager, blocking_animation);
  160. /* no starting timer because its blocking animation */
  161. animation_manager->state = AnimationManagerStateBlocked;
  162. }
  163. furi_record_close("storage");
  164. return !!blocking_animation;
  165. }
  166. static void animation_manager_replace_current_animation(
  167. AnimationManager* animation_manager,
  168. StorageAnimation* storage_animation) {
  169. furi_assert(storage_animation);
  170. StorageAnimation* previous_animation = animation_manager->current_animation;
  171. const BubbleAnimation* animation = animation_storage_get_bubble_animation(storage_animation);
  172. bubble_animation_view_set_animation(animation_manager->animation_view, animation);
  173. const char* new_name = string_get_cstr(animation_storage_get_meta(storage_animation)->name);
  174. FURI_LOG_I(TAG, "Select \'%s\' animation", new_name);
  175. animation_manager->current_animation = storage_animation;
  176. if(previous_animation) {
  177. animation_storage_free_storage_animation(&previous_animation);
  178. }
  179. }
  180. AnimationManager* animation_manager_alloc(void) {
  181. animation_storage_initialize_internal_animations();
  182. AnimationManager* animation_manager = furi_alloc(sizeof(AnimationManager));
  183. animation_manager->animation_view = bubble_animation_view_alloc();
  184. string_init(animation_manager->freezed_animation_name);
  185. animation_manager->idle_animation_timer =
  186. osTimerNew(animation_manager_timer_callback, osTimerOnce, animation_manager, NULL);
  187. bubble_animation_view_set_interact_callback(
  188. animation_manager->animation_view, animation_manager_interact_callback, animation_manager);
  189. Storage* storage = furi_record_open("storage");
  190. animation_manager->pubsub_subscription_storage = furi_pubsub_subscribe(
  191. storage_get_pubsub(storage), animation_manager_check_blocking_callback, animation_manager);
  192. furi_record_close("storage");
  193. Dolphin* dolphin = furi_record_open("dolphin");
  194. animation_manager->pubsub_subscription_dolphin = furi_pubsub_subscribe(
  195. dolphin_get_pubsub(dolphin), animation_manager_check_blocking_callback, animation_manager);
  196. furi_record_close("dolphin");
  197. animation_manager->sd_shown_sd_ok = true;
  198. if(!animation_manager_check_blocking(animation_manager)) {
  199. animation_manager_start_new_idle(animation_manager);
  200. }
  201. return animation_manager;
  202. }
  203. void animation_manager_free(AnimationManager* animation_manager) {
  204. furi_assert(animation_manager);
  205. Dolphin* dolphin = furi_record_open("dolphin");
  206. furi_pubsub_unsubscribe(
  207. dolphin_get_pubsub(dolphin), animation_manager->pubsub_subscription_dolphin);
  208. furi_record_close("dolphin");
  209. Storage* storage = furi_record_open("storage");
  210. furi_pubsub_unsubscribe(
  211. storage_get_pubsub(storage), animation_manager->pubsub_subscription_storage);
  212. furi_record_close("storage");
  213. string_clear(animation_manager->freezed_animation_name);
  214. bubble_animation_view_free(animation_manager->animation_view);
  215. osTimerDelete(animation_manager->idle_animation_timer);
  216. }
  217. View* animation_manager_get_animation_view(AnimationManager* animation_manager) {
  218. furi_assert(animation_manager);
  219. return bubble_animation_get_view(animation_manager->animation_view);
  220. }
  221. static StorageAnimation*
  222. animation_manager_select_idle_animation(AnimationManager* animation_manager) {
  223. StorageAnimationList_t animation_list;
  224. StorageAnimationList_init(animation_list);
  225. animation_storage_fill_animation_list(&animation_list);
  226. Power* power = furi_record_open("power");
  227. bool battery_is_well = power_is_battery_healthy(power);
  228. furi_record_close("power");
  229. Storage* storage = furi_record_open("storage");
  230. FS_Error sd_status = storage_sd_status(storage);
  231. furi_record_close("storage");
  232. Dolphin* dolphin = furi_record_open("dolphin");
  233. DolphinStats stats = dolphin_stats(dolphin);
  234. uint32_t whole_weight = 0;
  235. StorageAnimationList_it_t it;
  236. for(StorageAnimationList_it(it, animation_list); !StorageAnimationList_end_p(it);) {
  237. StorageAnimation* storage_animation = *StorageAnimationList_ref(it);
  238. const StorageAnimationMeta* meta = animation_storage_get_meta(storage_animation);
  239. bool skip_animation = false;
  240. if(battery_is_well && !string_cmp_str(meta->name, BAD_BATTERY_ANIMATION_NAME)) {
  241. skip_animation = true;
  242. } else if((sd_status != FSE_NOT_READY) && !string_cmp_str(meta->name, NO_SD_ANIMATION_NAME)) {
  243. skip_animation = true;
  244. } else if((stats.butthurt < meta->min_butthurt) || (stats.butthurt > meta->max_butthurt)) {
  245. skip_animation = true;
  246. } else if((stats.level < meta->min_level) || (stats.level > meta->max_level)) {
  247. skip_animation = true;
  248. }
  249. if(skip_animation) {
  250. animation_storage_free_storage_animation(&storage_animation);
  251. /* remove and increase iterator */
  252. StorageAnimationList_remove(animation_list, it);
  253. } else {
  254. whole_weight += meta->weight;
  255. StorageAnimationList_next(it);
  256. }
  257. }
  258. uint32_t lucky_number = random() % whole_weight;
  259. uint32_t weight = 0;
  260. StorageAnimation* selected = NULL;
  261. for
  262. M_EACH(item, animation_list, StorageAnimationList_t) {
  263. if(lucky_number < weight) {
  264. break;
  265. }
  266. weight += animation_storage_get_meta(*item)->weight;
  267. selected = *item;
  268. }
  269. for
  270. M_EACH(item, animation_list, StorageAnimationList_t) {
  271. if(*item != selected) {
  272. animation_storage_free_storage_animation(item);
  273. }
  274. }
  275. StorageAnimationList_clear(animation_list);
  276. furi_record_close("dolphin");
  277. /* cache animation, if failed - choose reliable animation */
  278. if(!animation_storage_get_bubble_animation(selected)) {
  279. const char* name = string_get_cstr(animation_storage_get_meta(selected)->name);
  280. FURI_LOG_E(TAG, "Can't upload animation described in manifest: \'%s\'", name);
  281. animation_storage_free_storage_animation(&selected);
  282. selected = animation_storage_find_animation(HARDCODED_ANIMATION_NAME);
  283. }
  284. furi_assert(selected);
  285. return selected;
  286. }
  287. void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager) {
  288. furi_assert(animation_manager);
  289. furi_assert(animation_manager->current_animation);
  290. furi_assert(!string_size(animation_manager->freezed_animation_name));
  291. furi_assert(
  292. (animation_manager->state == AnimationManagerStateIdle) ||
  293. (animation_manager->state == AnimationManagerStateBlocked));
  294. if(animation_manager->state == AnimationManagerStateBlocked) {
  295. animation_manager->state = AnimationManagerStateFreezedBlocked;
  296. } else if(animation_manager->state == AnimationManagerStateIdle) {
  297. animation_manager->state = AnimationManagerStateFreezedIdle;
  298. animation_manager->freezed_animation_time_left =
  299. xTimerGetExpiryTime(animation_manager->idle_animation_timer) - xTaskGetTickCount();
  300. if(animation_manager->freezed_animation_time_left < 0) {
  301. animation_manager->freezed_animation_time_left = 0;
  302. }
  303. osTimerStop(animation_manager->idle_animation_timer);
  304. } else {
  305. furi_assert(0);
  306. }
  307. StorageAnimationMeta* meta = animation_storage_get_meta(animation_manager->current_animation);
  308. /* copy str, not move, because it can be internal animation */
  309. string_set(animation_manager->freezed_animation_name, meta->name);
  310. bubble_animation_freeze(animation_manager->animation_view);
  311. animation_storage_free_storage_animation(&animation_manager->current_animation);
  312. }
  313. void animation_manager_load_and_continue_animation(AnimationManager* animation_manager) {
  314. furi_assert(animation_manager);
  315. furi_assert(!animation_manager->current_animation);
  316. furi_assert(string_size(animation_manager->freezed_animation_name));
  317. furi_assert(
  318. (animation_manager->state == AnimationManagerStateFreezedIdle) ||
  319. (animation_manager->state == AnimationManagerStateFreezedBlocked));
  320. if(animation_manager->state == AnimationManagerStateFreezedBlocked) {
  321. StorageAnimation* restore_animation = animation_storage_find_animation(
  322. string_get_cstr(animation_manager->freezed_animation_name));
  323. /* all blocked animations must be in flipper -> we can
  324. * always find blocking animation */
  325. furi_assert(restore_animation);
  326. animation_manager_replace_current_animation(animation_manager, restore_animation);
  327. animation_manager->state = AnimationManagerStateBlocked;
  328. } else if(animation_manager->state == AnimationManagerStateFreezedIdle) {
  329. /* check if we missed some system notifications, and set current_animation */
  330. bool blocked = animation_manager_check_blocking(animation_manager);
  331. if(!blocked) {
  332. /* if no blocking - try restore last one idle */
  333. StorageAnimation* restore_animation = animation_storage_find_animation(
  334. string_get_cstr(animation_manager->freezed_animation_name));
  335. if(restore_animation) {
  336. animation_manager_replace_current_animation(animation_manager, restore_animation);
  337. animation_manager->state = AnimationManagerStateIdle;
  338. if(animation_manager->freezed_animation_time_left) {
  339. osTimerStart(
  340. animation_manager->idle_animation_timer,
  341. animation_manager->freezed_animation_time_left);
  342. } else {
  343. const BubbleAnimation* animation = animation_storage_get_bubble_animation(
  344. animation_manager->current_animation);
  345. osTimerStart(
  346. animation_manager->idle_animation_timer, animation->duration * 1000);
  347. }
  348. } else {
  349. FURI_LOG_E(
  350. TAG,
  351. "Failed to restore \'%s\'",
  352. string_get_cstr(animation_manager->freezed_animation_name));
  353. }
  354. }
  355. } else {
  356. /* Unknown state is an error. But not in release version.*/
  357. furi_assert(0);
  358. }
  359. /* if can't restore previous animation - select new */
  360. if(!animation_manager->current_animation) {
  361. animation_manager_start_new_idle(animation_manager);
  362. }
  363. FURI_LOG_D(
  364. TAG,
  365. "Load & Continue with \'%s\'",
  366. string_get_cstr(animation_storage_get_meta(animation_manager->current_animation)->name));
  367. bubble_animation_unfreeze(animation_manager->animation_view);
  368. string_reset(animation_manager->freezed_animation_name);
  369. furi_assert(animation_manager->current_animation);
  370. }