1 /*
2 * Copyright (c) 2025 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 "language/language_ui.h"
20 #include "log/log.h"
21 #include "page/view_proxy.h"
22 #include "updater/updater_const.h"
23 #include "updater_ui_const.h"
24 #include "updater_ui_env.h"
25 #include "utils.h"
26 #include "view_api.h"
27
28 namespace Updater {
29 class ImgViewAdapter::ImgAnimatorCallback final : public OHOS::AnimatorCallback {
30 DISALLOW_COPY_MOVE(ImgAnimatorCallback);
31 public:
ImgAnimatorCallback(ImgViewAdapter * view)32 explicit ImgAnimatorCallback(ImgViewAdapter *view)
33 : animator_(nullptr), stop_(true)
34 {
35 view_ = view;
36 if (view_ == nullptr) {
37 static ImgViewAdapter dummy;
38 view_ = &dummy;
39 }
40 }
41 ~ImgAnimatorCallback() = default;
Init()42 void Init()
43 {
44 animator_ = std::make_unique<OHOS::Animator>(this, view_, 0, true);
45 }
46
Callback(OHOS::UIView * view)47 void Callback(OHOS::UIView *view) override
48 {
49 if (stop_) {
50 return;
51 }
52 view_->ShowNextImage();
53 }
Start()54 void Start()
55 {
56 view_->SetVisible(true);
57 stop_ = false;
58 }
Stop()59 void Stop()
60 {
61 view_->SetVisible(false);
62 stop_ = true;
63 }
GetAnimator() const64 OHOS::Animator *GetAnimator() const
65 {
66 return animator_.get();
67 }
68 protected:
69 ImgViewAdapter *view_;
70 std::unique_ptr<OHOS::Animator> animator_;
71 bool stop_;
72 };
73
74 ImgViewAdapter::~ImgViewAdapter() = default;
75
76 ImgViewAdapter::ImgViewAdapter() = default;
77
ImgViewAdapter(const UxViewInfo & info)78 ImgViewAdapter::ImgViewAdapter(const UxViewInfo &info)
79 {
80 const UxImageInfo &spec = AsSpecific(info.specificInfo);
81 dir_ = spec.resPath;
82 imgCnt_ = spec.imgCnt;
83 interval_ = spec.updInterval;
84 filePrefix_ = spec.filePrefix;
85 currId_ = 0;
86 valid_ = true;
87 this->SetAutoEnable(false);
88 this->SetTouchable(true);
89 SetViewCommonInfo(info.commonInfo);
90 LOG(DEBUG) << "dir:" << dir_ << ", imgCnt:" << imgCnt_ << ", interval:" << interval_;
91 if (interval_ == 0) {
92 GetRealImgPath();
93 this->SetSrc(dir_.c_str());
94 this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
95 } else {
96 cb_ = std::make_unique<ImgAnimatorCallback>(this);
97 cb_->Init();
98 }
99 }
100
IsValid(const UxImageInfo & info)101 bool ImgViewAdapter::IsValid(const UxImageInfo &info)
102 {
103 if (info.updInterval == 0) {
104 return IsValidForStaticImg(info);
105 }
106 return IsValidForAnimator(info);
107 }
108
GetRealImgPath()109 void ImgViewAdapter::GetRealImgPath()
110 {
111 using namespace Lang;
112 if (Fs::exists(dir_)) {
113 return;
114 }
115 const static std::unordered_map<Language, std::string> postFixMap {
116 {Language::CHINESE, "chn"},
117 {Language::ENGLISH, "eng"},
118 {Language::SPANISH, "esp"},
119 };
120 auto iter = postFixMap.find(LanguageUI::GetInstance().GetCurLanguage());
121 if (iter == postFixMap.end()) {
122 return;
123 }
124 std::string newPath = dir_ + "_" + iter->second + ".png";
125 if (!Fs::exists(newPath)) {
126 LOG(WARNING) << "newPath not existed " << newPath;
127 return;
128 }
129 dir_ = newPath;
130 return;
131 }
132
IsValidForStaticImg(const UxImageInfo & info)133 bool ImgViewAdapter::IsValidForStaticImg(const UxImageInfo &info)
134 {
135 if (info.resPath.empty()) {
136 LOG(ERROR) << "img viewinfo check failed, resPath is empty when this is a static image";
137 return false;
138 }
139 return true;
140 }
141
IsValidForAnimator(const UxImageInfo & info)142 bool ImgViewAdapter::IsValidForAnimator(const UxImageInfo &info)
143 {
144 if (info.filePrefix.empty() || info.resPath.empty()) {
145 LOG(ERROR) << "img viewinfo check failed, filePrefix is empty when this is a animator";
146 return false;
147 }
148
149 if ((info.imgCnt > MAX_IMG_CNT) || (info.imgCnt == 0)) {
150 LOG(ERROR) << "img viewinfo check failed, imgCnt: " << info.imgCnt;
151 return false;
152 }
153
154 if (info.updInterval > MAX_INTERVAL_MS) {
155 LOG(ERROR) << "img viewinfo check failed, updInterval: " << info.updInterval;
156 return false;
157 }
158
159 return true;
160 }
161
Start()162 bool ImgViewAdapter::Start()
163 {
164 if (!valid_ || !animatorStop_) {
165 return false;
166 }
167 if (cb_ == nullptr) {
168 LOG(ERROR) << "cb_ is null";
169 return false;
170 }
171 cb_->Start();
172 cb_->GetAnimator()->Start();
173 animatorStop_ = false;
174 return true;
175 }
176
Stop()177 bool ImgViewAdapter::Stop()
178 {
179 if (!valid_ || animatorStop_) {
180 return false;
181 }
182 if (cb_ == nullptr) {
183 LOG(ERROR) << "cb_ is null";
184 return false;
185 }
186 cb_->GetAnimator()->Stop();
187 cb_->Stop();
188 animatorStop_ = true;
189 return true;
190 }
191
ShowNextImage()192 void ImgViewAdapter::ShowNextImage()
193 {
194 std::stringstream ss;
195 ss << dir_ << filePrefix_ << std::setw(ANIMATION_FILE_NAME_LENGTH) << std::setfill('0') << currId_ << ".png";
196 currPath_ = ss.str();
197 if (access(currPath_.c_str(), F_OK) == -1) {
198 LOG(ERROR) << "not exist: " << currPath_;
199 }
200
201 this->SetSrc(currPath_.c_str());
202 this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
203 currId_ = (currId_ + 1) % imgCnt_;
204 Utils::UsSleep(interval_ * USECOND_TO_MSECOND);
205 }
206
207 #ifdef UPDATER_UT
GetAnimatorCallback() const208 const ImgViewAdapter::ImgAnimatorCallback *ImgViewAdapter::GetAnimatorCallback() const
209 {
210 return cb_.get();
211 }
212
GetAnimator() const213 const OHOS::Animator *ImgViewAdapter::GetAnimator() const
214 {
215 if (!cb_) {
216 return nullptr;
217 }
218 return cb_->GetAnimator();
219 }
220
221
GetCurrId() const222 uint32_t ImgViewAdapter::GetCurrId() const
223 {
224 return currId_;
225 }
226 #endif
227 } // namespace Updater
228