1 /*
2 * Copyright (c) 2024-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 "unlock_screen_manager.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "in_process_call_wrapper.h"
20 #include "parameters.h"
21 #include "permission_verification.h"
22 #ifdef SUPPORT_POWER
23 #include "power_mgr_client.h"
24 #endif
25
26 #ifdef SUPPORT_GRAPHICS
27 #include "unlock_screen_callback.h"
28 #ifdef SUPPORT_SCREEN
29 #ifdef ABILITY_RUNTIME_SCREENLOCK_ENABLE
30 #include "screenlock_manager.h"
31 #include "screenlock_common.h"
32 #endif // ABILITY_RUNTIME_SCREENLOCK_ENABLE
33 #endif
34 #endif
35
36 namespace OHOS {
37 namespace AbilityRuntime {
38 namespace {
39 constexpr char DEVELOPER_MODE_STATE[] = "const.security.developermode.state";
40 }
41
~UnlockScreenManager()42 UnlockScreenManager::~UnlockScreenManager() {}
43
UnlockScreenManager()44 UnlockScreenManager::UnlockScreenManager() {}
45
GetInstance()46 UnlockScreenManager &UnlockScreenManager::GetInstance()
47 {
48 static UnlockScreenManager instance;
49 return instance;
50 }
51
UnlockScreen()52 bool UnlockScreenManager::UnlockScreen()
53 {
54 bool isShellCall = AAFwk::PermissionVerification::GetInstance()->IsShellCall();
55 bool isDeveloperMode = system::GetBoolParameter(DEVELOPER_MODE_STATE, false);
56 if (!isShellCall || !isDeveloperMode) {
57 return true;
58 }
59
60 #ifdef SUPPORT_GRAPHICS
61 #ifdef SUPPORT_SCREEN
62 #ifdef ABILITY_RUNTIME_SCREENLOCK_ENABLE
63 bool isScreenLocked = OHOS::ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked();
64 bool isScreenSecured = OHOS::ScreenLock::ScreenLockManager::GetInstance()->GetSecure();
65 TAG_LOGD(AAFwkTag::ABILITYMGR, "isScreenLocked: %{public}d, isScreenSecured: %{public}d",
66 isScreenLocked, isScreenSecured);
67 if (isScreenLocked && isScreenSecured) {
68 return false;
69 }
70 #endif // ABILITY_RUNTIME_SCREENLOCK_ENABLE
71 #endif
72 #endif
73
74 TAG_LOGI(AAFwkTag::ABILITYMGR, "UnlockScreen begin");
75 #ifdef SUPPORT_POWER
76 bool isScreenOn = PowerMgr::PowerMgrClient::GetInstance().IsScreenOn();
77 TAG_LOGD(AAFwkTag::ABILITYMGR, "isScreenOn: %{public}d", isScreenOn);
78 if (!isScreenOn) {
79 PowerMgr::PowerMgrClient::GetInstance().WakeupDevice();
80 }
81 #endif
82
83 #ifdef SUPPORT_GRAPHICS
84 #ifdef ABILITY_RUNTIME_SCREENLOCK_ENABLE
85 if (isScreenLocked) {
86 auto promise = std::make_shared<std::promise<bool>>();
87 sptr<UnlockScreenCallback> listener = sptr<UnlockScreenCallback>::MakeSptr(promise);
88 IN_PROCESS_CALL(OHOS::ScreenLock::ScreenLockManager::GetInstance()->Unlock(
89 OHOS::ScreenLock::Action::UNLOCKSCREEN, listener));
90 auto future = promise->get_future();
91 std::future_status status = future.wait_for(std::chrono::milliseconds(500));
92 if (status == std::future_status::timeout) {
93 TAG_LOGE(AAFwkTag::ABILITYMGR, "UnlockScreen timeout");
94 return false;
95 }
96 TAG_LOGI(AAFwkTag::ABILITYMGR, "UnlockScreen end");
97 return future.get();
98 }
99 #endif // ABILITY_RUNTIME_SCREENLOCK_ENABLE
100 #endif
101 TAG_LOGI(AAFwkTag::ABILITYMGR, "UnlockScreen end");
102 return true;
103 }
104 } // namespace AbilityRuntime
105 } // namespace OHOS