• 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     SCLOCK_HILOGD("ScreenLockAppManager::SendScreenLockEvent succeeded.");
62     return proxy->SendScreenLockEvent(event, param);
63 }
64 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)65 int32_t ScreenLockAppManager::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
66 {
67     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent in");
68     auto proxy = GetProxy();
69     if (proxy == nullptr) {
70         SCLOCK_HILOGE("ScreenLockAppManager::OnSystemEvent quit because redoing GetScreenLockManagerProxy failed.");
71         return E_SCREENLOCK_NULLPTR;
72     }
73     if (listener == nullptr) {
74         SCLOCK_HILOGE("listener is nullptr.");
75         return E_SCREENLOCK_NULLPTR;
76     }
77     listenerLock_.lock();
78     systemEventListener_ = listener;
79     listenerLock_.unlock();
80     int32_t status = proxy->OnSystemEvent(listener);
81     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent out, status=%{public}d", status);
82     return status;
83 }
84 
GetScreenLockManagerProxy()85 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetScreenLockManagerProxy()
86 {
87     sptr<ISystemAbilityManager> systemAbilityManager =
88         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
89     if (systemAbilityManager == nullptr) {
90         SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
91         return nullptr;
92     }
93     auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
94     if (systemAbility == nullptr) {
95         SCLOCK_HILOGE("Get SystemAbility failed.");
96         return nullptr;
97     }
98     deathRecipient_ = new ScreenLockAppDeathRecipient();
99     systemAbility->AddDeathRecipient(deathRecipient_);
100     sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
101     if (screenlockServiceProxy == nullptr) {
102         SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
103         return nullptr;
104     }
105     SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
106     return screenlockServiceProxy;
107 }
108 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)109 void ScreenLockAppManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
110 {
111     {
112         std::lock_guard<std::mutex> autoLock(managerProxyLock_);
113         screenlockManagerProxy_ = GetScreenLockManagerProxy();
114     }
115     if (systemEventListener_ != nullptr) {
116         SystemEvent systemEvent(SERVICE_RESTART);
117         systemEventListener_->OnCallBack(systemEvent);
118     }
119 }
120 
GetProxy()121 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetProxy()
122 {
123     if (screenlockManagerProxy_ != nullptr) {
124         return screenlockManagerProxy_;
125     }
126     std::lock_guard<std::mutex> autoLock(managerProxyLock_);
127     if (screenlockManagerProxy_ == nullptr) {
128         SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
129         screenlockManagerProxy_ = GetScreenLockManagerProxy();
130     }
131     return screenlockManagerProxy_;
132 }
133 
ScreenLockAppDeathRecipient()134 ScreenLockAppDeathRecipient::ScreenLockAppDeathRecipient()
135 {
136 }
137 
OnRemoteDied(const wptr<IRemoteObject> & object)138 void ScreenLockAppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
139 {
140     SCLOCK_HILOGE("ScreenLockAppDeathRecipient on remote systemAbility died.");
141     ScreenLockAppManager::GetInstance()->OnRemoteSaDied(object);
142 }
143 } // namespace ScreenLock
144 } // namespace OHOS
145