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 #include "screen_setting_helper.h"
16
17 #include "window_manager_hilog.h"
18 #include "setting_provider.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 sptr<SettingObserver> ScreenSettingHelper::dpiObserver_;
24 sptr<SettingObserver> ScreenSettingHelper::castObserver_;
25 sptr<SettingObserver> ScreenSettingHelper::rotationObserver_;
26 sptr<SettingObserver> ScreenSettingHelper::screenSkipProtectedWindowObserver_;
27 const std::string SCREEN_SHARE_PROTECT_TABLE = "USER_SETTINGDATA_SECURE_";
28
RegisterSettingDpiObserver(SettingObserver::UpdateFunc func)29 void ScreenSettingHelper::RegisterSettingDpiObserver(SettingObserver::UpdateFunc func)
30 {
31 if (dpiObserver_) {
32 TLOGD(WmsLogTag::DMS, "setting dpi observer is already registered");
33 return;
34 }
35 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
36 dpiObserver_ = provider.CreateObserver(SETTING_DPI_KEY, func);
37 ErrCode ret = provider.RegisterObserver(dpiObserver_);
38 if (ret != ERR_OK) {
39 TLOGW(WmsLogTag::DMS, "register setting dpi observer failed, ret=%{public}d", ret);
40 dpiObserver_ = nullptr;
41 }
42 }
43
UnregisterSettingDpiObserver()44 void ScreenSettingHelper::UnregisterSettingDpiObserver()
45 {
46 if (dpiObserver_ == nullptr) {
47 TLOGD(WmsLogTag::DMS, "dpiObserver_ is nullptr, no need to unregister");
48 return;
49 }
50 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
51 ErrCode ret = provider.UnregisterObserver(dpiObserver_);
52 if (ret != ERR_OK) {
53 TLOGW(WmsLogTag::DMS, "unregister setting dpi observer failed, ret=%{public}d", ret);
54 }
55 dpiObserver_ = nullptr;
56 }
57
GetSettingDpi(uint32_t & dpi,const std::string & key)58 bool ScreenSettingHelper::GetSettingDpi(uint32_t& dpi, const std::string& key)
59 {
60 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
61 int32_t value;
62 ErrCode ret = provider.GetIntValue(key, value);
63 if (ret != ERR_OK) {
64 TLOGW(WmsLogTag::DMS, "get setting dpi failed, ret=%{public}d", ret);
65 return false;
66 }
67 dpi = static_cast<uint32_t>(value);
68 return true;
69 }
70
GetSettingValue(uint32_t & value,const std::string & key)71 bool ScreenSettingHelper::GetSettingValue(uint32_t& value, const std::string& key)
72 {
73 SettingProvider& settingData = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
74 int32_t getValue;
75 ErrCode ret = settingData.GetIntValue(key, getValue);
76 if (ret != ERR_OK) {
77 TLOGW(WmsLogTag::DMS, "get setting value failed, ret=%{public}d", ret);
78 return false;
79 }
80 value = static_cast<uint32_t>(getValue);
81 return true;
82 }
83
SetSettingDefaultDpi(uint32_t & dpi,const std::string & key)84 bool ScreenSettingHelper::SetSettingDefaultDpi(uint32_t& dpi, const std::string& key)
85 {
86 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
87 ErrCode ret = provider.PutIntValue(key, dpi, false);
88 if (ret != ERR_OK) {
89 TLOGW(WmsLogTag::DMS, "put int value failed, ret=%{public}d", ret);
90 return false;
91 }
92 return true;
93 }
94
RegisterSettingCastObserver(SettingObserver::UpdateFunc func)95 void ScreenSettingHelper::RegisterSettingCastObserver(SettingObserver::UpdateFunc func)
96 {
97 if (castObserver_) {
98 TLOGD(WmsLogTag::DMS, "setting cast observer is already registered");
99 return;
100 }
101 SettingProvider& castProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
102 castObserver_ = castProvider.CreateObserver(SETTING_CAST_KEY, func);
103 if (castObserver_ == nullptr) {
104 TLOGE(WmsLogTag::DMS, "create observer failed");
105 return;
106 }
107 ErrCode ret = castProvider.RegisterObserver(castObserver_);
108 if (ret != ERR_OK) {
109 TLOGW(WmsLogTag::DMS, "register setting cast observer failed, ret=%{public}d", ret);
110 castObserver_ = nullptr;
111 }
112 }
113
UnregisterSettingCastObserver()114 void ScreenSettingHelper::UnregisterSettingCastObserver()
115 {
116 if (castObserver_ == nullptr) {
117 TLOGD(WmsLogTag::DMS, "castObserver_ is nullptr, no need to unregister");
118 return;
119 }
120 SettingProvider& castProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
121 ErrCode ret = castProvider.UnregisterObserver(castObserver_);
122 if (ret != ERR_OK) {
123 TLOGW(WmsLogTag::DMS, "unregister setting cast observer failed, ret=%{public}d", ret);
124 }
125 castObserver_ = nullptr;
126 }
127
GetSettingCast(bool & enable,const std::string & key)128 bool ScreenSettingHelper::GetSettingCast(bool& enable, const std::string& key)
129 {
130 SettingProvider& castProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
131 ErrCode ret = castProvider.GetBoolValue(key, enable);
132 if (ret != ERR_OK) {
133 TLOGW(WmsLogTag::DMS, "get setting dpi failed, ret=%{public}d", ret);
134 return false;
135 }
136 return true;
137 }
138
RegisterSettingRotationObserver(SettingObserver::UpdateFunc func)139 void ScreenSettingHelper::RegisterSettingRotationObserver(SettingObserver::UpdateFunc func)
140 {
141 if (rotationObserver_ != nullptr) {
142 TLOGI(WmsLogTag::DMS, "setting rotation observer is already registered");
143 return;
144 }
145 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
146 rotationObserver_ = settingProvider.CreateObserver(SETTING_ROTATION_KEY, func);
147 if (rotationObserver_ == nullptr) {
148 TLOGE(WmsLogTag::DMS, "create observer failed");
149 return;
150 }
151 ErrCode ret = settingProvider.RegisterObserver(rotationObserver_);
152 if (ret != ERR_OK) {
153 TLOGE(WmsLogTag::DMS, "register setting rotation observer failed, ret:%{public}d", ret);
154 rotationObserver_ = nullptr;
155 }
156 }
157
UnregisterSettingRotationObserver()158 void ScreenSettingHelper::UnregisterSettingRotationObserver()
159 {
160 if (rotationObserver_ == nullptr) {
161 TLOGI(WmsLogTag::DMS, "rotationObserver_ is nullptr, no need to unregister");
162 return;
163 }
164 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
165 ErrCode ret = settingProvider.UnregisterObserver(rotationObserver_);
166 if (ret != ERR_OK) {
167 TLOGE(WmsLogTag::DMS, "unregister setting rotation observer failed, ret:%{public}d", ret);
168 }
169 rotationObserver_ = nullptr;
170 }
171
SetSettingRotation(int32_t rotation)172 void ScreenSettingHelper::SetSettingRotation(int32_t rotation)
173 {
174 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
175 ErrCode ret = settingProvider.PutIntValue(SETTING_ROTATION_KEY, rotation, true);
176 if (ret != ERR_OK) {
177 TLOGE(WmsLogTag::DMS, "set setting rotation failed, ret:%{public}d", ret);
178 return;
179 }
180 TLOGE(WmsLogTag::DMS, "set setting rotation succeed, ret:%{public}d", ret);
181 }
182
GetSettingRotation(int32_t & rotation,const std::string & key)183 bool ScreenSettingHelper::GetSettingRotation(int32_t& rotation, const std::string& key)
184 {
185 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
186 ErrCode ret = settingProvider.GetIntValue(key, rotation);
187 if (ret != ERR_OK) {
188 TLOGE(WmsLogTag::DMS, "get setting rotation failed, ret:%{public}d", ret);
189 return false;
190 }
191 TLOGE(WmsLogTag::DMS, "current rotation:%{public}d", rotation);
192 return true;
193 }
194
SetSettingRotationScreenId(int32_t screenId)195 void ScreenSettingHelper::SetSettingRotationScreenId(int32_t screenId)
196 {
197 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
198 ErrCode ret = settingProvider.PutIntValue(SETTING_ROTATION_SCREEN_ID_KEY, screenId, false);
199 if (ret != ERR_OK) {
200 TLOGE(WmsLogTag::DMS, "set setting rotation screen id failed, ret:%{public}d", ret);
201 return;
202 }
203 TLOGE(WmsLogTag::DMS, "set setting rotation screen id succeed, ret:%{public}d", ret);
204 }
205
GetSettingRotationScreenId(int32_t & screenId,const std::string & key)206 bool ScreenSettingHelper::GetSettingRotationScreenId(int32_t& screenId, const std::string& key)
207 {
208 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
209 ErrCode ret = settingProvider.GetIntValue(key, screenId);
210 if (ret != ERR_OK) {
211 TLOGE(WmsLogTag::DMS, "get setting rotation screen id failed, ret:%{public}d", ret);
212 return false;
213 }
214 TLOGE(WmsLogTag::DMS, "current rotation screen id:%{public}d", screenId);
215 return true;
216 }
217
RegisterSettingscreenSkipProtectedWindowObserver(SettingObserver::UpdateFunc func)218 void ScreenSettingHelper::RegisterSettingscreenSkipProtectedWindowObserver(SettingObserver::UpdateFunc func)
219 {
220 if (screenSkipProtectedWindowObserver_ != nullptr) {
221 TLOGI(WmsLogTag::DMS, "observer is registered");
222 return;
223 }
224 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
225 screenSkipProtectedWindowObserver_ = settingProvider.CreateObserver(SETTING_SCREEN_SHARE_PROTECT_KEY, func);
226 if (screenSkipProtectedWindowObserver_ == nullptr) {
227 TLOGE(WmsLogTag::DMS, "create observer failed");
228 return;
229 }
230 ErrCode ret = settingProvider.RegisterObserverByTable(screenSkipProtectedWindowObserver_,
231 SCREEN_SHARE_PROTECT_TABLE);
232 if (ret != ERR_OK) {
233 TLOGE(WmsLogTag::DMS, "failed, ret:%{public}d", ret);
234 screenSkipProtectedWindowObserver_ = nullptr;
235 }
236 }
237
UnregisterSettingscreenSkipProtectedWindowObserver()238 void ScreenSettingHelper::UnregisterSettingscreenSkipProtectedWindowObserver()
239 {
240 if (screenSkipProtectedWindowObserver_ == nullptr) {
241 TLOGI(WmsLogTag::DMS, "observer is nullptr");
242 return;
243 }
244 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
245 ErrCode ret = settingProvider.UnregisterObserverByTable(screenSkipProtectedWindowObserver_,
246 SCREEN_SHARE_PROTECT_TABLE);
247 if (ret != ERR_OK) {
248 TLOGE(WmsLogTag::DMS, "failed, ret:%{public}d", ret);
249 }
250 screenSkipProtectedWindowObserver_ = nullptr;
251 }
252
GetSettingscreenSkipProtectedWindow(bool & enable,const std::string & key)253 bool ScreenSettingHelper::GetSettingscreenSkipProtectedWindow(bool& enable, const std::string& key)
254 {
255 int32_t value = 0;
256 SettingProvider& settingProvider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_SA_ID);
257 ErrCode ret = settingProvider.GetIntValueMultiUserByTable(key, value, SCREEN_SHARE_PROTECT_TABLE);
258 if (ret != ERR_OK) {
259 TLOGE(WmsLogTag::DMS, "failed, ret=%{public}d", ret);
260 return false;
261 }
262 enable = (value == 1);
263 return true;
264 }
265 } // namespace Rosen
266 } // namespace OHOS
267