1 /*
2  * Copyright (C) 2022 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 #ifndef SERVICES_INCLUDE_SCLOCK_MANAGER_H
17 #define SERVICES_INCLUDE_SCLOCK_MANAGER_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "iremote_object.h"
23 #include "refbase.h"
24 #include "screenlock_callback_interface.h"
25 #include "screenlock_common.h"
26 #include "screenlock_manager_interface.h"
27 #include "visibility.h"
28 
29 namespace OHOS {
30 namespace ScreenLock {
31 class ScreenLockManager : public RefBase {
32 public:
33     SCREENLOCK_API static sptr<ScreenLockManager> GetInstance();
34     /**
35      * Lock the screen.
36      *
37      * @param userId Indicates the user ID.
38      * @return Returns E_SCREENLOCK_OK if success; otherwise failed.
39      */
40     SCREENLOCK_API int32_t Lock(int32_t userId);
41 
42     /**
43      * Check whether the screen is currently locked.
44      *
45      * @param isLocked Indicates the screen lock state.
46      * @return Returns E_SCREENLOCK_OK if success; otherwise failed.
47      */
48     SCREENLOCK_API int32_t IsLocked(bool &isLocked);
49 
50     SCREENLOCK_API bool IsScreenLocked();
51     SCREENLOCK_API bool GetSecure();
52     SCREENLOCK_API int32_t Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener);
53     SCREENLOCK_API int32_t RequestStrongAuth(int reasonFlag, int32_t userId);
54     int32_t Lock(const sptr<ScreenLockCallbackInterface> &listener);
55 private:
56     class ScreenLockSaDeathRecipient : public IRemoteObject::DeathRecipient {
57     public:
ScreenLockSaDeathRecipient()58         explicit ScreenLockSaDeathRecipient(){};
59         ~ScreenLockSaDeathRecipient() = default;
OnRemoteDied(const wptr<IRemoteObject> & object)60         void OnRemoteDied(const wptr<IRemoteObject> &object) override
61         {
62             ScreenLockManager::GetInstance()->OnRemoteSaDied(object);
63         }
64     };
65 
66     ScreenLockManager();
67     ~ScreenLockManager() override;
68     void RemoveDeathRecipient();
69     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
70     sptr<ScreenLockManagerInterface> GetProxy();
71     sptr<ScreenLockManagerInterface> GetScreenLockManagerProxy();
72     static std::mutex instanceLock_;
73     static sptr<ScreenLockManager> instance_;
74     sptr<ScreenLockSaDeathRecipient> deathRecipient_;
75     std::mutex managerProxyLock_;
76     sptr<ScreenLockManagerInterface> screenlockManagerProxy_;
77 };
78 } // namespace ScreenLock
79 } // namespace OHOS
80 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_MANAGER_H