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 "power_mgr_client_adapter_impl.h" 17 18 using namespace OHOS::PowerMgr; 19 using namespace OHOS::NWeb; 20 21 namespace { ConvertRunningLockType(RunningLockAdapterType type)22OHOS::PowerMgr::RunningLockType ConvertRunningLockType( 23 RunningLockAdapterType type) 24 { 25 OHOS::PowerMgr::RunningLockType rawType = 26 OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_BUTT; 27 switch (type) { 28 case RunningLockAdapterType::SCREEN: 29 rawType = OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_SCREEN; 30 break; 31 case RunningLockAdapterType::BACKGROUND: 32 rawType = OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND; 33 break; 34 case RunningLockAdapterType::PROXIMITY_SCREEN_CONTROL: 35 rawType = OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL; 36 break; 37 default: 38 break; 39 } 40 return rawType; 41 } 42 } 43 44 namespace OHOS::NWeb { RunningLockAdapterImpl(std::shared_ptr<OHOS::PowerMgr::RunningLock> lock)45RunningLockAdapterImpl::RunningLockAdapterImpl( 46 std::shared_ptr<OHOS::PowerMgr::RunningLock> lock) 47 : lock_(lock) {} 48 IsUsed()49bool RunningLockAdapterImpl::IsUsed() 50 { 51 if (lock_ != nullptr) { 52 return lock_->IsUsed(); 53 } 54 return false; 55 } 56 Lock(uint32_t timeOutMs)57int32_t RunningLockAdapterImpl::Lock(uint32_t timeOutMs) 58 { 59 if (lock_ != nullptr) { 60 return lock_->Lock(timeOutMs); 61 } 62 return -1; 63 } 64 UnLock()65int32_t RunningLockAdapterImpl::UnLock() 66 { 67 if (lock_ != nullptr) { 68 return lock_->UnLock(); 69 } 70 return -1; 71 } 72 CreateRunningLock(const std::string & name,RunningLockAdapterType type)73std::shared_ptr<RunningLockAdapter> PowerMgrClientAdapterImpl::CreateRunningLock( 74 const std::string& name, RunningLockAdapterType type) 75 { 76 RunningLockType rawType = ConvertRunningLockType(type); 77 std::shared_ptr<RunningLock> lock = PowerMgrClient::GetInstance().CreateRunningLock( 78 name, rawType); 79 return std::make_shared<RunningLockAdapterImpl>(lock); 80 } 81 }