1 /*
2 * Copyright (c) 2021-2023 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 "running_lock_mgr.h"
17
18 #include <cinttypes>
19 #include <datetime_ex.h>
20 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
21 #include <hisysevent.h>
22 #endif
23 #include <ipc_skeleton.h>
24 #include <securec.h>
25 #ifdef HAS_HIVIEWDFX_HITRACE_PART
26 #include "power_hitrace.h"
27 #endif
28 #include "ffrt_utils.h"
29 #include "power_log.h"
30 #include "power_mgr_factory.h"
31 #include "power_mgr_service.h"
32 #include "power_utils.h"
33 #include "system_suspend_controller.h"
34
35 using namespace std;
36
37 namespace OHOS {
38 namespace PowerMgr {
39 namespace {
40 const string TASK_RUNNINGLOCK_FORCEUNLOCK = "RunningLock_ForceUnLock";
41 constexpr int32_t VALID_PID_LIMIT = 1;
42 sptr<IPowerRunninglockCallback> g_runningLockCallback = nullptr;
43 const string INCALL_APP_BUNDLE_NAME = "com.ohos.callui";
44 constexpr uint32_t FOREGROUND_INCALL_DELAY_TIME_MS = 300;
45 constexpr uint32_t BACKGROUND_INCALL_DELAY_TIME_MS = 800;
46 }
47
~RunningLockMgr()48 RunningLockMgr::~RunningLockMgr() {}
49
Init()50 bool RunningLockMgr::Init()
51 {
52 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Init start");
53 if (runningLockDeathRecipient_ == nullptr) {
54 runningLockDeathRecipient_ = new RunningLockDeathRecipient();
55 }
56 if (runningLockAction_ == nullptr) {
57 runningLockAction_ = PowerMgrFactory::GetRunningLockAction();
58 if (!runningLockAction_) {
59 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Get running lock action fail");
60 return false;
61 }
62 }
63 if (runninglockProxy_ == nullptr) {
64 runninglockProxy_ = std::make_shared<RunningLockProxy>();
65 }
66 bool ret = InitLocks();
67 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Init success");
68 return ret;
69 }
70
InitLocks()71 bool RunningLockMgr::InitLocks()
72 {
73 InitLocksTypeScreen();
74 InitLocksTypeBackground();
75 #ifdef HAS_SENSORS_SENSOR_PART
76 InitLocksTypeProximity();
77 #endif
78 InitLocksTypeCoordination();
79 return true;
80 }
81
InitLocksTypeScreen()82 void RunningLockMgr::InitLocksTypeScreen()
83 {
84 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_SCREEN,
85 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_SCREEN,
86 [this](bool active, [[maybe_unused]] RunningLockParam runningLockParam) -> int32_t {
87 POWER_HILOGD(FEATURE_RUNNING_LOCK, "RUNNINGLOCK_SCREEN action start");
88 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
89 if (pms == nullptr) {
90 return RUNNINGLOCK_FAILURE;
91 }
92 auto stateMachine = pms->GetPowerStateMachine();
93 if (stateMachine == nullptr) {
94 return RUNNINGLOCK_FAILURE;
95 }
96 if (active) {
97 POWER_HILOGI(FEATURE_RUNNING_LOCK,
98 "[UL_POWER] RUNNINGLOCK_SCREEN active, and the currrent power state = %{public}d",
99 stateMachine->GetState());
100 pms->RefreshActivityInner(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE, true);
101 } else {
102 POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_SCREEN inactive");
103 if (stateMachine->GetState() == PowerState::AWAKE) {
104 stateMachine->ResetInactiveTimer();
105 } else {
106 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Screen unlock in state: %{public}d",
107 stateMachine->GetState());
108 }
109 }
110 return RUNNINGLOCK_SUCCESS;
111 })
112 );
113 }
114
InitLocksTypeBackground()115 void RunningLockMgr::InitLocksTypeBackground()
116 {
117 std::function<int32_t(bool, RunningLockParam)> activate =
118 [this](bool active, RunningLockParam lockInnerParam) -> int32_t {
119 if (active) {
120 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Background runningLock active, type=%{public}d name=%{public}s",
121 lockInnerParam.type, lockInnerParam.name.c_str());
122 return runningLockAction_->Lock(lockInnerParam);
123 } else {
124 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Background runningLock inactive, type=%{public}d name=%{public}s",
125 lockInnerParam.type, lockInnerParam.name.c_str());
126 return runningLockAction_->Unlock(lockInnerParam);
127 }
128 };
129 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE,
130 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE, activate));
131 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION,
132 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION, activate));
133 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO,
134 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO, activate));
135 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT,
136 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT, activate));
137 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION,
138 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION, activate));
139 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK,
140 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK, activate));
141 }
142
143 #ifdef HAS_SENSORS_SENSOR_PART
ProximityLockOn()144 void RunningLockMgr::ProximityLockOn()
145 {
146 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
147 if (pms == nullptr) {
148 return;
149 }
150 auto stateMachine = pms->GetPowerStateMachine();
151 auto suspendController = pms->GetSuspendController();
152 if (stateMachine == nullptr || suspendController == nullptr) {
153 return;
154 }
155
156 POWER_HILOGI(FEATURE_RUNNING_LOCK, "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL active");
157 proximityController_.Enable();
158 if (proximityController_.IsClose()) {
159 POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] INACTIVE when proximity is closed");
160 bool ret = stateMachine->SetState(PowerState::INACTIVE,
161 StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true);
162 if (ret) {
163 suspendController->StartSleepTimer(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION,
164 static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND), 0);
165 }
166 } else {
167 POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] AWAKE when proximity is away");
168 PreprocessBeforeAwake();
169 stateMachine->SetState(PowerState::AWAKE,
170 StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true);
171 }
172 }
173
InitLocksTypeProximity()174 void RunningLockMgr::InitLocksTypeProximity()
175 {
176 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL,
177 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL,
178 [this](bool active, [[maybe_unused]] RunningLockParam runningLockParam) -> int32_t {
179 POWER_HILOGD(FEATURE_RUNNING_LOCK, "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL action start");
180 if (active) {
181 POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL active");
182 proximityController_.Enable();
183 } else {
184 POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL inactive");
185 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
186 if (pms == nullptr) {
187 return RUNNINGLOCK_FAILURE;
188 }
189 auto stateMachine = pms->GetPowerStateMachine();
190 if (stateMachine == nullptr) {
191 return RUNNINGLOCK_FAILURE;
192 }
193 PreprocessBeforeAwake();
194 stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK);
195 proximityController_.Disable();
196 proximityController_.Clear();
197 }
198 return RUNNINGLOCK_SUCCESS;
199 })
200 );
201 }
202 #endif
203
InitLocksTypeCoordination()204 void RunningLockMgr::InitLocksTypeCoordination()
205 {
206 lockCounters_.emplace(RunningLockType::RUNNINGLOCK_COORDINATION,
207 std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_COORDINATION,
208 [this](bool active, [[maybe_unused]] RunningLockParam runningLockParam) -> int32_t {
209 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
210 if (pms == nullptr) {
211 return RUNNINGLOCK_FAILURE;
212 }
213 auto stateMachine = pms->GetPowerStateMachine();
214 if (stateMachine == nullptr) {
215 return RUNNINGLOCK_FAILURE;
216 }
217 auto stateAction = stateMachine->GetStateAction();
218 if (stateAction == nullptr) {
219 return RUNNINGLOCK_FAILURE;
220 }
221 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Coordination runninglock action, active=%{public}d, state=%{public}d",
222 active, stateMachine->GetState());
223 struct RunningLockParam backgroundLockParam = runningLockParam;
224 backgroundLockParam.name = PowerUtils::GetRunningLockTypeString(RunningLockType::RUNNINGLOCK_COORDINATION),
225 backgroundLockParam.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
226 auto iterator = lockCounters_.find(backgroundLockParam.type);
227 if (iterator == lockCounters_.end()) {
228 POWER_HILOGE(FEATURE_RUNNING_LOCK, "unsupported type, type=%{public}d", backgroundLockParam.type);
229 return RUNNINGLOCK_NOT_SUPPORT;
230 }
231 std::shared_ptr<LockCounter> counter = iterator->second;
232 int32_t result = RUNNINGLOCK_SUCCESS;
233 if (active) {
234 result = counter->Increase(backgroundLockParam);
235 stateAction->SetCoordinated(true);
236 } else {
237 stateAction->SetCoordinated(false);
238 stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK);
239 result = counter->Decrease(backgroundLockParam);
240 }
241 return result;
242 })
243 );
244 }
245
GetRunningLockInner( const sptr<IRemoteObject>& remoteObj)246 std::shared_ptr<RunningLockInner> RunningLockMgr::GetRunningLockInner(
247 const sptr<IRemoteObject>& remoteObj)
248 {
249 std::lock_guard<std::mutex> lock(mutex_);
250 auto iterator = runningLocks_.find(remoteObj);
251 if (iterator != runningLocks_.end()) {
252 return iterator->second;
253 }
254 return nullptr;
255 }
256
GetRunningLockInnerByName( const std::string& name)257 std::shared_ptr<RunningLockInner> RunningLockMgr::GetRunningLockInnerByName(
258 const std::string& name)
259 {
260 std::lock_guard<std::mutex> lock(mutex_);
261 std::shared_ptr<RunningLockInner> lockInner = nullptr;
262 std::string result = ToString(runningLocks_.size());
263 for (auto& iter : runningLocks_) {
264 if (iter.second == nullptr) {
265 POWER_HILOGE(FEATURE_RUNNING_LOCK, "GetRunningLockInnerByName nullptr");
266 continue;
267 }
268 if (iter.second->GetName() == name) {
269 lockInner = iter.second;
270 }
271 auto& lockParam = iter.second->GetParam();
272 result.append(" lockid=").append(ToString(lockParam.lockid))
273 .append(" name=").append(lockParam.name);
274 }
275 POWER_HILOGI(FEATURE_RUNNING_LOCK, "DumpInfo: %{public}s", result.c_str());
276 return lockInner;
277 }
278
CreateRunningLock(const sptr<IRemoteObject>& remoteObj, const RunningLockParam& runningLockParam)279 std::shared_ptr<RunningLockInner> RunningLockMgr::CreateRunningLock(const sptr<IRemoteObject>& remoteObj,
280 const RunningLockParam& runningLockParam)
281 {
282 auto lockInner = RunningLockInner::CreateRunningLockInner(runningLockParam);
283 if (lockInner == nullptr) {
284 return nullptr;
285 }
286 POWER_HILOGI(FEATURE_RUNNING_LOCK, "CreateRunningLock name:%{public}s, type:%{public}d, lockid:%{public}s",
287 lockInner->GetName().c_str(), lockInner->GetType(), std::to_string(runningLockParam.lockid).c_str());
288 {
289 std::lock_guard<std::mutex> lock(mutex_);
290 runningLocks_.emplace(remoteObj, lockInner);
291 }
292 if (lockInner->GetType() != RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) {
293 runninglockProxy_->AddRunningLock(lockInner->GetPid(), lockInner->GetUid(), remoteObj);
294 }
295 remoteObj->AddDeathRecipient(runningLockDeathRecipient_);
296 return lockInner;
297 }
298
ReleaseLock(const sptr<IRemoteObject> remoteObj, const std::string& name)299 bool RunningLockMgr::ReleaseLock(const sptr<IRemoteObject> remoteObj, const std::string& name)
300 {
301 bool result = false;
302 auto lockInner = GetRunningLockInner(remoteObj);
303 if (lockInner == nullptr) {
304 auto lockid = TransformLockid(remoteObj);
305 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner is nullptr, lockid=%{public}s", __func__,
306 std::to_string(lockid).c_str());
307 lockInner = GetRunningLockInnerByName(name);
308 if (lockInner == nullptr) {
309 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner not existed", __func__);
310 return result;
311 }
312 }
313 POWER_HILOGI(FEATURE_RUNNING_LOCK, "ReleaseLock name:%{public}s, type:%{public}d, bundleName:%{public}s",
314 lockInner->GetName().c_str(), lockInner->GetType(), lockInner->GetBundleName().c_str());
315 UnLock(remoteObj);
316 {
317 std::lock_guard<std::mutex> lock(mutex_);
318 runningLocks_.erase(remoteObj);
319 }
320 if (lockInner->GetType() != RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) {
321 runninglockProxy_->RemoveRunningLock(lockInner->GetPid(), lockInner->GetUid(), remoteObj);
322 }
323 result = remoteObj->RemoveDeathRecipient(runningLockDeathRecipient_);
324 return result;
325 }
326
IsSceneRunningLockType(RunningLockType type)327 bool RunningLockMgr::IsSceneRunningLockType(RunningLockType type)
328 {
329 return type == RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE ||
330 type == RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION ||
331 type == RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO ||
332 type == RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT ||
333 type == RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION ||
334 type == RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
335 }
336
NeedNotify(RunningLockType type)337 bool RunningLockMgr::NeedNotify(RunningLockType type)
338 {
339 return IsSceneRunningLockType(type) ||
340 type == RunningLockType::RUNNINGLOCK_SCREEN;
341 }
342
UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill)343 void RunningLockMgr::UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill)
344 {
345 std::lock_guard<std::mutex> lock(screenLockListsMutex_);
346 auto iterator = unSceneLockLists_.find(std::to_string(singleLockParam.lockid));
347 if (fill) {
348 if (iterator == unSceneLockLists_.end()) {
349 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Add non-scene lock information from lists");
350 RunningLockInfo unSceneAppLockInfo = FillAppRunningLockInfo(singleLockParam);
351 unSceneLockLists_.insert(
352 std::pair<std::string, RunningLockInfo>(std::to_string(singleLockParam.lockid), unSceneAppLockInfo));
353 }
354 return;
355 }
356 if (iterator != unSceneLockLists_.end()) {
357 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Remove non-scene lock information from lists");
358 unSceneLockLists_.erase(iterator);
359 }
360 return;
361 }
362
FillAppRunningLockInfo(const RunningLockParam& info)363 RunningLockInfo RunningLockMgr::FillAppRunningLockInfo(const RunningLockParam& info)
364 {
365 RunningLockInfo tempAppRunningLockInfo = {};
366 tempAppRunningLockInfo.name = info.name;
367 tempAppRunningLockInfo.bundleName = info.bundleName;
368 tempAppRunningLockInfo.type = info.type;
369 tempAppRunningLockInfo.pid = info.pid;
370 tempAppRunningLockInfo.uid = info.uid;
371 return tempAppRunningLockInfo;
372 }
373
IsValidType(RunningLockType type)374 bool RunningLockMgr::IsValidType(RunningLockType type)
375 {
376 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
377 if (pms == nullptr) {
378 return true;
379 }
380 PowerState state = pms->GetState();
381 POWER_HILOGD(FEATURE_RUNNING_LOCK, "state=%{public}d, type=%{public}d", state, type);
382 switch (state) {
383 case PowerState::AWAKE:
384 case PowerState::FREEZE:
385 case PowerState::INACTIVE:
386 case PowerState::STAND_BY:
387 case PowerState::DOZE:
388 return true;
389 case PowerState::SLEEP:
390 case PowerState::HIBERNATE:
391 return type != RunningLockType::RUNNINGLOCK_COORDINATION;
392 default:
393 break;
394 }
395 return true;
396 }
397
PreprocessBeforeAwake()398 void RunningLockMgr::PreprocessBeforeAwake()
399 {
400 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
401 if (pms == nullptr) {
402 return;
403 }
404 auto stateMachine = pms->GetPowerStateMachine();
405 auto suspendController = pms->GetSuspendController();
406 if (stateMachine == nullptr || suspendController == nullptr) {
407 return;
408 }
409
410 suspendController->StopSleep();
411 POWER_HILOGI(FEATURE_RUNNING_LOCK, "wake up.");
412 SystemSuspendController::GetInstance().Wakeup();
413 if (stateMachine->GetState() == PowerState::SLEEP) {
414 POWER_HILOGI(FEATURE_RUNNING_LOCK, "TriggerSyncSleepCallback start.");
415 suspendController->TriggerSyncSleepCallback(true);
416 }
417 }
418
UpdateWorkSource(const sptr<IRemoteObject>& remoteObj, const std::map<int32_t, std::string>& workSources)419 bool RunningLockMgr::UpdateWorkSource(const sptr<IRemoteObject>& remoteObj,
420 const std::map<int32_t, std::string>& workSources)
421 {
422 auto lockInner = GetRunningLockInner(remoteObj);
423 if (lockInner == nullptr) {
424 auto lockid = TransformLockid(remoteObj);
425 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner is nullptr, lockid=%{public}s", __func__,
426 std::to_string(lockid).c_str());
427 return false;
428 }
429 RunningLockParam lockInnerParam = lockInner->GetParam();
430 POWER_HILOGD(FEATURE_RUNNING_LOCK, "try UpdateWorkSource, name: %{public}s, type: %{public}d lockid: %{public}s",
431 lockInnerParam.name.c_str(), lockInnerParam.type, std::to_string(lockInnerParam.lockid).c_str());
432 runninglockProxy_->UpdateWorkSource(lockInner->GetPid(), lockInner->GetUid(), remoteObj, workSources);
433 return true;
434 }
435
Lock(const sptr<IRemoteObject>& remoteObj)436 bool RunningLockMgr::Lock(const sptr<IRemoteObject>& remoteObj)
437 {
438 #ifdef HAS_HIVIEWDFX_HITRACE_PART
439 PowerHitrace powerHitrace("RunningLock_Lock");
440 #endif
441 auto lockInner = GetRunningLockInner(remoteObj);
442 if (lockInner == nullptr) {
443 auto lockid = TransformLockid(remoteObj);
444 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner is nullptr, lockid=%{public}s", __func__,
445 std::to_string(lockid).c_str());
446 return false;
447 }
448 RunningLockParam lockInnerParam = lockInner->GetParam();
449 POWER_HILOGI(FEATURE_RUNNING_LOCK, "try Lock, name: %{public}s, type: %{public}d lockid: %{public}s",
450 lockInnerParam.name.c_str(), lockInnerParam.type, std::to_string(lockInnerParam.lockid).c_str());
451 if (lockInner->IsProxied()) {
452 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Runninglock is proxied");
453 return false;
454 }
455 if (!IsValidType(lockInnerParam.type)) {
456 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Runninglock type is invalid");
457 return false;
458 }
459 if (lockInner->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE) {
460 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Lock is already enabled name=%{public}s",
461 lockInner->GetName().c_str());
462 return false;
463 }
464 if (lockInnerParam.type == RunningLockType::RUNNINGLOCK_SCREEN) {
465 UpdateUnSceneLockLists(lockInnerParam, true);
466 }
467 auto iterator = lockCounters_.find(lockInnerParam.type);
468 if (iterator == lockCounters_.end()) {
469 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Lock failed unsupported type, type=%{public}d", lockInnerParam.type);
470 return false;
471 }
472 std::shared_ptr<LockCounter> counter = iterator->second;
473 if (counter->Increase(lockInnerParam) != RUNNINGLOCK_SUCCESS) {
474 POWER_HILOGE(FEATURE_RUNNING_LOCK, "LockCounter increase failed, type=%{public}d, count=%{public}d",
475 counter->GetType(), counter->GetCount());
476 return false;
477 }
478 lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_ENABLE);
479 return true;
480 }
481
UnLock(const sptr<IRemoteObject> remoteObj, const std::string& name)482 bool RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj, const std::string& name)
483 {
484 #ifdef HAS_HIVIEWDFX_HITRACE_PART
485 PowerHitrace powerHitrace("RunningLock_Unlock");
486 #endif
487 auto lockInner = GetRunningLockInner(remoteObj);
488 if (lockInner == nullptr) {
489 auto lockid = TransformLockid(remoteObj);
490 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner is nullptr, lockid=%{public}s", __func__,
491 std::to_string(lockid).c_str());
492 lockInner = GetRunningLockInnerByName(name);
493 if (lockInner == nullptr) {
494 POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner not existed", __func__);
495 return false;
496 }
497 }
498 if (lockInner->IsProxied()) {
499 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Runninglock is proxied, unProxy");
500 runninglockProxy_->UpdateProxyState(lockInner->GetPid(), lockInner->GetUid(), remoteObj, false);
501 }
502 auto lockInnerParam = lockInner->GetParam();
503 POWER_HILOGI(FEATURE_RUNNING_LOCK, "try UnLock, name: %{public}s, type: %{public}d lockid: %{public}s",
504 lockInnerParam.name.c_str(), lockInnerParam.type, std::to_string(lockInnerParam.lockid).c_str());
505 if (lockInner->GetState() == RunningLockState::RUNNINGLOCK_STATE_DISABLE) {
506 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Lock is already disabled, name=%{public}s", lockInner->GetName().c_str());
507 return false;
508 }
509 if (lockInnerParam.type == RunningLockType::RUNNINGLOCK_SCREEN) {
510 UpdateUnSceneLockLists(lockInnerParam, false);
511 }
512 auto iterator = lockCounters_.find(lockInnerParam.type);
513 if (iterator == lockCounters_.end()) {
514 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Unlock failed unsupported type, type=%{public}d",
515 lockInnerParam.type);
516 return false;
517 }
518 std::shared_ptr<LockCounter> counter = iterator->second;
519 if (counter->Decrease(lockInnerParam)) {
520 POWER_HILOGE(FEATURE_RUNNING_LOCK, "LockCounter decrease failed, type=%{public}d, count=%{public}d",
521 counter->GetType(), counter->GetCount());
522 return false;
523 }
524 lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_DISABLE);
525 return true;
526 }
527
RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)528 void RunningLockMgr::RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
529 {
530 if (g_runningLockCallback != nullptr) {
531 UnRegisterRunningLockCallback(callback);
532 }
533 g_runningLockCallback = callback;
534 POWER_HILOGI(FEATURE_RUNNING_LOCK, "RegisterRunningLockCallback success");
535 }
536
UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)537 void RunningLockMgr::UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
538 {
539 g_runningLockCallback = nullptr;
540 POWER_HILOGI(FEATURE_RUNNING_LOCK, "UnRegisterRunningLockCallback success");
541 }
542
QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)543 void RunningLockMgr::QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)
544 {
545 std::lock_guard<std::mutex> lock(screenLockListsMutex_);
546 for (auto &iter : unSceneLockLists_) {
547 runningLockLists.insert(std::pair<std::string, RunningLockInfo>(iter.first, iter.second));
548 }
549 return;
550 }
551
IsUsed(const sptr<IRemoteObject>& remoteObj)552 bool RunningLockMgr::IsUsed(const sptr<IRemoteObject>& remoteObj)
553 {
554 auto lockInner = GetRunningLockInner(remoteObj);
555 if (lockInner == nullptr || lockInner->GetState() != RunningLockState::RUNNINGLOCK_STATE_ENABLE) {
556 return false;
557 }
558 return true;
559 }
560
GetRunningLockNum(RunningLockType type)561 uint32_t RunningLockMgr::GetRunningLockNum(RunningLockType type)
562 {
563 std::lock_guard<std::mutex> lock(mutex_);
564 if (type == RunningLockType::RUNNINGLOCK_BUTT) {
565 return runningLocks_.size();
566 }
567 return std::count_if(runningLocks_.begin(), runningLocks_.end(),
568 [&type](const auto& pair) {
569 return pair.second->GetType() == type;
570 });
571 }
572
GetValidRunningLockNum(RunningLockType type)573 uint32_t RunningLockMgr::GetValidRunningLockNum(RunningLockType type)
574 {
575 auto iterator = lockCounters_.find(type);
576 if (iterator == lockCounters_.end()) {
577 POWER_HILOGD(FEATURE_RUNNING_LOCK, "No specific lock, type=%{public}d", type);
578 return 0;
579 }
580 std::shared_ptr<LockCounter> counter = iterator->second;
581 return counter->GetCount();
582 }
583
ExistValidRunningLock()584 bool RunningLockMgr::ExistValidRunningLock()
585 {
586 std::lock_guard<std::mutex> lock(mutex_);
587 for (auto it = runningLocks_.begin(); it != runningLocks_.end(); it++) {
588 auto& lockinner = it->second;
589 if (lockinner->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE) {
590 return true;
591 }
592 }
593 return false;
594 }
595
NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, const std::string &tag)596 void RunningLockMgr::NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, const std::string &tag)
597 {
598 uint64_t lockid = lockInnerParam.lockid;
599 int32_t pid = lockInnerParam.pid;
600 int32_t uid = lockInnerParam.uid;
601 int32_t type = static_cast <int32_t>(lockInnerParam.type);
602 auto pos = lockInnerParam.name.rfind('_');
603 string name = lockInnerParam.name.substr(0, pos);
604 string bundleName = lockInnerParam.bundleName;
605 auto now = std::chrono::system_clock::now();
606 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
607 std::string message;
608 message.append("LOCKID=").append(std::to_string(lockid))
609 .append(" PID=").append(std::to_string(pid))
610 .append(" UID=").append(std::to_string(uid))
611 .append(" TYPE=").append(std::to_string(type))
612 .append(" NAME=").append(name)
613 .append(" BUNDLENAME=").append(bundleName)
614 .append(" TAG=").append(tag)
615 .append(" TIMESTAMP=").append(std::to_string(timestamp));
616 POWER_HILOGI(FEATURE_RUNNING_LOCK, "runninglock message: %{public}s", message.c_str());
617 if (g_runningLockCallback != nullptr) {
618 g_runningLockCallback->HandleRunningLockMessage(message);
619 }
620 }
621
TransformLockid(const sptr<IRemoteObject>& remoteObj)622 uint64_t RunningLockMgr::TransformLockid(const sptr<IRemoteObject>& remoteObj)
623 {
624 uintptr_t remoteObjPtr = reinterpret_cast<uintptr_t>(remoteObj.GetRefPtr());
625 uint64_t lockid = std::hash<uintptr_t>()(remoteObjPtr);
626 return lockid;
627 }
628
ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)629 bool RunningLockMgr::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
630 {
631 if (pid < VALID_PID_LIMIT) {
632 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Proxy runninglock failed, pid=%{public}d is invalid", pid);
633 return false;
634 }
635
636 if (isProxied) {
637 runninglockProxy_->IncreaseProxyCnt(pid, uid);
638 } else {
639 runninglockProxy_->DecreaseProxyCnt(pid, uid);
640 }
641 return true;
642 }
643
ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)644 void RunningLockMgr::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
645 {
646 for (const auto& [pid, uid] : processInfos) {
647 ProxyRunningLock(isProxied, pid, uid);
648 }
649 }
650
ResetRunningLocks()651 void RunningLockMgr::ResetRunningLocks()
652 {
653 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Reset runninglock proxy");
654 runninglockProxy_->ResetRunningLocks();
655 }
656
LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner)657 void RunningLockMgr::LockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
658 std::shared_ptr<RunningLockInner>& lockInner)
659 {
660 if (!lockInner->IsProxied()) {
661 POWER_HILOGW(FEATURE_RUNNING_LOCK, "LockInnerByProxy failed, runninglock UnProxied");
662 return;
663 }
664 RunningLockParam lockInnerParam = lockInner->GetParam();
665 POWER_HILOGD(FEATURE_RUNNING_LOCK, "try LockInnerByProxy, name: %{public}s, type: %{public}d lockid: %{public}s",
666 lockInnerParam.name.c_str(), lockInnerParam.type, std::to_string(lockInnerParam.lockid).c_str());
667 RunningLockState lastState = lockInner->GetState();
668 if (lastState == RunningLockState::RUNNINGLOCK_STATE_PROXIED) {
669 lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_DISABLE);
670 Lock(remoteObj);
671 }
672 }
673
UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner)674 void RunningLockMgr::UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
675 std::shared_ptr<RunningLockInner>& lockInner)
676 {
677 RunningLockState lastState = lockInner->GetState();
678 if (lastState == RunningLockState::RUNNINGLOCK_STATE_DISABLE) {
679 POWER_HILOGW(FEATURE_RUNNING_LOCK, "UnlockInnerByProxy failed, runninglock Disable");
680 return;
681 }
682 RunningLockParam lockInnerParam = lockInner->GetParam();
683 POWER_HILOGD(FEATURE_RUNNING_LOCK, "try UnlockInnerByProxy, name: %{public}s, type: %{public}d lockid: %{public}s",
684 lockInnerParam.name.c_str(), lockInnerParam.type, std::to_string(lockInnerParam.lockid).c_str());
685 UnLock(remoteObj);
686 lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_PROXIED);
687 }
688
EnableMock(IRunningLockAction* mockAction)689 void RunningLockMgr::EnableMock(IRunningLockAction* mockAction)
690 {
691 // reset lock list
692 runningLocks_.clear();
693 for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) {
694 it->second->Clear();
695 }
696 runninglockProxy_->Clear();
697 #ifdef HAS_SENSORS_SENSOR_PART
698 proximityController_.Clear();
699 #endif
700 std::shared_ptr<IRunningLockAction> mock(mockAction);
701 runningLockAction_ = mock;
702 }
703
DumpInfo(std::string& result)704 void RunningLockMgr::DumpInfo(std::string& result)
705 {
706 auto validSize = GetValidRunningLockNum();
707 std::lock_guard<std::mutex> lock(mutex_);
708
709 result.append("RUNNING LOCK DUMP:\n");
710 result.append(" totalSize=").append(ToString(runningLocks_.size()))
711 .append(" validSize=").append(ToString(validSize)).append("\n");
712 result.append("Summary By Type: \n");
713 for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) {
714 result.append(" ")
715 .append(PowerUtils::GetRunningLockTypeString(it->first))
716 .append(": ")
717 .append(ToString(it->second->GetCount()))
718 .append("\n");
719 }
720
721 if (runningLocks_.empty()) {
722 result.append("Lock List is Empty. \n");
723 return;
724 }
725
726 result.append("Dump Lock List: \n");
727 auto curTick = GetTickCount();
728 int index = 0;
729 for (const auto& it : runningLocks_) {
730 index++;
731 auto lockInner = it.second;
732 if (lockInner == nullptr) {
733 return;
734 }
735 auto& lockParam = lockInner->GetParam();
736 result.append(" index=").append(ToString(index))
737 .append(" time=").append(ToString(curTick - lockInner->GetLockTimeMs()))
738 .append(" type=").append(PowerUtils::GetRunningLockTypeString(lockParam.type))
739 .append(" name=").append(lockParam.name)
740 .append(" uid=").append(ToString(lockInner->GetUid()))
741 .append(" pid=").append(ToString(lockInner->GetPid()))
742 .append(" state=").append(ToString(static_cast<uint32_t>(lockInner->GetState())))
743 .append("\n");
744 }
745
746 result.append("Dump Proxy List: \n");
747 result.append(runninglockProxy_->DumpProxyInfo());
748 #ifdef HAS_SENSORS_SENSOR_PART
749 result.append("Peripherals Info: \n")
750 .append(" Proximity: ")
751 .append("Enabled=")
752 .append(ToString(proximityController_.IsEnabled()))
753 .append(" Status=")
754 .append(ToString(proximityController_.GetStatus()))
755 .append("\n");
756 #endif
757 }
758
OnRemoteDied(const wptr<IRemoteObject>& remote)759 void RunningLockMgr::RunningLockDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
760 {
761 if (remote == nullptr || remote.promote() == nullptr) {
762 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Remote is nullptr");
763 return;
764 }
765 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
766 if (pms == nullptr) {
767 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Power service is nullptr");
768 return;
769 }
770 pms->ForceUnLock(remote.promote());
771 }
772
Increase(const RunningLockParam& lockInnerParam)773 int32_t RunningLockMgr::LockCounter::Increase(const RunningLockParam& lockInnerParam)
774 {
775 ++counter_;
776 int32_t result = RUNNINGLOCK_SUCCESS;
777 if (counter_ == 1) {
778 result = activate_(true, lockInnerParam);
779 if (result != RUNNINGLOCK_SUCCESS) {
780 --counter_;
781 }
782 }
783 if (result == RUNNINGLOCK_SUCCESS && NeedNotify(lockInnerParam.type)) {
784 NotifyRunningLockChanged(lockInnerParam, "DUBAI_TAG_RUNNINGLOCK_ADD");
785 }
786 return result;
787 }
788
Decrease(const RunningLockParam& lockInnerParam)789 int32_t RunningLockMgr::LockCounter::Decrease(const RunningLockParam& lockInnerParam)
790 {
791 --counter_;
792 int32_t result = RUNNINGLOCK_SUCCESS;
793 if (counter_ == 0) {
794 result = activate_(false, lockInnerParam);
795 if (result != RUNNINGLOCK_SUCCESS) {
796 ++counter_;
797 }
798 }
799 if (result == RUNNINGLOCK_SUCCESS && NeedNotify(lockInnerParam.type)) {
800 NotifyRunningLockChanged(lockInnerParam, "DUBAI_TAG_RUNNINGLOCK_REMOVE");
801 }
802 return result;
803 }
804
Clear()805 void RunningLockMgr::LockCounter::Clear()
806 {
807 counter_ = 0;
808 }
809
810 #ifdef HAS_SENSORS_SENSOR_PART
RecordSensorCallback(SensorEvent *event)811 void RunningLockMgr::ProximityController::RecordSensorCallback(SensorEvent *event)
812 {
813 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Sensor Callback come in");
814 if (event == nullptr) {
815 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Sensor event is nullptr");
816 return;
817 }
818 if (event->sensorTypeId != SENSOR_TYPE_ID_PROXIMITY) {
819 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Sensor type is not PROXIMITY");
820 return;
821 }
822 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
823 if (pms == nullptr) {
824 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
825 return;
826 }
827 auto runningLock = pms->GetRunningLockMgr();
828 ProximityData* data = reinterpret_cast<ProximityData*>(event->data);
829 int32_t distance = static_cast<int32_t>(data->distance);
830
831 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Sensor Callback data->distance=%{public}d", distance);
832 if (distance == PROXIMITY_CLOSE_SCALAR) {
833 runningLock->SetProximity(PROXIMITY_CLOSE);
834 } else if (distance == PROXIMITY_AWAY_SCALAR) {
835 runningLock->SetProximity(PROXIMITY_AWAY);
836 }
837 }
838
ProximityController()839 RunningLockMgr::ProximityController::ProximityController()
840 {
841 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Instance enter");
842 SensorInfo* sensorInfo = nullptr;
843 int32_t count;
844 int ret = GetAllSensors(&sensorInfo, &count);
845 if (ret != 0 || sensorInfo == nullptr) {
846 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Get sensors fail, ret=%{public}d", ret);
847 return;
848 }
849 for (int32_t i = 0; i < count; i++) {
850 if (sensorInfo[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY) {
851 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Support PROXIMITY sensor");
852 support_ = true;
853 break;
854 }
855 }
856 if (!support_) {
857 POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
858 return;
859 }
860 if (strcpy_s(user_.name, sizeof(user_.name), "RunningLock") != EOK) {
861 POWER_HILOGE(FEATURE_RUNNING_LOCK, "strcpy_s error");
862 return;
863 }
864 user_.userData = nullptr;
865 user_.callback = &RecordSensorCallback;
866 }
867
~ProximityController()868 RunningLockMgr::ProximityController::~ProximityController()
869 {
870 if (support_) {
871 UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
872 }
873 }
874
Enable()875 void RunningLockMgr::ProximityController::Enable()
876 {
877 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Enter");
878 enabled_ = true;
879 if (!support_) {
880 POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
881 return;
882 }
883
884 int32_t errorCode = SubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
885 if (errorCode != ERR_OK) {
886 POWER_HILOGW(FEATURE_RUNNING_LOCK, "SubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
887 return;
888 }
889 SetBatch(SENSOR_TYPE_ID_PROXIMITY, &user_, SAMPLING_RATE, SAMPLING_RATE);
890 errorCode = ActivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
891 if (errorCode != ERR_OK) {
892 POWER_HILOGW(FEATURE_RUNNING_LOCK, "ActivateSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
893 return;
894 }
895 SetMode(SENSOR_TYPE_ID_PROXIMITY, &user_, SENSOR_ON_CHANGE);
896 }
897
Disable()898 void RunningLockMgr::ProximityController::Disable()
899 {
900 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Enter");
901 enabled_ = false;
902 if (!support_) {
903 POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
904 return;
905 }
906
907 DeactivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
908 int32_t errorCode = UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
909 if (errorCode != ERR_OK) {
910 POWER_HILOGW(FEATURE_RUNNING_LOCK, "UnsubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
911 }
912 }
913
IsClose()914 bool RunningLockMgr::ProximityController::IsClose()
915 {
916 POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY IsClose: %{public}d", isClose_);
917 return isClose_;
918 }
919
OnClose()920 void RunningLockMgr::ProximityController::OnClose()
921 {
922 if (!enabled_ || IsClose()) {
923 POWER_HILOGI(FEATURE_RUNNING_LOCK, "PROXIMITY is disabled or closed already");
924 return;
925 }
926 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
927 if (pms == nullptr) {
928 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
929 return;
930 }
931 auto stateMachine = pms->GetPowerStateMachine();
932 auto suspendController = pms->GetSuspendController();
933 if (stateMachine == nullptr || suspendController == nullptr) {
934 POWER_HILOGE(FEATURE_RUNNING_LOCK, "state machine is nullptr");
935 return;
936 }
937 isClose_ = true;
938 POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY is closed");
939 auto runningLock = pms->GetRunningLockMgr();
940 if (runningLock->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) {
941 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Change state to INACITVE when holding PROXIMITY LOCK");
942 uint32_t delayTime = FOREGROUND_INCALL_DELAY_TIME_MS;
943 if (!PowerUtils::IsForegroundApplication(INCALL_APP_BUNDLE_NAME)) {
944 delayTime = BACKGROUND_INCALL_DELAY_TIME_MS;
945 }
946 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Start proximity-screen-off timer, delay time:%{public}u", delayTime);
947 stateMachine->SetDelayTimer(delayTime, PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG);
948 } else {
949 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Unholding PROXIMITY LOCK");
950 }
951 }
952
OnAway()953 void RunningLockMgr::ProximityController::OnAway()
954 {
955 if (!enabled_ || !IsClose()) {
956 POWER_HILOGI(FEATURE_RUNNING_LOCK, "PROXIMITY is disabled or away already");
957 return;
958 }
959 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
960 if (pms == nullptr) {
961 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
962 return;
963 }
964 auto stateMachine = pms->GetPowerStateMachine();
965 if (stateMachine == nullptr) {
966 POWER_HILOGE(FEATURE_RUNNING_LOCK, "state machine is nullptr");
967 return;
968 }
969 isClose_ = false;
970 POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY is away");
971 auto runningLock = pms->GetRunningLockMgr();
972 if (runningLock->GetValidRunningLockNum(
973 RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) {
974 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Change state to AWAKE when holding PROXIMITY LOCK");
975 runningLock->PreprocessBeforeAwake();
976 stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true);
977 } else {
978 POWER_HILOGI(FEATURE_RUNNING_LOCK, "Unholding PROXIMITY LOCK");
979 }
980 }
981
Clear()982 void RunningLockMgr::ProximityController::Clear()
983 {
984 isClose_ = false;
985 }
986
SetProximity(uint32_t status)987 void RunningLockMgr::SetProximity(uint32_t status)
988 {
989 switch (status) {
990 case PROXIMITY_CLOSE:
991 proximityController_.OnClose();
992 break;
993 case PROXIMITY_AWAY:
994 proximityController_.OnAway();
995 break;
996 default:
997 break;
998 }
999 }
1000
IsProximityClose()1001 bool RunningLockMgr::IsProximityClose()
1002 {
1003 return proximityController_.IsClose();
1004 }
1005 #endif
1006
1007 } // namespace PowerMgr
1008 } // namespace OHOS
1009