1 /*
2  * Copyright (c) 2021-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 POWERMGR_DEVICE_STATE_ACTION_H
17 #define POWERMGR_DEVICE_STATE_ACTION_H
18 
19 #include "dm_common.h"
20 #include "display_manager_lite.h"
21 
22 #include "actions/idevice_state_action.h"
23 #include "display_power_callback_stub.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 class DeviceStateAction : public IDeviceStateAction {
28 public:
29     DeviceStateAction();
30     ~DeviceStateAction();
31     void Suspend(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags) override;
32     void ForceSuspend() override;
33     void Wakeup(int64_t callTimeMs, WakeupDeviceType type, const std::string& details,
34         const std::string& pkgName) override;
RefreshActivity(const int64_t callTimeMs,UserActivityType type,const uint32_t flags)35     void RefreshActivity(const int64_t callTimeMs, UserActivityType type,
36         const uint32_t flags) override {}
37     DisplayState GetDisplayState() override;
38     uint32_t SetDisplayState(DisplayState state,
39         StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN) override;
40     bool TryToCancelScreenOff() override;
41     void BeginPowerkeyScreenOff() override;
42     void EndPowerkeyScreenOff() override;
43     void SetCoordinated(bool coordinated) override;
44     uint32_t GoToSleep(std::function<void()> onSuspend, std::function<void()> onWakeup, bool force) override;
45     void RegisterCallback(std::function<void(uint32_t)>& callback) override;
46 
47 private:
48     static constexpr const char * const LOCK_TAG_DISPLAY_POWER = "display_power_lock";
49     class DisplayPowerCallback : public DisplayPowerMgr::DisplayPowerCallbackStub {
50         friend DeviceStateAction;
51     public:
52         void OnDisplayStateChanged(uint32_t displayId, DisplayPowerMgr::DisplayState state, uint32_t reason) override;
53     private:
54         void NotifyDisplayActionDone(uint32_t event);
55         std::function<void(uint32_t)> notify_ {nullptr};
56         std::mutex notifyMutex_;
57     };
58     bool IsInterruptingScreenOff(Rosen::PowerStateChangeReason dispReason);
59     std::mutex cancelScreenOffMutex_;
60     std::condition_variable cancelScreenOffCv_;
61     bool cancelScreenOffCvUnblocked_ {false};
62     bool interruptingScreenOff_ {false};
63     bool screenOffInterrupted_ {false};
64     bool screenOffStarted_ {false};
65     Rosen::PowerStateChangeReason GetDmsReasonByPowerReason(StateChangeReason reason);
66     bool isRegister_ {false};
67     sptr<DisplayPowerCallback> dispCallback_ {nullptr};
68     std::function<void(uint32_t)> actionCallback_ {nullptr};
69     bool coordinated_ {false};
70 };
71 } // namespace PowerMgr
72 } // namespace OHOS
73 #endif // POWERMGR_DEVICE_STATE_ACTION_H
74