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 OHOS_ROSEN_WINDOW_PERSISTENT_STORAGE_H 17 #define OHOS_ROSEN_WINDOW_PERSISTENT_STORAGE_H 18 19 #include "preferences.h" 20 #include "preferences_helper.h" 21 22 #include "window_manager_hilog.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 using PersistentPerference = NativePreferences::Preferences; 27 28 enum class ScenePersistentStorageType : uint32_t { 29 UKNOWN = 0, 30 ASPECT_RATIO, 31 MAXIMIZE_STATE, 32 PIP_INFO, 33 }; 34 35 class ScenePersistentStorage { 36 public: 37 ScenePersistentStorage() = default; 38 ~ScenePersistentStorage() = default; 39 40 static constexpr int32_t MAX_KEY_LEN = 80; // 80: max length of preference's key 41 42 template <typename T> Insert(const std::string & key,const T & value,ScenePersistentStorageType storageType)43 static void Insert(const std::string& key, const T& value, ScenePersistentStorageType storageType) 44 { 45 auto pref = GetPreference(storageType); 46 if (!pref) { 47 WLOGE("[ScenePersistentStorage] Preferences is nullptr"); 48 return; 49 } 50 switch (storageType) { 51 case ScenePersistentStorageType::ASPECT_RATIO: { 52 pref->PutFloat(key, value); 53 WLOGD("[ScenePersistentStorage] Insert aspect ratio, key %{public}s, value %{public}f", 54 key.c_str(), static_cast<float>(value)); 55 break; 56 } 57 case ScenePersistentStorageType::MAXIMIZE_STATE: { 58 pref->PutInt(key, value); 59 WLOGD("[ScenePersistentStorage] Insert Maximize state, key %{public}s, value %{public}d", 60 key.c_str(), static_cast<int>(value)); 61 break; 62 } 63 case ScenePersistentStorageType::PIP_INFO: { 64 pref->PutInt(key, value); 65 WLOGD("[ScenePersistentStorage] Insert PicutreInPicture info, key %{public}s, value %{public}d", 66 key.c_str(), static_cast<int>(value)); 67 break; 68 } 69 default: 70 WLOGW("[ScenePersistentStorage] Unknown storage type!"); 71 } 72 pref->Flush(); 73 } 74 75 template <typename T> Get(const std::string & key,T & value,ScenePersistentStorageType storageType)76 static void Get(const std::string& key, T& value, ScenePersistentStorageType storageType) 77 { 78 auto pref = GetPreference(storageType); 79 if (!pref) { 80 WLOGE("[ScenePersistentStorage] Preferences is nullptr"); 81 return; 82 } 83 switch (storageType) { 84 case ScenePersistentStorageType::ASPECT_RATIO: { 85 value = pref->GetFloat(key); 86 WLOGD("[ScenePersistentStorage] Get aspect ratio, key: %{public}s, value:%{public}f", 87 key.c_str(), static_cast<float>(value)); 88 break; 89 } 90 case ScenePersistentStorageType::MAXIMIZE_STATE: { 91 value = pref->GetInt(key); 92 WLOGD("[ScenePersistentStorage] Get Maximize state, key: %{public}s, value:%{public}d", 93 key.c_str(), static_cast<int>(value)); 94 break; 95 } 96 case ScenePersistentStorageType::PIP_INFO: { 97 value = pref->GetInt(key); 98 WLOGD("[ScenePersistentStorage] Get PicutreInPicture info, key: %{public}s, value:%{public}d", 99 key.c_str(), static_cast<int>(value)); 100 break; 101 } 102 default: 103 WLOGW("[ScenePersistentStorage] Unknown storage type!"); 104 } 105 } 106 107 static bool HasKey(const std::string& key, ScenePersistentStorageType storageType); 108 static void Delete(const std::string& key, ScenePersistentStorageType storageType); 109 static void InitDir(std::string dir); 110 111 private: 112 static constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "ScenePersistentStorage"}; 113 static std::string saveDir_; 114 static std::shared_ptr<PersistentPerference> GetPreference(ScenePersistentStorageType storageType); 115 static std::map<ScenePersistentStorageType, std::string> storagePath_; 116 }; 117 } // namespace Rosen 118 } // namespace OHOS 119 #endif // OHOS_ROSEN_WINDOW_PERSISTENT_STORAGE_H 120