• 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 #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 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ScreenSettingHelper" };
25 }
26 
27 sptr<SettingObserver> ScreenSettingHelper::dpiObserver_;
28 
RegisterSettingDpiObserver(SettingObserver::UpdateFunc func)29 void ScreenSettingHelper::RegisterSettingDpiObserver(SettingObserver::UpdateFunc func)
30 {
31     if (dpiObserver_) {
32         WLOGFD("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         WLOGFW("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         WLOGFD("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         WLOGFW("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         WLOGFW("get setting dpi failed, ret=%{public}d", ret);
65         return false;
66     }
67     dpi = static_cast<uint32_t>(value);
68     return true;
69 }
70 } // namespace Rosen
71 } // namespace OHOS