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_IMPL_H 17 #define SYSTEM_SOUND_MANAGER_IMPL_H 18 19 #include <array> 20 21 #include "datashare_helper.h" 22 #include "data_ability_helper.h" 23 #include "ability_runtime/context/context.h" 24 #include "uri.h" 25 #include "want.h" 26 27 #include "audio_utils.h" 28 #include "access_token.h" 29 #include <iostream> 30 #include "system_ability_definition.h" 31 #include "ringtone_db_const.h" 32 #include "ringtone_type.h" 33 #include "ringtone_asset.h" 34 #include "simcard_setting_asset.h" 35 #include "vibrate_asset.h" 36 #include "ringtone_fetch_result.h" 37 #include "iservice_registry.h" 38 #include <unistd.h> 39 #include <fcntl.h> 40 #include <cerrno> 41 42 #include "audio_system_manager.h" 43 44 #include "system_sound_manager.h" 45 46 namespace OHOS { 47 namespace Media { 48 class RingerModeCallbackImpl; 49 50 enum HapticsStyle { 51 HAPTICS_STYLE_STANDARD = 1, 52 HAPTICS_STYLE_GENTLE, 53 }; 54 55 class SystemSoundManagerImpl : public SystemSoundManager { 56 public: 57 SystemSoundManagerImpl(); 58 ~SystemSoundManagerImpl(); 59 60 int32_t SetRingerMode(const AudioStandard::AudioRingerMode &ringerMode); 61 AudioStandard::AudioRingerMode GetRingerMode() const; 62 std::string GetDefaultRingtoneUri(RingtoneType ringtoneType); 63 std::string GetDefaultSystemToneUri(SystemToneType systemToneType); 64 65 // SystemSoundManager override 66 int32_t SetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 67 RingtoneType ringtoneType) override; 68 std::string GetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 69 RingtoneType ringtoneType) override; 70 std::shared_ptr<RingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 71 RingtoneType ringtoneType) override; 72 std::shared_ptr<RingtonePlayer> GetSpecificRingTonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 73 const RingtoneType ringtoneType, std::string &ringtoneUri) override; 74 75 int32_t SetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 76 SystemToneType systemToneType) override; 77 std::string GetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 78 SystemToneType systemToneType) override; 79 std::shared_ptr<SystemTonePlayer> GetSystemTonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 80 SystemToneType systemToneType) override; 81 82 std::shared_ptr<ToneAttrs> GetDefaultRingtoneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 83 RingtoneType ringtoneType) override; 84 std::vector<std::shared_ptr<ToneAttrs>> GetRingtoneAttrList(const std::shared_ptr<AbilityRuntime::Context> &context, 85 RingtoneType ringtoneType) override; 86 std::shared_ptr<ToneAttrs> GetDefaultSystemToneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 87 SystemToneType systemToneType) override; 88 std::vector<std::shared_ptr<ToneAttrs>> GetSystemToneAttrList( 89 const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) override; 90 std::shared_ptr<ToneAttrs> GetDefaultAlarmToneAttrs( 91 const std::shared_ptr<AbilityRuntime::Context> &context) override; 92 std::vector<std::shared_ptr<ToneAttrs>> GetAlarmToneAttrList( 93 const std::shared_ptr<AbilityRuntime::Context> &context) override; 94 std::string GetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context) override; 95 int32_t SetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 96 int32_t OpenAlarmTone(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 97 int32_t Close(const int32_t &fd) override; 98 std::string AddCustomizedToneByExternalUri(const std::shared_ptr<AbilityRuntime::Context> &context, 99 const std::shared_ptr<ToneAttrs> &toneAttrs, const std::string &externalUri) override; 100 std::string AddCustomizedToneByFd(const std::shared_ptr<AbilityRuntime::Context> &context, 101 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd) override; 102 std::string AddCustomizedToneByFdAndOffset(const std::shared_ptr<AbilityRuntime::Context> &context, 103 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd, const int32_t &offset, 104 const int32_t &length) override; 105 int32_t RemoveCustomizedTone(const std::shared_ptr<AbilityRuntime::Context> &context, 106 const std::string &uri) override; 107 std::string GetRingtoneTitle(const std::string &ringtoneUri); 108 109 int32_t GetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context, 110 ToneHapticsType toneHapticsType, ToneHapticsSettings &settings) override; 111 int32_t SetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context, 112 ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings) override; 113 int32_t GetToneHapticsList(const std::shared_ptr<AbilityRuntime::Context> &context, 114 bool isSynced, std::vector<std::shared_ptr<ToneHapticsAttrs>> &toneHapticsAttrsArray) override; 115 int32_t GetHapticsAttrsSyncedWithTone(const std::shared_ptr<AbilityRuntime::Context> &context, 116 const std::string &toneUri, std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs) override; 117 int32_t OpenToneHaptics(const std::shared_ptr<AbilityRuntime::Context> &context, 118 const std::string &hapticsUri) override; 119 120 std::string GetHapticsUriByStyle(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 121 const std::string &standardHapticsUri, HapticsStyle hapticsStyle); 122 int32_t GetToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 123 const std::string &toneUri, ToneHapticsType toneHapticsType, ToneHapticsSettings &settings); 124 int32_t GetHapticsAttrsSyncedWithTone(const std::string &toneUri, 125 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 126 std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs); 127 int32_t SetToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 128 const std::string &toneUri, ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings); 129 bool CheckVibrateSwitchStatus(); 130 int32_t OpenToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 131 const std::string &uri, int32_t toneType) override; 132 133 private: 134 void InitDefaultUriMap(); 135 void InitDefaultRingtoneUriMap(const std::string &ringtoneJsonPath); 136 void InitDefaultSystemToneUriMap(const std::string &systemToneJsonPath); 137 void InitDefaultToneHapticsMap(); 138 void ReadDefaultToneHaptics(const char *paramName, ToneHapticsType toneHapticsType); 139 std::string GetFullPath(const std::string &originalUri); 140 std::string GetJsonValue(const std::string &jsonPath); 141 142 int32_t AddCustomizedTone(const std::shared_ptr<DataShare::DataShareHelper> &dataShareHelper, 143 const std::shared_ptr<ToneAttrs> &toneAttrs); 144 int32_t WriteUriToDatabase(const std::string &key, const std::string &uri); 145 std::string GetUriFromDatabase(const std::string &key); 146 std::string GetKeyForDatabase(const std::string &systemSoundType, int32_t type); 147 void InitRingerMode(void); 148 void GetCustomizedTone(const std::shared_ptr<ToneAttrs> &toneAttrs); 149 void InitMap(); 150 std::string GetRingtoneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 151 const std::string &type); 152 std::string GetPresetRingToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 153 const std::string &type); 154 int32_t SetNoRingToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 155 RingtoneType ringtoneType); 156 int32_t RemoveSourceTypeForRingTone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 157 RingtoneType ringtoneType, SourceType sourceType); 158 int32_t UpdateRingtoneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 159 RingtoneType ringtoneType, const int32_t &num); 160 std::string GetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 161 const std::string &type); 162 std::string GetNotificationToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 163 std::string GetPresetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 164 const std::string &type); 165 std::string GetPresetNotificationToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 166 int32_t UpdateShotToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 167 SystemToneType systemToneType, const int32_t &num); 168 int32_t UpdateNotificatioToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 169 const int32_t &toneId); 170 int32_t SetNoSystemToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 171 SystemToneType systemToneType); 172 int32_t RemoveSourceTypeForSystemTone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 173 SystemToneType systemToneType, SourceType sourceType); 174 175 bool ConvertToRingtoneType(ToneHapticsType toneHapticsType, RingtoneType &ringtoneType); 176 bool ConvertToSystemToneType(ToneHapticsType toneHapticsType, SystemToneType &systemToneType); 177 std::string ConvertToHapticsFileName(const std::string &fileName); 178 ToneHapticsMode IntToToneHapticsMode(int32_t value); 179 std::string GetCurrentToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 180 ToneHapticsType toneHapticsType); 181 std::unique_ptr<SimcardSettingAsset> GetSimcardSettingAssetByToneHapticsType( 182 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, ToneHapticsType toneHapticsType); 183 184 std::string GetToneSyncedHapticsUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 185 const std::string &toneUri); 186 std::string GetDefaultNonSyncedHapticsUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 187 ToneHapticsType toneHapticsType); 188 std::string GetFirstNonSyncedHapticsUri(); 189 int32_t GetDefaultToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 190 const std::string ¤tToneUri, ToneHapticsType toneHapticsType, ToneHapticsSettings &settings); 191 192 int32_t UpdateToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 193 const std::string &toneUri, ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings); 194 bool GetVibrateTypeByStyle(int standardVibrateType, HapticsStyle hapticsStyle, int &vibrateType); 195 std::unique_ptr<RingtoneAsset> IsPresetRingtone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 196 const std::string &toneUri); 197 int GetStandardVibrateType(int toneType); 198 199 bool IsRingtoneTypeValid(RingtoneType ringtongType); 200 bool IsSystemToneTypeValid(SystemToneType systemToneType); 201 bool IsSystemToneType(const std::unique_ptr<RingtoneAsset> &ringtoneAsset, 202 const SystemToneType &systemToneType); 203 bool IsToneHapticsTypeValid(ToneHapticsType toneHapticsType); 204 205 static int32_t GetCurrentUserId(); 206 static Uri AssembleUri(const std::string &key, std::string tableType = ""); 207 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperProxy(std::string tableType = ""); 208 int32_t GetStringValue(const std::string &key, std::string &value, std::string tableType = ""); 209 void SetExtRingtoneUri(const std::string &uri, const std::string &title, 210 int32_t ringType, int32_t toneType, int32_t changedRows); 211 int32_t SetExtRingToneUri(const std::string &uri, const std::string &title, int32_t toneType); 212 213 std::string systemSoundPath_ = ""; 214 std::mutex uriMutex_; 215 std::mutex playerMutex_; 216 #ifdef SUPPORT_VIBRATOR 217 std::mutex toneHapticsMutex_; 218 #endif 219 std::string mimeType_ = ""; 220 std::string displayName_ = ""; 221 std::unordered_map<RingtoneType, std::string> defaultRingtoneUriMap_; 222 std::unordered_map<SystemToneType, std::string> defaultSystemToneUriMap_; 223 std::unordered_map<ToneHapticsType, std::string> defaultToneHapticsUriMap_; 224 std::shared_ptr<ToneAttrs> ringtoneAttrs_; 225 std::shared_ptr<ToneAttrs> systemtoneAttrs_; 226 std::shared_ptr<ToneAttrs> alarmtoneAttrs_; 227 228 std::atomic<AudioStandard::AudioRingerMode> ringerMode_ = AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL; 229 std::shared_ptr<AudioStandard::AudioGroupManager> audioGroupManager_ = nullptr; 230 std::shared_ptr<AudioStandard::AudioRingerModeCallback> ringerModeCallback_ = nullptr; 231 std::vector<std::shared_ptr<ToneAttrs>> ringtoneAttrsArray_; 232 std::vector<std::shared_ptr<ToneAttrs>> systemtoneAttrsArray_; 233 std::vector<std::shared_ptr<ToneAttrs>> alarmtoneAttrsArray_; 234 }; 235 236 class RingerModeCallbackImpl : public AudioStandard::AudioRingerModeCallback { 237 public: 238 explicit RingerModeCallbackImpl(SystemSoundManagerImpl &systemSoundManagerImpl); 239 virtual ~RingerModeCallbackImpl() = default; 240 void OnRingerModeUpdated(const AudioStandard::AudioRingerMode &ringerMode) override; 241 242 private: 243 SystemSoundManagerImpl &sysSoundMgr_; 244 AudioStandard::AudioRingerMode ringerMode_ { AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL }; 245 }; 246 } // namespace Media 247 } // namespace OHOS 248 #endif // SYSTEM_SOUND_MANAGER_IMPL_H 249