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