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 19namespace OHOS { 20namespace NFC { 21NfcPreferences::NfcPreferences() 22{ 23 fileName_ = "/data/nfc/nfc_preferences.xml"; 24 errCode_ = 0; 25} 26 27NfcPreferences::~NfcPreferences() 28{ 29} 30 31NfcPreferences& NfcPreferences::GetInstance() 32{ 33 static NfcPreferences sNfcPrefImpl; 34 return sNfcPrefImpl; 35} 36 37std::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 43void 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 55std::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 66void 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 78int 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 89void 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 101void 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 113void NfcPreferences::UpdateNfcState(int newState) 114{ 115 SetInt(PREF_KEY_STATE, newState); 116 117 Uri nfcEnableUri(KITS::NFC_DATA_URI); 118 KITS::ErrorCode err = DelayedSingleton<NfcDataShareImpl>::GetInstance()-> 119 SetValue(nfcEnableUri, KITS::DATA_SHARE_KEY_STATE, newState); 120 if (err != ERR_NONE) { 121 ErrorLog("NfcPreferences: UpdateNfcState set datashare fail, newState = %{public}d", newState); 122 } 123} 124 125int NfcPreferences::GetNfcState() 126{ 127 return GetInt(PREF_KEY_STATE); 128} 129} // NFC 130} // OHOS