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 "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 = std::get<UxImageInfo>(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 SetViewCommonInfo(info.commonInfo);
89 LOG(DEBUG) << "dir:" << dir_ << ", imgCnt:" << imgCnt_ << ", interval:" << interval_;
90 if (interval_ == 0) {
91 GetRealImgPath();
92 this->SetSrc(dir_.c_str());
93 this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
94 } else {
95 cb_ = std::make_unique<ImgAnimatorCallback>(this);
96 cb_->Init();
97 }
98 }
99
IsValid(const UxImageInfo & info)100 bool ImgViewAdapter::IsValid(const UxImageInfo &info)
101 {
102 if (info.updInterval == 0) {
103 return IsValidForStaticImg(info);
104 }
105 return IsValidForAnimator(info);
106 }
107
GetRealImgPath()108 void ImgViewAdapter::GetRealImgPath()
109 {
110 using namespace Lang;
111 if (Fs::exists(dir_)) {
112 return;
113 }
114 const static std::unordered_map<Language, std::string> postFixMap {
115 {Language::CHINESE, "chn"},
116 {Language::ENGLISH, "eng"},
117 {Language::SPANISH, "esp"},
118 };
119 auto iter = postFixMap.find(LanguageUI::GetInstance().GetCurLanguage());
120 if (iter == postFixMap.end()) {
121 return;
122 }
123 std::string newPath = dir_ + "_" + iter->second + ".png";
124 if (!Fs::exists(newPath)) {
125 LOG(WARNING) << "newPath not existed " << newPath;
126 return;
127 }
128 dir_ = newPath;
129 return;
130 }
131
IsValidForStaticImg(const UxImageInfo & info)132 bool ImgViewAdapter::IsValidForStaticImg(const UxImageInfo &info)
133 {
134 if (info.resPath.empty()) {
135 LOG(ERROR) << "img viewinfo check failed, resPath is empty when this is a static image";
136 return false;
137 }
138 return true;
139 }
140
IsValidForAnimator(const UxImageInfo & info)141 bool ImgViewAdapter::IsValidForAnimator(const UxImageInfo &info)
142 {
143 if (info.filePrefix.empty() || info.resPath.empty()) {
144 LOG(ERROR) << "img viewinfo check failed, filePrefix is empty when this is a animator";
145 return false;
146 }
147
148 if ((info.imgCnt > MAX_IMG_CNT) || (info.imgCnt == 0)) {
149 LOG(ERROR) << "img viewinfo check failed, imgCnt: " << info.imgCnt;
150 return false;
151 }
152
153 if (info.updInterval > MAX_INTERVAL_MS) {
154 LOG(ERROR) << "img viewinfo check failed, updInterval: " << info.updInterval;
155 return false;
156 }
157
158 return true;
159 }
160
Start()161 bool ImgViewAdapter::Start()
162 {
163 if (!valid_ || !animatorStop_) {
164 return false;
165 }
166 if (cb_ == nullptr) {
167 LOG(ERROR) << "cb_ is null";
168 return false;
169 }
170 cb_->Start();
171 cb_->GetAnimator()->Start();
172 animatorStop_ = false;
173 return true;
174 }
175
Stop()176 bool ImgViewAdapter::Stop()
177 {
178 if (!valid_ || animatorStop_) {
179 return false;
180 }
181 if (cb_ == nullptr) {
182 LOG(ERROR) << "cb_ is null";
183 return false;
184 }
185 cb_->GetAnimator()->Stop();
186 cb_->Stop();
187 animatorStop_ = true;
188 return true;
189 }
190
ShowNextImage()191 void ImgViewAdapter::ShowNextImage()
192 {
193 std::stringstream ss;
194 ss << dir_ << filePrefix_ << std::setw(ANIMATION_FILE_NAME_LENGTH) << std::setfill('0') << currId_ << ".png";
195 currPath_ = ss.str();
196 if (access(currPath_.c_str(), F_OK) == -1) {
197 LOG(ERROR) << "not exist: " << currPath_;
198 }
199
200 this->SetSrc(currPath_.c_str());
201 this->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL);
202 currId_ = (currId_ + 1) % imgCnt_;
203 Utils::UsSleep(interval_ * USECOND_TO_MSECOND);
204 }
205
206 #ifdef UPDATER_UT
GetAnimatorCallback() const207 const ImgViewAdapter::ImgAnimatorCallback *ImgViewAdapter::GetAnimatorCallback() const
208 {
209 return cb_.get();
210 }
211
GetAnimator() const212 const OHOS::Animator *ImgViewAdapter::GetAnimator() const
213 {
214 if (!cb_) {
215 return nullptr;
216 }
217 return cb_->GetAnimator();
218 }
219
220
GetCurrId() const221 uint32_t ImgViewAdapter::GetCurrId() const
222 {
223 return currId_;
224 }
225 #endif
226 } // namespace Updater
227