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 "display_change_event_listener.h"
17 
18 #undef LOG_TAG
19 #define LOG_TAG "DisplayChangeEventListener"
20 
21 namespace OHOS {
22 namespace Msdp {
23 namespace DeviceStatus {
24 
DisplayChangeEventListener(IContext * context)25 DisplayChangeEventListener::DisplayChangeEventListener(IContext *context)
26     : context_(context)
27 {
28 }
29 
OnCreate(Rosen::DisplayId displayId)30 void DisplayChangeEventListener::OnCreate(Rosen::DisplayId displayId)
31 {
32     FI_HILOGI("display:%{public}" PRIu64"", displayId);
33 }
34 
OnDestroy(Rosen::DisplayId displayId)35 void DisplayChangeEventListener::OnDestroy(Rosen::DisplayId displayId)
36 {
37     FI_HILOGI("display:%{public}" PRIu64"", displayId);
38 }
39 
OnChange(Rosen::DisplayId displayId)40 void DisplayChangeEventListener::OnChange(Rosen::DisplayId displayId)
41 {
42     if (Rosen::DisplayManager::GetInstance().IsFoldable()) {
43         Rosen::FoldDisplayMode foldMode = Rosen::DisplayManager::GetInstance().GetFoldDisplayMode();
44         if (foldMode == Rosen::FoldDisplayMode::FULL) {
45             if (lastRotation_ == Rosen::Rotation::ROTATION_0) {
46                 FI_HILOGD("FoldDisplayMode is full");
47                 return;
48             }
49             lastRotation_ = Rosen::Rotation::ROTATION_0;
50             CHKPV(context_);
51             int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this] {
52                 return this->context_->GetDragManager().RotateDragWindow(Rosen::Rotation::ROTATION_0);
53             });
54             if (ret != RET_OK) {
55                 FI_HILOGE("Post async task failed");
56             }
57             return;
58         }
59     }
60     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
61     if (display == nullptr) {
62         FI_HILOGD("Get display info failed, display:%{public}" PRIu64"", displayId);
63         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
64         if (display == nullptr) {
65             FI_HILOGE("Get display info failed, display is nullptr");
66             return;
67         }
68     }
69     Rosen::Rotation currentRotation = display->GetRotation();
70     if (currentRotation == lastRotation_) {
71         return;
72     }
73 
74     bool isScreenRotation = false;
75     std::vector<std::string> foldRotatePolicys;
76     GetRotatePolicy(isScreenRotation, foldRotatePolicys);
77     if (isScreenRotation) {
78         ScreenRotate(currentRotation, lastRotation_);
79         lastRotation_ = currentRotation;
80         return;
81     }
82     lastRotation_ = currentRotation;
83     RotateDragWindow(currentRotation);
84 }
85 
RotateDragWindow(Rosen::Rotation rotation)86 void DisplayChangeEventListener::RotateDragWindow(Rosen::Rotation rotation)
87 {
88     CHKPV(context_);
89     int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, rotation] {
90         return this->context_->GetDragManager().RotateDragWindow(rotation);
91     });
92     if (ret != RET_OK) {
93         FI_HILOGE("Post async task failed");
94     }
95 }
96 
ScreenRotate(Rosen::Rotation rotation,Rosen::Rotation lastRotation)97 void DisplayChangeEventListener::ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation)
98 {
99     CHKPV(context_);
100     int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, rotation, lastRotation] {
101         return this->context_->GetDragManager().ScreenRotate(rotation, lastRotation);
102     });
103     if (ret != RET_OK) {
104         FI_HILOGE("Post async task failed");
105     }
106 }
107 
DisplayAbilityStatusChange(IContext * context)108 DisplayAbilityStatusChange::DisplayAbilityStatusChange(IContext *context)
109     : context_(context)
110 {}
111 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)112 void DisplayAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
113 {
114     FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
115     if (systemAbilityId != DISPLAY_MANAGER_SERVICE_SA_ID) {
116         FI_HILOGE("systemAbilityId is not DISPLAY_MANAGER_SERVICE_SA_ID");
117         return;
118     }
119     CHKPV(context_);
120     displayChangeEventListener_ = sptr<DisplayChangeEventListener>::MakeSptr(context_);
121     CHKPV(displayChangeEventListener_);
122     Rosen::DisplayManager::GetInstance().RegisterDisplayListener(displayChangeEventListener_);
123 }
124 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)125 void DisplayAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
126 {
127     FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
128 }
129 
AppStateObserverStatusChange(IContext * context)130 AppStateObserverStatusChange::AppStateObserverStatusChange(IContext *context)
131     : context_(context)
132 {}
133 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)134 void AppStateObserverStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
135 {
136     FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
137     if (systemAbilityId != APP_MGR_SERVICE_ID) {
138         FI_HILOGE("systemAbilityId is not APP_MGR_SERVICE_ID");
139         return;
140     }
141     CHKPV(context_);
142     context_->GetSocketSessionManager().RegisterApplicationState();
143 }
144 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)145 void AppStateObserverStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
146 {
147     FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
148 }
149 } // namespace DeviceStatus
150 } // namespace Msdp
151 } // namespace OHOS