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
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "PersistentStorage"};
22 }
23
24 template void PersistentStorage::Insert(const std::string&, const int&, PersistentStorageType);
25 template void PersistentStorage::Insert(const std::string&, const float&, PersistentStorageType);
26 template void PersistentStorage::Get(const std::string&, int&, PersistentStorageType);
27 template void PersistentStorage::Get(const std::string&, float&, PersistentStorageType);
28
29 std::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
HasKey(const std::string & key,PersistentStorageType storageType)34 bool 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
Delete(const std::string & key,PersistentStorageType storageType)48 void 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
GetPreference(PersistentStorageType storageType)60 std::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
73 template <typename T>
Insert(const std::string & key,const T & value,PersistentStorageType storageType)74 void 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
100 template <typename T>
Get(const std::string & key,T & value,PersistentStorageType storageType)101 void 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