1 /*
2 * Copyright (c) 2025 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 "screen_power_utils.h"
17 #include "power_mgr_client.h"
18 #include "window_manager_hilog.h"
19
20 using namespace OHOS::PowerMgr;
21
22 namespace OHOS {
23 namespace Rosen {
24
25 std::mutex ScreenPowerUtils::powerTimingMutex_;
26 bool ScreenPowerUtils::isEnablePowerForceTimingOut_ = false;
27
EnablePowerForceTimingOut()28 void ScreenPowerUtils::EnablePowerForceTimingOut()
29 {
30 std::lock_guard<std::mutex> lock(powerTimingMutex_);
31 TLOGI(WmsLogTag::DMS, "Enable power timeout begin.");
32 if (isEnablePowerForceTimingOut_) {
33 TLOGI(WmsLogTag::DMS, "Already enable.");
34 return;
35 }
36
37 auto& powerMgrClient = PowerMgrClient::GetInstance();
38 PowerErrors setRet = powerMgrClient.SetForceTimingOut(true);
39 if (setRet != PowerErrors::ERR_OK) {
40 TLOGE(WmsLogTag::DMS, "SetForceTimingOut failed: %{public}d", setRet);
41 return;
42 }
43
44 PowerErrors lockRet = powerMgrClient.LockScreenAfterTimingOut(true, true, false);
45 if (lockRet != PowerErrors::ERR_OK) {
46 setRet = powerMgrClient.SetForceTimingOut(false);
47 TLOGE(WmsLogTag::DMS, "LockScreenAfterTimingOut failed, lockRet: %{public}d, setRet: %{public}d",
48 lockRet, setRet);
49 return;
50 }
51 isEnablePowerForceTimingOut_ = true;
52 }
53
DisablePowerForceTimingOut()54 void ScreenPowerUtils::DisablePowerForceTimingOut()
55 {
56 std::lock_guard<std::mutex> lock(powerTimingMutex_);
57 TLOGI(WmsLogTag::DMS, "Disable power timeout begin.");
58 if (!isEnablePowerForceTimingOut_) {
59 TLOGI(WmsLogTag::DMS, "Already disable.");
60 return;
61 }
62 auto& powerMgrClient = PowerMgrClient::GetInstance();
63 PowerErrors setRet = powerMgrClient.SetForceTimingOut(false);
64 if (setRet != PowerErrors::ERR_OK) {
65 TLOGE(WmsLogTag::DMS, "SetForceTimingOut failed: %{public}d", setRet);
66 }
67
68 PowerErrors lockRet = powerMgrClient.LockScreenAfterTimingOut(true, false, true);
69 if (lockRet != PowerErrors::ERR_OK) {
70 TLOGE(WmsLogTag::DMS, "LockScreenAfterTimingOut failed, lockRet: %{public}d", lockRet);
71 }
72 isEnablePowerForceTimingOut_ = false;
73 }
74
LightAndLockScreen(const std::string & detail)75 void ScreenPowerUtils::LightAndLockScreen(const std::string& detail)
76 {
77 auto& powerMgrClient = PowerMgrClient::GetInstance();
78 if (!powerMgrClient.IsScreenOn()) {
79 PowerErrors wakeupRet = powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail);
80 PowerErrors suspendRet = powerMgrClient.SuspendDevice();
81 TLOGI(WmsLogTag::DMS, "wakeupRet: %{public}d, suspendRet: %{public}d", wakeupRet, suspendRet);
82 }
83 }
GetEnablePowerForceTimingOut()84 bool ScreenPowerUtils::GetEnablePowerForceTimingOut()
85 {
86 return isEnablePowerForceTimingOut_;
87 }
88 } // Rosen
89 } // OHOS