• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "wm_common_inner.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 using PersistentPerference = NativePreferences::Preferences;
28 class PersistentStorage {
29 public:
30     PersistentStorage() = default;
31     ~PersistentStorage() = default;
32 
33     template <typename T>
Insert(const std::string & key,const T & value,PersistentStorageType storageType)34     static void Insert(const std::string& key, const T& value, PersistentStorageType storageType)
35     {
36         auto pref = GetPreference(storageType);
37         if (!pref) {
38             WLOG_E("[PersistentStorage] Preferences is nullptr");
39             return;
40         }
41         switch (storageType) {
42             case PersistentStorageType::ASPECT_RATIO: {
43                 pref->PutFloat(key, value);
44                 WLOG_D("[PersistentStorage] Insert aspect ratio, key %{public}s, value %{public}f",
45                     key.c_str(), static_cast<float>(value));
46                 break;
47             }
48             case PersistentStorageType::MAXIMIZE_STATE: {
49                 pref->PutInt(key, value);
50                 WLOG_D("[PersistentStorage] Insert Maximize state, key %{public}s, value %{public}d",
51                     key.c_str(), static_cast<int>(value));
52                 break;
53             }
54             default:
55                 WLOG_W("[PersistentStorage] Unknown storage type!");
56         }
57         pref->Flush();
58     }
59 
60     template <typename T>
Get(const std::string & key,T & value,PersistentStorageType storageType)61     static void Get(const std::string& key, T& value, PersistentStorageType storageType)
62     {
63         auto pref = GetPreference(storageType);
64         if (!pref) {
65             WLOG_E("[PersistentStorage] Preferences is nullptr");
66             return;
67         }
68         switch (storageType) {
69             case PersistentStorageType::ASPECT_RATIO: {
70                 value = pref->GetFloat(key);
71                 WLOG_D("[PersistentStorage] Get aspect ratio, key: %{public}s, value:%{public}f",
72                     key.c_str(), static_cast<float>(value));
73                 break;
74             }
75             case PersistentStorageType::MAXIMIZE_STATE: {
76                 value = pref->GetInt(key);
77                 WLOG_D("[PersistentStorage] Get Maximize state, key: %{public}s, value:%{public}d",
78                     key.c_str(), static_cast<int>(value));
79                 break;
80             }
81             default:
82                 WLOG_W("[PersistentStorage] Unknown storage type!");
83         }
84     }
85 
86     static bool HasKey(const std::string& key, PersistentStorageType storageType);
87     static void Delete(const std::string& key, PersistentStorageType storageType);
88 
89 private:
90     static std::shared_ptr<PersistentPerference> GetPreference(PersistentStorageType storageType);
91     static std::map<PersistentStorageType, std::string> storagePath_;
92 };
93 } // namespace Rosen
94 } // namespace OHOS
95 #endif // OHOS_ROSEN_WINDOW_PERSISTENT_STORAGE_H