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 #ifndef SCREEN_OFF_PRE_CONTROLLER_H
17 #define SCREEN_OFF_PRE_CONTROLLER_H
18 
19 #include <mutex>
20 #include <set>
21 
22 #include "iscreen_off_pre_callback.h"
23 #include "ffrt_utils.h"
24 #include "power_state_machine.h"
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 
29 class ScreenOffPreController {
30 public:
31     explicit ScreenOffPreController(std::shared_ptr<PowerStateMachine>& stateMachine);
32     ~ScreenOffPreController() = default;
33     void Init();
34     bool IsRegistered();
35     void SchedulEyeDetectTimeout(int64_t nextTimeOut, int64_t now);
36     void AddScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback);
37     void DelScreenStateCallback(const sptr<IScreenOffPreCallback>& callback);
38     void Reset();
39 
40 private:
41     class CallbackMgr : public IRemoteObject::DeathRecipient {
42     public:
43         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
44         void AddCallback(const sptr<IScreenOffPreCallback>& callback);
45         void RemoveCallback(const sptr<IScreenOffPreCallback>& callback);
46         void Notify();
47 
48     private:
49         std::mutex mutex_;
50         std::set<sptr<IRemoteObject>> callbackSet_;
51     };
52 
53     int32_t remainTime_ = 0;
54     bool isRegistered_ = false;
55     sptr<CallbackMgr> callbackMgr_;
56     std::mutex mutexLock_;
57     std::mutex ffrtMutex_;
58     std::shared_ptr<PowerStateMachine> powerStateMachine_;
59     std::shared_ptr<FFRTQueue> queue_;
60     FFRTHandle eyeDetectTimeOutHandle_ {nullptr};
61     void TriggerCallback();
62     bool NeedEyeDetectLocked(int64_t nextEyeDetectTime);
63     void HandleEyeDetectTimeout(int64_t delayTime);
64     void CancelEyeDetectTimeout();
65 };
66 
67 } // namespace PowerMgr
68 } // namespace OHOS
69 
70 #endif // SCREEN_OFF_PRE_CONTROLLER_H