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