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 "camera_log.h"
17 #include "camera_util.h"
18 #include "camera_window_manager_agent.h"
19 #include "camera_window_manager_client.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include <cstdint>
23
24 namespace OHOS {
25 namespace CameraStandard {
26 std::mutex CameraWindowManagerClient::instanceMutex_;
27 sptr<CameraWindowManagerClient> CameraWindowManagerClient::cameraWindowManagerClient_;
28
CameraWindowManagerClient()29 CameraWindowManagerClient::CameraWindowManagerClient()
30 {
31 SubscribeSystemAbility();
32 }
33
~CameraWindowManagerClient()34 CameraWindowManagerClient::~CameraWindowManagerClient()
35 {
36 CameraWindowManagerClient::GetInstance()->UnregisterWindowManagerAgent();
37 }
38
GetInstance()39 sptr<CameraWindowManagerClient>& CameraWindowManagerClient::GetInstance()
40 {
41 if (cameraWindowManagerClient_ == nullptr) {
42 std::unique_lock<std::mutex> lock(instanceMutex_);
43 if (cameraWindowManagerClient_ == nullptr) {
44 MEDIA_INFO_LOG("Initializing CameraWindowManagerClient instance");
45 cameraWindowManagerClient_ = new CameraWindowManagerClient();
46 }
47 }
48 return cameraWindowManagerClient_;
49 }
50
RegisterWindowManagerAgent()51 int32_t CameraWindowManagerClient::RegisterWindowManagerAgent()
52 {
53 MEDIA_DEBUG_LOG("RegisterWindowManagerAgent start");
54 int32_t ret = CAMERA_UNKNOWN_ERROR;
55 if (sceneSessionManagerProxy_) {
56 ret = sceneSessionManagerProxy_->RegisterWindowManagerAgent(
57 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, windowManagerAgent_);
58 } else {
59 MEDIA_ERR_LOG("sceneSessionManagerProxy_ is null");
60 }
61 if (ret != CAMERA_OK) {
62 MEDIA_ERR_LOG("failed to RegisterWindowManagerAgent error code: %{public}d", ret);
63 }
64 MEDIA_DEBUG_LOG("RegisterWindowManagerAgent end");
65 return ret;
66 }
67
UnregisterWindowManagerAgent()68 int32_t CameraWindowManagerClient::UnregisterWindowManagerAgent()
69 {
70 MEDIA_DEBUG_LOG("UnregisterWindowManagerAgent start");
71 int32_t ret = CAMERA_UNKNOWN_ERROR;
72 if (sceneSessionManagerProxy_) {
73 ret = sceneSessionManagerProxy_->UnregisterWindowManagerAgent(
74 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, windowManagerAgent_);
75 } else {
76 MEDIA_ERR_LOG("sceneSessionManagerProxy_ is null");
77 }
78 CHECK_ERROR_PRINT_LOG(ret != CAMERA_OK, "failed to UnregisterWindowManagerAgent error code: %{public}d", ret);
79 MEDIA_DEBUG_LOG("UnregisterWindowManagerAgent end");
80 return ret;
81 }
82
GetFocusWindowInfo(pid_t & pid)83 void CameraWindowManagerClient::GetFocusWindowInfo(pid_t& pid)
84 {
85 MEDIA_DEBUG_LOG("GetFocusWindowInfo start");
86 sptr<OHOS::Rosen::FocusChangeInfo> focusInfo = new OHOS::Rosen::FocusChangeInfo();
87 if (sceneSessionManagerProxy_) {
88 sceneSessionManagerProxy_->GetFocusWindowInfo(*focusInfo);
89 } else {
90 MEDIA_ERR_LOG("sceneSessionManagerProxy_ is null");
91 }
92 MEDIA_DEBUG_LOG("GetFocusWindowInfo pid_: %{public}d", focusInfo->pid_);
93 pid = focusInfo->pid_;
94 MEDIA_DEBUG_LOG("GetFocusWindowInfo end");
95 }
96
InitWindowProxy()97 void CameraWindowManagerClient::InitWindowProxy()
98 {
99 MEDIA_DEBUG_LOG("InitWindowProxy begin");
100 sptr<ISystemAbilityManager> systemAbilityManager =
101 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 CHECK_ERROR_RETURN_LOG(!systemAbilityManager, "Failed to get system ability manager");
103
104 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
105 if (!remoteObject) {
106 MEDIA_ERR_LOG("remoteObjectWmMgrService is null");
107 return;
108 }
109
110 mockSessionManagerServiceProxy_ = iface_cast<IMockSessionManagerInterface>(remoteObject);
111 CHECK_ERROR_RETURN_LOG(!mockSessionManagerServiceProxy_, "Failed to get mockSessionManagerServiceProxy_");
112
113 sptr<IRemoteObject> remoteObjectMgrService = mockSessionManagerServiceProxy_->GetSessionManagerService();
114 CHECK_ERROR_RETURN_LOG(!remoteObjectMgrService, "remoteObjectMgrService is null");
115
116 sessionManagerServiceProxy_ = iface_cast<ISessionManagerService>(remoteObjectMgrService);
117 CHECK_ERROR_RETURN_LOG(!sessionManagerServiceProxy_, "Failed to get sessionManagerServiceProxy_");
118
119 sptr<IRemoteObject> remoteObjectMgr = sessionManagerServiceProxy_->GetSceneSessionManager();
120 CHECK_ERROR_RETURN_LOG(!remoteObjectMgr, "remoteObjectMgr is null");
121
122 sceneSessionManagerProxy_ = iface_cast<ISceneSessionManager>(remoteObjectMgr);
123 CHECK_ERROR_RETURN_LOG(!sceneSessionManagerProxy_, "Failed to get sceneSessionManagerProxy_");
124
125 MEDIA_DEBUG_LOG("InitWindowProxy end");
126 }
127
InitWindowManagerAgent()128 void CameraWindowManagerClient::InitWindowManagerAgent()
129 {
130 MEDIA_DEBUG_LOG("InitWindowManagerAgent start");
131 windowManagerAgent_ = new CameraWindowManagerAgent();
132 CHECK_ERROR_PRINT_LOG(windowManagerAgent_ == nullptr, "Failed to init windowManagerAgent_");
133 int32_t windowRet = CameraWindowManagerClient::GetInstance()->RegisterWindowManagerAgent();
134 CHECK_ERROR_PRINT_LOG(windowRet != 0, "RegisterWindowManagerAgent faild");
135 MEDIA_DEBUG_LOG("InitWindowManagerAgent end");
136 }
137
GetWindowManagerAgent()138 sptr<IWindowManagerAgent> CameraWindowManagerClient::GetWindowManagerAgent()
139 {
140 return windowManagerAgent_;
141 }
142
SubscribeSystemAbility()143 int32_t CameraWindowManagerClient::SubscribeSystemAbility()
144 {
145 MEDIA_DEBUG_LOG("SubscribeSystemAbility start");
146 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
147 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, CAMERA_UNKNOWN_ERROR, "Failed to get system ability manager");
148 saStatusChangeCallback_ = new CameraWindowManagerClient::WMSSaStatusChangeCallback();
149 CHECK_AND_RETURN_RET_LOG(saStatusChangeCallback_ != nullptr, CAMERA_UNKNOWN_ERROR,
150 "saStatusChangeCallback_ init error");
151 int32_t ret = samgr->SubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, saStatusChangeCallback_);
152 MEDIA_DEBUG_LOG("SubscribeSystemAbility ret = %{public}d", ret);
153 return ret == 0? CAMERA_OK : CAMERA_UNKNOWN_ERROR;
154 }
155
UnSubscribeSystemAbility()156 int32_t CameraWindowManagerClient::UnSubscribeSystemAbility()
157 {
158 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
159 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, CAMERA_UNKNOWN_ERROR, "Failed to get system ability manager");
160 if (saStatusChangeCallback_ == nullptr) {
161 return CAMERA_OK;
162 }
163 CHECK_ERROR_RETURN_RET(saStatusChangeCallback_ == nullptr, CAMERA_OK);
164 int32_t ret = samgr->UnSubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, saStatusChangeCallback_);
165 MEDIA_DEBUG_LOG("SubscribeSystemAbility ret = %{public}d", ret);
166 return ret == 0? CAMERA_OK : CAMERA_UNKNOWN_ERROR;
167 }
168
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)169 void CameraWindowManagerClient::WMSSaStatusChangeCallback::OnAddSystemAbility(
170 int32_t systemAbilityId, const std::string& deviceId)
171 {
172 CameraWindowManagerClient::GetInstance()->InitWindowProxy();
173 CameraWindowManagerClient::GetInstance()->InitWindowManagerAgent();
174 }
175
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)176 void CameraWindowManagerClient::WMSSaStatusChangeCallback::OnRemoveSystemAbility(
177 int32_t systemAbilityId, const std::string& deviceId)
178 {
179 CameraWindowManagerClient::GetInstance()->mockSessionManagerServiceProxy_ = nullptr;
180 CameraWindowManagerClient::GetInstance()->sessionManagerServiceProxy_ = nullptr;
181 CameraWindowManagerClient::GetInstance()->sceneSessionManagerProxy_ = nullptr;
182 CameraWindowManagerClient::GetInstance()->windowManagerAgent_ = nullptr;
183 }
184 } // namespace CameraStandard
185 } // namespace OHOS