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 #include "devicestatus_define.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "DisplayChangeEventListener"
22
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 namespace {
27 constexpr int32_t INDEX_FOLDED { 0 };
28 constexpr int32_t INDEX_EXPAND { 1 };
29 constexpr size_t MAX_INDEX_LENGTH { 2 };
30 const std::string SCREEN_ROTATION { "1" };
31 } // namespace
32
DisplayChangeEventListener(IContext * context)33 DisplayChangeEventListener::DisplayChangeEventListener(IContext *context)
34 : context_(context)
35 {
36 }
37
OnCreate(Rosen::DisplayId displayId)38 void DisplayChangeEventListener::OnCreate(Rosen::DisplayId displayId)
39 {
40 FI_HILOGI("display:%{public}" PRIu64"", displayId);
41 }
42
OnDestroy(Rosen::DisplayId displayId)43 void DisplayChangeEventListener::OnDestroy(Rosen::DisplayId displayId)
44 {
45 FI_HILOGI("display:%{public}" PRIu64"", displayId);
46 }
47
OnChange(Rosen::DisplayId displayId)48 void DisplayChangeEventListener::OnChange(Rosen::DisplayId displayId)
49 {
50 if (Rosen::DisplayManager::GetInstance().IsFoldable()) {
51 bool isScreenRotation = false;
52 std::vector<std::string> foldRotatePolicys;
53 GetRotatePolicy(isScreenRotation, foldRotatePolicys);
54 if (foldRotatePolicys.size() < MAX_INDEX_LENGTH) {
55 FI_HILOGE("foldRotatePolicys is invalid");
56 return;
57 }
58 Rosen::FoldStatus foldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
59 bool isExpand = (foldStatus == Rosen::FoldStatus::EXPAND);
60 bool isFold = (foldStatus == Rosen::FoldStatus::FOLDED);
61 if (IsSecondaryDevice()) {
62 isExpand = (foldStatus == Rosen::FoldStatus::EXPAND || foldStatus == Rosen::FoldStatus::HALF_FOLD ||
63 foldStatus == Rosen::FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_EXPAND ||
64 foldStatus == Rosen::FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_HALF_FOLDED ||
65 foldStatus == Rosen::FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_EXPAND ||
66 foldStatus == Rosen::FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_HALF_FOLDED);
67 isFold = (foldStatus == Rosen::FoldStatus::FOLDED ||
68 foldStatus == Rosen::FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_EXPAND ||
69 foldStatus == Rosen::FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_HALF_FOLDED);
70 }
71 if ((isExpand && (foldRotatePolicys[INDEX_EXPAND] == SCREEN_ROTATION)) ||
72 (isFold && (foldRotatePolicys[INDEX_FOLDED] == SCREEN_ROTATION))) {
73 if (lastRotation_ == Rosen::Rotation::ROTATION_0) {
74 FI_HILOGD("Last rotation is zero");
75 return;
76 }
77 lastRotation_ = Rosen::Rotation::ROTATION_0;
78 CHKPV(context_);
79 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this] {
80 CHKPR(this->context_, RET_ERR);
81 return this->context_->GetDragManager().RotateDragWindow(Rosen::Rotation::ROTATION_0);
82 });
83 if (ret != RET_OK) {
84 FI_HILOGE("Post async task failed");
85 }
86 return;
87 }
88 }
89 sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
90 if (display == nullptr) {
91 FI_HILOGW("Get display info failed, display:%{public}" PRIu64"", displayId);
92 display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
93 if (display == nullptr) {
94 FI_HILOGE("Get display info failed, display is nullptr");
95 return;
96 }
97 }
98 Rosen::Rotation currentRotation = display->GetRotation();
99 if (currentRotation == lastRotation_) {
100 return;
101 }
102
103 bool isScreenRotation = false;
104 std::vector<std::string> foldRotatePolicys;
105 GetRotatePolicy(isScreenRotation, foldRotatePolicys);
106 FI_HILOGI("Current rotation:%{public}d, lastRotation:%{public}d",
107 static_cast<int32_t>(currentRotation), static_cast<int32_t>(lastRotation_));
108 if (isScreenRotation) {
109 ScreenRotate(currentRotation, lastRotation_);
110 lastRotation_ = currentRotation;
111 return;
112 }
113 lastRotation_ = currentRotation;
114 RotateDragWindow(currentRotation);
115 }
116
RotateDragWindow(Rosen::Rotation rotation)117 void DisplayChangeEventListener::RotateDragWindow(Rosen::Rotation rotation)
118 {
119 FI_HILOGI("Rotation:%{public}d", static_cast<int32_t>(rotation));
120 CHKPV(context_);
121 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, rotation] {
122 CHKPR(this->context_, RET_ERR);
123 return this->context_->GetDragManager().RotateDragWindow(rotation);
124 });
125 if (ret != RET_OK) {
126 FI_HILOGE("Post async task failed");
127 }
128 }
129
ScreenRotate(Rosen::Rotation rotation,Rosen::Rotation lastRotation)130 void DisplayChangeEventListener::ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation)
131 {
132 FI_HILOGI("Rotation:%{public}d, lastRotation:%{public}d",
133 static_cast<int32_t>(rotation), static_cast<int32_t>(lastRotation));
134 CHKPV(context_);
135 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, rotation, lastRotation] {
136 CHKPR(this->context_, RET_ERR);
137 return this->context_->GetDragManager().ScreenRotate(rotation, lastRotation);
138 });
139 if (ret != RET_OK) {
140 FI_HILOGE("Post async task failed");
141 }
142 }
143
DisplayAbilityStatusChange(IContext * context)144 DisplayAbilityStatusChange::DisplayAbilityStatusChange(IContext *context)
145 : context_(context)
146 {}
147
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)148 void DisplayAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
149 {
150 FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
151 if (systemAbilityId != DISPLAY_MANAGER_SERVICE_SA_ID) {
152 FI_HILOGE("systemAbilityId is not DISPLAY_MANAGER_SERVICE_SA_ID");
153 return;
154 }
155 CHKPV(context_);
156 displayChangeEventListener_ = sptr<DisplayChangeEventListener>::MakeSptr(context_);
157 CHKPV(displayChangeEventListener_);
158 Rosen::DisplayManager::GetInstance().RegisterDisplayListener(displayChangeEventListener_);
159 }
160
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)161 void DisplayAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
162 {
163 FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
164 }
165
AppStateObserverStatusChange(IContext * context)166 AppStateObserverStatusChange::AppStateObserverStatusChange(IContext *context)
167 : context_(context)
168 {}
169
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)170 void AppStateObserverStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
171 {
172 FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
173 if (systemAbilityId != APP_MGR_SERVICE_ID) {
174 FI_HILOGE("systemAbilityId is not APP_MGR_SERVICE_ID");
175 return;
176 }
177 CHKPV(context_);
178 context_->GetSocketSessionManager().RegisterApplicationState();
179 }
180
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)181 void AppStateObserverStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
182 {
183 FI_HILOGI("systemAbilityId:%{public}d", systemAbilityId);
184 }
185 } // namespace DeviceStatus
186 } // namespace Msdp
187 } // namespace OHOS