• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "img_view_adapter.h"
16 #include <sstream>
17 #include <unistd.h>
18 #include "core/render_manager.h"
19 #include "log/log.h"
20 #include "page/view_proxy.h"
21 #include "updater/updater_const.h"
22 #include "updater_ui_const.h"
23 #include "updater_ui_env.h"
24 #include "utils.h"
25 
26 namespace Updater {
27 class ImgViewAdapter::ImgAnimatorCallback final : public OHOS::AnimatorCallback {
28     DISALLOW_COPY_MOVE(ImgAnimatorCallback);
29 public:
ImgAnimatorCallback(ImgViewAdapter * view)30     explicit ImgAnimatorCallback(ImgViewAdapter *view)
31         : animator_(nullptr), stop_(true)
32     {
33         view_ = view;
34         if (view_ == nullptr) {
35             static ImgViewAdapter dummy;
36             view_ = &dummy;
37         }
38     }
39     ~ImgAnimatorCallback() = default;
Init()40     void Init()
41     {
42         animator_ = std::make_unique<OHOS::Animator>(this, view_, 0, true);
43     }
44 
Callback(OHOS::UIView * view)45     void Callback(OHOS::UIView *view) override
46     {
47         if (stop_) {
48             return;
49         }
50         view_->ShowNextImage();
51     }
Start()52     void Start()
53     {
54         view_->SetVisible(true);
55         stop_ = false;
56     }
Stop()57     void Stop()
58     {
59         view_->SetVisible(false);
60         stop_ = true;
61     }
GetAnimator() const62     OHOS::Animator *GetAnimator() const
63     {
64         return animator_.get();
65     }
66 protected:
67     ImgViewAdapter *view_;
68     std::unique_ptr<OHOS::Animator> animator_;
69     bool stop_;
70 };
71 
72 ImgViewAdapter::~ImgViewAdapter() = default;
73 
74 ImgViewAdapter::ImgViewAdapter() = default;
75 
ImgViewAdapter(const UxViewInfo & info)76 ImgViewAdapter::ImgViewAdapter(const UxViewInfo &info)
77 {
78     const UxViewCommonInfo &common = info.commonInfo;
79     const UxImageInfo &spec = std::get<UxImageInfo>(info.specificInfo);
80     dir_ = spec.resPath;
81     imgCnt_ = spec.imgCnt;
82     interval_ = spec.updInterval;
83     filePrefix_ = spec.filePrefix;
84     currId_ = 0;
85     valid_ = true;
86     viewId_ = common.id;
87     this->SetAutoEnable(false);
88     this->SetPosition(common.x, common.y);
89     this->SetWidth(common.w);
90     this->SetHeight(common.h);
91     this->SetVisible(common.visible);
92     this->SetViewId(viewId_.c_str());
93     LOG(INFO) << "dir:" << dir_ << ", imgCnt:" << imgCnt_ << ", interval:" << interval_;
94     if (interval_ == 0) {
95         this->SetSrc(dir_.c_str());
96         this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
97     } else {
98         cb_ = std::make_unique<ImgAnimatorCallback>(this);
99         cb_->Init();
100     }
101 }
102 
IsValid(const UxImageInfo & info)103 bool ImgViewAdapter::IsValid(const UxImageInfo &info)
104 {
105     if (info.updInterval == 0) {
106         return IsValidForStaticImg(info);
107     }
108     return IsValidForAnimator(info);
109 }
110 
IsValidForStaticImg(const UxImageInfo & info)111 bool ImgViewAdapter::IsValidForStaticImg(const UxImageInfo &info)
112 {
113     if (info.resPath.empty()) {
114         LOG(ERROR) << "img viewinfo check failed, resPath is empty when this is a static image";
115         return false;
116     }
117     return true;
118 }
119 
IsValidForAnimator(const UxImageInfo & info)120 bool ImgViewAdapter::IsValidForAnimator(const UxImageInfo &info)
121 {
122     if (info.filePrefix.empty() || info.resPath.empty()) {
123         LOG(ERROR) << "img viewinfo check failed, filePrefix is empty when this is a animator";
124         return false;
125     }
126 
127     if ((info.imgCnt > MAX_IMG_CNT) || (info.imgCnt == 0)) {
128         LOG(ERROR) << "img viewinfo check failed, imgCnt: " << info.imgCnt;
129         return false;
130     }
131 
132     if (info.updInterval > MAX_INTERVAL_MS) {
133         LOG(ERROR) << "img viewinfo check failed, updInterval: " << info.updInterval;
134         return false;
135     }
136 
137     return true;
138 }
139 
Start()140 bool ImgViewAdapter::Start()
141 {
142     if (!valid_ || !animatorStop_) {
143         return false;
144     }
145     if (cb_ == nullptr) {
146         LOG(ERROR) << "cb_ is null";
147         return false;
148     }
149     cb_->Start();
150     cb_->GetAnimator()->Start();
151     animatorStop_ = false;
152     return true;
153 }
154 
Stop()155 bool ImgViewAdapter::Stop()
156 {
157     if (!valid_ || animatorStop_) {
158         return false;
159     }
160     if (cb_ == nullptr) {
161         LOG(ERROR) << "cb_ is null";
162         return false;
163     }
164     cb_->GetAnimator()->Stop();
165     cb_->Stop();
166     animatorStop_ = true;
167     return true;
168 }
169 
ShowNextImage()170 void ImgViewAdapter::ShowNextImage()
171 {
172     std::stringstream ss;
173     ss << dir_ << filePrefix_ << std::setw(ANIMATION_FILE_NAME_LENGTH) << std::setfill('0') << currId_ << ".png";
174     currPath_ = ss.str();
175     if (access(currPath_.c_str(), F_OK) == -1) {
176         LOG(ERROR) << "not exist: " << currPath_;
177     }
178 
179     this->SetSrc(currPath_.c_str());
180     this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
181     currId_ = (currId_ + 1) % imgCnt_;
182     Utils::UsSleep(interval_ * USECOND_TO_MSECOND);
183 }
184 
185 #ifdef UPDATER_UT
GetAnimatorCallback() const186 const ImgViewAdapter::ImgAnimatorCallback *ImgViewAdapter::GetAnimatorCallback() const
187 {
188     return cb_.get();
189 }
190 
GetAnimator() const191 const OHOS::Animator *ImgViewAdapter::GetAnimator() const
192 {
193     if (!cb_) {
194         return nullptr;
195     }
196     return cb_->GetAnimator();
197 }
198 
199 
GetCurrId() const200 uint32_t ImgViewAdapter::GetCurrId() const
201 {
202     return currId_;
203 }
204 #endif
205 } // namespace Updater
206