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 CHECK_PRINT_ELOG(ret != CAMERA_OK, "failed to UnregisterWindowManagerAgent error code: %{public}d", ret);
62 MEDIA_DEBUG_LOG("RegisterWindowManagerAgent end");
63 return ret;
64 }
65
UnregisterWindowManagerAgent()66 int32_t CameraWindowManagerClient::UnregisterWindowManagerAgent()
67 {
68 MEDIA_DEBUG_LOG("UnregisterWindowManagerAgent start");
69 int32_t ret = CAMERA_UNKNOWN_ERROR;
70 if (sceneSessionManagerProxy_) {
71 ret = sceneSessionManagerProxy_->UnregisterWindowManagerAgent(
72 WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, windowManagerAgent_);
73 } else {
74 MEDIA_ERR_LOG("sceneSessionManagerProxy_ is null");
75 }
76 CHECK_PRINT_ELOG(ret != CAMERA_OK, "failed to UnregisterWindowManagerAgent error code: %{public}d", ret);
77 MEDIA_DEBUG_LOG("UnregisterWindowManagerAgent end");
78 return ret;
79 }
80
GetFocusWindowInfo(pid_t & pid)81 void CameraWindowManagerClient::GetFocusWindowInfo(pid_t& pid)
82 {
83 MEDIA_DEBUG_LOG("GetFocusWindowInfo start");
84 sptr<OHOS::Rosen::FocusChangeInfo> focusInfo = new OHOS::Rosen::FocusChangeInfo();
85 if (sceneSessionManagerProxy_) {
86 sceneSessionManagerProxy_->GetFocusWindowInfo(*focusInfo);
87 } else {
88 MEDIA_ERR_LOG("sceneSessionManagerProxy_ is null");
89 }
90 MEDIA_ERR_LOG("GetFocusWindowInfo pid_: %{public}d", focusInfo->pid_);
91 pid = focusInfo->pid_;
92 MEDIA_DEBUG_LOG("GetFocusWindowInfo end");
93 }
94
InitWindowProxy()95 void CameraWindowManagerClient::InitWindowProxy()
96 {
97 MEDIA_DEBUG_LOG("InitWindowProxy begin");
98 sptr<ISystemAbilityManager> systemAbilityManager =
99 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
100 CHECK_RETURN_ELOG(!systemAbilityManager, "Failed to get system ability manager");
101
102 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
103 CHECK_RETURN_ELOG(!remoteObject, "remoteObjectWmMgrService is null");
104
105 mockSessionManagerServiceProxy_ = iface_cast<IMockSessionManagerInterface>(remoteObject);
106 CHECK_RETURN_ELOG(!mockSessionManagerServiceProxy_, "Failed to get mockSessionManagerServiceProxy_");
107
108 sptr<IRemoteObject> remoteObjectMgrService = mockSessionManagerServiceProxy_->GetSessionManagerService();
109 CHECK_RETURN_ELOG(!remoteObjectMgrService, "remoteObjectMgrService is null");
110
111 sessionManagerServiceProxy_ = iface_cast<ISessionManagerService>(remoteObjectMgrService);
112 CHECK_RETURN_ELOG(!sessionManagerServiceProxy_, "Failed to get sessionManagerServiceProxy_");
113
114 sptr<IRemoteObject> remoteObjectMgr = sessionManagerServiceProxy_->GetSceneSessionManager();
115 CHECK_RETURN_ELOG(!remoteObjectMgr, "remoteObjectMgr is null");
116
117 sceneSessionManagerProxy_ = iface_cast<ISceneSessionManager>(remoteObjectMgr);
118 CHECK_RETURN_ELOG(!sceneSessionManagerProxy_, "Failed to get sceneSessionManagerProxy_");
119
120 MEDIA_DEBUG_LOG("InitWindowProxy end");
121 }
122
InitWindowManagerAgent()123 void CameraWindowManagerClient::InitWindowManagerAgent()
124 {
125 MEDIA_DEBUG_LOG("InitWindowManagerAgent start");
126 windowManagerAgent_ = new CameraWindowManagerAgent();
127 CHECK_PRINT_ELOG(windowManagerAgent_ == nullptr, "Failed to init windowManagerAgent_");
128 int32_t windowRet = CameraWindowManagerClient::GetInstance()->RegisterWindowManagerAgent();
129 CHECK_PRINT_ELOG(windowRet != 0, "RegisterWindowManagerAgent faild");
130 MEDIA_DEBUG_LOG("InitWindowManagerAgent end");
131 }
132
GetWindowManagerAgent()133 sptr<IWindowManagerAgent> CameraWindowManagerClient::GetWindowManagerAgent()
134 {
135 return windowManagerAgent_;
136 }
137
SubscribeSystemAbility()138 int32_t CameraWindowManagerClient::SubscribeSystemAbility()
139 {
140 MEDIA_DEBUG_LOG("SubscribeSystemAbility start");
141 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 CHECK_RETURN_RET_ELOG(samgr == nullptr, CAMERA_UNKNOWN_ERROR, "Failed to get system ability manager");
143 saStatusChangeCallback_ = new CameraWindowManagerClient::WMSSaStatusChangeCallback();
144 CHECK_RETURN_RET_ELOG(saStatusChangeCallback_ == nullptr, CAMERA_UNKNOWN_ERROR,
145 "saStatusChangeCallback_ init error");
146 int32_t ret = samgr->SubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, saStatusChangeCallback_);
147 MEDIA_DEBUG_LOG("SubscribeSystemAbility ret = %{public}d", ret);
148 return ret == 0? CAMERA_OK : CAMERA_UNKNOWN_ERROR;
149 }
150
UnSubscribeSystemAbility()151 int32_t CameraWindowManagerClient::UnSubscribeSystemAbility()
152 {
153 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
154 CHECK_RETURN_RET_ELOG(samgr == nullptr, CAMERA_UNKNOWN_ERROR, "Failed to get system ability manager");
155 CHECK_RETURN_RET(saStatusChangeCallback_ == nullptr, CAMERA_OK);
156 int32_t ret = samgr->UnSubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, saStatusChangeCallback_);
157 MEDIA_DEBUG_LOG("SubscribeSystemAbility ret = %{public}d", ret);
158 return ret == 0? CAMERA_OK : CAMERA_UNKNOWN_ERROR;
159 }
160
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)161 void CameraWindowManagerClient::WMSSaStatusChangeCallback::OnAddSystemAbility(
162 int32_t systemAbilityId, const std::string& deviceId)
163 {
164 CameraWindowManagerClient::GetInstance()->InitWindowProxy();
165 CameraWindowManagerClient::GetInstance()->InitWindowManagerAgent();
166 }
167
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)168 void CameraWindowManagerClient::WMSSaStatusChangeCallback::OnRemoveSystemAbility(
169 int32_t systemAbilityId, const std::string& deviceId)
170 {
171 CameraWindowManagerClient::GetInstance()->mockSessionManagerServiceProxy_ = nullptr;
172 CameraWindowManagerClient::GetInstance()->sessionManagerServiceProxy_ = nullptr;
173 CameraWindowManagerClient::GetInstance()->sceneSessionManagerProxy_ = nullptr;
174 CameraWindowManagerClient::GetInstance()->windowManagerAgent_ = nullptr;
175 }
176 } // namespace CameraStandard
177 } // namespace OHOS