1/*
2 * Copyright (c) 2020 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
16#ifndef OHOS_PLAYER_ABILITY_SLICE_H
17#define OHOS_PLAYER_ABILITY_SLICE_H
18
19#include "ability_loader.h"
20#include "animator/animator.h"
21#include "animator/easing_equation.h"
22#include "components/root_view.h"
23#include "components/ui_label.h"
24#include "components/ui_slider.h"
25#include "components/ui_surface_view.h"
26#include "components/ui_toggle_button.h"
27#include "event_listener.h"
28#include "gallery_config.h"
29#include "player.h"
30#include "securec.h"
31#include "source.h"
32
33namespace OHOS {
34using OHOS::Media::Player;
35using OHOS::Media::Source;
36using namespace OHOS::Media;
37struct PlayerAdapter {
38    std::shared_ptr<Player> adapter;
39    int32_t sourceType;
40    char filePath[MAX_PATH_LENGTH];
41};
42
43class ToggleBtnListener : public UIView::OnClickListener {
44public:
45    explicit ToggleBtnListener(UIToggleButton* btn,
46                               PlayerAdapter* sample,
47                               Animator* animator,
48                               UISurfaceView* surfaceView)
49        : button_(btn),
50          videoPlayer_(sample),
51          animator_(animator) {}
52
53    virtual ~ToggleBtnListener() {}
54
55    bool OnClick(UIView &view, const ClickEvent& event) override;
56
57    void SetCompleteFlag(bool state)
58    {
59        completeFlag_ = state;
60    }
61
62private:
63    UIToggleButton* button_;
64    PlayerAdapter* videoPlayer_;
65    Animator* animator_;
66    bool completeFlag_ { false };
67};
68
69class SliderAnimator : public Animator, public AnimatorCallback {
70public:
71    explicit SliderAnimator(PlayerAdapter *sample,
72                            UISlider *slider,
73                            UILabel *label,
74                            int64_t duration,
75                            UISurfaceView* surfaceView)
76        : Animator(this, slider, duration, true),
77          videoPlayer_(sample),
78          slider_(slider),
79          timeLabel_(label),
80          duration_(duration),
81          surfaceView_(surfaceView),
82          needRefreshPlayer_(false) {}
83
84    virtual ~SliderAnimator() {}
85
86    void Callback(UIView* view) override;
87
88    void SetToggleButton(UIToggleButton* toggleButton)
89    {
90        toggleButton_ = toggleButton;
91    }
92
93    void SetToggleBtnListener(ToggleBtnListener* listener)
94    {
95        listener_ = listener;
96    }
97
98private:
99    PlayerAdapter* videoPlayer_;
100    UISlider* slider_;
101    UILabel* timeLabel_;
102    int64_t duration_;
103    UISurfaceView* surfaceView_;
104    UIToggleButton* toggleButton_ { nullptr };
105    ToggleBtnListener* listener_ { nullptr };
106    bool needRefreshPlayer_;
107};
108
109class PlayerAbilitySlice : public AbilitySlice {
110public:
111    PlayerAbilitySlice() = default;
112    ~PlayerAbilitySlice() override;
113    static std::shared_ptr<Player> CreatePlayer();
114protected:
115    void OnStart(const Want &want) override;
116    void OnInactive() override;
117    void OnActive(const Want &want) override;
118    void OnBackground() override;
119    void OnStop() override;
120
121private:
122    void Clear();
123    void ShowErrorTips();
124    void SetUpRootView();
125    void SetUpBackArea(const char* pathHeader);
126    void SetUpVideoPlayer(const Want &want);
127    bool SetUpSurfaceView();
128    void SetUpProgress(int64_t duration);
129    void SetUpAnimatorGroup(const char* pathHeader);
130    void SetUpToggleButton(const char* pathHeader);
131
132    PlayerAdapter* videoPlayer_ { nullptr };
133    SliderAnimator* animator_ { nullptr };
134    EventListener* backIconListener_ { nullptr };
135    ToggleBtnListener* onClickListener_ { nullptr };
136
137    RootView* rootView_ { nullptr };
138    UIViewGroup* backArea_ { nullptr };
139    UIImageView* backIcon_ { nullptr };
140    UISurfaceView* surfaceView_ { nullptr };
141    UIViewGroup* animatorGroup_ { nullptr };
142    UIToggleButton* toggleButton_ { nullptr };
143    UIViewGroup* toggleButtonArea_ { nullptr };
144    UILabel* currentTimeLabel_ { nullptr };
145    UISlider* slider_ { nullptr };
146    UILabel* totalTimeLabel_ { nullptr };
147    UILabel* errorTips_ { nullptr };
148    char backIconAbsolutePath[MAX_PATH_LENGTH] = { 0 };
149    char videoPlayAbsolutePath[MAX_PATH_LENGTH] = { 0 };
150    char videoPauseAbsolutePath[MAX_PATH_LENGTH] = { 0 };
151};
152} // namespace OHOS
153#endif // OHOS_PLAYER_ABILITY_SLICE_H
154