!1562 修复使能输入法列表多用户场景问题

Merge pull request !1562 from guojin31/master
This commit is contained in:
openharmony_ci 2024-11-02 07:36:51 +00:00 committed by Gitee
commit dbd0aacf49
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 130 additions and 59 deletions

View File

@ -30,6 +30,11 @@
namespace OHOS {
namespace MiscServices {
constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD";
constexpr const char *SETTING_COLUMN_VALUE = "VALUE";
const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/"
"SETTINGSDATA?Proxy=true";
const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
struct UserImeConfig : public Serializable {
std::string userId;
std::vector<std::string> identities;
@ -43,11 +48,12 @@ struct UserImeConfig : public Serializable {
class SettingsDataUtils : public RefBase {
public:
static sptr<SettingsDataUtils> GetInstance();
std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper();
std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string &uriProxy);
int32_t CreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func);
int32_t GetStringValue(const std::string &key, std::string &value);
int32_t GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value);
int32_t SetStringValue(const std::string &uriProxy, const std::string &key, const std::string &value);
bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
Uri GenerateTargetUri(const std::string &key);
Uri GenerateTargetUri(const std::string &uriProxy, const std::string &key);
bool EnableIme(int32_t userId, const std::string &bundleName);
private:

View File

@ -23,11 +23,6 @@ namespace OHOS {
namespace MiscServices {
std::mutex SettingsDataUtils::instanceMutex_;
sptr<SettingsDataUtils> SettingsDataUtils::instance_ = nullptr;
constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD";
constexpr const char *SETTING_COLUMN_VALUE = "VALUE";
constexpr const char *SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/"
"SETTINGSDATA?Proxy=true";
constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
SettingsDataUtils::~SettingsDataUtils()
{
remoteObj_ = nullptr;
@ -73,8 +68,8 @@ int32_t SettingsDataUtils::RegisterObserver(const sptr<SettingsDataObserver> &ob
return ErrorCode::ERROR_NULL_POINTER;
}
auto uri = GenerateTargetUri(observer->GetKey());
auto helper = SettingsDataUtils::CreateDataShareHelper();
auto uri = GenerateTargetUri(SETTING_URI_PROXY, observer->GetKey());
auto helper = SettingsDataUtils::CreateDataShareHelper(SETTING_URI_PROXY);
if (helper == nullptr) {
IMSA_HILOGE("helper is nullptr!");
return ErrorCode::ERROR_NULL_POINTER;
@ -90,8 +85,8 @@ int32_t SettingsDataUtils::RegisterObserver(const sptr<SettingsDataObserver> &ob
int32_t SettingsDataUtils::UnregisterObserver(const sptr<SettingsDataObserver> &observer)
{
auto uri = GenerateTargetUri(observer->GetKey());
auto helper = SettingsDataUtils::CreateDataShareHelper();
auto uri = GenerateTargetUri(SETTING_URI_PROXY, observer->GetKey());
auto helper = SettingsDataUtils::CreateDataShareHelper(SETTING_URI_PROXY);
if (helper == nullptr) {
return ErrorCode::ERROR_ENABLE_IME;
}
@ -101,7 +96,7 @@ int32_t SettingsDataUtils::UnregisterObserver(const sptr<SettingsDataObserver> &
return ErrorCode::NO_ERROR;
}
std::shared_ptr<DataShare::DataShareHelper> SettingsDataUtils::CreateDataShareHelper()
std::shared_ptr<DataShare::DataShareHelper> SettingsDataUtils::CreateDataShareHelper(const std::string &uriProxy)
{
auto remoteObj = GetToken();
if (remoteObj == nullptr) {
@ -109,9 +104,9 @@ std::shared_ptr<DataShare::DataShareHelper> SettingsDataUtils::CreateDataShareHe
return nullptr;
}
auto helper = DataShare::DataShareHelper::Creator(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
auto helper = DataShare::DataShareHelper::Creator(remoteObj_, uriProxy, SETTINGS_DATA_EXT_URI);
if (helper == nullptr) {
IMSA_HILOGE("create helper failed, uri: %{public}s!", SETTING_URI_PROXY);
IMSA_HILOGE("create helper failed, uri: %{public}s!", uriProxy.c_str());
return nullptr;
}
return helper;
@ -130,16 +125,43 @@ bool SettingsDataUtils::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataSh
return true;
}
Uri SettingsDataUtils::GenerateTargetUri(const std::string &key)
Uri SettingsDataUtils::GenerateTargetUri(const std::string &uriProxy, const std::string &key)
{
Uri uri(std::string(SETTING_URI_PROXY) + "&key=" + key);
Uri uri(std::string(uriProxy) + "&key=" + key);
return uri;
}
int32_t SettingsDataUtils::GetStringValue(const std::string &key, std::string &value)
int32_t SettingsDataUtils::SetStringValue(const std::string &uriProxy, const std::string &key, const std::string &value)
{
IMSA_HILOGD("start.");
auto helper = CreateDataShareHelper();
auto helper = CreateDataShareHelper(uriProxy);
if (helper == nullptr) {
IMSA_HILOGE("helper is nullptr.");
return false;
}
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(GenerateTargetUri(uriProxy, key));
if (helper->Update(uri, predicates, bucket) <= 0) {
int index = helper->Insert(uri, bucket);
IMSA_HILOGI("no data exists, insert ret index: %{public}d", index);
} else {
IMSA_HILOGI("data exits");
}
bool ret = ReleaseDataShareHelper(helper);
IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret);
return ret;
}
int32_t SettingsDataUtils::GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value)
{
IMSA_HILOGD("start.");
auto helper = CreateDataShareHelper(uriProxy);
if (helper == nullptr) {
IMSA_HILOGE("helper is nullptr.");
return ErrorCode::ERROR_NULL_POINTER;
@ -147,7 +169,7 @@ int32_t SettingsDataUtils::GetStringValue(const std::string &key, std::string &v
std::vector<std::string> columns = { SETTING_COLUMN_VALUE };
DataShare::DataSharePredicates predicates;
predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
Uri uri(GenerateTargetUri(key));
Uri uri(GenerateTargetUri(uriProxy, key));
auto resultSet = helper->Query(uri, predicates, columns);
ReleaseDataShareHelper(helper);
if (resultSet == nullptr) {
@ -201,11 +223,9 @@ bool SettingsDataUtils::EnableIme(int32_t userId, const std::string &bundleName)
IMSA_HILOGE("user is not main.");
return false;
}
const char *SETTING_COLUMN_KEYWORD = "KEYWORD";
const char *SETTING_COLUMN_VALUE = "VALUE";
const char *settingKey = "settings.inputmethod.enable_ime";
std::string settingValue = "";
GetStringValue(settingKey, settingValue);
GetStringValue(SETTING_URI_PROXY, settingKey, settingValue);
IMSA_HILOGI("settingValue: %{public}s", settingValue.c_str());
std::string value = "";
if (settingValue == "") {
@ -214,27 +234,7 @@ bool SettingsDataUtils::EnableIme(int32_t userId, const std::string &bundleName)
value = SetSettingValues(settingValue, bundleName);
}
IMSA_HILOGI("value: %{public}s", value.c_str());
auto helper = CreateDataShareHelper();
if (helper == nullptr) {
IMSA_HILOGE("helper is nullptr.");
return false;
}
DataShare::DataShareValueObject keyObj(settingKey);
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, settingKey);
Uri uri(GenerateTargetUri(settingKey));
if (helper->Update(uri, predicates, bucket) <= 0) {
int index = helper->Insert(uri, bucket);
IMSA_HILOGI("no data exists, insert ret index: %{public}d", index);
} else {
IMSA_HILOGI("data exits");
}
bool ret = ReleaseDataShareHelper(helper);
IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret);
auto ret = SetStringValue(SETTING_URI_PROXY, settingKey, value);
return ret;
}

View File

@ -89,6 +89,8 @@ private:
EnableImeDataParser() = default;
~EnableImeDataParser();
int32_t UpdateEnableData(int32_t userId, const std::string &key);
int32_t CoverGlobalTable(const std::string &uriProxy, const std::string &key, std::string &valueStr);
std::string ParseEnableValueUserId(const std::string &valueStr);
int32_t GetEnableImeFromCache(std::vector<std::string> &enableVec);
bool ParseEnableIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec);
bool ParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec);

View File

@ -182,28 +182,76 @@ bool EnableImeDataParser::CheckTargetEnableName(const std::string &key, const st
return true;
}
int32_t EnableImeDataParser::GetEnableData(const std::string &key, std::vector<std::string> &enableVec,
const int32_t userId)
int32_t EnableImeDataParser::CoverGlobalTable(
const std::string &uriProxy, const std::string &key, std::string &valueStr)
{
int32_t ret;
if (!valueStr.empty()) {
std::string userId = ParseEnableValueUserId(valueStr);
if (userId != "") {
if (userId == std::to_string(currentUserId_)) {
return ErrorCode::NO_ERROR;
}
ret = SettingsDataUtils::GetInstance()->SetStringValue(uriProxy + userId + "?Proxy=true", key, valueStr);
if (ret != ErrorCode::NO_ERROR) {
IMSA_HILOGW("global cover user table failed");
}
}
}
ret = SettingsDataUtils::GetInstance()->GetStringValue(
uriProxy + std::to_string(currentUserId_) + "?Proxy=true", key, valueStr);
if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
IMSA_HILOGW("user table no keyword exist");
return ErrorCode::NO_ERROR;
}
if (ret != ErrorCode::NO_ERROR || valueStr.empty()) {
IMSA_HILOGW("get current user table value failed, or valueStr is empty.");
return ErrorCode::ERROR_ENABLE_IME;
}
SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, key, valueStr);
return ErrorCode::NO_ERROR;
}
int32_t EnableImeDataParser::GetEnableData(
const std::string &key, std::vector<std::string> &enableVec, const int32_t userId)
{
if (key != std::string(ENABLE_IME) && key != std::string(ENABLE_KEYBOARD) && key != std::string(TEMP_IME)) {
IMSA_HILOGD("invalid key: %{public}s.", key.c_str());
return ErrorCode::ERROR_ENABLE_IME;
}
IMSA_HILOGD("userId: %{public}d, key: %{public}s.", userId, key.c_str());
std::string valueStr;
int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(key, valueStr);
int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, key, valueStr);
if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
IMSA_HILOGW("no keyword exist");
enableVec.clear();
return ErrorCode::NO_ERROR;
}
if (ret != ErrorCode::NO_ERROR || valueStr.empty()) {
if (key != ENABLE_IME && (ret != ErrorCode::NO_ERROR || valueStr.empty())) {
IMSA_HILOGW("get value failed, or valueStr is empty.");
return ErrorCode::ERROR_ENABLE_IME;
}
auto parseRet = false;
if (key == ENABLE_IME) {
std::string uriProxy = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_";
CoverGlobalTable(uriProxy, key, valueStr);
if (currentUserId_ != userId) {
ret = SettingsDataUtils::GetInstance()->GetStringValue(
uriProxy + std::to_string(userId) + "?Proxy=true", key, valueStr);
if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
IMSA_HILOGW("no keyword exist");
enableVec.clear();
return ErrorCode::NO_ERROR;
}
if (ret != ErrorCode::NO_ERROR || valueStr.empty()) {
IMSA_HILOGW("get user table value failed, or valueStr is empty.");
return ErrorCode::ERROR_ENABLE_IME;
}
} else if (valueStr.empty()) {
IMSA_HILOGW("get enable ime, value is empty");
enableVec.clear();
return ErrorCode::NO_ERROR;
}
parseRet = ParseEnableIme(valueStr, userId, enableVec);
}
if (key == ENABLE_KEYBOARD) {
@ -215,6 +263,21 @@ int32_t EnableImeDataParser::GetEnableData(const std::string &key, std::vector<s
return parseRet ? ErrorCode::NO_ERROR : ErrorCode::ERROR_ENABLE_IME;
}
std::string EnableImeDataParser::ParseEnableValueUserId(const std::string &valueStr)
{
auto root = cJSON_Parse(valueStr.c_str());
auto subNode = Serializable::GetSubNode(root, "enableImeList");
if (subNode == nullptr || cJSON_IsObject(subNode)) {
IMSA_HILOGW("subNode is null or not object");
return "";
}
if (subNode->child == nullptr) {
IMSA_HILOGW("subNode has not child");
return "";
}
return subNode->child->string;
}
bool EnableImeDataParser::ParseTempIme(const std::string &valueStr, int32_t userId,
std::vector<std::string> &tempVector)
{

View File

@ -63,7 +63,7 @@ int32_t SecurityModeParser::UpdateFullModeList(int32_t userId)
{
IMSA_HILOGD("key: %{public}s.", SECURITY_MODE);
std::string valueStr;
int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SECURITY_MODE, valueStr);
int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, SECURITY_MODE, valueStr);
if (ret != ErrorCode::NO_ERROR || valueStr.empty()) {
IMSA_HILOGW("get value failed, or valueStr is empty");
return ErrorCode::ERROR_ENABLE_SECURITY_MODE;

View File

@ -83,8 +83,6 @@ public:
}
return ret;
}
private:
static cJSON *GetSubNode(cJSON *node, const std::string &name);
};
} // namespace MiscServices

View File

@ -28,7 +28,7 @@ void FuzzGetToken()
void FuzzDataShareHelper()
{
auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper();
auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(SETTING_URI_PROXY);
SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper);
}
@ -51,7 +51,7 @@ void FuzzUnregisterObserver(const std::string &key, SettingsDataObserver::Callba
void FuzzGenerateTargetUri(const std::string &key)
{
SettingsDataUtils::GetInstance()->GenerateTargetUri(key);
SettingsDataUtils::GetInstance()->GenerateTargetUri(SETTING_URI_PROXY, key);
}
} // namespace OHOS

View File

@ -55,8 +55,6 @@ constexpr int32_t FIRST_PARAM_INDEX = 0;
constexpr int32_t SECOND_PARAM_INDEX = 1;
constexpr int32_t THIRD_PARAM_INDEX = 2;
constexpr int32_t FOURTH_PARAM_INDEX = 3;
constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD";
constexpr const char *SETTING_COLUMN_VALUE = "VALUE";
static constexpr int32_t MAX_TIMEOUT_WAIT_FOCUS = 2000;
uint64_t TddUtil::selfTokenID_ = 0;
int32_t TddUtil::userID_ = INVALID_USER_ID;
@ -301,7 +299,7 @@ void TddUtil::GrantNativePermission()
void TddUtil::PushEnableImeValue(const std::string &key, const std::string &value)
{
IMSA_HILOGI("key: %{public}s, value: %{public}s", key.c_str(), value.c_str());
auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper();
auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(SETTING_URI_PROXY);
if (helper == nullptr) {
IMSA_HILOGE("helper is nullptr.");
return;
@ -313,7 +311,7 @@ void TddUtil::PushEnableImeValue(const std::string &key, const std::string &valu
bucket.Put(SETTING_COLUMN_VALUE, valueObj);
DataShare::DataSharePredicates predicates;
predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
Uri uri(SettingsDataUtils::GetInstance()->GenerateTargetUri(key));
Uri uri(SettingsDataUtils::GetInstance()->GenerateTargetUri(SETTING_URI_PROXY, key));
if (helper->Update(uri, predicates, bucket) <= 0) {
int index = helper->Insert(uri, bucket);
IMSA_HILOGI("no data exists, insert ret index: %{public}d", index);
@ -326,7 +324,8 @@ void TddUtil::PushEnableImeValue(const std::string &key, const std::string &valu
int32_t TddUtil::GetEnableData(std::string &value)
{
auto ret = SettingsDataUtils::GetInstance()->GetStringValue(EnableImeDataParser::ENABLE_IME, value);
auto ret =
SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, EnableImeDataParser::ENABLE_IME, value);
if (ret == ErrorCode::NO_ERROR) {
IMSA_HILOGI("success, value: %{public}s", value.c_str());
}

View File

@ -58,6 +58,9 @@
},
{
"name": "ohos.permission.MANAGE_SECURE_SETTINGS"
},
{
"name": "ohos.permission.MANAGE_SETTINGS"
}
]
}