• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const std::string NO_RING_SOUND = "no_ring_sound";
32 const std::string FILE_SIZE_EXCEEDS_LIMIT = "20700004";
33 const std::string FILE_COUNT_EXCEEDS_LIMIT = "20700005";
34 const std::string ROM_IS_INSUFFICIENT = "20700006";
35 
36 
37 enum RingtoneType {
38     RINGTONE_TYPE_SIM_CARD_0 = 0,
39     RINGTONE_TYPE_SIM_CARD_1 = 1,
40 };
41 
42 enum SystemToneType {
43     SYSTEM_TONE_TYPE_SIM_CARD_0 = 0,
44     SYSTEM_TONE_TYPE_SIM_CARD_1 = 1,
45     SYSTEM_TONE_TYPE_NOTIFICATION = 32,
46 };
47 
48 enum ToneHapticsType {
49     CALL_SIM_CARD_0 = 0,
50     CALL_SIM_CARD_1 = 1,
51     TEXT_MESSAGE_SIM_CARD_0 = 20,
52     TEXT_MESSAGE_SIM_CARD_1 = 21,
53     NOTIFICATION = 40,
54 };
55 
56 enum SystemToneUriType {
57     UNKNOW_RINGTONES = -1,
58     NO_RINGTONES = 0,
59     PRESET_RINGTONES = 1,
60     CUSTOM_RINGTONES = 2,
61 };
62 
63 enum SystemSoundError {
64     ERROR_IO = 5400103,
65     ERROR_OK = 20700000,
66     ERROR_TYPE_MISMATCH = 20700001,
67     ERROR_UNSUPPORTED_OPERATION = 20700003,
68     ERROR_DATA_TOO_LARGE = 20700004,
69     ERROR_TOO_MANY_FILES = 20700005,
70     ERROR_INSUFFICIENT_ROM = 20700006,
71     ERROR_INVALID_PARAM = 20700007,
72 };
73 
74 struct ParamsForAddCustomizedTone {
75     std::string dstPath;
76     int32_t srcFd;
77     int32_t length;
78     int32_t offset;
79     bool duplicateFile;
80 };
81 
82 class SystemSoundManager {
83 public:
84     virtual ~SystemSoundManager() = default;
85 
86     /**
87      * @brief Returns the ringtone player instance.
88      *
89      * @param context Indicates the Context object on OHOS.
90      * @param ringtoneType Indicates the ringtone type for which player instance has to be returned.
91      * @return Returns RingtonePlayer.
92      * @since 10
93      */
94     virtual std::shared_ptr<RingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context,
95         RingtoneType ringtoneType) = 0;
96 
97     /**
98      * @brief Return to the dedicated contact ringtone player instance.
99      *
100      * @param context Indicates the Context object on OHOS.
101      * @param ringtoneType Indicates the ringtone type for which player instance has to be returned.
102      * @param ringtoneUri Indicates the ringtone URI for a specific contact.
103      * @return Returns SpecificRingTonePlayer.
104      */
105     virtual std::shared_ptr<RingtonePlayer> GetSpecificRingTonePlayer(const std::shared_ptr<AbilityRuntime::Context>
106         &context, const RingtoneType ringtoneType, std::string &ringtoneUri) = 0;
107 
108     /**
109      * @brief API used for setting the ringtone uri.
110      *
111      * @param context Indicates the Context object on OHOS.
112      * @param uri Indicates which uri to be set for the tone type.
113      * @param ringtoneType Indicates the ringtone type.
114      * @return Returns {@link MSERR_OK} if set the ringtone uri successfully;
115      * returns an error code defined in {@link media_errors.h} otherwise.
116      * @since 10
117      */
118     virtual int32_t SetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri,
119         RingtoneType ringtoneType) = 0;
120 
121     /**
122      * @brief Returns the current ringtone uri.
123      *
124      * @param context Indicates the Context object on OHOS.
125      * @param ringtoneType Indicates the ringtone type.
126      * @return Returns the current ringtone uri.
127      * @since 10
128      */
129     virtual std::string GetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
130         RingtoneType ringtoneType) = 0;
131 
132     /**
133      * @brief Returns the current ringtone attrs.
134      *
135      * @param ringtoneType Indicates the ringtone type.
136      * @return Returns the current ringtone attrs.
137      * @since 12
138      */
139     virtual ToneAttrs GetCurrentRingtoneAttribute(RingtoneType ringtoneType) = 0;
140 
141     /**
142      * @brief Returns the system tone player instance
143      *
144      * @param context Indicates the Context object on OHOS.
145      * @param systemToneType Indicates the system tone type for which player instance has to be returned.
146      * @return Returns SystemTonePlayer.
147      * @since 11
148      */
149     virtual std::shared_ptr<SystemTonePlayer> GetSystemTonePlayer(
150         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) = 0;
151 
152     /**
153      * @brief API used for setting the system tone uri
154      *
155      * @param context Indicates the Context object on OHOS.
156      * @param uri indicates which uri to be set for system tone.
157      * @param systemToneType Indicates the system tone type.
158      * @return Returns {@link MSERR_OK} if set the system tone uri successfully;
159      * returns an error code defined in {@link media_errors.h} otherwise.
160      * @since 11
161      */
162     virtual int32_t SetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri,
163         SystemToneType systemToneType) = 0;
164 
165     /**
166      * @brief Returns the current system tone uri
167      *
168      * @param context Indicates the Context object on OHOS.
169      * @return Returns the system tone uri
170      * @since 11
171      */
172     virtual std::string GetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
173         SystemToneType systemToneType)= 0;
174 
175     /**
176      * @brief Returns the default ringtone attributes.
177      *
178      * @param context Indicates the Context object on OHOS.
179      * @param ringtoneType Indicates the ringtone type.
180      * @return Returns the default ringtone attrs.
181      * @since 12
182      */
183     virtual std::shared_ptr<ToneAttrs> GetDefaultRingtoneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context,
184         RingtoneType ringtoneType) = 0;
185 
186     /**
187      * @brief Returns the list of ringtone attributes.
188      *
189      * @param context Indicates the Context object on OHOS.
190      * @param ringtoneType Indicates the ringtone type.
191      * @return Returns the list of ringtone attrs.
192      * @since 12
193      */
194     virtual std::vector<std::shared_ptr<ToneAttrs>> GetRingtoneAttrList(
195         const std::shared_ptr<AbilityRuntime::Context> &context, RingtoneType ringtoneType) = 0;
196 
197     /**
198      * @brief Returns the default systemtone attributes.
199      *
200      * @param context Indicates the Context object on OHOS.
201      * @param systemToneType Indicates the systemtone type.
202      * @return Returns the default systemtone attrs.
203      * @since 12
204      */
205     virtual std::shared_ptr<ToneAttrs>  GetDefaultSystemToneAttrs(
206         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemtoneType) = 0;
207 
208     /**
209      * @brief Returns the list of systemtone attributes.
210      *
211      * @param context Indicates the Context object on OHOS.
212      * @param systemToneType Indicates the systemtone type.
213      * @return Returns the list of systemtone attrs.
214      * @since 12
215      */
216     virtual std::vector<std::shared_ptr<ToneAttrs>> GetSystemToneAttrList(
217         const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) = 0;
218 
219     /**
220      * @brief  Sets uri of the current alarm tone.
221      *
222      * @param context Indicates the Context object on OHOS.
223      * @param uri indicates which uri to be set for system tone.
224      * @return Returns {@link MSERR_OK} if set the system tone uri successfully;
225      * returns an error code defined in {@link media_errors.h} otherwise.
226      * @since 12
227      */
228     virtual int32_t SetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
229         const std::string &uri) = 0;
230 
231     /**
232      * @brief Gets uri of the current alarm tone.
233      *
234      * @param context Indicates the Context object on OHOS.
235      * @return Returns the alarm tone uri
236      * @since 12
237      */
238     virtual std::string GetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
239 
240     /**
241      * @brief Returns the default alarmTone attributes.
242      *
243      * @param context Indicates the Context object on OHOS.
244      * @return Returns the default alarmTone attrs.
245      * @since 12
246      */
247     virtual std::shared_ptr<ToneAttrs> GetDefaultAlarmToneAttrs(
248         const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
249 
250     /**
251      * @brief Returns the list of alarmTone attributes.
252      *
253      * @param context Indicates the Context object on OHOS.
254      * @return Returns the list of alarmTone attrs.
255      * @since 12
256      */
257     virtual std::vector<std::shared_ptr<ToneAttrs>> GetAlarmToneAttrList(
258         const std::shared_ptr<AbilityRuntime::Context> &context) = 0;
259 
260     /**
261      * @brief Open the alarm tone file.
262      *
263      * @param context Indicates the Context object on OHOS.
264      * @param uri Uri of alarm tone to open.
265      * @return Returns the fd of tone.
266      * @since 12
267      */
268     virtual int32_t OpenAlarmTone(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) = 0;
269 
270     /**
271      * @brief Close the tone file.
272      *
273      * @param fd File descriptor.
274      * @return Returns {@link MSERR_OK} if close the fd successfully;
275      * returns an error code defined in {@link media_errors.h} otherwise.
276      * @since 12
277      */
278     virtual int32_t Close(const int32_t &fd) = 0;
279 
280     /**
281      * @brief Add customized tone into ringtone library.
282      *
283      * @param context Indicates the Context object on OHOS.
284      * @param toneAttrs Tone attributes.
285      * @param externalUri Tone uri in external storage.
286      * @return Returns the tone uri after adding into ringtone library.
287      * @since 12
288      */
289     virtual std::string AddCustomizedToneByExternalUri(const std::shared_ptr<AbilityRuntime::Context> &context,
290         const std::shared_ptr<ToneAttrs> &toneAttrs, const std::string &externalUri) = 0;
291 
292     /**
293      * @brief Add customized tone into ringtone library.
294      *
295      * @param context Indicates the Context object on OHOS.
296      * @param toneAttrs Tone attributes.
297      * @param fd File descriptor.
298      * @return Returns the tone uri after adding into ringtone library.
299      * @since 12
300      */
301     virtual std::string AddCustomizedToneByFd(const std::shared_ptr<AbilityRuntime::Context> &context,
302         const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd) = 0;
303 
304     /**
305      * @brief Add customized tone into ringtone library.
306      *
307      * @param context Indicates the Context object on OHOS.
308      * @param toneAttrs Tone attributes.
309      * @param fd File descriptor.
310      * @param offset The offset in the file where the data to be read, in bytes.
311      * @param length The length in bytes of the data to be read.
312      * @return Returns the tone uri after adding into ringtone library.
313      * @since 12
314      */
315     virtual std::string AddCustomizedToneByFdAndOffset(
316         const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<ToneAttrs> &toneAttrs,
317             ParamsForAddCustomizedTone &paramsForAddCustomizedTone) = 0;
318 
319     /**
320      * @brief Remove customized tone in ringtone library.
321      *
322      * @param context Indicates the Context object on OHOS.
323      * @param uri tone uri
324      * @return Returns {@link MSERR_OK} if remove the customized tone successfully;
325      * returns an error code defined in {@link media_errors.h} otherwise.
326      * @since 12
327      */
328     virtual int32_t RemoveCustomizedTone(const std::shared_ptr<AbilityRuntime::Context> &context,
329         const std::string &uri) = 0;
330 
331     /**
332      * @brief Remove customized tones in ringtone library.
333      *
334      * @param uriList tone uris
335      * @return Returns {@link MSERR_OK} if remove the customized tone successfully;
336      * returns error codes defined in {@link media_errors.h} otherwise.
337      * @since 12
338      */
339     virtual std::vector<std::pair<std::string, SystemSoundError>> RemoveCustomizedToneList(
340         const std::vector<std::string> &uriList, SystemSoundError &errCode) = 0;
341 
342     /**
343      * @brief Returns the tone haptics settings.
344      *
345      * @param context Indicates the Context object on OHOS.
346      * @param toneHapticsType Indicates the tone type.
347      * @param settings tone haptics settings.
348      * @return Returns {@link MSERR_OK} if get the tone haptics settings successfully;
349      * returns an error code defined in {@link media_errors.h} otherwise.
350      * @since 12
351      */
352     virtual int32_t GetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context,
353         ToneHapticsType toneHapticsType, ToneHapticsSettings &settings) = 0;
354 
355      /**
356      * @brief  Sets tone haptics of the current tone.
357      *
358      * @param context Indicates the Context object on OHOS.
359      * @param toneHapticsType Indicates which haptics to be set for tone.
360      * @param settings Indicates the haptics settings.
361      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
362      * returns an error code defined in {@link media_errors.h} otherwise.
363      * @since 12
364      */
365     virtual int32_t SetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context,
366         ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings) = 0;
367 
368     /**
369      * @brief Returns the list of tone haptics attributes.
370      *
371      * @param context Indicates the Context object on OHOS.
372      * @param isSynced Indicates the haptics is synced.
373      * @param toneHapticsAttrsArray the list of tone haptics attrs.
374      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
375      * returns an error code defined in {@link media_errors.h} otherwise.
376      * @since 12
377      */
378     virtual int32_t GetToneHapticsList(const std::shared_ptr<AbilityRuntime::Context> &context,
379         bool isSynced, std::vector<std::shared_ptr<ToneHapticsAttrs>> &toneHapticsAttrsArray) = 0;
380 
381     /**
382      * @brief Returns the haptics attributes synced with tone.
383      *
384      * @param context Indicates the Context object on OHOS.
385      * @param toneUri Get the ringtone uri of the sync haptics.
386      * @param toneHapticsAttrsthe haptics attrs synced with tone.
387      * @return Returns {@link MSERR_OK} if set the tone haptics successfully;
388      * returns an error code defined in {@link media_errors.h} otherwise.
389      * @since 12
390      */
391     virtual int32_t GetHapticsAttrsSyncedWithTone(const std::shared_ptr<AbilityRuntime::Context> &context,
392         const std::string &toneUri, std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs) = 0;
393 
394     /**
395      * @brief Open the tone haptics file.
396      *
397      * @param context Indicates the Context object on OHOS.
398      * @param hapticsUri Uri of haptics to open.
399      * @return Returns the fd of haptics.
400      * @since 12
401      */
402     virtual int32_t OpenToneHaptics(const std::shared_ptr<AbilityRuntime::Context> &context,
403         const std::string &hapticsUri) = 0;
404 
405     /**
406      * @brief Open the tone file.
407      *
408      * @param context Indicates the Context object on OHOS.
409      * @param uri Uri of tone to open.
410      * @return Returns the fd of tone.
411      * @since 12
412      */
413     virtual int32_t OpenToneUri(const std::shared_ptr<AbilityRuntime::Context> &context,
414         const std::string &uri, int32_t toneType) = 0;
415 
416     /**
417      * @brief Returns fds of the uris in uriList.
418      *
419      * @param uriList Indicates the uris to open.
420      * @return Returns fds of the uris in uriList.
421      * @since 12
422      */
423     virtual std::vector<std::tuple<std::string, int64_t, SystemSoundError>> OpenToneList(
424         const std::vector<std::string> &uriList, SystemSoundError &errCode) = 0;
425 };
426 
427 class __attribute__((visibility("default"))) SystemSoundManagerFactory {
428 public:
429     static std::shared_ptr<SystemSoundManager> CreateSystemSoundManager();
430 
431 private:
432     static std::shared_ptr<SystemSoundManager> systemSoundManager_;
433     static std::mutex systemSoundManagerMutex_;
434     SystemSoundManagerFactory() = default;
435     ~SystemSoundManagerFactory() = default;
436 };
437 } // Media
438 } // OHOS
439 #endif // SYSTEM_SOUND_MANAGER_H
440