1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci#include "nfc_preferences.h"
16bc2ed2b3Sopenharmony_ci#include "loghelper.h"
17bc2ed2b3Sopenharmony_ci#include "nfc_data_share_impl.h"
18bc2ed2b3Sopenharmony_ci
19bc2ed2b3Sopenharmony_cinamespace OHOS {
20bc2ed2b3Sopenharmony_cinamespace NFC {
21bc2ed2b3Sopenharmony_ciNfcPreferences::NfcPreferences()
22bc2ed2b3Sopenharmony_ci{
23bc2ed2b3Sopenharmony_ci    fileName_ = "/data/nfc/nfc_preferences.xml";
24bc2ed2b3Sopenharmony_ci    errCode_ = 0;
25bc2ed2b3Sopenharmony_ci}
26bc2ed2b3Sopenharmony_ci
27bc2ed2b3Sopenharmony_ciNfcPreferences::~NfcPreferences()
28bc2ed2b3Sopenharmony_ci{
29bc2ed2b3Sopenharmony_ci}
30bc2ed2b3Sopenharmony_ci
31bc2ed2b3Sopenharmony_ciNfcPreferences& NfcPreferences::GetInstance()
32bc2ed2b3Sopenharmony_ci{
33bc2ed2b3Sopenharmony_ci    static NfcPreferences sNfcPrefImpl;
34bc2ed2b3Sopenharmony_ci    return sNfcPrefImpl;
35bc2ed2b3Sopenharmony_ci}
36bc2ed2b3Sopenharmony_ci
37bc2ed2b3Sopenharmony_cistd::shared_ptr<NativePreferences::Preferences> NfcPreferences::GetPreference(const std::string& fileName)
38bc2ed2b3Sopenharmony_ci{
39bc2ed2b3Sopenharmony_ci    DebugLog("Getting preference from distributed data management system");
40bc2ed2b3Sopenharmony_ci    return NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode_);
41bc2ed2b3Sopenharmony_ci}
42bc2ed2b3Sopenharmony_ci
43bc2ed2b3Sopenharmony_civoid NfcPreferences::SetString(const std::string& key, const std::string& value)
44bc2ed2b3Sopenharmony_ci{
45bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
46bc2ed2b3Sopenharmony_ci    if (!pref) {
47bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
48bc2ed2b3Sopenharmony_ci        return;
49bc2ed2b3Sopenharmony_ci    }
50bc2ed2b3Sopenharmony_ci    DebugLog("Set preference with key %{public}s, value %{public}s", key.c_str(), value.c_str());
51bc2ed2b3Sopenharmony_ci    pref->PutString(key, value);
52bc2ed2b3Sopenharmony_ci    pref->Flush();
53bc2ed2b3Sopenharmony_ci}
54bc2ed2b3Sopenharmony_ci
55bc2ed2b3Sopenharmony_cistd::string NfcPreferences::GetString(const std::string& key)
56bc2ed2b3Sopenharmony_ci{
57bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
58bc2ed2b3Sopenharmony_ci    if (!pref) {
59bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
60bc2ed2b3Sopenharmony_ci        return "";
61bc2ed2b3Sopenharmony_ci    }
62bc2ed2b3Sopenharmony_ci    DebugLog("Get preference with key %{public}s", key.c_str());
63bc2ed2b3Sopenharmony_ci    return pref->GetString(key, "");
64bc2ed2b3Sopenharmony_ci}
65bc2ed2b3Sopenharmony_ci
66bc2ed2b3Sopenharmony_civoid NfcPreferences::SetInt(const std::string& key, const int value)
67bc2ed2b3Sopenharmony_ci{
68bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
69bc2ed2b3Sopenharmony_ci    if (!pref) {
70bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
71bc2ed2b3Sopenharmony_ci        return;
72bc2ed2b3Sopenharmony_ci    }
73bc2ed2b3Sopenharmony_ci    DebugLog("Set preference with key %{public}s, value %{public}d", key.c_str(), value);
74bc2ed2b3Sopenharmony_ci    pref->PutInt(key, value);
75bc2ed2b3Sopenharmony_ci    pref->Flush();
76bc2ed2b3Sopenharmony_ci}
77bc2ed2b3Sopenharmony_ci
78bc2ed2b3Sopenharmony_ciint NfcPreferences::GetInt(const std::string& key)
79bc2ed2b3Sopenharmony_ci{
80bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
81bc2ed2b3Sopenharmony_ci    if (!pref) {
82bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
83bc2ed2b3Sopenharmony_ci        return 0;
84bc2ed2b3Sopenharmony_ci    }
85bc2ed2b3Sopenharmony_ci    DebugLog("Get preference with key %{public}s", key.c_str());
86bc2ed2b3Sopenharmony_ci    return pref->GetInt(key, 0);
87bc2ed2b3Sopenharmony_ci}
88bc2ed2b3Sopenharmony_ci
89bc2ed2b3Sopenharmony_civoid NfcPreferences::Clear()
90bc2ed2b3Sopenharmony_ci{
91bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
92bc2ed2b3Sopenharmony_ci    if (!pref) {
93bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
94bc2ed2b3Sopenharmony_ci        return;
95bc2ed2b3Sopenharmony_ci    }
96bc2ed2b3Sopenharmony_ci    pref->Clear();
97bc2ed2b3Sopenharmony_ci    DebugLog("NfcPreferences: Clear preferences");
98bc2ed2b3Sopenharmony_ci    NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
99bc2ed2b3Sopenharmony_ci}
100bc2ed2b3Sopenharmony_ci
101bc2ed2b3Sopenharmony_civoid NfcPreferences::Delete(const std::string& key)
102bc2ed2b3Sopenharmony_ci{
103bc2ed2b3Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
104bc2ed2b3Sopenharmony_ci    if (!pref) {
105bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: Preference get null");
106bc2ed2b3Sopenharmony_ci        return;
107bc2ed2b3Sopenharmony_ci    }
108bc2ed2b3Sopenharmony_ci    DebugLog("NfcPreferences: Delete preference with key %{public}s", key.c_str());
109bc2ed2b3Sopenharmony_ci    pref->Delete(key);
110bc2ed2b3Sopenharmony_ci    pref->FlushSync();
111bc2ed2b3Sopenharmony_ci}
112bc2ed2b3Sopenharmony_ci
113bc2ed2b3Sopenharmony_civoid NfcPreferences::UpdateNfcState(int newState)
114bc2ed2b3Sopenharmony_ci{
115bc2ed2b3Sopenharmony_ci    SetInt(PREF_KEY_STATE, newState);
116bc2ed2b3Sopenharmony_ci
117bc2ed2b3Sopenharmony_ci    Uri nfcEnableUri(KITS::NFC_DATA_URI);
118bc2ed2b3Sopenharmony_ci    KITS::ErrorCode err = DelayedSingleton<NfcDataShareImpl>::GetInstance()->
119bc2ed2b3Sopenharmony_ci        SetValue(nfcEnableUri, KITS::DATA_SHARE_KEY_STATE, newState);
120bc2ed2b3Sopenharmony_ci    if (err != ERR_NONE) {
121bc2ed2b3Sopenharmony_ci        ErrorLog("NfcPreferences: UpdateNfcState set datashare fail, newState = %{public}d", newState);
122bc2ed2b3Sopenharmony_ci    }
123bc2ed2b3Sopenharmony_ci}
124bc2ed2b3Sopenharmony_ci
125bc2ed2b3Sopenharmony_ciint NfcPreferences::GetNfcState()
126bc2ed2b3Sopenharmony_ci{
127bc2ed2b3Sopenharmony_ci    return GetInt(PREF_KEY_STATE);
128bc2ed2b3Sopenharmony_ci}
129bc2ed2b3Sopenharmony_ci} // NFC
130bc2ed2b3Sopenharmony_ci} // OHOS