• 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 #include "nfc_preferences.h"
16 #include "loghelper.h"
17 #include "nfc_data_share_impl.h"
18 
19 namespace OHOS {
20 namespace NFC {
NfcPreferences()21 NfcPreferences::NfcPreferences()
22 {
23     fileName_ = "/data/nfc/nfc_preferences.xml";
24     errCode_ = 0;
25 }
26 
~NfcPreferences()27 NfcPreferences::~NfcPreferences()
28 {
29 }
30 
GetInstance()31 NfcPreferences& NfcPreferences::GetInstance()
32 {
33     static NfcPreferences nfcPrefImpl;
34     return nfcPrefImpl;
35 }
36 
GetPreference(const std::string & fileName)37 std::shared_ptr<NativePreferences::Preferences> NfcPreferences::GetPreference(const std::string& fileName)
38 {
39     DebugLog("Getting preference from distributed data management system");
40     return NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode_);
41 }
42 
SetString(const std::string & key,const std::string & value)43 void NfcPreferences::SetString(const std::string& key, const std::string& value)
44 {
45     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
46     if (!pref) {
47         ErrorLog("NfcPreferences: Preference get null");
48         return;
49     }
50     DebugLog("Set preference with key %{public}s, value %{public}s", key.c_str(), value.c_str());
51     pref->PutString(key, value);
52     pref->Flush();
53 }
54 
GetString(const std::string & key)55 std::string NfcPreferences::GetString(const std::string& key)
56 {
57     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
58     if (!pref) {
59         ErrorLog("NfcPreferences: Preference get null");
60         return "";
61     }
62     DebugLog("Get preference with key %{public}s", key.c_str());
63     return pref->GetString(key, "");
64 }
65 
SetInt(const std::string & key,const int value)66 void NfcPreferences::SetInt(const std::string& key, const int value)
67 {
68     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
69     if (!pref) {
70         ErrorLog("NfcPreferences: Preference get null");
71         return;
72     }
73     DebugLog("Set preference with key %{public}s, value %{public}d", key.c_str(), value);
74     pref->PutInt(key, value);
75     pref->Flush();
76 }
77 
GetInt(const std::string & key)78 int NfcPreferences::GetInt(const std::string& key)
79 {
80     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
81     if (!pref) {
82         ErrorLog("NfcPreferences: Preference get null");
83         return 0;
84     }
85     DebugLog("Get preference with key %{public}s", key.c_str());
86     return pref->GetInt(key, 0);
87 }
88 
Clear()89 void NfcPreferences::Clear()
90 {
91     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
92     if (!pref) {
93         ErrorLog("NfcPreferences: Preference get null");
94         return;
95     }
96     pref->Clear();
97     DebugLog("NfcPreferences: Clear preferences");
98     NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
99 }
100 
Delete(const std::string & key)101 void NfcPreferences::Delete(const std::string& key)
102 {
103     std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
104     if (!pref) {
105         ErrorLog("NfcPreferences: Preference get null");
106         return;
107     }
108     DebugLog("NfcPreferences: Delete preference with key %{public}s", key.c_str());
109     pref->Delete(key);
110     pref->FlushSync();
111 }
112 } // NFC
113 } // OHOS