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#ifndef UPDATER_UI_IMG_VIEW_ADAPTER_H
16#define UPDATER_UI_IMG_VIEW_ADAPTER_H
17
18#include <string>
19#include <thread>
20#include "animator/animator_manager.h"
21#include "component_common.h"
22#include "components/ui_image_view.h"
23#include "macros_updater.h"
24
25namespace Updater {
26struct UxImageInfo {
27    std::string resPath;
28    std::string filePrefix;
29    uint32_t imgCnt;
30    uint32_t updInterval;
31};
32struct UxViewInfo;
33class ImgViewAdapter : public OHOS::UIImageView, public ComponentCommon<ImgViewAdapter> {
34    DISALLOW_COPY_MOVE(ImgViewAdapter);
35    class ImgAnimatorCallback;
36    static constexpr uint32_t MAX_IMG_CNT = 300;
37    static constexpr uint32_t MAX_INTERVAL_MS = 5000;
38    static constexpr uint32_t USECOND_TO_MSECOND = 1000;
39    static constexpr int ANIMATION_FILE_NAME_LENGTH = 5;
40public:
41    using SpecificInfoType = UxImageInfo;
42    static constexpr auto COMPONENT_TYPE = "UIImageView";
43    ImgViewAdapter();
44    explicit ImgViewAdapter(const UxViewInfo &info);
45    ~ImgViewAdapter();
46    bool Start();
47    bool Stop();
48#ifdef UPDATER_UT
49    const ImgAnimatorCallback *GetAnimatorCallback() const;
50    const OHOS::Animator *GetAnimator() const;
51    uint32_t GetCurrId() const;
52#endif
53    static bool IsValid(const UxImageInfo &info);
54private:
55    void GetRealImgPath();
56    void ShowNextImage();
57    void ThreadCb();
58    static bool IsValidForAnimator(const UxImageInfo &info);
59    static bool IsValidForStaticImg(const UxImageInfo &info);
60    bool animatorStop_ { true };
61    bool valid_ { false };
62    uint32_t currId_ { 0 };
63    uint32_t imgCnt_ { 0 };
64    uint32_t interval_ { 0 };
65    std::string currPath_ {};
66    std::string dir_ {};
67    std::string filePrefix_ {};
68    std::unique_ptr<ImgAnimatorCallback> cb_ {};
69};
70} // namespace Updater
71#endif // UPDATER_UI_ANIMATION_LABLE_H
72