mirror of
https://gitee.com/openharmony/applications_settings
synced 2024-11-26 16:10:49 +00:00
add ffi settings
Signed-off-by: dinghong <dinghong10@huawei.com> Change-Id: I72e1058e21988cda6f8d3877abd795bac0bad855
This commit is contained in:
parent
45bdf8eca4
commit
d6d8b3fe7f
@ -38,7 +38,8 @@
|
||||
},
|
||||
"build": {
|
||||
"sub_component": [
|
||||
"//applications/standard/settings/napi/settings:settings"
|
||||
"//applications/standard/settings/napi/settings:settings",
|
||||
"//applications/standard/settings/cj/settings:cj_settings_ffi"
|
||||
],
|
||||
"inner_kits": [],
|
||||
"test": []
|
||||
|
44
cj/settings/BUILD.gn
Normal file
44
cj/settings/BUILD.gn
Normal file
@ -0,0 +1,44 @@
|
||||
# Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("cj_settings_ffi") {
|
||||
include_dirs = []
|
||||
|
||||
sources = [
|
||||
"src/cj_settings.cpp",
|
||||
"src/cj_settings_observer.cpp",
|
||||
"src/settings_ffi.cpp",
|
||||
]
|
||||
|
||||
deps = []
|
||||
|
||||
external_deps = [
|
||||
"ability_runtime:ability_manager",
|
||||
"ability_runtime:abilitykit_native",
|
||||
"ability_runtime:data_ability_helper",
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"hilog:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"napi:ace_napi",
|
||||
"napi:cj_bind_ffi",
|
||||
"napi:cj_bind_native",
|
||||
"os_account:os_account_innerkits",
|
||||
]
|
||||
|
||||
innerapi_tags = [ "platformsdk" ]
|
||||
part_name = "settings"
|
||||
subsystem_name = "applications"
|
||||
}
|
293
cj/settings/src/cj_settings.cpp
Normal file
293
cj/settings/src/cj_settings.cpp
Normal file
@ -0,0 +1,293 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "cj_settings.h"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "cj_common_ffi.h"
|
||||
#include "cj_settings_log.h"
|
||||
#include "cj_settings_utils.h"
|
||||
#include "os_account_manager.h"
|
||||
#include "securec.h"
|
||||
#include "uri.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
const std::string SETTINGS_DATA_BASE_URI = "dataability:///com.ohos.settingsdata.DataAbility";
|
||||
const std::string SETTINGS_DATA_FIELD_KEYWORD = "KEYWORD";
|
||||
const std::string SETTINGS_DATA_FIELD_VALUE = "VALUE";
|
||||
const int32_t PERMISSION_EXCEPTION_CODE = 201;
|
||||
const int32_t PERMISSION_DENIED_CODE = -2;
|
||||
const int32_t USERID_HELPER_NUMBER = 100;
|
||||
const int32_t PARAM_ERROR = 401;
|
||||
const int32_t MEMORY_CODE = 14700104;
|
||||
|
||||
char* TransformFromString(std::string str, int32_t* ret)
|
||||
{
|
||||
int64_t len = str.size() + 1;
|
||||
char* retValue = static_cast<char *>(malloc(len));
|
||||
if (retValue == nullptr) {
|
||||
*ret = MEMORY_CODE;
|
||||
return nullptr;
|
||||
}
|
||||
*ret = memcpy_s(retValue, len, str.c_str(), len);
|
||||
if (*ret != 0) {
|
||||
*ret = MEMORY_CODE;
|
||||
}
|
||||
return retValue;
|
||||
}
|
||||
|
||||
bool CheckoutStatus(int32_t status, int32_t* result)
|
||||
{
|
||||
if (status >= 0) {
|
||||
*result = 0;
|
||||
return true;
|
||||
}
|
||||
*result = status;
|
||||
if (status == PERMISSION_DENIED_CODE) {
|
||||
*result = PERMISSION_EXCEPTION_CODE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string GetUserIdStr()
|
||||
{
|
||||
std::vector<int32_t> tmpId;
|
||||
OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(tmpId);
|
||||
std::string tmpIdStr = "100";
|
||||
if (tmpId.size() > 0 && tmpId[0] >= 0) {
|
||||
tmpIdStr = std::to_string(tmpId[0]);
|
||||
} else {
|
||||
LOGE("userid is invalid, use id 100 instead");
|
||||
}
|
||||
return tmpIdStr;
|
||||
}
|
||||
|
||||
bool IsTableNameInvalid(std::string tableName)
|
||||
{
|
||||
if (tableName != "global" && tableName != "system" && tableName != "secure") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr)
|
||||
{
|
||||
if (std::stoi(idStr) < USERID_HELPER_NUMBER) {
|
||||
idStr = "100";
|
||||
}
|
||||
if (tableName == "global") {
|
||||
std::string retStr =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=" + keyStr;
|
||||
return retStr;
|
||||
} else if (tableName == "system") {
|
||||
std::string retStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_" +
|
||||
idStr + "?Proxy=true&key=" + keyStr;
|
||||
return retStr;
|
||||
} else if (tableName == "secure") {
|
||||
std::string retStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" +
|
||||
idStr + "?Proxy=true&key=" + keyStr;
|
||||
return retStr;
|
||||
} else {
|
||||
// return global uri
|
||||
std::string retStr =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=" + keyStr;
|
||||
return retStr;
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetProxyUriStr(std::string tableName, std::string idStr)
|
||||
{
|
||||
if (std::stoi(idStr) < USERID_HELPER_NUMBER) {
|
||||
idStr = "100";
|
||||
}
|
||||
if (tableName == "global") {
|
||||
std::string retStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
|
||||
return retStr;
|
||||
} else if (tableName == "system") {
|
||||
std::string retStr =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_" + idStr + "?Proxy=true";
|
||||
return retStr;
|
||||
} else {
|
||||
std::string retStr =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" + idStr + "?Proxy=true";
|
||||
return retStr;
|
||||
}
|
||||
}
|
||||
|
||||
bool SetValueExecuteExt(SettingsInfo* info, const std::string setValue, int32_t* ret)
|
||||
{
|
||||
if (info->dataShareHelper == nullptr) {
|
||||
LOGE("helper is null");
|
||||
*ret = 0;
|
||||
return false;
|
||||
}
|
||||
OHOS::DataShare::DataShareValuesBucket val;
|
||||
val.Put(SETTINGS_DATA_FIELD_KEYWORD, info->key);
|
||||
val.Put(SETTINGS_DATA_FIELD_VALUE, setValue);
|
||||
std::string tmpIdStr = GetUserIdStr();
|
||||
std::string strUri = GetStageUriStr(info->tableName, tmpIdStr, info->key);
|
||||
LOGD("Set uri : %{public}s, key: %{public}s", strUri.c_str(), info->key.c_str());
|
||||
OHOS::Uri uri(strUri);
|
||||
|
||||
OHOS::DataShare::DataSharePredicates predicates;
|
||||
predicates.EqualTo(SETTINGS_DATA_FIELD_KEYWORD, info->key);
|
||||
|
||||
int32_t retInt = info->dataShareHelper->Update(uri, predicates, val);
|
||||
LOGD("update ret: %{public}d", retInt);
|
||||
if (retInt <= 0) {
|
||||
retInt = info->dataShareHelper->Insert(uri, val);
|
||||
LOGD("insert ret: %{public}d", retInt);
|
||||
}
|
||||
return CheckoutStatus(retInt, ret);
|
||||
}
|
||||
|
||||
std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper(
|
||||
OHOS::AbilityRuntime::Context* context, std::string tableName)
|
||||
{
|
||||
std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = nullptr;
|
||||
std::string tmpIdStr = GetUserIdStr();
|
||||
std::string strUri = "datashare:///com.ohos.settingsdata.DataAbility";
|
||||
std::string strProxyUri = GetProxyUriStr(tableName, tmpIdStr);
|
||||
OHOS::Uri proxyUri(strProxyUri);
|
||||
LOGD("<Ver-11-14> strProxyUri: %{public}s", strProxyUri.c_str());
|
||||
dataShareHelper = OHOS::DataShare::DataShareHelper::Creator(context->GetToken(), strProxyUri, strUri);
|
||||
return dataShareHelper;
|
||||
}
|
||||
|
||||
bool Settings::SetValue(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* value, const char* domainName, int32_t* ret)
|
||||
{
|
||||
if (context == nullptr || name == nullptr || value == nullptr) {
|
||||
LOGE("Invalid parameter.");
|
||||
*ret = PARAM_ERROR;
|
||||
return false;
|
||||
}
|
||||
SettingsInfo info;
|
||||
info.key = name;
|
||||
if (domainName == nullptr) {
|
||||
info.tableName = "global";
|
||||
} else {
|
||||
info.tableName = domainName;
|
||||
if (IsTableNameInvalid(info.tableName)) {
|
||||
LOGE("Invalid domainName.");
|
||||
*ret = PARAM_ERROR;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
info.dataShareHelper = GetDataShareHelper(context, info.tableName);
|
||||
return SetValueExecuteExt(&info, value, ret);
|
||||
}
|
||||
|
||||
void QueryValue(SettingsInfo* info, OHOS::Uri uri)
|
||||
{
|
||||
std::vector<std::string> columns;
|
||||
columns.push_back(SETTINGS_DATA_FIELD_VALUE);
|
||||
|
||||
DataShare::DataSharePredicates predicates;
|
||||
predicates.EqualTo(SETTINGS_DATA_FIELD_KEYWORD, info->key);
|
||||
|
||||
DataShare::DatashareBusinessError businessError;
|
||||
std::shared_ptr<DataShare::DataShareResultSet> resultset = nullptr;
|
||||
resultset = info->dataShareHelper->Query(uri, predicates, columns, &businessError);
|
||||
int32_t errorCode = businessError.GetCode();
|
||||
if (resultset == nullptr) {
|
||||
LOGD("Get value is empty");
|
||||
info->status = (errorCode == 0) ? -1 : errorCode;
|
||||
return;
|
||||
}
|
||||
int numRows = 0;
|
||||
resultset->GetRowCount(numRows);
|
||||
if (errorCode != 0) {
|
||||
info->status = errorCode;
|
||||
} else if (numRows <= 0) {
|
||||
info->status = -1;
|
||||
} else {
|
||||
std::string val;
|
||||
int32_t columnIndex = 0;
|
||||
resultset->GoToFirstRow();
|
||||
resultset->GetString(columnIndex, val);
|
||||
info->value = val;
|
||||
info->status = 0;
|
||||
}
|
||||
resultset->Close();
|
||||
}
|
||||
|
||||
void GetValueExecuteExt(SettingsInfo* info)
|
||||
{
|
||||
if (info->dataShareHelper == nullptr) {
|
||||
LOGE("dataShareHelper is empty");
|
||||
info->status = -1;
|
||||
return;
|
||||
}
|
||||
std::string tmpIdStr = GetUserIdStr();
|
||||
std::string strUri = GetStageUriStr(info->tableName, tmpIdStr, info->key);
|
||||
LOGD("Get uri : %{public}s, key: %{public}s", strUri.c_str(), info->key.c_str())
|
||||
OHOS::Uri uri(strUri);
|
||||
QueryValue(info, uri);
|
||||
}
|
||||
|
||||
char* Settings::GetValue(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* value, const char* domainName, int32_t* ret)
|
||||
{
|
||||
if (context == nullptr || name == nullptr || value == nullptr) {
|
||||
LOGE("Invalid parameter.");
|
||||
*ret = PARAM_ERROR;
|
||||
return nullptr;
|
||||
}
|
||||
SettingsInfo info;
|
||||
if (domainName == nullptr) {
|
||||
info.tableName = "global";
|
||||
} else {
|
||||
info.tableName = domainName;
|
||||
if (IsTableNameInvalid(info.tableName)) {
|
||||
LOGE("Invalid domainName.");
|
||||
*ret = PARAM_ERROR;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
info.key = name;
|
||||
info.dataShareHelper = GetDataShareHelper(context, info.tableName);
|
||||
GetValueExecuteExt(&info);
|
||||
int64_t len = info.value.size();
|
||||
if (len <= 0) {
|
||||
CheckoutStatus(info.status, ret);
|
||||
return nullptr;
|
||||
}
|
||||
return TransformFromString(info.value, ret);
|
||||
}
|
||||
|
||||
char* Settings::GetUriSync(const char* name, const char* tableName)
|
||||
{
|
||||
int32_t tempErrorCode = 0;
|
||||
if (name == nullptr) {
|
||||
LOGE("Invalid parameter.");
|
||||
return nullptr;
|
||||
}
|
||||
std::string uriStr = name;
|
||||
if (tableName == nullptr) {
|
||||
uriStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=" + uriStr;
|
||||
return TransformFromString(uriStr, &tempErrorCode);
|
||||
} else {
|
||||
std::string tmpIdStr = GetUserIdStr();
|
||||
std::string strUri = GetStageUriStr(tableName, tmpIdStr, uriStr);
|
||||
LOGD("Set uri : %{public}s, key: %{public}s", strUri.c_str(), uriStr.c_str());
|
||||
return TransformFromString(strUri, &tempErrorCode);
|
||||
}
|
||||
}
|
||||
} // namespace CJSettings
|
||||
} // namespace CJSystemapi
|
||||
} // namespace OHOS
|
44
cj/settings/src/cj_settings.h
Normal file
44
cj/settings/src/cj_settings.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_CJ_SETTINGS_H
|
||||
#define OHOS_CJ_SETTINGS_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include "context.h"
|
||||
#include "datashare_helper.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
|
||||
class Settings {
|
||||
public:
|
||||
static bool SetValue(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret);
|
||||
static char* GetValue(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret);
|
||||
static char* GetUriSync(const char* name, const char* tableName);
|
||||
};
|
||||
std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper(
|
||||
OHOS::AbilityRuntime::Context* context, std::string tableName);
|
||||
std::string GetUserIdStr();
|
||||
std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr);
|
||||
} // namespace CJSettings
|
||||
} // namespace CJSystemapi
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // OHOS_CJ_SETTINGS_H
|
46
cj/settings/src/cj_settings_log.h
Normal file
46
cj/settings/src/cj_settings_log.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_CJ_SETTINGS_LOG_H
|
||||
#define OHOS_CJ_SETTINGS_LOG_H
|
||||
|
||||
#include "hilog/log.h"
|
||||
|
||||
#ifdef LOG_DOMAIN
|
||||
#undef LOG_DOMAIN
|
||||
#endif
|
||||
#ifdef LOG_TAG
|
||||
#undef LOG_TAG
|
||||
#endif
|
||||
|
||||
#define LOG_DOMAIN 0x0500
|
||||
#define LOG_TAG "CJ-SETTINGS"
|
||||
|
||||
#define LOGD(...) \
|
||||
if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_DEBUG)) { \
|
||||
HILOG_DEBUG(LOG_CORE, ##__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOGI(...) \
|
||||
if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) { \
|
||||
HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOGE(...) \
|
||||
if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) { \
|
||||
HILOG_ERROR(LOG_CORE, ##__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#endif // OHOS_CJ_SETTINGS_LOG_H
|
103
cj/settings/src/cj_settings_observer.cpp
Normal file
103
cj/settings/src/cj_settings_observer.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "cj_settings_observer.h"
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include "cj_lambda.h"
|
||||
#include "cj_settings.h"
|
||||
#include "cj_settings_log.h"
|
||||
#include "cj_settings_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
|
||||
std::map<std::string, sptr<SettingsObserver>> g_observerMap;
|
||||
std::mutex g_observerMapMutex;
|
||||
|
||||
void SettingsObserver::OnChange()
|
||||
{
|
||||
if (this->cjCallback == nullptr || this->toBeDelete) {
|
||||
return;
|
||||
}
|
||||
this->cjCallback();
|
||||
return;
|
||||
}
|
||||
|
||||
bool RegisterKeyObserver(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* domainName, int64_t observer)
|
||||
{
|
||||
if (context == nullptr || name == nullptr || domainName == nullptr) {
|
||||
LOGE("Invalid parameter.");
|
||||
return false;
|
||||
}
|
||||
std::string key = name;
|
||||
std::string tableName = domainName;
|
||||
std::lock_guard<std::mutex> lockGuard(g_observerMapMutex);
|
||||
if (g_observerMap.find(key) != g_observerMap.end() && g_observerMap[key] != nullptr) {
|
||||
LOGE("%{public}s, already registered.", key.c_str());
|
||||
return false;
|
||||
}
|
||||
auto dataShareHelper = GetDataShareHelper(context, tableName);
|
||||
if (dataShareHelper == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::string strUri = GetStageUriStr(tableName, GetUserIdStr(), key);
|
||||
OHOS::Uri uri(strUri);
|
||||
sptr<SettingsObserver> settingsObserver = sptr<SettingsObserver>(new (std::nothrow)SettingsObserver());
|
||||
auto func = reinterpret_cast<void(*)(void)>(observer);
|
||||
settingsObserver->cjInfo.key = key;
|
||||
settingsObserver->cjInfo.tableName = tableName;
|
||||
settingsObserver->cjCallback = CJLambda::Create(func);
|
||||
g_observerMap[key] = settingsObserver;
|
||||
dataShareHelper->RegisterObserver(uri, settingsObserver);
|
||||
dataShareHelper->Release();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UnregisterKeyObserver(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* domainName)
|
||||
{
|
||||
if (context == nullptr || name == nullptr || domainName == nullptr) {
|
||||
LOGE("Invalid parameter.");
|
||||
return false;
|
||||
}
|
||||
std::string key = name;
|
||||
std::string tableName = domainName;
|
||||
std::lock_guard<std::mutex> lockGuard(g_observerMapMutex);
|
||||
if (g_observerMap.find(key) == g_observerMap.end()) {
|
||||
return false;
|
||||
}
|
||||
if (g_observerMap[key] == nullptr) {
|
||||
g_observerMap.erase(key);
|
||||
return false;
|
||||
}
|
||||
auto dataShareHelper = GetDataShareHelper(context, tableName);
|
||||
if (dataShareHelper == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::string strUri = GetStageUriStr(tableName, GetUserIdStr(), key);
|
||||
OHOS::Uri uri(strUri);
|
||||
dataShareHelper->UnregisterObserver(uri, g_observerMap[key]);
|
||||
dataShareHelper->Release();
|
||||
g_observerMap[key]->toBeDelete = true;
|
||||
g_observerMap[key] = nullptr;
|
||||
g_observerMap.erase(key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
45
cj/settings/src/cj_settings_observer.h
Normal file
45
cj/settings/src/cj_settings_observer.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_CJ_SETTINGS_OBSERVER_H
|
||||
#define OHOS_CJ_SETTINGS_OBSERVER_H
|
||||
|
||||
#include <functional>
|
||||
#include "cj_settings_utils.h"
|
||||
#include "data_ability_observer_interface.h"
|
||||
#include "data_ability_observer_stub.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
|
||||
class SettingsObserver : public OHOS::AAFwk::DataAbilityObserverStub {
|
||||
public:
|
||||
explicit SettingsObserver() {}
|
||||
~SettingsObserver() {}
|
||||
void OnChange();
|
||||
SettingsInfo cjInfo;
|
||||
bool toBeDelete = false;
|
||||
std::function<void(void)> cjCallback;
|
||||
};
|
||||
|
||||
bool RegisterKeyObserver(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* domainName, int64_t observer);
|
||||
bool UnregisterKeyObserver(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* domainName);
|
||||
} // namespace CJSettings
|
||||
} // namespace CJSystemapi
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_CJ_SETTINGS_OBSERVER_H
|
39
cj/settings/src/cj_settings_utils.h
Normal file
39
cj/settings/src/cj_settings_utils.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_CJ_SETTINGS_UTILS_H
|
||||
#define OHOS_CJ_SETTINGS_UTILS_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "context.h"
|
||||
#include "datashare_helper.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
|
||||
struct SettingsInfo {
|
||||
std::string key;
|
||||
std::string value;
|
||||
std::string uri;
|
||||
std::string tableName;
|
||||
int32_t status;
|
||||
std::shared_ptr<OHOS::DataShare::DataShareHelper> dataShareHelper = nullptr;
|
||||
};
|
||||
} // namespace CJSettings
|
||||
} // namespace CJSystemapi
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_CJ_SETTINGS_UTILS_H
|
56
cj/settings/src/settings_ffi.cpp
Normal file
56
cj/settings/src/settings_ffi.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "settings_ffi.h"
|
||||
#include "cj_settings.h"
|
||||
#include "cj_settings_observer.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CJSystemapi {
|
||||
namespace CJSettings {
|
||||
|
||||
extern "C" {
|
||||
bool FfiSettingsSetValue(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret)
|
||||
{
|
||||
return Settings::SetValue(context, name, value, domainName, ret);
|
||||
}
|
||||
|
||||
char* FfiSettingsGetValue(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret)
|
||||
{
|
||||
return Settings::GetValue(context, name, value, domainName, ret);
|
||||
}
|
||||
|
||||
bool FfiSettingsRegisterKeyObserver(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* domainName, int64_t observer)
|
||||
{
|
||||
return RegisterKeyObserver(context, name, domainName, observer);
|
||||
}
|
||||
|
||||
bool FfiSettingsUnregisterKeyObserver(OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* domainName)
|
||||
{
|
||||
return UnregisterKeyObserver(context, name, domainName);
|
||||
}
|
||||
|
||||
char* FfiSettingsGetUriSync(const char* name, const char* tableName)
|
||||
{
|
||||
return Settings::GetUriSync(name, tableName);
|
||||
}
|
||||
}
|
||||
} // namespace CJSettings
|
||||
} // namespace CJSystemapi
|
||||
} // namespace OHOS
|
37
cj/settings/src/settings_ffi.h
Normal file
37
cj/settings/src/settings_ffi.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_SETTINGS_FFI_H
|
||||
#define OHOS_SETTINGS_FFI_H
|
||||
|
||||
#include <cstdint>
|
||||
#include "cj_common_ffi.h"
|
||||
#include "context.h"
|
||||
|
||||
extern "C" {
|
||||
FFI_EXPORT bool FfiSettingsSetValue(
|
||||
OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret);
|
||||
FFI_EXPORT char* FfiSettingsGetValue(
|
||||
OHOS::AbilityRuntime::Context* context,
|
||||
const char* name, const char* value, const char* domainName, int32_t* ret);
|
||||
FFI_EXPORT bool FfiSettingsRegisterKeyObserver(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* domainName, int64_t observer);
|
||||
FFI_EXPORT bool FfiSettingsUnregisterKeyObserver(
|
||||
OHOS::AbilityRuntime::Context* context, const char* name, const char* domainName);
|
||||
FFI_EXPORT char* FfiSettingsGetUriSync(const char* name, const char* tableName);
|
||||
}
|
||||
|
||||
#endif // OHOS_SETTINGS_FFI_H
|
Loading…
Reference in New Issue
Block a user