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