19596a2c1Sopenharmony_ci/*
29596a2c1Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License.
59596a2c1Sopenharmony_ci * You may obtain a copy of the License at
69596a2c1Sopenharmony_ci *
79596a2c1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89596a2c1Sopenharmony_ci *
99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and
139596a2c1Sopenharmony_ci * limitations under the License.
149596a2c1Sopenharmony_ci */
159596a2c1Sopenharmony_ci
169596a2c1Sopenharmony_ci#include "i18n_hilog.h"
179596a2c1Sopenharmony_ci#include "locale_config.h"
189596a2c1Sopenharmony_ci#include "os_account_manager.h"
199596a2c1Sopenharmony_ci#include "parameter.h"
209596a2c1Sopenharmony_ci#include "utils.h"
219596a2c1Sopenharmony_ci
229596a2c1Sopenharmony_ci#include "multi_users.h"
239596a2c1Sopenharmony_ci
249596a2c1Sopenharmony_cinamespace OHOS {
259596a2c1Sopenharmony_cinamespace Global {
269596a2c1Sopenharmony_cinamespace I18n {
279596a2c1Sopenharmony_ciconst std::string MultiUsers::MULTI_USERS_LANGUAGE_KEY = "languageData";
289596a2c1Sopenharmony_ciconst std::string MultiUsers::MULTI_USERS_LOCALE_KEY = "localeData";
299596a2c1Sopenharmony_ciconst std::string MultiUsers::MULTI_USERS_HOUR_KEY = "is24HourData";
309596a2c1Sopenharmony_ciconst std::string MultiUsers::INIT_KEY = "init";
319596a2c1Sopenharmony_ciconst std::string MultiUsers::PREFERENCE_PATH = "/data/service/el1/public/i18n/global/GlobalParamData";
329596a2c1Sopenharmony_ciconst int32_t MultiUsers::DEFAULT_LOCAL_ID = 100;
339596a2c1Sopenharmony_ciconst int MultiUsers::CONFIG_LEN = 128;
349596a2c1Sopenharmony_cistd::shared_ptr<NativePreferences::Preferences> MultiUsers::preferences = nullptr;
359596a2c1Sopenharmony_ci
369596a2c1Sopenharmony_civoid MultiUsers::InitMultiUser()
379596a2c1Sopenharmony_ci{
389596a2c1Sopenharmony_ci    InitPreferences();
399596a2c1Sopenharmony_ci    if (preferences == nullptr) {
409596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("InitMultiUser: InitPreferences failed");
419596a2c1Sopenharmony_ci        return;
429596a2c1Sopenharmony_ci    }
439596a2c1Sopenharmony_ci    bool init = preferences->GetBool(INIT_KEY, false);
449596a2c1Sopenharmony_ci    std::string localId;
459596a2c1Sopenharmony_ci    I18nErrorCode errCode = GetForegroundLocalId(localId);
469596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
479596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("InitMultiUser: get foreground local id failed");
489596a2c1Sopenharmony_ci        return;
499596a2c1Sopenharmony_ci    }
509596a2c1Sopenharmony_ci    if (!init) {
519596a2c1Sopenharmony_ci        AddUser(localId);
529596a2c1Sopenharmony_ci        preferences->PutBool(INIT_KEY, true);
539596a2c1Sopenharmony_ci        preferences->Flush();
549596a2c1Sopenharmony_ci        HILOG_INFO_I18N("InitMultiUser: init multi user data success");
559596a2c1Sopenharmony_ci    }
569596a2c1Sopenharmony_ci}
579596a2c1Sopenharmony_ci
589596a2c1Sopenharmony_civoid MultiUsers::SwitchUser(const std::string& curLocalId)
599596a2c1Sopenharmony_ci{
609596a2c1Sopenharmony_ci    if (!IsValidLocalId(curLocalId)) {
619596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SwitchUser: curLocalId is an invalid LocalId");
629596a2c1Sopenharmony_ci        return;
639596a2c1Sopenharmony_ci    }
649596a2c1Sopenharmony_ci    I18nErrorCode errCode = LoadGlobalParam(curLocalId);
659596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
669596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SwitchUser: load global params failed");
679596a2c1Sopenharmony_ci    }
689596a2c1Sopenharmony_ci}
699596a2c1Sopenharmony_ci
709596a2c1Sopenharmony_civoid MultiUsers::AddUser(const std::string& localId)
719596a2c1Sopenharmony_ci{
729596a2c1Sopenharmony_ci    if (!IsValidLocalId(localId)) {
739596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("AddUser: localId is invalid");
749596a2c1Sopenharmony_ci        return;
759596a2c1Sopenharmony_ci    }
769596a2c1Sopenharmony_ci    I18nErrorCode errCode = SaveGlobalParam(localId);
779596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
789596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("AddUser: add global param failed");
799596a2c1Sopenharmony_ci    }
809596a2c1Sopenharmony_ci}
819596a2c1Sopenharmony_ci
829596a2c1Sopenharmony_civoid MultiUsers::RemoveUser(const std::string& localId)
839596a2c1Sopenharmony_ci{
849596a2c1Sopenharmony_ci    if (!IsValidLocalId(localId)) {
859596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("RemoveUser: localId is invalid");
869596a2c1Sopenharmony_ci        return;
879596a2c1Sopenharmony_ci    }
889596a2c1Sopenharmony_ci    I18nErrorCode errCode = RemoveGlobalParam(localId);
899596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
909596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("RemoveUser: remove global param failed");
919596a2c1Sopenharmony_ci    }
929596a2c1Sopenharmony_ci}
939596a2c1Sopenharmony_ci
949596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::GetForegroundLocalId(std::string& localId)
959596a2c1Sopenharmony_ci{
969596a2c1Sopenharmony_ci    int id = 0;
979596a2c1Sopenharmony_ci    int errCode = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(id);
989596a2c1Sopenharmony_ci    if (errCode != 0) {
999596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("GetForegroundLocalId: get foreground locale Id failed, errCode is %{public}d", errCode);
1009596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1019596a2c1Sopenharmony_ci    }
1029596a2c1Sopenharmony_ci    localId = std::to_string(id);
1039596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
1049596a2c1Sopenharmony_ci}
1059596a2c1Sopenharmony_ci
1069596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::SaveLanguage(const std::string& localId, const std::string& language)
1079596a2c1Sopenharmony_ci{
1089596a2c1Sopenharmony_ci    std::string foregroundLocalId = localId;
1099596a2c1Sopenharmony_ci    I18nErrorCode errCode = I18nErrorCode::SUCCESS;
1109596a2c1Sopenharmony_ci    if (localId.empty()) {
1119596a2c1Sopenharmony_ci        errCode = MultiUsers::GetForegroundLocalId(foregroundLocalId);
1129596a2c1Sopenharmony_ci    }
1139596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1149596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveLanguage: get foreground locale Id failed");
1159596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1169596a2c1Sopenharmony_ci    }
1179596a2c1Sopenharmony_ci
1189596a2c1Sopenharmony_ci    errCode =
1199596a2c1Sopenharmony_ci        WriteMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, language, foregroundLocalId, false);
1209596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1219596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveLanguage: save language failed");
1229596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1239596a2c1Sopenharmony_ci    }
1249596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
1259596a2c1Sopenharmony_ci}
1269596a2c1Sopenharmony_ci
1279596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::SaveLocale(const std::string& localId, const std::string& locale)
1289596a2c1Sopenharmony_ci{
1299596a2c1Sopenharmony_ci    std::string foregroundLocalId = localId;
1309596a2c1Sopenharmony_ci    I18nErrorCode errCode = I18nErrorCode::SUCCESS;
1319596a2c1Sopenharmony_ci    if (localId.empty()) {
1329596a2c1Sopenharmony_ci        errCode = MultiUsers::GetForegroundLocalId(foregroundLocalId);
1339596a2c1Sopenharmony_ci    }
1349596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1359596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveLocale: get foreground locale Id failed");
1369596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1379596a2c1Sopenharmony_ci    }
1389596a2c1Sopenharmony_ci
1399596a2c1Sopenharmony_ci    errCode =
1409596a2c1Sopenharmony_ci        WriteMultiUsersParameter(MULTI_USERS_LOCALE_KEY, locale, foregroundLocalId, false);
1419596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1429596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveLocale: save locale failed");
1439596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1449596a2c1Sopenharmony_ci    }
1459596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
1469596a2c1Sopenharmony_ci}
1479596a2c1Sopenharmony_ci
1489596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::SaveIs24Hour(const std::string& localId, const std::string& is24Hour)
1499596a2c1Sopenharmony_ci{
1509596a2c1Sopenharmony_ci    std::string foregroundLocalId = localId;
1519596a2c1Sopenharmony_ci    I18nErrorCode errCode = I18nErrorCode::SUCCESS;
1529596a2c1Sopenharmony_ci    if (localId.empty()) {
1539596a2c1Sopenharmony_ci        errCode = MultiUsers::GetForegroundLocalId(foregroundLocalId);
1549596a2c1Sopenharmony_ci    }
1559596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1569596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveLanguage: get foreground locale Id failed");
1579596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1589596a2c1Sopenharmony_ci    }
1599596a2c1Sopenharmony_ci    errCode =
1609596a2c1Sopenharmony_ci        WriteMultiUsersParameter(MULTI_USERS_HOUR_KEY, is24Hour, foregroundLocalId, false);
1619596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1629596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveIs24Hour: save is24Hour failed");
1639596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1649596a2c1Sopenharmony_ci    }
1659596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
1669596a2c1Sopenharmony_ci}
1679596a2c1Sopenharmony_ci
1689596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::SaveGlobalParam(const std::string& localId)
1699596a2c1Sopenharmony_ci{
1709596a2c1Sopenharmony_ci    std::string language = ReadSystemParameter(LocaleConfig::GetLanguageKey().data(), CONFIG_LEN);
1719596a2c1Sopenharmony_ci    std::string locale = ReadSystemParameter(LocaleConfig::GetLocaleKey().data(), CONFIG_LEN);
1729596a2c1Sopenharmony_ci    std::string is24Hour = ReadSystemParameter(LocaleConfig::GetHourKey().data(), CONFIG_LEN);
1739596a2c1Sopenharmony_ci    I18nErrorCode errCode = SaveLanguage(localId, language);
1749596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1759596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveGlobalParam: save language failed");
1769596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1779596a2c1Sopenharmony_ci    }
1789596a2c1Sopenharmony_ci    errCode = SaveLocale(localId, locale);
1799596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1809596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveGlobalParam: save locale failed");
1819596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1829596a2c1Sopenharmony_ci    }
1839596a2c1Sopenharmony_ci    errCode = SaveIs24Hour(localId, is24Hour);
1849596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
1859596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SaveGlobalParam: save is24Hour failed");
1869596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1879596a2c1Sopenharmony_ci    }
1889596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
1899596a2c1Sopenharmony_ci}
1909596a2c1Sopenharmony_ci
1919596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::LoadGlobalParam(const std::string& localId)
1929596a2c1Sopenharmony_ci{
1939596a2c1Sopenharmony_ci    std::string newLocale = ReadMultiUsersParameter(MULTI_USERS_LOCALE_KEY, localId);
1949596a2c1Sopenharmony_ci    if (!newLocale.empty() && SetParameter(LocaleConfig::GetLocaleKey().data(), newLocale.data()) != 0) {
1959596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("LoadGlobalParam: set locale failed");
1969596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
1979596a2c1Sopenharmony_ci    }
1989596a2c1Sopenharmony_ci
1999596a2c1Sopenharmony_ci    std::string newLanguage = ReadMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, localId);
2009596a2c1Sopenharmony_ci    if (!newLanguage.empty() && LocaleConfig::SetSystemLanguage(newLanguage) != 0) {
2019596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("LoadGlobalParam: set language failed");
2029596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2039596a2c1Sopenharmony_ci    }
2049596a2c1Sopenharmony_ci
2059596a2c1Sopenharmony_ci    std::string newIs24Hour = ReadMultiUsersParameter(MULTI_USERS_HOUR_KEY, localId);
2069596a2c1Sopenharmony_ci    if (!newIs24Hour.empty() && LocaleConfig::Set24HourClock(newIs24Hour) != 0) {
2079596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("LoadGlobalParam: set is24Hour failed");
2089596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2099596a2c1Sopenharmony_ci    }
2109596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
2119596a2c1Sopenharmony_ci}
2129596a2c1Sopenharmony_ci
2139596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::RemoveGlobalParam(const std::string& localId)
2149596a2c1Sopenharmony_ci{
2159596a2c1Sopenharmony_ci    I18nErrorCode errCode = WriteMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, "", localId, true);
2169596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
2179596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("RemoveGlobalParam: remove language failed");
2189596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2199596a2c1Sopenharmony_ci    }
2209596a2c1Sopenharmony_ci
2219596a2c1Sopenharmony_ci    errCode = WriteMultiUsersParameter(MULTI_USERS_LOCALE_KEY, "", localId, true);
2229596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
2239596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("RemoveGlobalParam: remove locale failed");
2249596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2259596a2c1Sopenharmony_ci    }
2269596a2c1Sopenharmony_ci
2279596a2c1Sopenharmony_ci    errCode = WriteMultiUsersParameter(MULTI_USERS_HOUR_KEY, "", localId, true);
2289596a2c1Sopenharmony_ci    if (errCode != I18nErrorCode::SUCCESS) {
2299596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("RemoveGlobalParam: remove is24Hour failed");
2309596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2319596a2c1Sopenharmony_ci    }
2329596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
2339596a2c1Sopenharmony_ci}
2349596a2c1Sopenharmony_ci
2359596a2c1Sopenharmony_cistd::string MultiUsers::ReadMultiUsersParameter(const std::string& paramKey, const std::string& localId)
2369596a2c1Sopenharmony_ci{
2379596a2c1Sopenharmony_ci    std::string param = GetParamFromPreferences(paramKey);
2389596a2c1Sopenharmony_ci    if (param.empty()) {
2399596a2c1Sopenharmony_ci        return "";
2409596a2c1Sopenharmony_ci    }
2419596a2c1Sopenharmony_ci    std::vector<std::string> multiUsersParam;
2429596a2c1Sopenharmony_ci    Split(param, ";", multiUsersParam);
2439596a2c1Sopenharmony_ci    for (auto& userParam : multiUsersParam) {
2449596a2c1Sopenharmony_ci        std::vector<std::string> content;
2459596a2c1Sopenharmony_ci        Split(userParam, ":", content);
2469596a2c1Sopenharmony_ci        // 2 is number of param
2479596a2c1Sopenharmony_ci        if (content.size() != 2) {
2489596a2c1Sopenharmony_ci            continue;
2499596a2c1Sopenharmony_ci        }
2509596a2c1Sopenharmony_ci        if (content[0] == localId) {
2519596a2c1Sopenharmony_ci            return content[1];
2529596a2c1Sopenharmony_ci        }
2539596a2c1Sopenharmony_ci    }
2549596a2c1Sopenharmony_ci    return "";
2559596a2c1Sopenharmony_ci}
2569596a2c1Sopenharmony_ci
2579596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::WriteMultiUsersParameter(const std::string& paramKey, const std::string& paramValue,
2589596a2c1Sopenharmony_ci    const std::string& localId, bool isDel)
2599596a2c1Sopenharmony_ci{
2609596a2c1Sopenharmony_ci    std::string param = GetParamFromPreferences(paramKey);
2619596a2c1Sopenharmony_ci    std::vector<std::string> multiUsersParam;
2629596a2c1Sopenharmony_ci    Split(param, ";", multiUsersParam);
2639596a2c1Sopenharmony_ci    std::vector<std::string> newMultiUsersParam;
2649596a2c1Sopenharmony_ci    bool userIsExist = false;
2659596a2c1Sopenharmony_ci    for (auto& userParam : multiUsersParam) {
2669596a2c1Sopenharmony_ci        std::vector<std::string> content;
2679596a2c1Sopenharmony_ci        Split(userParam, ":", content);
2689596a2c1Sopenharmony_ci        // 2 is number of param
2699596a2c1Sopenharmony_ci        if (content.size() != 2) {
2709596a2c1Sopenharmony_ci            continue;
2719596a2c1Sopenharmony_ci        }
2729596a2c1Sopenharmony_ci        std::string userLocalId = content[0];
2739596a2c1Sopenharmony_ci        if (!isDel && userLocalId == localId) {
2749596a2c1Sopenharmony_ci            content[1] = paramValue;
2759596a2c1Sopenharmony_ci            Merge(content, ":", userParam);
2769596a2c1Sopenharmony_ci            userIsExist = true;
2779596a2c1Sopenharmony_ci        }
2789596a2c1Sopenharmony_ci        newMultiUsersParam.emplace_back(userParam);
2799596a2c1Sopenharmony_ci        if (isDel && userLocalId == localId) {
2809596a2c1Sopenharmony_ci            newMultiUsersParam.pop_back();
2819596a2c1Sopenharmony_ci        }
2829596a2c1Sopenharmony_ci    }
2839596a2c1Sopenharmony_ci    if (!isDel && !userIsExist) {
2849596a2c1Sopenharmony_ci        newMultiUsersParam.push_back(localId + ":" + paramValue);
2859596a2c1Sopenharmony_ci    }
2869596a2c1Sopenharmony_ci    std::string newParam;
2879596a2c1Sopenharmony_ci    Merge(newMultiUsersParam, ";", newParam);
2889596a2c1Sopenharmony_ci    if (SetParamFromPreferences(paramKey, newParam) != I18nErrorCode::SUCCESS) {
2899596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("WriteMultiUsersParameter: set param %{public}s failed", paramKey.c_str());
2909596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
2919596a2c1Sopenharmony_ci    }
2929596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
2939596a2c1Sopenharmony_ci}
2949596a2c1Sopenharmony_ci
2959596a2c1Sopenharmony_cibool MultiUsers::IsValidLocalId(const std::string& localId)
2969596a2c1Sopenharmony_ci{
2979596a2c1Sopenharmony_ci    if (std::atoi(localId.c_str()) < DEFAULT_LOCAL_ID) {
2989596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("IsValidLocalId: invalid local ID");
2999596a2c1Sopenharmony_ci        return false;
3009596a2c1Sopenharmony_ci    }
3019596a2c1Sopenharmony_ci    return true;
3029596a2c1Sopenharmony_ci}
3039596a2c1Sopenharmony_ci
3049596a2c1Sopenharmony_civoid MultiUsers::InitPreferences()
3059596a2c1Sopenharmony_ci{
3069596a2c1Sopenharmony_ci    if (preferences == nullptr) {
3079596a2c1Sopenharmony_ci        HILOG_INFO_I18N("InitPreferences: preferences Init");
3089596a2c1Sopenharmony_ci        OHOS::NativePreferences::Options opt(PREFERENCE_PATH);
3099596a2c1Sopenharmony_ci        int status;
3109596a2c1Sopenharmony_ci        preferences = NativePreferences::PreferencesHelper::GetPreferences(opt, status);
3119596a2c1Sopenharmony_ci        if (status != 0) {
3129596a2c1Sopenharmony_ci            HILOG_ERROR_I18N("InitPreferences: get preferences failed");
3139596a2c1Sopenharmony_ci            preferences = nullptr;
3149596a2c1Sopenharmony_ci        }
3159596a2c1Sopenharmony_ci    }
3169596a2c1Sopenharmony_ci}
3179596a2c1Sopenharmony_ci
3189596a2c1Sopenharmony_cistd::string MultiUsers::GetParamFromPreferences(const std::string& paramKey)
3199596a2c1Sopenharmony_ci{
3209596a2c1Sopenharmony_ci    InitPreferences();
3219596a2c1Sopenharmony_ci    if (preferences == nullptr) {
3229596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("GetParamFromPreferences: preferences is nullptr");
3239596a2c1Sopenharmony_ci        return "";
3249596a2c1Sopenharmony_ci    }
3259596a2c1Sopenharmony_ci    return preferences->GetString(paramKey, "");
3269596a2c1Sopenharmony_ci}
3279596a2c1Sopenharmony_ci
3289596a2c1Sopenharmony_ciI18nErrorCode MultiUsers::SetParamFromPreferences(const std::string& paramKey, const std::string& paramValue)
3299596a2c1Sopenharmony_ci{
3309596a2c1Sopenharmony_ci    InitPreferences();
3319596a2c1Sopenharmony_ci    if (preferences == nullptr) {
3329596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SetParamFromPreferences: preferences is nullptr");
3339596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
3349596a2c1Sopenharmony_ci    }
3359596a2c1Sopenharmony_ci    int status = preferences->PutString(paramKey, paramValue);
3369596a2c1Sopenharmony_ci    if (status != 0) {
3379596a2c1Sopenharmony_ci        HILOG_ERROR_I18N("SetParamFromPreferences: put param %{public}s failed", paramKey.c_str());
3389596a2c1Sopenharmony_ci        return I18nErrorCode::FAILED;
3399596a2c1Sopenharmony_ci    }
3409596a2c1Sopenharmony_ci    preferences->Flush();
3419596a2c1Sopenharmony_ci    return I18nErrorCode::SUCCESS;
3429596a2c1Sopenharmony_ci}
3439596a2c1Sopenharmony_ci} // namespace I18n
3449596a2c1Sopenharmony_ci} // namespace Global
3459596a2c1Sopenharmony_ci} // namespace OHOS
346