• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "screenlock_app_manager.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "sclock_log.h"
21 #include "screenlock_common.h"
22 #include "system_ability_definition.h"
23 #include "scene_board_judgement.h"
24 #include "session_manager.h"
25 
26 namespace OHOS {
27 namespace ScreenLock {
28 std::mutex ScreenLockAppManager::instanceLock_;
29 sptr<ScreenLockAppManager> ScreenLockAppManager::instance_;
30 sptr<ScreenLockAppDeathRecipient> ScreenLockAppManager::deathRecipient_;
31 std::mutex ScreenLockAppManager::listenerLock_;
32 sptr<ScreenLockSystemAbilityInterface> ScreenLockAppManager::systemEventListener_;
33 
ScreenLockAppManager()34 ScreenLockAppManager::ScreenLockAppManager()
35 {
36 }
37 
~ScreenLockAppManager()38 ScreenLockAppManager::~ScreenLockAppManager()
39 {
40 }
41 
GetInstance()42 sptr<ScreenLockAppManager> ScreenLockAppManager::GetInstance()
43 {
44     if (instance_ == nullptr) {
45         std::lock_guard<std::mutex> autoLock(instanceLock_);
46         if (instance_ == nullptr) {
47             instance_ = new ScreenLockAppManager;
48             std::lock_guard<std::mutex> guard(instance_->managerProxyLock_);
49             instance_->screenlockManagerProxy_ = GetScreenLockManagerProxy();
50         }
51     }
52     return instance_;
53 }
54 
SendScreenLockEvent(const std::string & event,int param)55 int32_t ScreenLockAppManager::SendScreenLockEvent(const std::string &event, int param)
56 {
57     auto proxy = GetProxy();
58     if (proxy == nullptr) {
59         SCLOCK_HILOGE("ScreenLockAppManager::SendScreenLockEvent quit because redoing GetScreenLockManagerProxy "
60                       "failed.");
61         return E_SCREENLOCK_NULLPTR;
62     }
63     int ret = proxy->SendScreenLockEvent(event, param);
64     SCLOCK_HILOGD("SendScreenLockEvent result = %{public}d", ret);
65     return ret;
66 }
67 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)68 int32_t ScreenLockAppManager::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
69 {
70     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent in");
71     auto proxy = GetProxy();
72     if (proxy == nullptr) {
73         SCLOCK_HILOGE("ScreenLockAppManager::OnSystemEvent quit because redoing GetScreenLockManagerProxy failed.");
74         return E_SCREENLOCK_NULLPTR;
75     }
76     if (listener == nullptr) {
77         SCLOCK_HILOGE("listener is nullptr.");
78         return E_SCREENLOCK_NULLPTR;
79     }
80     listenerLock_.lock();
81     systemEventListener_ = listener;
82     listenerLock_.unlock();
83     int32_t status = proxy->OnSystemEvent(listener);
84     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent out, status=%{public}d", status);
85     return status;
86 }
87 
GetScreenLockManagerProxy()88 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetScreenLockManagerProxy()
89 {
90     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
91         return Rosen::SessionManager::GetInstance().GetScreenLockManagerProxy();
92     }
93     sptr<ISystemAbilityManager> systemAbilityManager =
94         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95     if (systemAbilityManager == nullptr) {
96         SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
97         return nullptr;
98     }
99     auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
100     if (systemAbility == nullptr) {
101         SCLOCK_HILOGE("Get SystemAbility failed.");
102         return nullptr;
103     }
104     deathRecipient_ = new ScreenLockAppDeathRecipient();
105     systemAbility->AddDeathRecipient(deathRecipient_);
106     sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
107     if (screenlockServiceProxy == nullptr) {
108         SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
109         return nullptr;
110     }
111     SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
112     return screenlockServiceProxy;
113 }
114 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)115 void ScreenLockAppManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
116 {
117     {
118         std::lock_guard<std::mutex> autoLock(managerProxyLock_);
119         screenlockManagerProxy_ = GetScreenLockManagerProxy();
120     }
121     if (systemEventListener_ != nullptr) {
122         SystemEvent systemEvent(SERVICE_RESTART);
123         systemEventListener_->OnCallBack(systemEvent);
124     }
125 }
126 
GetProxy()127 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetProxy()
128 {
129     if (screenlockManagerProxy_ != nullptr) {
130         return screenlockManagerProxy_;
131     }
132     std::lock_guard<std::mutex> autoLock(managerProxyLock_);
133     if (screenlockManagerProxy_ == nullptr) {
134         SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
135         screenlockManagerProxy_ = GetScreenLockManagerProxy();
136     }
137     return screenlockManagerProxy_;
138 }
139 
ScreenLockAppDeathRecipient()140 ScreenLockAppDeathRecipient::ScreenLockAppDeathRecipient()
141 {
142 }
143 
~ScreenLockAppDeathRecipient()144 ScreenLockAppDeathRecipient::~ScreenLockAppDeathRecipient()
145 {
146 }
147 
OnRemoteDied(const wptr<IRemoteObject> & object)148 void ScreenLockAppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
149 {
150     SCLOCK_HILOGE("ScreenLockAppDeathRecipient on remote systemAbility died.");
151     ScreenLockAppManager::GetInstance()->OnRemoteSaDied(object);
152 }
153 } // namespace ScreenLock
154 } // namespace OHOS
155