• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
480     lockInner->SetBeginTime(GetTickCount());
481 #endif
482     lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_ENABLE);
483     return true;
484 }
485 
UnLock(const sptr<IRemoteObject> remoteObj,const std::string & name)486 bool RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj, const std::string& name)
487 {
488     PowerHitrace powerHitrace("RunningLock_Unlock");
489     auto lockInner = GetRunningLockInner(remoteObj);
490     if (lockInner == nullptr) {
491         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s:LockInner is nullptr", __func__);
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         lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_DISABLE);
502     }
503     auto lockInnerParam = lockInner->GetParam();
504     POWER_HILOGI(FEATURE_RUNNING_LOCK, "try UnLock, name: %{public}s, type: %{public}d",
505         lockInnerParam.name.c_str(), lockInnerParam.type);
506     if (lockInner->GetState() == RunningLockState::RUNNINGLOCK_STATE_DISABLE) {
507         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Lock is already disabled, name=%{public}s", lockInner->GetName().c_str());
508         return false;
509     }
510     if (lockInnerParam.type == RunningLockType::RUNNINGLOCK_SCREEN) {
511         UpdateUnSceneLockLists(lockInnerParam, false);
512     }
513     auto iterator = lockCounters_.find(lockInnerParam.type);
514     if (iterator == lockCounters_.end()) {
515         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Unlock failed unsupported type, type=%{public}d",
516             lockInnerParam.type);
517         return false;
518     }
519     std::shared_ptr<LockCounter> counter = iterator->second;
520     if (counter->Decrease(lockInnerParam)) {
521         POWER_HILOGE(FEATURE_RUNNING_LOCK, "LockCounter decrease failed, type=%{public}d, count=%{public}d",
522             counter->GetType(), counter->GetCount());
523         return false;
524     }
525 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
526     WriteHiSysEvent(lockInner);
527 #endif
528     lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_DISABLE);
529     return true;
530 }
531 
WriteHiSysEvent(std::shared_ptr<RunningLockInner> & lockInner)532 void RunningLockMgr::WriteHiSysEvent(std::shared_ptr<RunningLockInner>& lockInner)
533 {
534 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
535     constexpr int32_t APP_HOLD_RUNNINGLOCK_TIMEOUT = 7200000;
536     int32_t endTimeMs = GetTickCount();
537     int32_t beginTimeMs = lockInner->GetBeginTime();
538     if (endTimeMs - beginTimeMs > APP_HOLD_RUNNINGLOCK_TIMEOUT) {
539         POWER_HILOGI(FEATURE_RUNNING_LOCK, "app hold runninglock timeout=%{public}d", (endTimeMs - beginTimeMs));
540         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "APP_HOLD_RUNNINGLOCK_TIMEOUT",
541             HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PID", lockInner->GetPid(), "UID", lockInner->GetUid(),
542             "TYPE", static_cast<int32_t>(lockInner->GetParam().type), "NAME", lockInner->GetParam().name);
543     }
544 #endif
545 }
546 
RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)547 void RunningLockMgr::RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
548 {
549     if (g_runningLockCallback != nullptr) {
550         UnRegisterRunningLockCallback(callback);
551     }
552     g_runningLockCallback = callback;
553     POWER_HILOGI(FEATURE_RUNNING_LOCK, "RegisterRunningLockCallback success");
554 }
555 
UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)556 void RunningLockMgr::UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
557 {
558     g_runningLockCallback = nullptr;
559     POWER_HILOGI(FEATURE_RUNNING_LOCK, "UnRegisterRunningLockCallback success");
560 }
561 
QueryRunningLockLists(std::map<std::string,RunningLockInfo> & runningLockLists)562 void RunningLockMgr::QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)
563 {
564     std::lock_guard<std::mutex> lock(screenLockListsMutex_);
565     for (auto &iter : unSceneLockLists_) {
566         runningLockLists.insert(std::pair<std::string, RunningLockInfo>(iter.first, iter.second));
567     }
568     return;
569 }
570 
IsUsed(const sptr<IRemoteObject> & remoteObj)571 bool RunningLockMgr::IsUsed(const sptr<IRemoteObject>& remoteObj)
572 {
573     auto lockInner = GetRunningLockInner(remoteObj);
574     if (lockInner == nullptr || lockInner->GetState() != RunningLockState::RUNNINGLOCK_STATE_ENABLE) {
575         return false;
576     }
577     return true;
578 }
579 
GetRunningLockNum(RunningLockType type)580 uint32_t RunningLockMgr::GetRunningLockNum(RunningLockType type)
581 {
582     std::lock_guard<std::mutex> lock(mutex_);
583     if (type == RunningLockType::RUNNINGLOCK_BUTT) {
584         return runningLocks_.size();
585     }
586     return std::count_if(runningLocks_.begin(), runningLocks_.end(),
587         [&type](const auto& pair) {
588             return pair.second->GetType() == type;
589         });
590 }
591 
GetValidRunningLockNum(RunningLockType type)592 uint32_t RunningLockMgr::GetValidRunningLockNum(RunningLockType type)
593 {
594     auto iterator = lockCounters_.find(type);
595     if (iterator == lockCounters_.end()) {
596         POWER_HILOGD(FEATURE_RUNNING_LOCK, "No specific lock, type=%{public}d", type);
597         return 0;
598     }
599     std::shared_ptr<LockCounter> counter = iterator->second;
600     return counter->GetCount();
601 }
602 
ExistValidRunningLock()603 bool RunningLockMgr::ExistValidRunningLock()
604 {
605     std::lock_guard<std::mutex> lock(mutex_);
606     for (auto it = runningLocks_.begin(); it != runningLocks_.end(); it++) {
607         auto& lockinner = it->second;
608         if (lockinner->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE) {
609             return true;
610         }
611     }
612     return false;
613 }
614 
NotifyRunningLockChanged(const RunningLockParam & lockInnerParam,const std::string & tag)615 void RunningLockMgr::NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, const std::string &tag)
616 {
617     uint64_t lockid = lockInnerParam.lockid;
618     int32_t pid = lockInnerParam.pid;
619     int32_t uid = lockInnerParam.uid;
620     int32_t type = static_cast <int32_t>(lockInnerParam.type);
621     auto pos = lockInnerParam.name.rfind('_');
622     string name = lockInnerParam.name.substr(0, pos);
623     string bundleName = lockInnerParam.bundleName;
624     auto now = std::chrono::system_clock::now();
625     auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
626     std::string message;
627     message.append("LOCKID=").append(std::to_string(lockid))
628             .append(" PID=").append(std::to_string(pid))
629             .append(" UID=").append(std::to_string(uid))
630             .append(" TYPE=").append(std::to_string(type))
631             .append(" NAME=").append(name)
632             .append(" BUNDLENAME=").append(bundleName)
633             .append(" TAG=").append(tag)
634             .append(" TIMESTAMP=").append(std::to_string(timestamp));
635     POWER_HILOGI(FEATURE_RUNNING_LOCK, "runninglock message: PID=%{public}d UID=%{public}d TYPE=%{public}d "
636         "NAME=%{public}s BUNDLENAME=%{public}s TAG=%{public}s",
637         pid, uid, type, lockInnerParam.name.c_str(), bundleName.c_str(), tag.c_str());
638     if (g_runningLockCallback != nullptr) {
639         g_runningLockCallback->HandleRunningLockMessage(message);
640     }
641 }
642 
TransformLockid(const sptr<IRemoteObject> & remoteObj)643 uint64_t RunningLockMgr::TransformLockid(const sptr<IRemoteObject>& remoteObj)
644 {
645     uintptr_t remoteObjPtr = reinterpret_cast<uintptr_t>(remoteObj.GetRefPtr());
646     uint64_t lockid = std::hash<uintptr_t>()(remoteObjPtr);
647     return lockid;
648 }
649 
ProxyRunningLock(bool isProxied,pid_t pid,pid_t uid)650 bool RunningLockMgr::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
651 {
652     if (pid < VALID_PID_LIMIT) {
653         POWER_HILOGW(FEATURE_RUNNING_LOCK, "Proxy runninglock failed, pid=%{public}d is invalid", pid);
654         return false;
655     }
656 
657     if (isProxied) {
658         runninglockProxy_->IncreaseProxyCnt(pid, uid);
659     } else {
660         runninglockProxy_->DecreaseProxyCnt(pid, uid);
661     }
662     return true;
663 }
664 
ProxyRunningLocks(bool isProxied,const std::vector<std::pair<pid_t,pid_t>> & processInfos)665 void RunningLockMgr::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
666 {
667     for (const auto& [pid, uid] : processInfos) {
668         ProxyRunningLock(isProxied, pid, uid);
669     }
670 }
671 
ResetRunningLocks()672 void RunningLockMgr::ResetRunningLocks()
673 {
674     POWER_HILOGI(FEATURE_RUNNING_LOCK, "Reset runninglock proxy");
675     runninglockProxy_->ResetRunningLocks();
676 }
677 
LockInnerByProxy(const sptr<IRemoteObject> & remoteObj,std::shared_ptr<RunningLockInner> & lockInner)678 void RunningLockMgr::LockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
679     std::shared_ptr<RunningLockInner>& lockInner)
680 {
681     if (!lockInner->IsProxied()) {
682         POWER_HILOGW(FEATURE_RUNNING_LOCK, "LockInnerByProxy failed, runninglock UnProxied");
683         return;
684     }
685     RunningLockParam lockInnerParam = lockInner->GetParam();
686     POWER_HILOGD(FEATURE_RUNNING_LOCK, "try LockInnerByProxy, name: %{public}s, type: %{public}d",
687         lockInnerParam.name.c_str(), lockInnerParam.type);
688     RunningLockState lastState = lockInner->GetState();
689     if (lastState == RunningLockState::RUNNINGLOCK_STATE_PROXIED) {
690         lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_DISABLE);
691         Lock(remoteObj);
692     }
693 }
694 
UnlockInnerByProxy(const sptr<IRemoteObject> & remoteObj,std::shared_ptr<RunningLockInner> & lockInner)695 void RunningLockMgr::UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
696     std::shared_ptr<RunningLockInner>& lockInner)
697 {
698     RunningLockState lastState = lockInner->GetState();
699     if (lastState == RunningLockState::RUNNINGLOCK_STATE_DISABLE) {
700         POWER_HILOGW(FEATURE_RUNNING_LOCK, "UnlockInnerByProxy failed, runninglock Disable");
701         return;
702     }
703     if (lastState == RunningLockState::RUNNINGLOCK_STATE_PROXIED) {
704         POWER_HILOGW(FEATURE_RUNNING_LOCK, "UnlockInnerByProxy failed, runninglock Proxied");
705         return;
706     }
707     RunningLockParam lockInnerParam = lockInner->GetParam();
708     POWER_HILOGD(FEATURE_RUNNING_LOCK, "try UnlockInnerByProxy, name: %{public}s, type: %{public}d",
709         lockInnerParam.name.c_str(), lockInnerParam.type);
710     UnLock(remoteObj);
711     lockInner->SetState(RunningLockState::RUNNINGLOCK_STATE_PROXIED);
712 }
713 
EnableMock(IRunningLockAction * mockAction)714 void RunningLockMgr::EnableMock(IRunningLockAction* mockAction)
715 {
716     // reset lock list
717     runningLocks_.clear();
718     for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) {
719         it->second->Clear();
720     }
721     runninglockProxy_->Clear();
722 #ifdef HAS_SENSORS_SENSOR_PART
723     proximityController_.Clear();
724 #endif
725     std::shared_ptr<IRunningLockAction> mock(mockAction);
726     runningLockAction_ = mock;
727 }
728 
DumpInfo(std::string & result)729 void RunningLockMgr::DumpInfo(std::string& result)
730 {
731     auto validSize = GetValidRunningLockNum();
732     std::lock_guard<std::mutex> lock(mutex_);
733 
734     result.append("RUNNING LOCK DUMP:\n");
735     result.append("  totalSize=").append(ToString(runningLocks_.size()))
736             .append(" validSize=").append(ToString(validSize)).append("\n");
737     result.append("Summary By Type: \n");
738     for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) {
739         result.append("  ")
740             .append(PowerUtils::GetRunningLockTypeString(it->first))
741             .append(": ")
742             .append(ToString(it->second->GetCount()))
743             .append("\n");
744     }
745 
746     if (runningLocks_.empty()) {
747         result.append("Lock List is Empty. \n");
748         return;
749     }
750 
751     result.append("Dump Lock List: \n");
752     auto curTick = GetTickCount();
753     int index = 0;
754     for (const auto& it : runningLocks_) {
755         index++;
756         auto lockInner = it.second;
757         if (lockInner == nullptr) {
758             return;
759         }
760         auto& lockParam = lockInner->GetParam();
761         result.append("  index=").append(ToString(index))
762             .append(" time=").append(ToString(curTick - lockInner->GetLockTimeMs()))
763             .append(" type=").append(PowerUtils::GetRunningLockTypeString(lockParam.type))
764             .append(" name=").append(lockParam.name)
765             .append(" uid=").append(ToString(lockInner->GetUid()))
766             .append(" pid=").append(ToString(lockInner->GetPid()))
767             .append(" state=").append(ToString(static_cast<uint32_t>(lockInner->GetState())))
768             .append("\n");
769     }
770 
771     result.append("Dump Proxy List: \n");
772     result.append(runninglockProxy_->DumpProxyInfo());
773 #ifdef HAS_SENSORS_SENSOR_PART
774     result.append("Peripherals Info: \n")
775             .append("  Proximity: ")
776             .append("Enabled=")
777             .append(ToString(proximityController_.IsEnabled()))
778             .append(" Status=")
779             .append(ToString(proximityController_.GetStatus()))
780             .append("\n");
781 #endif
782 }
783 
OnRemoteDied(const wptr<IRemoteObject> & remote)784 void RunningLockMgr::RunningLockDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
785 {
786     if (remote == nullptr || remote.promote() == nullptr) {
787         POWER_HILOGW(FEATURE_RUNNING_LOCK, "Remote is nullptr");
788         return;
789     }
790     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
791     if (pms == nullptr) {
792         POWER_HILOGW(FEATURE_RUNNING_LOCK, "Power service is nullptr");
793         return;
794     }
795     pms->ForceUnLock(remote.promote());
796 }
797 
Increase(const RunningLockParam & lockInnerParam)798 int32_t RunningLockMgr::LockCounter::Increase(const RunningLockParam& lockInnerParam)
799 {
800     ++counter_;
801     int32_t result = RUNNINGLOCK_SUCCESS;
802     if (counter_ == 1) {
803         result = activate_(true, lockInnerParam);
804         if (result != RUNNINGLOCK_SUCCESS) {
805             --counter_;
806         }
807     }
808     if (result == RUNNINGLOCK_SUCCESS  && NeedNotify(lockInnerParam.type)) {
809         NotifyRunningLockChanged(lockInnerParam, "DUBAI_TAG_RUNNINGLOCK_ADD");
810     }
811     return result;
812 }
813 
Decrease(const RunningLockParam & lockInnerParam)814 int32_t RunningLockMgr::LockCounter::Decrease(const RunningLockParam& lockInnerParam)
815 {
816     --counter_;
817     int32_t result = RUNNINGLOCK_SUCCESS;
818     if (counter_ == 0) {
819         result = activate_(false, lockInnerParam);
820         if (result != RUNNINGLOCK_SUCCESS) {
821             ++counter_;
822         }
823     }
824     if (result == RUNNINGLOCK_SUCCESS && NeedNotify(lockInnerParam.type)) {
825         NotifyRunningLockChanged(lockInnerParam, "DUBAI_TAG_RUNNINGLOCK_REMOVE");
826     }
827     return result;
828 }
829 
Clear()830 void RunningLockMgr::LockCounter::Clear()
831 {
832     counter_ = 0;
833 }
834 
835 #ifdef HAS_SENSORS_SENSOR_PART
RecordSensorCallback(SensorEvent * event)836 void RunningLockMgr::ProximityController::RecordSensorCallback(SensorEvent *event)
837 {
838     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Sensor Callback come in");
839     if (event == nullptr) {
840         POWER_HILOGW(FEATURE_RUNNING_LOCK, "Sensor event is nullptr");
841         return;
842     }
843     if (event->sensorTypeId != SENSOR_TYPE_ID_PROXIMITY) {
844         POWER_HILOGW(FEATURE_RUNNING_LOCK, "Sensor type is not PROXIMITY");
845         return;
846     }
847     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
848     if (pms == nullptr) {
849         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
850         return;
851     }
852     auto runningLock = pms->GetRunningLockMgr();
853     ProximityData* data = reinterpret_cast<ProximityData*>(event->data);
854     int32_t distance = static_cast<int32_t>(data->distance);
855 
856     POWER_HILOGI(FEATURE_RUNNING_LOCK, "Sensor Callback data->distance=%{public}d", distance);
857     if (distance == PROXIMITY_CLOSE_SCALAR) {
858         runningLock->SetProximity(PROXIMITY_CLOSE);
859     } else if (distance == PROXIMITY_AWAY_SCALAR) {
860         runningLock->SetProximity(PROXIMITY_AWAY);
861     }
862 }
863 
ProximityController()864 RunningLockMgr::ProximityController::ProximityController()
865 {
866     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Instance enter");
867     SensorInfo* sensorInfo = nullptr;
868     int32_t count;
869     int ret = GetAllSensors(&sensorInfo, &count);
870     if (ret != 0 || sensorInfo == nullptr) {
871         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Get sensors fail, ret=%{public}d", ret);
872         return;
873     }
874     for (int32_t i = 0; i < count; i++) {
875         if (sensorInfo[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY) {
876             POWER_HILOGI(FEATURE_RUNNING_LOCK, "Support PROXIMITY sensor");
877             support_ = true;
878             break;
879         }
880     }
881     if (!support_) {
882         POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
883         return;
884     }
885     if (strcpy_s(user_.name, sizeof(user_.name), "RunningLock") != EOK) {
886         POWER_HILOGE(FEATURE_RUNNING_LOCK, "strcpy_s error");
887         return;
888     }
889     user_.userData = nullptr;
890     user_.callback = &RecordSensorCallback;
891 }
892 
~ProximityController()893 RunningLockMgr::ProximityController::~ProximityController()
894 {
895     if (support_) {
896         UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
897     }
898 }
899 
Enable()900 void RunningLockMgr::ProximityController::Enable()
901 {
902     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Enter");
903     enabled_ = true;
904     if (!support_) {
905         POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
906         return;
907     }
908 
909     int32_t errorCode = SubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
910     if (errorCode != ERR_OK) {
911         POWER_HILOGW(FEATURE_RUNNING_LOCK, "SubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
912         return;
913     }
914     SetBatch(SENSOR_TYPE_ID_PROXIMITY, &user_, SAMPLING_RATE, SAMPLING_RATE);
915     errorCode = ActivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
916     if (errorCode != ERR_OK) {
917         POWER_HILOGW(FEATURE_RUNNING_LOCK, "ActivateSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
918         return;
919     }
920     SetMode(SENSOR_TYPE_ID_PROXIMITY, &user_, SENSOR_ON_CHANGE);
921 }
922 
Disable()923 void RunningLockMgr::ProximityController::Disable()
924 {
925     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Enter");
926     enabled_ = false;
927     if (!support_) {
928         POWER_HILOGE(FEATURE_RUNNING_LOCK, "PROXIMITY sensor not support");
929         return;
930     }
931 
932     DeactivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
933     int32_t errorCode = UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
934     if (errorCode != ERR_OK) {
935         POWER_HILOGW(FEATURE_RUNNING_LOCK, "UnsubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
936     }
937 }
938 
IsClose()939 bool RunningLockMgr::ProximityController::IsClose()
940 {
941     POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY IsClose: %{public}d", isClose_);
942     return isClose_;
943 }
944 
OnClose()945 void RunningLockMgr::ProximityController::OnClose()
946 {
947     if (!enabled_ || IsClose()) {
948         POWER_HILOGI(FEATURE_RUNNING_LOCK, "PROXIMITY is disabled or closed already");
949         return;
950     }
951     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
952     if (pms == nullptr) {
953         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
954         return;
955     }
956     auto stateMachine = pms->GetPowerStateMachine();
957     auto suspendController = pms->GetSuspendController();
958     if (stateMachine == nullptr || suspendController == nullptr) {
959         POWER_HILOGE(FEATURE_RUNNING_LOCK, "state machine is nullptr");
960         return;
961     }
962     isClose_ = true;
963     POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY is closed");
964     auto runningLock = pms->GetRunningLockMgr();
965     if (runningLock->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) {
966         POWER_HILOGD(FEATURE_RUNNING_LOCK, "Change state to INACITVE when holding PROXIMITY LOCK");
967         uint32_t delayTime = FOREGROUND_INCALL_DELAY_TIME_MS;
968         if (!PowerUtils::IsForegroundApplication(INCALL_APP_BUNDLE_NAME)) {
969             delayTime = BACKGROUND_INCALL_DELAY_TIME_MS;
970         }
971         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Start proximity-screen-off timer, delay time:%{public}u", delayTime);
972         stateMachine->SetDelayTimer(delayTime, PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG);
973     } else {
974         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Unholding PROXIMITY LOCK");
975     }
976 }
977 
OnAway()978 void RunningLockMgr::ProximityController::OnAway()
979 {
980     if (!enabled_ || !IsClose()) {
981         POWER_HILOGI(FEATURE_RUNNING_LOCK, "PROXIMITY is disabled or away already");
982         return;
983     }
984     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
985     if (pms == nullptr) {
986         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Power service is nullptr");
987         return;
988     }
989     auto stateMachine = pms->GetPowerStateMachine();
990     if (stateMachine == nullptr) {
991         POWER_HILOGE(FEATURE_RUNNING_LOCK, "state machine is nullptr");
992         return;
993     }
994     isClose_ = false;
995     POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY is away");
996     auto runningLock = pms->GetRunningLockMgr();
997     if (runningLock->GetValidRunningLockNum(
998         RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) {
999         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Change state to AWAKE when holding PROXIMITY LOCK");
1000         runningLock->PreprocessBeforeAwake();
1001         stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true);
1002     } else {
1003         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Unholding PROXIMITY LOCK");
1004     }
1005 }
1006 
Clear()1007 void RunningLockMgr::ProximityController::Clear()
1008 {
1009     isClose_ = false;
1010 }
1011 
SetProximity(uint32_t status)1012 void RunningLockMgr::SetProximity(uint32_t status)
1013 {
1014     switch (status) {
1015         case PROXIMITY_CLOSE:
1016             proximityController_.OnClose();
1017             break;
1018         case PROXIMITY_AWAY:
1019             proximityController_.OnAway();
1020             break;
1021         default:
1022             break;
1023     }
1024 }
1025 
IsProximityClose()1026 bool RunningLockMgr::IsProximityClose()
1027 {
1028     return proximityController_.IsClose();
1029 }
1030 #endif
1031 
1032 } // namespace PowerMgr
1033 } // namespace OHOS
1034