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