!1411 fix: copy database key and value in the update scene

Merge pull request !1411 from cjw123qq/1030
This commit is contained in:
openharmony_ci 2024-11-01 08:49:46 +00:00 committed by Gitee
commit c16f7cb4f8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 117 additions and 1 deletions

View File

@ -153,6 +153,9 @@ void PowerMgrService::RegisterBootCompletedCallback()
}
auto powerStateMachine = power->GetPowerStateMachine();
SettingHelper::UpdateCurrentUserId(); // update setting user id before get setting values
#ifdef POWER_PICKUP_ENABLE
SettingHelper::CopyDataForUpdateScene();
#endif
#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
power->PowerConnectStatusInit();
power->UpdateSettingInvalidDisplayOffTime(); // update setting value if invalid before register

View File

@ -36,7 +36,13 @@ void SettingHelper::UpdateCurrentUserId()
SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
settingProvider.UpdateCurrentUserId();
}
#ifdef POWER_PICKUP_ENABLE
void SettingHelper::CopyDataForUpdateScene()
{
SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
settingProvider.CopyDataForUpdateScene();
}
#endif
bool SettingHelper::IsSettingKeyValid(const std::string& key)
{
return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(key);

View File

@ -31,6 +31,9 @@ public:
ENABLE = 1,
};
static void UpdateCurrentUserId();
#ifdef POWER_PICKUP_ENABLE
static void CopyDataForUpdateScene();
#endif
static void UnregisterSettingObserver(sptr<SettingObserver>& observer);
static bool IsDisplayOffTimeSettingValid();
static bool IsSuspendSourcesSettingValid();

View File

@ -327,6 +327,23 @@ HWTEST_F(PowerMgrUtilTest, SettingProvider005, TestSize.Level0)
POWER_HILOGD(LABEL_TEST, "SettingProvider005::fun is end!");
}
/**
* @tc.name: SettingProvider006
* @tc.desc: test CopyDataForUpdateScene func
* @tc.type: FUNC
*/
HWTEST_F(PowerMgrUtilTest, SettingProvider006, TestSize.Level0)
{
POWER_HILOGD(LABEL_TEST, "SettingProvider006::fun is start!");
auto& settingProvider = SettingProvider::GetInstance(OHOS::POWER_MANAGER_SERVICE_ID);
settingProvider.CopyDataForUpdateScene();
static constexpr const char* SETTING_POWER_WAKEUP_PICKUP_KEY {"settings.power.wakeup_pick_up"};
bool isValidKeyGlobal = settingProvider.IsValidKeyGlobal(SETTING_POWER_WAKEUP_PICKUP_KEY);
bool isValidKeyUser = settingProvider.IsValidKey(SETTING_POWER_WAKEUP_PICKUP_KEY);
EXPECT_FALSE(isValidKeyGlobal && !isValidKeyUser);
POWER_HILOGD(LABEL_TEST, "SettingProvider006::fun is end!");
}
/**
* @tc.name: Sysparam001
* @tc.desc: test GetIntValue in proxy

View File

@ -40,6 +40,7 @@ public:
ErrCode RegisterObserver(const sptr<SettingObserver>& observer);
ErrCode UnregisterObserver(const sptr<SettingObserver>& observer);
void UpdateCurrentUserId();
void CopyDataForUpdateScene();
protected:
~SettingProvider() override;
@ -78,6 +79,10 @@ private:
static Uri AssembleUri(const std::string& key);
static bool IsNeedMultiUser(const std::string& key);
static std::string ReplaceUserIdForUri(int32_t userId);
bool IsNeedDataMigrationCopy();
void DataMigrationCopy();
ErrCode GetStringValueGlobal(const std::string& key, std::string& value);
bool IsValidKeyGlobal(const std::string& key);
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -62,6 +62,13 @@ SettingProvider& SettingProvider::GetInstance(int32_t systemAbilityId)
return *instance_;
}
void SettingProvider::CopyDataForUpdateScene()
{
if (IsNeedDataMigrationCopy()) {
DataMigrationCopy();
}
}
ErrCode SettingProvider::GetIntValue(const std::string& key, int32_t& value)
{
int64_t valueLong;
@ -122,6 +129,17 @@ bool SettingProvider::IsValidKey(const std::string& key)
return (ret != ERR_NAME_NOT_FOUND) && (!value.empty());
}
bool SettingProvider::IsValidKeyGlobal(const std::string& key)
{
std::string value;
ErrCode ret = GetStringValueGlobal(key, value);
if (!value.empty()) {
POWER_HILOGI(COMP_UTILS, "the getValueGlobal is:%{public}s", value.c_str());
}
POWER_HILOGI(COMP_UTILS, "the getRetGlobal is:%{public}u", ret);
return (ret != ERR_NAME_NOT_FOUND) && (!value.empty());
}
sptr<SettingObserver> SettingProvider::CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func)
{
sptr<SettingObserver> observer = new SettingObserver();
@ -300,6 +318,70 @@ Uri SettingProvider::AssembleUri(const std::string& key)
return uri;
}
bool SettingProvider::IsNeedDataMigrationCopy()
{
bool isNeedMigrationCopy = false;
if (IsValidKeyGlobal(SETTING_POWER_WAKEUP_PICKUP_KEY)) {
if (!IsValidKey(SETTING_POWER_WAKEUP_PICKUP_KEY)) {
isNeedMigrationCopy = true;
}
}
POWER_HILOGI(COMP_UTILS, "IsNeedDataMigrationCopy:(%{public}d)", isNeedMigrationCopy);
return isNeedMigrationCopy;
}
void SettingProvider::DataMigrationCopy()
{
std::string value;
GetStringValueGlobal(SETTING_POWER_WAKEUP_DOUBLE_KEY, value);
PutStringValue(SETTING_POWER_WAKEUP_DOUBLE_KEY, value);
GetStringValueGlobal(SETTING_POWER_WAKEUP_PICKUP_KEY, value);
PutStringValue(SETTING_POWER_WAKEUP_PICKUP_KEY, value);
GetStringValueGlobal(SETTING_POWER_WAKEUP_SOURCES_KEY, value);
PutStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, value);
}
ErrCode SettingProvider::GetStringValueGlobal(const std::string& key, std::string& value)
{
std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
auto helper = DataShare::DataShareHelper::Creator(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
if (helper == nullptr) {
IPCSkeleton::SetCallingIdentity(callingIdentity);
return ERR_NO_INIT;
}
std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
DataShare::DataSharePredicates predicates;
predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
Uri uri(SETTING_URI_PROXY + "&key=" + key);
auto resultSet = helper->Query(uri, predicates, columns);
ReleaseDataShareHelper(helper);
if (resultSet == nullptr) {
POWER_HILOGE(COMP_UTILS, "helper->Query return nullptr");
IPCSkeleton::SetCallingIdentity(callingIdentity);
return ERR_INVALID_OPERATION;
}
int32_t count;
resultSet->GetRowCount(count);
if (count == 0) {
POWER_HILOGW(COMP_UTILS, "not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
IPCSkeleton::SetCallingIdentity(callingIdentity);
resultSet->Close();
return ERR_NAME_NOT_FOUND;
}
const int32_t INDEX = 0;
resultSet->GoToRow(INDEX);
int32_t ret = resultSet->GetString(INDEX, value);
if (ret != NativeRdb::E_OK) {
POWER_HILOGW(COMP_UTILS, "resultSet->GetString return not ok, ret=%{public}d", ret);
IPCSkeleton::SetCallingIdentity(callingIdentity);
resultSet->Close();
return ERR_INVALID_VALUE;
}
resultSet->Close();
IPCSkeleton::SetCallingIdentity(callingIdentity);
return ERR_OK;
}
bool SettingProvider::IsNeedMultiUser(const std::string& key)
{
std::vector<std::string> needMultiUserStrVec {