1 /*
2  * Copyright (c) 2023 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 SYSTEM_SOUND_MANAGER_H
17 #define SYSTEM_SOUND_MANAGER_H
18 
19 #include <string>
20 
21 #include "ability_runtime/context/context.h"
22 
23 #include "ringtone_player.h"
24 #include "system_tone_player.h"
25 #include "tone_attrs.h"
26 #include "tone_haptics_attrs.h"
27 
28 namespace OHOS {
29 namespace Media {
30 const std::string NO_SYSTEM_SOUND = "no_system_sound";
31 
32 enum RingtoneType {
33     RINGTONE_TYPE_SIM_CARD_0 = 0,
34     RINGTONE_TYPE_SIM_CARD_1 = 1,
35 };
36 
37 enum SystemToneType {
38     SYSTEM_TONE_TYPE_SIM_CARD_0 = 0,
39     SYSTEM_TONE_TYPE_SIM_CARD_1 = 1,
40     SYSTEM_TONE_TYPE_NOTIFICATION = 32,
41 };
42 
43 enum ToneHapticsType {
44     CALL_SIM_CARD_0 = 0,
45     CALL_SIM_CARD_1 = 1,
46     TEXT_MESSAGE_SIM_CARD_0 = 20,
47     TEXT_MESSAGE_SIM_CARD_1 = 21,
48     NOTIFICATION = 40,
49 };
50 
51 class SystemSoundManager {
52 public:
53     virtual ~SystemSoundManager() = default;
54 
55     /**
56      * @brief Returns the ringtone player instance.
57      *
58      * @param context Indicates the Context object on OHOS.
59      * @param ringtoneType Indicates the ringtone type for which player instance has to be returned.
60      * @return Returns RingtonePlayer.
61      * @since 10
62      */
63     virtual std::shared_ptr<RingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context,
64         RingtoneType ringtoneType) = 0;
65 
66     /**
67      * @brief API used for setting the ringtone uri.
68      *
69      * @param context Indicates the Context object on OHOS.
70      * @param uri Indicates which uri to be set for the tone type.
71      * @param ringtoneType Indicates the ringtone type.
72      * @return Returns {@link MSERR_OK} if set the ringtone uri successfully;
73      * returns an error code defined in {@link media_errors.h} otherwise.
74      * @since 10
75      */
76     virtual int32_t SetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri,
77         RingtoneType ringtoneType) = 0;
78 
79     /**
80      * @brief Returns the current ringtone uri.
81      *
82      * @param context Indicates the Context object on OHOS.
83      * @param ringtoneType Indicates the ringtone type.
84      * @return Returns the current ringtone uri.
85      * @since 10
86      */
87     virtual std::string GetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
88         RingtoneType ringtoneType) = 0;
89 
90     /**
91      * @brief Returns the system tone player instance
92      *
93      * @param context Indicates the Context object on OHOS.
94      * @param systemToneType Indicates the system tone type for which player instance has to be returned.
95      * @return Returns SystemTonePlayer.
96      * @since 11
97      */
98     virtual std::shared_ptr<SystemTonePlayer> GetSystemTonePlayer(
99         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) = 0;
100 
101     /**
102      * @brief API used for setting the system tone uri
103      *
104      * @param context Indicates the Context object on OHOS.
105      * @param uri indicates which uri to be set for system tone.
106      * @param systemToneType Indicates the system tone type.
107      * @return Returns {@link MSERR_OK} if set the system tone uri successfully;
108      * returns an error code defined in {@link media_errors.h} otherwise.
109      * @since 11
110      */
111     virtual int32_t SetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri,
112         SystemToneType systemToneType) = 0;
113 
114     /**
115      * @brief Returns the current system tone uri
116      *
117      * @param context Indicates the Context object on OHOS.
118      * @return Returns the system tone uri
119      * @since 11
120      */
121     virtual std::string GetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
122         SystemToneType systemToneType)= 0;
123 
124     /**
125      * @brief Returns the default ringtone attributes.
126      *
127      * @param context Indicates the Context object on OHOS.
128      * @param ringtoneType Indicates the ringtone type.
129      * @return Returns the default ringtone attrs.
130      * @since 12
131      */
132     virtual std::shared_ptr<ToneAttrs> GetDefaultRingtoneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context,
133         RingtoneType ringtoneType) = 0;
134 
135     /**
136      * @brief Returns the list of ringtone attributes.
137      *
138      * @param context Indicates the Context object on OHOS.
139      * @param ringtoneType Indicates the ringtone type.
140      * @return Returns the list of ringtone attrs.
141      * @since 12
142      */
143     virtual std::vector<std::shared_ptr<ToneAttrs>> GetRingtoneAttrList(
144         const std::shared_ptr<AbilityRuntime::Context> &context, RingtoneType ringtoneType) = 0;
145 
146     /**
147      * @brief Returns the default systemtone attributes.
148      *
149      * @param context Indicates the Context object on OHOS.
150      * @param systemToneType Indicates the systemtone type.
151      * @return Returns the default systemtone attrs.
152      * @since 12
153      */
154     virtual std::shared_ptr<ToneAttrs>  GetDefaultSystemToneAttrs(
155         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemtoneType) = 0;
156 
157     /**
158      * @brief Returns the list of systemtone attributes.
159      *
160      * @param context Indicates the Context object on OHOS.
161      * @param systemToneType Indicates the systemtone type.
162      * @return Returns the list of systemtone attrs.
163      * @since 12
164      */
165     virtual std::vector<std::shared_ptr<ToneAttrs>> GetSystemToneAttrList(
166         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) = 0;
167 
168     /**
169      * @brief  Sets uri of the current alarm tone.
170      *
171      * @param context Indicates the Context object on OHOS.
172      * @param uri indicates which uri to be set for system tone.
173      * @return Returns {@link MSERR_OK} if set the system tone uri successfully;
174      * returns an error code defined in {@link media_errors.h} otherwise.
175      * @since 12
176      */
177     virtual int32_t SetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
178         const std::string &uri) = 0;
179 
180     /**
181      * @brief Gets uri of the current alarm tone.
182      *
183      * @param context Indicates the Context object on OHOS.
184      * @return Returns the alarm tone uri
185      * @since 12
186      */
187     virtual std::string GetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
188 
189     /**
190      * @brief Returns the default alarmTone attributes.
191      *
192      * @param context Indicates the Context object on OHOS.
193      * @return Returns the default alarmTone attrs.
194      * @since 12
195      */
196     virtual std::shared_ptr<ToneAttrs> GetDefaultAlarmToneAttrs(
197         const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
198 
199     /**
200      * @brief Returns the list of alarmTone attributes.
201      *
202      * @param context Indicates the Context object on OHOS.
203      * @return Returns the list of alarmTone attrs.
204      * @since 12
205      */
206     virtual std::vector<std::shared_ptr<ToneAttrs>> GetAlarmToneAttrList(
207         const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
208 
209     /**
210      * @brief Open the alarm tone file.
211      *
212      * @param context Indicates the Context object on OHOS.
213      * @param uri Uri of alarm tone to open.
214      * @return Returns the fd of tone.
215      * @since 12
216      */
217     virtual int32_t OpenAlarmTone(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) = 0;
218 
219     /**
220      * @brief Close the tone file.
221      *
222      * @param fd File descriptor.
223      * @return Returns {@link MSERR_OK} if close the fd successfully;
224      * returns an error code defined in {@link media_errors.h} otherwise.
225      * @since 12
226      */
227     virtual int32_t Close(const int32_t &fd) = 0;
228 
229     /**
230      * @brief Add customized tone into ringtone library.
231      *
232      * @param context Indicates the Context object on OHOS.
233      * @param toneAttrs Tone attributes.
234      * @param externalUri Tone uri in external storage.
235      * @return Returns the tone uri after adding into ringtone library.
236      * @since 12
237      */
238     virtual std::string AddCustomizedToneByExternalUri(const std::shared_ptr<AbilityRuntime::Context> &context,
239         const std::shared_ptr<ToneAttrs> &toneAttrs, const std::string &externalUri) = 0;
240 
241     /**
242      * @brief Add customized tone into ringtone library.
243      *
244      * @param context Indicates the Context object on OHOS.
245      * @param toneAttrs Tone attributes.
246      * @param fd File descriptor.
247      * @return Returns the tone uri after adding into ringtone library.
248      * @since 12
249      */
250     virtual std::string AddCustomizedToneByFd(const std::shared_ptr<AbilityRuntime::Context> &context,
251         const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd) = 0;
252 
253     /**
254      * @brief Add customized tone into ringtone library.
255      *
256      * @param context Indicates the Context object on OHOS.
257      * @param toneAttrs Tone attributes.
258      * @param fd File descriptor.
259      * @param offset The offset in the file where the data to be read, in bytes.
260      * @param length The length in bytes of the data to be read.
261      * @return Returns the tone uri after adding into ringtone library.
262      * @since 12
263      */
264     virtual std::string AddCustomizedToneByFdAndOffset(
265         const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<ToneAttrs> &toneAttrs,
266             const int32_t &fd, const int32_t &offset, const int32_t &length) = 0;
267 
268     /**
269      * @brief Remove customized tone in ringtone library.
270      *
271      * @param context Indicates the Context object on OHOS.
272      * @param uri tone uri
273      * @return Returns {@link MSERR_OK} if remove the customized tone successfully;
274      * returns an error code defined in {@link media_errors.h} otherwise.
275      * @since 12
276      */
277     virtual int32_t RemoveCustomizedTone(const std::shared_ptr<AbilityRuntime::Context> &context,
278         const std::string &uri) = 0;
279 
280     /**
281      * @brief Returns the tone haptics settings.
282      *
283      * @param context Indicates the Context object on OHOS.
284      * @param toneHapticsType Indicates the tone type.
285      * @param settings tone haptics settings.
286      * @return Returns {@link MSERR_OK} if get the tone haptics settings successfully;
287      * returns an error code defined in {@link media_errors.h} otherwise.
288      * @since 12
289      */
290     virtual int32_t GetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context,
291         ToneHapticsType toneHapticsType, ToneHapticsSettings &settings) = 0;
292 
293      /**
294      * @brief  Sets tone haptics of the current tone.
295      *
296      * @param context Indicates the Context object on OHOS.
297      * @param toneHapticsType Indicates which haptics to be set for tone.
298      * @param settings Indicates the haptics settings.
299      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
300      * returns an error code defined in {@link media_errors.h} otherwise.
301      * @since 12
302      */
303     virtual int32_t SetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context,
304         ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings) = 0;
305 
306     /**
307      * @brief Returns the list of tone haptics attributes.
308      *
309      * @param context Indicates the Context object on OHOS.
310      * @param isSynced Indicates the haptics is synced.
311      * @param toneHapticsAttrsArray the list of tone haptics attrs.
312      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
313      * returns an error code defined in {@link media_errors.h} otherwise.
314      * @since 12
315      */
316     virtual int32_t GetToneHapticsList(const std::shared_ptr<AbilityRuntime::Context> &context,
317         bool isSynced, std::vector<std::shared_ptr<ToneHapticsAttrs>> &toneHapticsAttrsArray) = 0;
318 
319     /**
320      * @brief Returns the haptics attributes synced with tone.
321      *
322      * @param context Indicates the Context object on OHOS.
323      * @param toneUri Get the ringtone uri of the sync haptics.
324      * @param toneHapticsAttrsthe haptics attrs synced with tone.
325      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
326      * returns an error code defined in {@link media_errors.h} otherwise.
327      * @since 12
328      */
329     virtual int32_t GetHapticsAttrsSyncedWithTone(const std::shared_ptr<AbilityRuntime::Context> &context,
330         const std::string &toneUri, std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs) = 0;
331 
332     /**
333      * @brief Open the tone haptics file.
334      *
335      * @param context Indicates the Context object on OHOS.
336      * @param hapticsUri Uri of haptics to open.
337      * @return Returns the fd of haptics.
338      * @since 12
339      */
340     virtual int32_t OpenToneHaptics(const std::shared_ptr<AbilityRuntime::Context> &context,
341         const std::string &hapticsUri) = 0;
342 };
343 
344 class __attribute__((visibility("default"))) SystemSoundManagerFactory {
345 public:
346     static std::shared_ptr<SystemSoundManager> CreateSystemSoundManager();
347 
348 private:
349     static std::shared_ptr<SystemSoundManager> systemSoundManager_;
350     static std::mutex systemSoundManagerMutex_;
351     SystemSoundManagerFactory() = default;
352     ~SystemSoundManagerFactory() = default;
353 };
354 } // Media
355 } // OHOS
356 #endif // SYSTEM_SOUND_MANAGER_H
357