1 /*
2 * Copyright (c) 2025 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 <map>
17 #include "ohos.runningLock.proj.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20 #include "power_mgr_client.h"
21 #include "power_log.h"
22 #include "power_mgr_errors.h"
23 #include "power_errors.h"
24 #include "ohos.runningLock.impl.hpp"
25
26 using namespace taihe;
27 using namespace ohos::runningLock;
28 using namespace OHOS::PowerMgr;
29
30 namespace {
31 std::map<PowerErrors, std::string> g_errorTable = {
32 {PowerErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."},
33 {PowerErrors::ERR_PERMISSION_DENIED, "Permission is denied" },
34 {PowerErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" },
35 {PowerErrors::ERR_PARAM_INVALID, "Invalid input parameter." }
36 };
37
38 class RunningLockImpl {
39 public:
RunningLockImpl()40 RunningLockImpl() {}
41
RunningLockImpl(std::shared_ptr<OHOS::PowerMgr::RunningLock> runLock)42 explicit RunningLockImpl(std::shared_ptr<OHOS::PowerMgr::RunningLock> runLock)
43 {
44 runningLock_ = runLock;
45 }
46
Hold(int32_t timeout)47 void Hold(int32_t timeout)
48 {
49 POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Hold interface");
50 if (runningLock_ == nullptr) {
51 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr");
52 return;
53 }
54 OHOS::ErrCode code = runningLock_->Lock(timeout);
55 if (code == E_PERMISSION_DENIED) {
56 taihe::set_business_error(static_cast<int32_t>(PowerErrors::ERR_PERMISSION_DENIED),
57 g_errorTable[PowerErrors::ERR_PERMISSION_DENIED]);
58 }
59 }
60
IsHolding()61 bool IsHolding()
62 {
63 POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets IsHolding interface");
64 if (runningLock_ == nullptr) {
65 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr");
66 return false;
67 }
68 bool isUsed = runningLock_->IsUsed();
69 return isUsed;
70 }
71
Unhold()72 void Unhold()
73 {
74 POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Unhold interface");
75 if (runningLock_ == nullptr) {
76 POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr");
77 return;
78 }
79 OHOS::ErrCode code = runningLock_->UnLock();
80 if (code == E_PERMISSION_DENIED) {
81 taihe::set_business_error(static_cast<int32_t>(PowerErrors::ERR_PERMISSION_DENIED),
82 g_errorTable[PowerErrors::ERR_PERMISSION_DENIED]);
83 }
84 }
85
86 private:
87 std::shared_ptr<OHOS::PowerMgr::RunningLock> runningLock_ = nullptr;
88 };
89
CreateSync(string_view name,ohos::runningLock::RunningLockType type)90 ohos::runningLock::RunningLock CreateSync(string_view name, ohos::runningLock::RunningLockType type)
91 {
92 std::shared_ptr<OHOS::PowerMgr::RunningLock> runLock = nullptr;
93 OHOS::PowerMgr::RunningLockType tp = static_cast<OHOS::PowerMgr::RunningLockType>(type.get_value());
94 runLock = PowerMgrClient::GetInstance().CreateRunningLock(std::string(name), tp);
95 PowerErrors code = PowerMgrClient::GetInstance().GetError();
96 if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) {
97 taihe::set_business_error(static_cast<int32_t>(code), g_errorTable[code]);
98 }
99 return make_holder<RunningLockImpl, ohos::runningLock::RunningLock>(runLock);
100 }
101
IsSupported(ohos::runningLock::RunningLockType type)102 bool IsSupported(ohos::runningLock::RunningLockType type)
103 {
104 OHOS::PowerMgr::RunningLockType tp = static_cast<OHOS::PowerMgr::RunningLockType>(type.get_value());
105 return tp == OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL;
106 }
107 } // namespace
108
109 // Since these macros are auto-generate, lint will cause false positive
110 // NOLINTBEGIN
111 TH_EXPORT_CPP_API_CreateSync(CreateSync);
112 TH_EXPORT_CPP_API_IsSupported(IsSupported);
113 // NOLINTEND