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#include "persistent_storage.h" 17 18namespace OHOS { 19namespace Rosen { 20namespace { 21constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "PersistentStorage"}; 22} 23 24template void PersistentStorage::Insert(const std::string&, const int&, PersistentStorageType); 25template void PersistentStorage::Insert(const std::string&, const float&, PersistentStorageType); 26template void PersistentStorage::Get(const std::string&, int&, PersistentStorageType); 27template void PersistentStorage::Get(const std::string&, float&, PersistentStorageType); 28 29std::map<PersistentStorageType, std::string> PersistentStorage::storagePath_ = { 30 { PersistentStorageType::ASPECT_RATIO, "/data/service/el1/public/window/window_aspect_ratio.xml" }, 31 { PersistentStorageType::MAXIMIZE_STATE, "/data/service/el1/public/window/window_maximize_state.xml" }, 32}; 33 34bool PersistentStorage::HasKey(const std::string& key, PersistentStorageType storageType) 35{ 36 bool res = false; 37 auto pref = GetPreference(storageType); 38 if (!pref) { 39 WLOGE("[PersistentStorage] Preferences is nullptr"); 40 return res; 41 } 42 res = pref->HasKey(key); 43 WLOGD("[PersistentStorage] %{public}s %{public}s", key.c_str(), 44 (res ? "Has persisted key:" : "Don't have persisted key:")); 45 return res; 46} 47 48void PersistentStorage::Delete(const std::string& key, PersistentStorageType storageType) 49{ 50 auto pref = GetPreference(storageType); 51 if (!pref) { 52 WLOGE("[PersistentStorage] Preferences is nullptr"); 53 return; 54 } 55 pref->Delete(key); 56 pref->Flush(); 57 WLOGD("[PersistentStorage] Delete key %{public}s", key.c_str()); 58} 59 60std::shared_ptr<PersistentPerference> PersistentStorage::GetPreference(PersistentStorageType storageType) 61{ 62 auto iter = storagePath_.find(storageType); 63 if (iter == storagePath_.end()) { 64 return nullptr; 65 } 66 auto fileName = storagePath_[storageType]; 67 int errCode; 68 auto pref = NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode); 69 WLOGD("[PersistentStorage] GetPreference fileName: %{public}s, errCode: %{public}d", fileName.c_str(), errCode); 70 return pref; 71} 72 73template <typename T> 74void PersistentStorage::Insert(const std::string& key, const T& value, PersistentStorageType storageType) 75{ 76 auto pref = GetPreference(storageType); 77 if (!pref) { 78 WLOGFE("[PersistentStorage] Preferences is nullptr"); 79 return; 80 } 81 switch (storageType) { 82 case PersistentStorageType::ASPECT_RATIO: { 83 pref->PutFloat(key, value); 84 WLOGFD("[PersistentStorage] Insert aspect ratio, key %{public}s, value %{public}f", 85 key.c_str(), static_cast<float>(value)); 86 break; 87 } 88 case PersistentStorageType::MAXIMIZE_STATE: { 89 pref->PutInt(key, value); 90 WLOGFD("[PersistentStorage] Insert Maximize state, key %{public}s, value %{public}d", 91 key.c_str(), static_cast<int>(value)); 92 break; 93 } 94 default: 95 WLOGFW("[PersistentStorage] Unknown storage type!"); 96 } 97 pref->Flush(); 98} 99 100template <typename T> 101void PersistentStorage::Get(const std::string& key, T& value, PersistentStorageType storageType) 102{ 103 auto pref = GetPreference(storageType); 104 if (!pref) { 105 WLOGFE("[PersistentStorage] Preferences is nullptr"); 106 return; 107 } 108 switch (storageType) { 109 case PersistentStorageType::ASPECT_RATIO: { 110 value = pref->GetFloat(key); 111 WLOGFD("[PersistentStorage] Get aspect ratio, key: %{public}s, value:%{public}f", 112 key.c_str(), static_cast<float>(value)); 113 break; 114 } 115 case PersistentStorageType::MAXIMIZE_STATE: { 116 value = pref->GetInt(key); 117 WLOGFD("[PersistentStorage] Get Maximize state, key: %{public}s, value:%{public}d", 118 key.c_str(), static_cast<int>(value)); 119 break; 120 } 121 default: 122 WLOGFW("[PersistentStorage] Unknown storage type!"); 123 } 124} 125} // namespace Rosen 126} // namespace OHOS