1 /*
2  * Copyright (c) 2024 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 FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_TIMEPICKER_AUDIO_HAPTIC_CONTROLLER_H
17 #define FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_TIMEPICKER_AUDIO_HAPTIC_CONTROLLER_H
18 
19 #include <condition_variable>
20 #include <cmath>
21 #include <mutex>
22 #include <thread>
23 
24 #include "core/components_ng/pattern/time_picker/timepicker_haptic_interface.h"
25 #include "core/components/common/layout/screen_system_manager.h"
26 #include "core/gestures/velocity_tracker.h"
27 #include "frameworks/base/memory/ace_type.h"
28 #include "audio_haptic_manager.h"
29 #include "audio_haptic_player.h"
30 #include "audio_group_manager.h"
31 #include "audio_system_manager.h"
32 
33 namespace OHOS::Ace::NG {
34 
35 class TimePickerHapticController : public ITimepickerAudioHaptic {
36 public:
37     enum class ThreadStatus {
38         NONE,
39         START,
40         READY,
41         PLAYING,
42         PLAY_ONCE,
43     };
44 
45     TimePickerHapticController() noexcept;
46     ~TimePickerHapticController() noexcept;
47     void Play(size_t speed) override;
48     void PlayOnce() override;
49     void Stop() override;
50     void HandleDelta(double dy) override;
51 
52 private:
53     void ThreadLoop();
54     void ThreadRelease();
55     void InitPlayThread();
56     bool IsThreadReady();
57     bool IsThreadPlaying();
58     bool IsThreadPlayOnce();
59     bool IsThreadNone();
60     double ConvertPxToMillimeters(double px) const;
61     size_t GetCurrentSpeedInMm();
62 
63     ThreadStatus playThreadStatus_ = ThreadStatus::NONE;
64     std::recursive_mutex threadMutex_;
65     std::condition_variable_any threadCv_;
66     VelocityTracker velocityTracker_;
67     double scrollValue_ = 0.0;
68     int32_t effectSourceId_ = -1;
69     size_t absSpeedInMm_ = 0;
70     std::shared_ptr<Media::AudioHapticPlayer> effectAudioHapticPlayer_ = nullptr;
71     std::shared_ptr<Media::AudioHapticManager> audioHapticManager_ = nullptr;
72     std::unique_ptr<std::thread> playThread_ = nullptr;
73     std::shared_ptr<AudioStandard::AudioGroupManager> audioGroupMngr_ = nullptr;
74     ACE_DISALLOW_COPY_AND_MOVE(TimePickerHapticController);
75 };
76 } // namespace OHOS::Ace::NG
77 
78 #endif // FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_TIMEPICKER_AUDIO_HAPTIC_CONTROLLER_H
79