diff --git a/services/audio_policy/server/infra/datashare/include/audio_setting_provider.h b/services/audio_policy/server/infra/datashare/include/audio_setting_provider.h index 624b4f63c956776143fb89a6fc0e0879b111c473..d40698ceec8306f88339557259197dfbefd8ade5 100644 --- a/services/audio_policy/server/infra/datashare/include/audio_setting_provider.h +++ b/services/audio_policy/server/infra/datashare/include/audio_setting_provider.h @@ -33,6 +33,12 @@ namespace AudioStandard { constexpr int32_t MAX_STRING_LENGTH = 10; constexpr int32_t MIN_USER_ACCOUNT = 100; +struct IntValueInfo { + std::string key; + int32_t defaultValue; + int32_t value; +}; + class AudioSettingObserver : public AAFwk::DataAbilityObserverStub { public: AudioSettingObserver() = default; @@ -82,6 +88,25 @@ public: std::string ParseFirstOfKey(size_t &pos, size_t len, std::string input); std::string ParseSecondOfValue(size_t &pos, size_t len, std::string input); +// rewrite database operations +public: + void GetIntValues(std::vector &infos, std::string tableType); + ErrCode PutIntValues(std::vector& infos, std::string tableType); +private: + void GetIntValuesInner(std::vector &infos, std::string tableType); + ErrCode GetIntValueInner(std::shared_ptr helper, + std::string key, std::string tableType, int32_t &res); + + ErrCode PutIntValuesInner(std::vector &infos, std::string tableType); + ErrCode PutIntValueInner(std::shared_ptr helper, + std::string key, std::string value, std::string tableType); + +// tools +public: + void TrimLeft(std::string &str); + int32_t StringToInt32(const std::string &str, int32_t &result); + int64_t StringToInt64(const std::string &str, int64_t &result); + protected: ~AudioSettingProvider() override; diff --git a/services/audio_policy/server/infra/datashare/src/audio_setting_provider.cpp b/services/audio_policy/server/infra/datashare/src/audio_setting_provider.cpp index 608378783c60d59e3abe47a0110fe5299ab877bc..1ef80aae0cad255ab188b77e37510bad7fe04007 100644 --- a/services/audio_policy/server/infra/datashare/src/audio_setting_provider.cpp +++ b/services/audio_policy/server/infra/datashare/src/audio_setting_provider.cpp @@ -22,6 +22,7 @@ #include "audio_errors.h" #include "system_ability_definition.h" #include "audio_utils.h" +#include "audio_log.h" namespace OHOS { namespace AudioStandard { @@ -520,5 +521,168 @@ Uri AudioSettingProvider::AssembleUri(const std::string &key, std::string tableT Uri uri(SETTING_URI_PROXY + "&key=" + key); return uri; } + +// rewrite database operations +void AudioSettingProvider::TrimLeft(std::string &str) +{ + if (str.empty()) { + return; + } + size_t start = str.find_first_not_of(" \t\n\r"); + if (start != std::string::npos) { + str =str.substr(start); + } else { + str.clear(); + } +} + +int32_t AudioSettingProvider::StringToInt32(const std::string &str, int32_t &result) +{ + if (str.empty()) { + return ERROR; + } + int32_t oldValue = result; + + std::string s = str; + TrimLeft(s); + if (s.empty()) { + return ERROR; + } + + const auto *first = str.data(); + const auto *last = first +str.size(); + std::from_chars_result res = std::from_chars(first, last, result); + if (res.ec == std::errc{} && res.ptr ==last) { + return SUCCESS; + } else { + result = oldValue; + return ERROR; + } +} + +int64_t AudioSettingProvider::StringToInt64(const std::string &str, int64_t &result) +{ + if (str.empty()) { + return ERROR; + } + int64_t oldValue = result; + + std::string s = str; + TrimLeft(s); + if (s.empty()) { + return ERROR; + } + + const auto *first = str.data(); + const auto *last = first +str.size(); + std::from_chars_result res = std::from_chars(first, last, result); + if (res.ec == std::errc{} && res.ptr ==last) { + return SUCCESS; + } else { + result = oldValue; + return ERROR; + } +} + +void AudioSettingProvider::GetIntValues(std::vector &infos, std::string tableType) +{ + std::string callingIdentity = IPCSkeleton::ResetCallingIdentity(); + for (auto &info : infos) { + info.value = info.defaultValue; + } + GetIntValuesInner(infos, tableType); + IPCSkeleton::SetCallingIdentity(callingIdentity); +} + +void AudioSettingProvider::GetIntValuesInner(std::vector &infos, std::string tableType) +{ + auto helper = CreateDataShareHelper(tableType); + CHECK_AND_RETURN_LOG(helper != nullptr, "helper is null"); + for (auto &info : infos) { + int32_t res = 0; + int32_t ret = GetIntValueInner(helper, info.key, tableType, res); + CHECK_AND_CONTINUE(ret == SUCCESS); + info.value = res; + } + ReleaseDataShareHelper(helper); +} + +ErrCode AudioSettingProvider::GetIntValueInner(std::shared_ptr helper, + std::string key, std::string tableType, int32_t &res) +{ + CHECK_AND_RETURN_RET_LOG(helper != nullptr, ERR_NO_INIT, "helper is null"); + std::vector columns = {SETTING_COLUMN_VALUE}; + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); + Uri uri(AssembleUri(key, tableType)); + auto resultSet = helper->Query(uri, predicates, columns); + CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, ERR_INVALID_OPERATION, "helper->Query return nullptr"); + + int32_t count; + resultSet->GetRowCount(count); + if (count == 0) { + AUDIO_WARNING_LOG("not found value, key=%{public}s, uri=%{public}s, count=%{public}d", + key.c_str(), uri.ToString().c_str(), count); + resultSet->Close(); + return ERR_NAME_NOT_FOUND; + } + const int32_t INDEX = 0; + resultSet->GoToRow(INDEX); + + std::string value; + int32_t ret = resultSet->GetString(INDEX, value); + if (ret != SUCCESS) { + AUDIO_WARNING_LOG("resultSet->GetString return not ok, ret=%{public}d", ret); + resultSet->Close(); + return ERR_INVALID_VALUE; + } + + AUDIO_INFO_LOG("Read audio_info_database with key: %{public}s value: %{public}s in uri=%{public}s ", + key.c_str(), value.c_str(), uri.ToString().c_str()); + ret = StringToInt32(value, res); + resultSet->Close(); + return ret; +} + +ErrCode AudioSettingProvider::PutIntValues(std::vector &infos, std::string tableType) +{ + std::string callingIdentity = IPCSkeleton::ResetCallingIdentity(); + int32_t ret = PutIntValuesInner(infos, tableType); + IPCSkeleton::SetCallingIdentity(callingIdentity); + return ret; +} + +ErrCode AudioSettingProvider::PutIntValuesInner(std::vector &infos, std::string tableType) +{ + auto helper = CreateDataShareHelper(tableType); + CHECK_AND_RETURN_RET_LOG(helper != nullptr, ERR_NO_INIT, "helper is null"); + for (auto &info : infos) { + PutIntValueInner(helper, info.key, std::to_string(info.value), tableType); + } + ReleaseDataShareHelper(helper); + return SUCCESS; +} + +ErrCode AudioSettingProvider::PutIntValueInner(std::shared_ptr helper, + std::string key, std::string value, std::string tableType) +{ + CHECK_AND_RETURN_RET_LOG(helper != nullptr, ERR_NO_INIT, "helper is null"); + AUDIO_INFO_LOG("Write audio_info_database with key: %{public}s value: %{public}s", key.c_str(), value.c_str()); + DataShare::DataShareValueObject keyObj(key); + DataShare::DataShareValueObject valueObj(value); + DataShare::DataShareValuesBucket bucket; + bucket.Put(SETTING_COLUMN_KEYWORD, keyObj); + bucket.Put(SETTING_COLUMN_VALUE, valueObj); + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); + Uri uri(AssembleUri(key, tableType)); + if (helper->Update(uri, predicates, bucket) <= 0) { + AUDIO_INFO_LOG("audio_info_database no data exist, insert one row"); + helper->Insert(uri, bucket); + } + helper->NotifyChange(AssembleUri(key, tableType)); + return SUCCESS; +} + } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/test/unittest/audio_setting_provider_unit_test/src/audio_setting_provider_unit_test.cpp b/services/audio_policy/test/unittest/audio_setting_provider_unit_test/src/audio_setting_provider_unit_test.cpp index a78f5e0edac77427687dc16e0d904da9056173bd..5c87d09b63fb26e5ef1defa5e52f703bd278d81e 100644 --- a/services/audio_policy/test/unittest/audio_setting_provider_unit_test/src/audio_setting_provider_unit_test.cpp +++ b/services/audio_policy/test/unittest/audio_setting_provider_unit_test/src/audio_setting_provider_unit_test.cpp @@ -1154,5 +1154,76 @@ HWTEST(AudioSettingProviderUnitTest, ParseJsonArray_020, TestSize.Level1) EXPECT_FALSE(result.empty()); } + +/** + * @tc.name : Test StringToInt32 API + * @tc.type : FUNC + * @tc.number: StringToInt32 + * @tc.desc : Test StringToInt32 + */ +HWTEST(AudioSettingProviderUnitTest, StringToInt32, TestSize.Level1) +{ + AudioSettingProvider* pder = nullptr; + int32_t systemAbilityId = 1; + pder = &AudioSettingProvider::GetInstance(systemAbilityId); + ASSERT_TRUE(pder != nullptr); + + int32_t value = 0; + EXPECT_EQ(pder->StringToInt32("1", value), SUCCESS); + EXPECT_EQ(pder->StringToInt32(" 1", value), SUCCESS); + EXPECT_NE(pder->StringToInt32(" ", value), SUCCESS); + EXPECT_NE(pder->StringToInt32("", value), SUCCESS); + EXPECT_NE(pder->StringToInt32("1a", value), SUCCESS); + EXPECT_NE(pder->StringToInt32("99999999999999", value), SUCCESS); + EXPECT_NE(pder->StringToInt32("a1", value), SUCCESS); +} + +/** + * @tc.name : Test StringToInt64 API + * @tc.type : FUNC + * @tc.number: StringToInt64 + * @tc.desc : Test StringToInt64 + */ +HWTEST(AudioSettingProviderUnitTest, StringToInt64, TestSize.Level1) +{ + AudioSettingProvider* pder = nullptr; + int32_t systemAbilityId = 1; + pder = &AudioSettingProvider::GetInstance(systemAbilityId); + ASSERT_TRUE(pder != nullptr); + + int64_t value = 0; + EXPECT_EQ(pder->StringToInt64("1", value), SUCCESS); + EXPECT_EQ(pder->StringToInt64(" 1", value), SUCCESS); + EXPECT_NE(pder->StringToInt64(" ", value), SUCCESS); + EXPECT_NE(pder->StringToInt64("1a", value), SUCCESS); + EXPECT_NE(pder->StringToInt64("99999999999999", value), SUCCESS); + EXPECT_NE(pder->StringToInt64("a1", value), SUCCESS); +} + +/** + * @tc.name : Test TrimLeft API + * @tc.type : FUNC + * @tc.number: TrimLeft + * @tc.desc : Test TrimLeft + */ +HWTEST(AudioSettingProviderUnitTest, TrimLeft, TestSize.Level1) +{ + AudioSettingProvider* pder = nullptr; + int32_t systemAbilityId = 1; + pder = &AudioSettingProvider::GetInstance(systemAbilityId); + ASSERT_TRUE(pder != nullptr); + + std::string value = ""; + pder->TrimLeft(value); + EXPECT_EQ(value, ""); + + value = " "; + pder->TrimLeft(value); + EXPECT_EQ(value, ""); + + value = " 1"; + pder->TrimLeft(value); + EXPECT_EQ(value, "1"); +} } // namespace AudioStandard } // namespace OHOS