1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "animator/animator_manager.h"
17 
18 #include "common/task_manager.h"
19 #include "hal_tick.h"
20 
21 namespace OHOS {
GetInstance()22 AnimatorManager* AnimatorManager::GetInstance()
23 {
24     static AnimatorManager animatorManager;
25     return &animatorManager;
26 }
27 
Init()28 void AnimatorManager::Init()
29 {
30     Task::Init();
31 }
32 
Add(Animator * animator)33 void AnimatorManager::Add(Animator* animator)
34 {
35     if (animator == nullptr) {
36         return;
37     }
38 
39     ListNode<Animator*>* pos = list_.Begin();
40     while (pos != list_.End()) {
41         if (pos->data_ == animator) {
42             return;
43         }
44         pos = pos->next_;
45     }
46 
47     list_.PushBack(animator);
48 }
49 
Remove(const Animator * animator)50 void AnimatorManager::Remove(const Animator* animator)
51 {
52     if (animator == nullptr) {
53         return;
54     }
55     ListNode<Animator*>* pos = list_.Begin();
56     while (pos != list_.End()) {
57         if (pos->data_ == animator) {
58             pos->data_ = nullptr;
59             return;
60         }
61         pos = pos->next_;
62     }
63 }
64 
AnimatorTask()65 void AnimatorManager::AnimatorTask()
66 {
67     ListNode<Animator*>* pos = list_.Begin();
68     Animator* animator = nullptr;
69 
70     while (pos != list_.End()) {
71         animator = pos->data_;
72         if (animator == nullptr) {
73             // clean up animator list
74             pos = list_.Remove(pos);
75             continue;
76         }
77         if (animator->GetState() == Animator::START) {
78             animator->Run();
79         }
80         pos = pos->next_;
81     }
82 }
83 } // namespace OHOS
84