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