animation_manager.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. PowerInfo info;
  228. power_get_info(power, &info);
  229. bool battery_is_well = power_is_battery_well(&info);
  230. furi_record_close("power");
  231. Storage* storage = furi_record_open("storage");
  232. FS_Error sd_status = storage_sd_status(storage);
  233. furi_record_close("storage");
  234. Dolphin* dolphin = furi_record_open("dolphin");
  235. DolphinStats stats = dolphin_stats(dolphin);
  236. uint32_t whole_weight = 0;
  237. StorageAnimationList_it_t it;
  238. for(StorageAnimationList_it(it, animation_list); !StorageAnimationList_end_p(it);) {
  239. StorageAnimation* storage_animation = *StorageAnimationList_ref(it);
  240. const StorageAnimationMeta* meta = animation_storage_get_meta(storage_animation);
  241. bool skip_animation = false;
  242. if(battery_is_well && !string_cmp_str(meta->name, BAD_BATTERY_ANIMATION_NAME)) {
  243. skip_animation = true;
  244. } else if((sd_status != FSE_NOT_READY) && !string_cmp_str(meta->name, NO_SD_ANIMATION_NAME)) {
  245. skip_animation = true;
  246. } else if((stats.butthurt < meta->min_butthurt) || (stats.butthurt > meta->max_butthurt)) {
  247. skip_animation = true;
  248. } else if((stats.level < meta->min_level) || (stats.level > meta->max_level)) {
  249. skip_animation = true;
  250. }
  251. if(skip_animation) {
  252. animation_storage_free_storage_animation(&storage_animation);
  253. /* remove and increase iterator */
  254. StorageAnimationList_remove(animation_list, it);
  255. } else {
  256. whole_weight += meta->weight;
  257. StorageAnimationList_next(it);
  258. }
  259. }
  260. uint32_t lucky_number = random() % whole_weight;
  261. uint32_t weight = 0;
  262. StorageAnimation* selected = NULL;
  263. for
  264. M_EACH(item, animation_list, StorageAnimationList_t) {
  265. if(lucky_number < weight) {
  266. break;
  267. }
  268. weight += animation_storage_get_meta(*item)->weight;
  269. selected = *item;
  270. }
  271. for
  272. M_EACH(item, animation_list, StorageAnimationList_t) {
  273. if(*item != selected) {
  274. animation_storage_free_storage_animation(item);
  275. }
  276. }
  277. StorageAnimationList_clear(animation_list);
  278. furi_record_close("dolphin");
  279. /* cache animation, if failed - choose reliable animation */
  280. if(!animation_storage_get_bubble_animation(selected)) {
  281. const char* name = string_get_cstr(animation_storage_get_meta(selected)->name);
  282. FURI_LOG_E(TAG, "Can't upload animation described in manifest: \'%s\'", name);
  283. animation_storage_free_storage_animation(&selected);
  284. selected = animation_storage_find_animation(HARDCODED_ANIMATION_NAME);
  285. }
  286. furi_assert(selected);
  287. return selected;
  288. }
  289. void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager) {
  290. furi_assert(animation_manager);
  291. furi_assert(animation_manager->current_animation);
  292. furi_assert(!string_size(animation_manager->freezed_animation_name));
  293. furi_assert(
  294. (animation_manager->state == AnimationManagerStateIdle) ||
  295. (animation_manager->state == AnimationManagerStateBlocked));
  296. if(animation_manager->state == AnimationManagerStateBlocked) {
  297. animation_manager->state = AnimationManagerStateFreezedBlocked;
  298. } else if(animation_manager->state == AnimationManagerStateIdle) {
  299. animation_manager->state = AnimationManagerStateFreezedIdle;
  300. animation_manager->freezed_animation_time_left =
  301. xTimerGetExpiryTime(animation_manager->idle_animation_timer) - xTaskGetTickCount();
  302. if(animation_manager->freezed_animation_time_left < 0) {
  303. animation_manager->freezed_animation_time_left = 0;
  304. }
  305. osTimerStop(animation_manager->idle_animation_timer);
  306. } else {
  307. furi_assert(0);
  308. }
  309. StorageAnimationMeta* meta = animation_storage_get_meta(animation_manager->current_animation);
  310. /* copy str, not move, because it can be internal animation */
  311. string_set(animation_manager->freezed_animation_name, meta->name);
  312. bubble_animation_freeze(animation_manager->animation_view);
  313. animation_storage_free_storage_animation(&animation_manager->current_animation);
  314. }
  315. void animation_manager_load_and_continue_animation(AnimationManager* animation_manager) {
  316. furi_assert(animation_manager);
  317. furi_assert(!animation_manager->current_animation);
  318. furi_assert(string_size(animation_manager->freezed_animation_name));
  319. furi_assert(
  320. (animation_manager->state == AnimationManagerStateFreezedIdle) ||
  321. (animation_manager->state == AnimationManagerStateFreezedBlocked));
  322. if(animation_manager->state == AnimationManagerStateFreezedBlocked) {
  323. StorageAnimation* restore_animation = animation_storage_find_animation(
  324. string_get_cstr(animation_manager->freezed_animation_name));
  325. /* all blocked animations must be in flipper -> we can
  326. * always find blocking animation */
  327. furi_assert(restore_animation);
  328. animation_manager_replace_current_animation(animation_manager, restore_animation);
  329. animation_manager->state = AnimationManagerStateBlocked;
  330. } else if(animation_manager->state == AnimationManagerStateFreezedIdle) {
  331. /* check if we missed some system notifications, and set current_animation */
  332. bool blocked = animation_manager_check_blocking(animation_manager);
  333. if(!blocked) {
  334. /* if no blocking - try restore last one idle */
  335. StorageAnimation* restore_animation = animation_storage_find_animation(
  336. string_get_cstr(animation_manager->freezed_animation_name));
  337. if(restore_animation) {
  338. animation_manager_replace_current_animation(animation_manager, restore_animation);
  339. animation_manager->state = AnimationManagerStateIdle;
  340. if(animation_manager->freezed_animation_time_left) {
  341. osTimerStart(
  342. animation_manager->idle_animation_timer,
  343. animation_manager->freezed_animation_time_left);
  344. } else {
  345. const BubbleAnimation* animation = animation_storage_get_bubble_animation(
  346. animation_manager->current_animation);
  347. osTimerStart(
  348. animation_manager->idle_animation_timer, animation->duration * 1000);
  349. }
  350. } else {
  351. FURI_LOG_E(
  352. TAG,
  353. "Failed to restore \'%s\'",
  354. string_get_cstr(animation_manager->freezed_animation_name));
  355. }
  356. }
  357. } else {
  358. /* Unknown state is an error. But not in release version.*/
  359. furi_assert(0);
  360. }
  361. /* if can't restore previous animation - select new */
  362. if(!animation_manager->current_animation) {
  363. animation_manager_start_new_idle(animation_manager);
  364. }
  365. FURI_LOG_D(
  366. TAG,
  367. "Load & Continue with \'%s\'",
  368. string_get_cstr(animation_storage_get_meta(animation_manager->current_animation)->name));
  369. bubble_animation_unfreeze(animation_manager->animation_view);
  370. string_reset(animation_manager->freezed_animation_name);
  371. furi_assert(animation_manager->current_animation);
  372. }