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