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 #include "system_sound_manager_impl.h"
17
18 #include <fstream>
19
20 #include "config_policy_utils.h"
21 #include "file_ex.h"
22 #include "nlohmann/json.hpp"
23
24 #include "media_log.h"
25 #include "media_errors.h"
26 #include "ringtone_player_impl.h"
27 #include "vibrate_type.h"
28 #include "os_account_manager.h"
29 #include "system_tone_player_impl.h"
30 #include "parameter.h"
31 #include "system_sound_manager_utils.h"
32
33 using namespace std;
34 using namespace nlohmann;
35 using namespace OHOS::AbilityRuntime;
36 using namespace OHOS::DataShare;
37
38 namespace {
39 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "SystemSoundManagerImpl"};
40 }
41
42 namespace OHOS {
43 namespace Media {
44 const std::string RING_TONE = "ring_tone";
45 const std::string SYSTEM_TONE = "system_tone";
46 const std::string DEFAULT_SYSTEM_SOUND_PATH = "resource/media/audio/";
47 const std::string DEFAULT_RINGTONE_URI_JSON = "ringtone_incall.json";
48 const std::string DEFAULT_RINGTONE_PATH = "ringtones/";
49 const std::string DEFAULT_SYSTEM_TONE_URI_JSON = "ringtone_sms-notification.json";
50 const std::string DEFAULT_SYSTEM_TONE_PATH = "notifications/";
51 const int STORAGE_MANAGER_MANAGER_ID = 5003;
52
53 const int SUCCESS = 0;
54
55 constexpr int32_t MIN_USER_ACCOUNT = 100;
56 const std::string SETTING_COLUMN_KEYWORD = "KEYWORD";
57 const std::string SETTING_COLUMN_VALUE = "VALUE";
58 const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
59 const std::string SETTING_USER_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_";
60 const std::string SETTING_USER_SECURE_URI_PROXY =
61 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
62 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
63
GetStringValue(const std::string & key,std::string & value,std::string tableType)64 int32_t SystemSoundManagerImpl::GetStringValue(const std::string &key,
65 std::string &value, std::string tableType)
66 {
67 auto helper = CreateDataShareHelperProxy(tableType);
68 if (helper == nullptr) {
69 MEDIA_LOGE("helper return nullptr");
70 return MSERR_INVALID_VAL;
71 }
72 std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
73 DataShare::DataSharePredicates predicates;
74 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
75 Uri uri(AssembleUri(key, tableType));
76 auto resultSet = helper->Query(uri, predicates, columns);
77 helper->Release();
78 if (resultSet == nullptr) {
79 MEDIA_LOGE("helper->Query return nullptr");
80 return MSERR_INVALID_OPERATION;
81 }
82 int32_t count;
83 resultSet->GetRowCount(count);
84 if (count == 0) {
85 MEDIA_LOGW("not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
86 resultSet->Close();
87 return MSERR_INVALID_OPERATION;
88 }
89 int32_t index = 0;
90 resultSet->GoToRow(index);
91 int32_t ret = resultSet->GetString(index, value);
92 if (ret != SUCCESS) {
93 MEDIA_LOGW("resultSet->GetString return not ok, ret=%{public}d", ret);
94 resultSet->Close();
95 return MSERR_INVALID_VAL;
96 }
97 resultSet->Close();
98 return MSERR_OK;
99 }
100
CheckVibrateSwitchStatus()101 bool SystemSoundManagerImpl::CheckVibrateSwitchStatus()
102 {
103 std::string key = "hw_vibrate_when_ringing";
104 std::string valueStr;
105 std::string tableType = "system";
106 int32_t ret = GetStringValue(key, valueStr, tableType);
107 if (ret != MSERR_OK) {
108 return true; // default status is open
109 }
110 MEDIA_LOGI("vibrare switch value %{public}s", valueStr.c_str());
111 return valueStr == "1"; // 1 for open, 0 for close
112 }
113
AssembleUri(const std::string & key,std::string tableType)114 Uri SystemSoundManagerImpl::AssembleUri(const std::string &key, std::string tableType)
115 {
116 int32_t currentuserId = SystemSoundManagerUtils::GetCurrentUserId();
117 if (currentuserId < MIN_USER_ACCOUNT) {
118 currentuserId = MIN_USER_ACCOUNT;
119 }
120 std::string settingSystemUrlProxy = "";
121
122 // deal with multi useraccount table
123 if (currentuserId > 0 && tableType == "system") {
124 settingSystemUrlProxy = SETTING_USER_URI_PROXY + std::to_string(currentuserId) + "?Proxy=true";
125 Uri uri(settingSystemUrlProxy + "&key=" + key);
126 return uri;
127 } else if (currentuserId > 0 && tableType == "secure") {
128 settingSystemUrlProxy = SETTING_USER_SECURE_URI_PROXY + std::to_string(currentuserId) + "?Proxy=true";
129 Uri uri(settingSystemUrlProxy + "&key=" + key);
130 return uri;
131 }
132 Uri uri(SETTING_URI_PROXY + "&key=" + key);
133 return uri;
134 }
135
CreateDataShareHelperProxy(std::string tableType)136 std::shared_ptr<DataShare::DataShareHelper> SystemSoundManagerImpl::CreateDataShareHelperProxy(std::string
137 tableType)
138 {
139 int32_t currentuserId = SystemSoundManagerUtils::GetCurrentUserId();
140 if (currentuserId < MIN_USER_ACCOUNT) {
141 currentuserId = MIN_USER_ACCOUNT;
142 }
143 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
144 if (saManager == nullptr) {
145 MEDIA_LOGE("saManager return nullptr");
146 return nullptr;
147 }
148 auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
149 if (remoteObj == nullptr) {
150 MEDIA_LOGE("saManager->GetSystemAbility return nullptr");
151 return nullptr;
152 }
153 std::shared_ptr<DataShare::DataShareHelper> helper = nullptr;
154 std::string SettingSystemUrlProxy = "";
155
156 // deal with multi useraccount table
157 if (currentuserId > 0 && tableType == "system") {
158 SettingSystemUrlProxy =
159 SETTING_USER_URI_PROXY + std::to_string(currentuserId) + "?Proxy=true";
160 helper = DataShare::DataShareHelper::Creator(remoteObj, SettingSystemUrlProxy, SETTINGS_DATA_EXT_URI);
161 } else if (currentuserId > 0 && tableType == "secure") {
162 SettingSystemUrlProxy =
163 SETTING_USER_SECURE_URI_PROXY + std::to_string(currentuserId) + "?Proxy=true";
164 helper = DataShare::DataShareHelper::Creator(remoteObj, SettingSystemUrlProxy, SETTINGS_DATA_EXT_URI);
165 } else {
166 helper = DataShare::DataShareHelper::Creator(remoteObj, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
167 }
168 if (helper == nullptr) {
169 MEDIA_LOGW("helper is nullptr, uri=%{public}s", SettingSystemUrlProxy.c_str());
170 return nullptr;
171 }
172 return helper;
173 }
174
175 // Ringer mode callback class symbols
RingerModeCallbackImpl(SystemSoundManagerImpl & systemSoundManagerImpl)176 RingerModeCallbackImpl::RingerModeCallbackImpl(SystemSoundManagerImpl &systemSoundManagerImpl)
177 : sysSoundMgr_(systemSoundManagerImpl) {}
178
OnRingerModeUpdated(const AudioStandard::AudioRingerMode & ringerMode)179 void RingerModeCallbackImpl::OnRingerModeUpdated(const AudioStandard::AudioRingerMode &ringerMode)
180 {
181 ringerMode_ = ringerMode;
182 int32_t result = sysSoundMgr_.SetRingerMode(ringerMode_);
183 if (result == MSERR_OK && ringerMode_ == AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) {
184 SystemSoundVibrator::StopVibrator();
185 }
186 }
187 } // namesapce AudioStandard
188 } // namespace OHOS