1 /*
2  * Copyright (c) 2024 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 "key_crypto_utils.h"
17 
18 #include "os_account_manager.h"
19 #ifdef ENABLE_SCREENLOCK_MANAGER
20 #include "screenlock_manager.h"
21 #endif
22 #include "storage_service_log.h"
23 
24 namespace OHOS {
25 namespace StorageService {
ForceLockUserScreen()26 void KeyCryptoUtils::ForceLockUserScreen()
27 {
28     LOGI("KeyCryptoUtils::ForceLockUserScreen");
29 #ifdef ENABLE_SCREENLOCK_MANAGER
30     std::vector<int32_t> ids;
31     int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
32     if (ret != ERR_OK || ids.empty()) {
33         LOGE("Query active userid failed, ret = %{public}u", ret);
34         return;
35     }
36     int reasonFlag = static_cast<int>(ScreenLock::StrongAuthReasonFlags::ACTIVE_REQUEST);
37     ret = ScreenLock::ScreenLockManager::GetInstance()->RequestStrongAuth(reasonFlag, ids[0]);
38     if (ret != ScreenLock::E_SCREENLOCK_OK) {
39         LOGE("Request strong auth by screen lock manager failed.");
40         return;
41     }
42     ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(ids[0]);
43     if (ret != ScreenLock::E_SCREENLOCK_OK) {
44         LOGE("Lock user screen by screen lock manager failed.");
45     }
46     LOGI("Force lock user screen and request strong auth success for userId = %{public}d.", ids[0]);
47 #endif
48 }
49 }
50 }