1365d9939Sopenharmony_ci/* 2365d9939Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3365d9939Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4365d9939Sopenharmony_ci * you may not use this file except in compliance with the License. 5365d9939Sopenharmony_ci * You may obtain a copy of the License at 6365d9939Sopenharmony_ci * 7365d9939Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8365d9939Sopenharmony_ci * 9365d9939Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10365d9939Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11365d9939Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12365d9939Sopenharmony_ci * See the License for the specific language governing permissions and 13365d9939Sopenharmony_ci * limitations under the License. 14365d9939Sopenharmony_ci */ 15365d9939Sopenharmony_ci 16365d9939Sopenharmony_ci#include "screenlock_manager.h" 17365d9939Sopenharmony_ci#include "screenlock_manager_proxy.h" 18365d9939Sopenharmony_ci#include <hitrace_meter.h> 19365d9939Sopenharmony_ci 20365d9939Sopenharmony_ci#include "if_system_ability_manager.h" 21365d9939Sopenharmony_ci#include "iservice_registry.h" 22365d9939Sopenharmony_ci#include "sclock_log.h" 23365d9939Sopenharmony_ci#include "screenlock_common.h" 24365d9939Sopenharmony_ci#include "system_ability_definition.h" 25365d9939Sopenharmony_ci 26365d9939Sopenharmony_cinamespace OHOS { 27365d9939Sopenharmony_cinamespace ScreenLock { 28365d9939Sopenharmony_cistd::mutex ScreenLockManager::instanceLock_; 29365d9939Sopenharmony_cisptr<ScreenLockManager> ScreenLockManager::instance_; 30365d9939Sopenharmony_ciScreenLockManager::ScreenLockManager() 31365d9939Sopenharmony_ci{ 32365d9939Sopenharmony_ci} 33365d9939Sopenharmony_ci 34365d9939Sopenharmony_ciScreenLockManager::~ScreenLockManager() 35365d9939Sopenharmony_ci{ 36365d9939Sopenharmony_ci SCLOCK_HILOGW("~ScreenLockManager"); 37365d9939Sopenharmony_ci RemoveDeathRecipient(); 38365d9939Sopenharmony_ci} 39365d9939Sopenharmony_ci 40365d9939Sopenharmony_cisptr<ScreenLockManager> ScreenLockManager::GetInstance() 41365d9939Sopenharmony_ci{ 42365d9939Sopenharmony_ci if (instance_ == nullptr) { 43365d9939Sopenharmony_ci std::lock_guard<std::mutex> autoLock(instanceLock_); 44365d9939Sopenharmony_ci if (instance_ == nullptr) { 45365d9939Sopenharmony_ci instance_ = new ScreenLockManager; 46365d9939Sopenharmony_ci } 47365d9939Sopenharmony_ci } 48365d9939Sopenharmony_ci return instance_; 49365d9939Sopenharmony_ci} 50365d9939Sopenharmony_ci 51365d9939Sopenharmony_ciint32_t ScreenLockManager::IsLocked(bool &isLocked) 52365d9939Sopenharmony_ci{ 53365d9939Sopenharmony_ci auto proxy = GetProxy(); 54365d9939Sopenharmony_ci if (proxy == nullptr) { 55365d9939Sopenharmony_ci SCLOCK_HILOGE("IsLocked quit because GetScreenLockManagerProxy failed."); 56365d9939Sopenharmony_ci return E_SCREENLOCK_SENDREQUEST_FAILED; 57365d9939Sopenharmony_ci } 58365d9939Sopenharmony_ci return proxy->IsLocked(isLocked); 59365d9939Sopenharmony_ci} 60365d9939Sopenharmony_ci 61365d9939Sopenharmony_cibool ScreenLockManager::IsScreenLocked() 62365d9939Sopenharmony_ci{ 63365d9939Sopenharmony_ci auto proxy = GetProxy(); 64365d9939Sopenharmony_ci if (proxy == nullptr) { 65365d9939Sopenharmony_ci SCLOCK_HILOGE("IsScreenLocked quit because GetScreenLockManagerProxy failed."); 66365d9939Sopenharmony_ci return false; 67365d9939Sopenharmony_ci } 68365d9939Sopenharmony_ci return proxy->IsScreenLocked(); 69365d9939Sopenharmony_ci} 70365d9939Sopenharmony_ci 71365d9939Sopenharmony_cibool ScreenLockManager::GetSecure() 72365d9939Sopenharmony_ci{ 73365d9939Sopenharmony_ci auto proxy = GetProxy(); 74365d9939Sopenharmony_ci if (proxy == nullptr) { 75365d9939Sopenharmony_ci SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed."); 76365d9939Sopenharmony_ci return false; 77365d9939Sopenharmony_ci } 78365d9939Sopenharmony_ci return proxy->GetSecure(); 79365d9939Sopenharmony_ci} 80365d9939Sopenharmony_ci 81365d9939Sopenharmony_ciint32_t ScreenLockManager::Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener) 82365d9939Sopenharmony_ci{ 83365d9939Sopenharmony_ci auto proxy = GetProxy(); 84365d9939Sopenharmony_ci if (proxy == nullptr) { 85365d9939Sopenharmony_ci SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed."); 86365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 87365d9939Sopenharmony_ci } 88365d9939Sopenharmony_ci if (listener == nullptr) { 89365d9939Sopenharmony_ci SCLOCK_HILOGE("listener is nullptr."); 90365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 91365d9939Sopenharmony_ci } 92365d9939Sopenharmony_ci StartAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock start", HITRACE_UNLOCKSCREEN); 93365d9939Sopenharmony_ci int32_t ret = 0; 94365d9939Sopenharmony_ci if (action == Action::UNLOCKSCREEN) { 95365d9939Sopenharmony_ci ret = proxy->UnlockScreen(listener); 96365d9939Sopenharmony_ci } else { 97365d9939Sopenharmony_ci ret = proxy->Unlock(listener); 98365d9939Sopenharmony_ci } 99365d9939Sopenharmony_ci FinishAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock end", HITRACE_UNLOCKSCREEN); 100365d9939Sopenharmony_ci return ret; 101365d9939Sopenharmony_ci} 102365d9939Sopenharmony_ci 103365d9939Sopenharmony_ci 104365d9939Sopenharmony_ciint32_t ScreenLockManager::Lock(const sptr<ScreenLockCallbackInterface> &listener) 105365d9939Sopenharmony_ci{ 106365d9939Sopenharmony_ci auto proxy = GetProxy(); 107365d9939Sopenharmony_ci if (proxy == nullptr) { 108365d9939Sopenharmony_ci SCLOCK_HILOGE("RequestLock quit because redoing GetScreenLockManagerProxy failed."); 109365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 110365d9939Sopenharmony_ci } 111365d9939Sopenharmony_ci if (listener == nullptr) { 112365d9939Sopenharmony_ci SCLOCK_HILOGE("listener is nullptr."); 113365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 114365d9939Sopenharmony_ci } 115365d9939Sopenharmony_ci SCLOCK_HILOGD("ScreenLockManager RequestLock succeeded."); 116365d9939Sopenharmony_ci return proxy->Lock(listener); 117365d9939Sopenharmony_ci} 118365d9939Sopenharmony_ci 119365d9939Sopenharmony_ciint32_t ScreenLockManager::Lock(int32_t userId) 120365d9939Sopenharmony_ci{ 121365d9939Sopenharmony_ci auto proxy = GetProxy(); 122365d9939Sopenharmony_ci if (proxy == nullptr) { 123365d9939Sopenharmony_ci SCLOCK_HILOGE("GetProxy failed."); 124365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 125365d9939Sopenharmony_ci } 126365d9939Sopenharmony_ci return proxy->Lock(userId); 127365d9939Sopenharmony_ci} 128365d9939Sopenharmony_ci 129365d9939Sopenharmony_ciint32_t ScreenLockManager::RequestStrongAuth(int reasonFlag, int32_t userId) 130365d9939Sopenharmony_ci{ 131365d9939Sopenharmony_ci auto proxy = GetProxy(); 132365d9939Sopenharmony_ci if (proxy == nullptr) { 133365d9939Sopenharmony_ci SCLOCK_HILOGE("RequestStrongAuth quit because GetProxy failed."); 134365d9939Sopenharmony_ci return E_SCREENLOCK_NULLPTR; 135365d9939Sopenharmony_ci } 136365d9939Sopenharmony_ci return proxy->RequestStrongAuth(reasonFlag, userId); 137365d9939Sopenharmony_ci} 138365d9939Sopenharmony_ci 139365d9939Sopenharmony_cisptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy() 140365d9939Sopenharmony_ci{ 141365d9939Sopenharmony_ci sptr<ISystemAbilityManager> systemAbilityManager = 142365d9939Sopenharmony_ci SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 143365d9939Sopenharmony_ci if (systemAbilityManager == nullptr) { 144365d9939Sopenharmony_ci SCLOCK_HILOGE("Getting SystemAbilityManager failed."); 145365d9939Sopenharmony_ci return nullptr; 146365d9939Sopenharmony_ci } 147365d9939Sopenharmony_ci auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, ""); 148365d9939Sopenharmony_ci if (systemAbility == nullptr) { 149365d9939Sopenharmony_ci SCLOCK_HILOGE("Get SystemAbility failed."); 150365d9939Sopenharmony_ci return nullptr; 151365d9939Sopenharmony_ci } 152365d9939Sopenharmony_ci deathRecipient_ = new ScreenLockSaDeathRecipient(); 153365d9939Sopenharmony_ci systemAbility->AddDeathRecipient(deathRecipient_); 154365d9939Sopenharmony_ci sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility); 155365d9939Sopenharmony_ci if (screenlockServiceProxy == nullptr) { 156365d9939Sopenharmony_ci SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed."); 157365d9939Sopenharmony_ci return nullptr; 158365d9939Sopenharmony_ci } 159365d9939Sopenharmony_ci SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded."); 160365d9939Sopenharmony_ci return screenlockServiceProxy; 161365d9939Sopenharmony_ci} 162365d9939Sopenharmony_ci 163365d9939Sopenharmony_civoid ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote) 164365d9939Sopenharmony_ci{ 165365d9939Sopenharmony_ci std::lock_guard<std::mutex> autoLock(managerProxyLock_); 166365d9939Sopenharmony_ci screenlockManagerProxy_ = GetScreenLockManagerProxy(); 167365d9939Sopenharmony_ci} 168365d9939Sopenharmony_ci 169365d9939Sopenharmony_cisptr<ScreenLockManagerInterface> ScreenLockManager::GetProxy() 170365d9939Sopenharmony_ci{ 171365d9939Sopenharmony_ci std::lock_guard<std::mutex> autoLock(managerProxyLock_); 172365d9939Sopenharmony_ci if (screenlockManagerProxy_ != nullptr) { 173365d9939Sopenharmony_ci return screenlockManagerProxy_; 174365d9939Sopenharmony_ci } 175365d9939Sopenharmony_ci if (screenlockManagerProxy_ == nullptr) { 176365d9939Sopenharmony_ci SCLOCK_HILOGW("Redo GetScreenLockManagerProxy"); 177365d9939Sopenharmony_ci screenlockManagerProxy_ = GetScreenLockManagerProxy(); 178365d9939Sopenharmony_ci } 179365d9939Sopenharmony_ci return screenlockManagerProxy_; 180365d9939Sopenharmony_ci} 181365d9939Sopenharmony_ci 182365d9939Sopenharmony_civoid ScreenLockManager::RemoveDeathRecipient() 183365d9939Sopenharmony_ci{ 184365d9939Sopenharmony_ci if (screenlockManagerProxy_ != nullptr && deathRecipient_ != nullptr) { 185365d9939Sopenharmony_ci screenlockManagerProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_); 186365d9939Sopenharmony_ci } 187365d9939Sopenharmony_ci} 188365d9939Sopenharmony_ci} // namespace ScreenLock 189365d9939Sopenharmony_ci} // namespace OHOS 190