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