8b6e661628656fa09fdf86dfa840122cdf72d4aa
[mirrors/FD230.git] /
1 /*
2  * @Author: luozheng 1732036298@qq.com
3  * @Date: 2024-04-19 10:08:31
4  * @LastEditors: luozheng 1732036298@qq.com
5  * @LastEditTime: 2024-09-13 18:31:03
6  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/soundandvibration/src/SoundAndVibrationVolumeSettingPresenter.cpp
7  */
8 #include "soundandvibration/include/SoundAndVibrationVolumeSettingPresenter.h"
9 #include "soundandvibration/include/SoundAndVibrationVolumeSettingC.h"
10 #include "wearable_log.h"
11 #include <audio_manager.h>
12 #include <custom_utils.h>
13 #include "phoneservice/include/PhoneAudio.h"
14 #include "phoneservice/include/PhoneService.h"
15 #include "../../../../../../middleware/services/media/foundation/audiomanager/frameworks/audioservice/include/audio_service.h"
16 #include "vibrator_service.h"
17 #include "ConfigModel.h"
18
19 namespace OHOS
20 {
21
22     static constexpr int16_t VOLUMEVALUE_CHANGE = 10;
23     // 步骤 2: 实现具体的策略
24     class LowerVolumeStrategy : public VolumeChangeStrategy
25     {
26     public:
27         void ApplyChange(SoundAndVibrationVolumeSettingView *view, UISlider *slider, int32_t value) override
28         {
29             uint16_t newVolumeValue = slider->GetValue() - VOLUMEVALUE_CHANGE;
30             if (newVolumeValue < VOLUME_MIN)
31             {
32                 newVolumeValue = VOLUME_MIN;
33             }
34             if (view->GetVolumeType() == SYSTEM_VOLUME)
35             {
36                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_SYSTEM_VOLUME, (void *)(&value), sizeof(value));
37             }
38             else if (view->GetVolumeType() == CALL_VOLUME)
39             {
40                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_CALL_VOLUME, (void *)(&value), sizeof(value));
41             }
42             slider->SetValue(newVolumeValue);
43         }
44     };
45
46     class ElevateVolumeStrategy : public VolumeChangeStrategy
47     {
48     public:
49         void ApplyChange(SoundAndVibrationVolumeSettingView *view, UISlider *slider, int32_t value) override
50         {
51             uint16_t newVolumeValue = slider->GetValue() + VOLUMEVALUE_CHANGE;
52             if (newVolumeValue > VOLUME_MAX)
53             {
54                 newVolumeValue = VOLUME_MAX;
55             }
56             if (view->GetVolumeType() == SYSTEM_VOLUME)
57             {
58                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_SYSTEM_VOLUME, (void *)(&value), sizeof(value));
59             }
60             else if (view->GetVolumeType() == CALL_VOLUME)
61             {
62                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_CALL_VOLUME, (void *)(&value), sizeof(value));
63             }
64             slider->SetValue(newVolumeValue);
65         }
66     };
67
68     class DragVolumeStrategy : public VolumeChangeStrategy
69     {
70     public:
71         void ApplyChange(SoundAndVibrationVolumeSettingView *view, UISlider *slider, int32_t value) override
72         {
73             if (view->GetVolumeType() == SYSTEM_VOLUME)
74             {
75                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_SYSTEM_VOLUME, (void *)(&value), sizeof(value));
76             }
77             else if (view->GetVolumeType() == CALL_VOLUME)
78             {
79
80                 do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_CHANGE_CALL_VOLUME, (void *)(&value), sizeof(value));
81             }
82             slider->SetValue(value);
83         }
84     };
85
86     /* --------------------- VoumeProcessSliderEventListener -------------------- */
87     static VoumeProcessSliderEventListener *g_pVoumeProcessSliderEventListener{nullptr};
88     VoumeProcessSliderEventListener::VoumeProcessSliderEventListener()
89     {
90         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "VoumeProcessSliderEventListener::VoumeProcessSliderEventListener");
91         // 步骤 3: 创建一个映射表将状态映射到策略
92         strategyMap[VOLUMEREDUCE] = std::make_shared<LowerVolumeStrategy>();
93         strategyMap[VOLUMEADD] = std::make_shared<ElevateVolumeStrategy>();
94         strategyMap[OTHER] = std::make_shared<DragVolumeStrategy>();
95         g_pVoumeProcessSliderEventListener = this;
96     }
97
98     VoumeProcessSliderEventListener::~VoumeProcessSliderEventListener()
99     {
100         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "VoumeProcessSliderEventListener::~VoumeProcessSliderEventListener");
101         g_pVoumeProcessSliderEventListener = nullptr;
102     }
103
104     VoumeProcessSliderEventListener *VoumeProcessSliderEventListener::GetInstance(void)
105     {
106         return g_pVoumeProcessSliderEventListener;
107     }
108
109     void VoumeProcessSliderEventListener::FindAndApplyStrategy(uint8_t status, int32_t value)
110     {
111         auto it = strategyMap.find(status);
112         if (it != strategyMap.end())
113         {
114             SoundAndVibrationVolumeSettingView *view = SoundAndVibrationVolumeSettingView::GetInstance();
115             UISlider *slider = view ? view->GetVolumeProcess() : nullptr;
116             if (view && slider)
117             {
118                 it->second->ApplyChange(view, slider, value);
119             }
120         }
121     }
122
123     void VoumeProcessSliderEventListener::ChangeVolumeValue(uint8_t status, int32_t value)
124     {
125         FindAndApplyStrategy(status, value);
126     }
127
128     // 滑块变化事件处理
129     void VoumeProcessSliderEventListener::OnChange(int32_t value)
130     {
131         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnChange %d", value);
132         if (isOnClickSlider && !isDragSlider && !isRotateSlider)
133         {
134             ChangeVolumeValue(OTHER, value);
135         }
136     }
137
138     // 滑块释放事件处理
139     void VoumeProcessSliderEventListener::OnRelease(int32_t value)
140     {
141         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnRelease %d", value);
142         ChangeVolumeValue(OTHER, value);
143     }
144
145     // 滑块点击事件处理
146     bool VoumeProcessSliderEventListener::OnClick(UIView &view, const ClickEvent &event)
147     {
148         UNUSED(event);
149         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "VoumeProcessSliderEventListener::OnClick");
150         UISlider *slider = dynamic_cast<UISlider *>(&view);
151         SoundAndVibrationVolumeSettingView *soundAndVibrationVolumeSettingView = SoundAndVibrationVolumeSettingView::GetInstance();
152         if (slider != nullptr && soundAndVibrationVolumeSettingView != nullptr)
153         {
154             if (slider == soundAndVibrationVolumeSettingView->GetVolumeProcess())
155             {
156                 isOnClickSlider = true;
157                 isDragSlider = false;
158                 isRotateSlider = false;
159             }
160         }
161         return true;
162     }
163
164     // 滑块拖动事件处理
165     bool VoumeProcessSliderEventListener::OnDrag(UIView &view, const DragEvent &event)
166     {
167         UNUSED(event);
168         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "VoumeProcessSliderEventListener::OnDrag");
169         UISlider *slider = dynamic_cast<UISlider *>(&view);
170         SoundAndVibrationVolumeSettingView *soundAndVibrationVolumeSettingView = SoundAndVibrationVolumeSettingView::GetInstance();
171         if (slider != nullptr && soundAndVibrationVolumeSettingView != nullptr)
172         {
173             if (slider == soundAndVibrationVolumeSettingView->GetVolumeProcess())
174             {
175                 isOnClickSlider = false;
176                 isDragSlider = true;
177                 isRotateSlider = false;
178             }
179         }
180         return true;
181     }
182
183     // 滑块旋转结束事件处理
184     bool VoumeProcessSliderEventListener::OnRotateEnd(UIView &view, const RotateEvent &event)
185     {
186         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "VoumeProcessSliderEventListener::OnRotateEnd");
187         UISlider *slider = dynamic_cast<UISlider *>(&view);
188         SoundAndVibrationVolumeSettingView *soundAndVibrationVolumeSettingView = SoundAndVibrationVolumeSettingView::GetInstance();
189         if (slider != nullptr && soundAndVibrationVolumeSettingView != nullptr)
190         {
191             if (slider == soundAndVibrationVolumeSettingView->GetVolumeProcess())
192             {
193                 isDragSlider = false;
194                 isOnClickSlider = false;
195                 isRotateSlider = true;
196
197                 int value = slider->GetValue();
198                 ChangeVolumeValue(OTHER, value);
199             }
200         }
201         return true;
202     }
203 }
204 #ifdef __cplusplus
205 #if __cplusplus
206 extern "C"
207 {
208 #endif /* __cplusplus */
209 #endif /* __cplusplus */
210
211     extern void custom_VibratorFunc_int(int vibratorType);
212     /*
213      * @brief 系统音量设置
214      */
215     void SystemVolumeSet(int32_t sounding)
216     {
217         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SystemVolumeSet %d", sounding);
218         Audio::AudioManager::GetInstance().SetVolume(AUDIO_STREAM_MUSIC, sounding); // 系统音量设置
219     }
220
221     int SystemVolumeGet(void)
222     {
223         return Audio::AudioManager::GetInstance().GetVolume(AUDIO_STREAM_MUSIC);
224     }
225
226     /*
227      * @brief 通话音量设置
228      */
229     void CallVolumeSet(int32_t sounding)
230     {
231         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CallVolumeSet %d", sounding);
232         // Audio::AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_CALL_BT_SCO, sounding); // 通话音量设置
233         // Audio::AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_CALL_VOLTE, sounding); // 通话音量设置
234         set_speaker_volume(sounding,AUDIO_STREAM_VOICE_CALL_BT_SCO);
235         set_speaker_volume(sounding,AUDIO_STREAM_VOICE_CALL_VOLTE);
236     }
237
238     int CallVolumeGet(void)
239     {
240         // return Audio::AudioManager::GetInstance().GetVolume(AUDIO_STREAM_VOICE_CALL_BT_SCO);
241         return GetCallCurrVolume();
242     }
243
244     /*
245      * @brief 静音模式设置
246      */
247     void SilentModelSet(bool isopen)
248     {
249         AudioServiceSetMasterMute(isopen);
250     }
251 #ifdef __cplusplus
252 #if __cplusplus
253 }
254 #endif /* __cplusplus */
255 #endif /* __cplusplus */