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