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