1 /* 2 * Copyright (c) 2023 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 #ifndef PREFERENCES_UTILS_H 17 #define PREFERENCES_UTILS_H 18 19 #include <ohos_types.h> 20 #include <string> 21 #include <vector> 22 23 #ifdef NATIVE_PREFERENCES_ENABLE 24 #include "preferences.h" 25 #include "preferences_errno.h" 26 #endif 27 28 namespace OHOS { 29 namespace UpdateEngine { 30 class PreferencesUtil { 31 public: 32 PreferencesUtil() = default; 33 virtual ~PreferencesUtil() = default; 34 35 bool SaveString(const std::string &key, const std::string &value); 36 bool SaveInt(const std::string &key, int value); 37 bool SaveBool(const std::string &key, bool value); 38 bool SaveLong(const std::string &key, int64_t value); 39 bool SaveFloat(const std::string &key, float value); 40 41 std::string ObtainString(const std::string &key, const std::string &defValue); 42 int ObtainInt(const std::string &key, int defValue); 43 bool ObtainBool(const std::string &key, bool defValue); 44 int64_t ObtainLong(const std::string &key, int64_t defValue); 45 float ObtainFloat(const std::string &key, float defValue); 46 47 bool IsExist(const std::string &key); 48 bool Remove(const std::string &key); 49 bool RemoveAll(); 50 51 bool DeletePreference(); 52 53 protected: 54 virtual std::string GetPath() = 0; 55 56 private: 57 #ifdef NATIVE_PREFERENCES_ENABLE 58 std::shared_ptr<NativePreferences::Preferences> GetPreference(); 59 #endif 60 61 bool RefreshSync(); 62 63 template <typename T> 64 bool Save(const std::string &key, const T &value); 65 66 #ifdef NATIVE_PREFERENCES_ENABLE 67 bool SaveInner( 68 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const std::string &value); 69 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int &value); 70 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &value); 71 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int64_t &value); 72 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const float &value); 73 #endif 74 template <typename T> 75 T Obtain(const std::string &key, const T &defValue); 76 77 #ifdef NATIVE_PREFERENCES_ENABLE 78 std::string ObtainInner( 79 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const std::string &defValue); 80 int ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int &defValue); 81 bool ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &defValue); 82 int64_t ObtainInner( 83 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int64_t &defValue); 84 float ObtainInner( 85 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const float &defValue); 86 #endif 87 }; 88 } // namespace UpdateEngine 89 } // namespace OHOS 90 #endif // PREFERENCES_UTILS_H