1 /*
2  * Copyright (c) 2024 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 "window_display_change_adapter.h"
17 #include "window_manager_hilog.h"
18 #include "singleton_container.h"
19 #include "display_info.h"
20 #include "scene_board_judgement.h"
21 #include "ws_common.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 
WindowDisplayChangeAdapter(const sptr<IRemoteObject> & token,const sptr<IDisplayInfoChangedListener> & listener)26 WindowDisplayChangeAdapter::WindowDisplayChangeAdapter(const sptr<IRemoteObject>& token,
27                                                        const sptr<IDisplayInfoChangedListener>& listener)
28     : token_(token),
29       displayInfoChangeListener_(listener)
30 {
31     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
32         TLOGI(WmsLogTag::DMS, "sceneBoard is not useable, register this to DM.");
33         DisplayManager::GetInstance().RegisterDisplayListener(this);
34     }
35 }
36 
~WindowDisplayChangeAdapter()37 WindowDisplayChangeAdapter::~WindowDisplayChangeAdapter()
38 {
39     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
40         TLOGI(WmsLogTag::DMS, "sceneBoard is not useable, unregister this from DM.");
41         DisplayManager::GetInstance().UnregisterDisplayListener(this);
42     }
43 }
44 
OnCreate(DisplayId displayId)45 void WindowDisplayChangeAdapter::OnCreate(DisplayId displayId)
46 {
47     TLOGI(WmsLogTag::DMS, "not need notify onCreate");
48 }
49 
OnDestroy(DisplayId displayId)50 void WindowDisplayChangeAdapter::OnDestroy(DisplayId displayId)
51 {
52     TLOGI(WmsLogTag::DMS, "not need notify OnDestroy");
53 }
54 
OnChange(DisplayId displayId)55 void WindowDisplayChangeAdapter::OnChange(DisplayId displayId)
56 {
57     if (displayInfoChangeListener_ == nullptr) {
58         TLOGE(WmsLogTag::DMS, "notify display info failed because of the listener is nullptr");
59         return;
60     }
61     auto display = SingletonContainer::Get<DisplayManager>().GetDisplayById(displayId);
62     if (display == nullptr) {
63         TLOGE(WmsLogTag::DMS, "get display by displayId %{public}" PRIu64 " failed.", displayId);
64         return;
65     }
66     auto displayInfo = display->GetDisplayInfo();
67     if (displayInfo == nullptr) {
68         TLOGE(WmsLogTag::DMS, "get display info %{public}" PRIu64 " failed.", displayId);
69         return;
70     }
71     auto density = displayInfo->GetVirtualPixelRatio();
72     auto orientation = displayInfo->GetDisplayOrientation();
73     if (displayId_ == displayId && NearEqual(density_, density) && orientation_ == orientation) {
74         TLOGW(WmsLogTag::DMS, "not need notify display info, data is not changed.");
75         return;
76     }
77     displayId_ = displayId;
78     density_ = density;
79     orientation_ = orientation;
80     displayInfoChangeListener_->OnDisplayInfoChange(token_, displayId, density, orientation);
81 }
82 
OnDisplayInfoChange(const sptr<IRemoteObject> & token,DisplayId displayId,float density,DisplayOrientation orientation)83 void WindowDisplayChangeAdapter::OnDisplayInfoChange(const sptr<IRemoteObject>& token,
84     DisplayId displayId, float density, DisplayOrientation orientation)
85 {
86     if (displayInfoChangeListener_ == nullptr) {
87         TLOGE(WmsLogTag::DMS, "notify display info failed because of the listener is nullptr");
88         return;
89     }
90     if (displayId_ == displayId && NearEqual(density_, density) && orientation_ == orientation) {
91         TLOGW(WmsLogTag::DMS, "not need notify display info, data is not changed.");
92         return;
93     }
94     displayId_ = displayId;
95     density_ = density;
96     orientation_ = orientation;
97     displayInfoChangeListener_->OnDisplayInfoChange(token, displayId, density, orientation);
98 }
99 
GetListener() const100 const sptr<IDisplayInfoChangedListener> WindowDisplayChangeAdapter::GetListener() const
101 {
102     return displayInfoChangeListener_;
103 }
104 
105 } // namespace Rosen
106 } // namespace OHOS
107