• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "screen_saver_handler.h"
17 
18 #include <thread>
19 
20 #include <element_name.h>
21 #include <samgr_lite.h>
22 #include <securec.h>
23 #include <want.h>
24 
25 #include "hilog_wrapper.h"
26 #include "running_lock_mgr.h"
27 
28 namespace OHOS {
~ScreenSaverHandler()29 ScreenSaverHandler::~ScreenSaverHandler()
30 {
31     if (timer_ != nullptr) {
32         PowerMgrDestroyTimer(timer_);
33         timer_ = nullptr;
34     }
35 }
36 
GetAmsInterface()37 AmsInterface *ScreenSaverHandler::GetAmsInterface()
38 {
39     static struct AmsInterface *ams = nullptr;
40     if (ams != nullptr) {
41         return ams;
42     }
43 
44     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE);
45     if (iUnknown == nullptr) {
46         POWER_HILOGE("Failed to get ams iUnknown");
47         return nullptr;
48     }
49 
50     int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&ams);
51     if ((ret != EC_SUCCESS) || (ams == nullptr)) {
52         POWER_HILOGE("Failed to query ams interface");
53         return nullptr;
54     }
55     POWER_HILOGI("Succeed to get ams interface");
56     return ams;
57 }
58 
StartScreenSaverLocked()59 bool ScreenSaverHandler::StartScreenSaverLocked()
60 {
61     AmsInterface *ams = GetAmsInterface();
62     if (ams == nullptr) {
63         POWER_HILOGE("Failed to get ams interface");
64         return false;
65     }
66 
67     Want want = { nullptr };
68     ElementName element = { nullptr };
69     SetElementBundleName(&element, "com.huawei.screensaver");
70     SetElementAbilityName(&element, "ScreensaverAbility");
71     SetWantElement(&want, element);
72     SetWantData(&want, "WantData", strlen("WantData") + 1);
73     int32_t ret = ams->StartAbility(&want);
74     ClearElement(&element);
75     ClearWant(&want);
76     return ret == EC_SUCCESS;
77 }
78 
StartScreenSaver()79 void ScreenSaverHandler::StartScreenSaver()
80 {
81     POWER_HILOGI("Time to start screen saver");
82     std::lock_guard<std::mutex> lock(mutex_);
83     if (!enabled_ || screenSaverStarted_ || (RunningLockMgrIsLockHolding(RUNNINGLOCK_SCREEN) == TRUE)) {
84         return;
85     }
86     screenSaverStarted_ = StartScreenSaverLocked();
87     POWER_HILOGI("Start screen saver: %d", static_cast<int32_t>(screenSaverStarted_));
88 }
89 
PowerTimerCallback(void * data)90 void PowerTimerCallback(void *data)
91 {
92     if (data == nullptr) {
93         POWER_HILOGE("Invalid timer data");
94         return;
95     }
96     ScreenSaverHandler *handler = static_cast<ScreenSaverHandler *>(data);
97     handler->StartScreenSaver();
98 }
99 
Init()100 void ScreenSaverHandler::Init()
101 {
102     if (timer_ == nullptr) {
103         timer_ = PowerMgrCreateTimer(intervalMsec_, intervalMsec_, PowerTimerCallback);
104     }
105     std::thread lateInit([this] {
106         sleep(20); // set delay to avoid registing input evnet listener failure
107         SetState(true);
108     });
109     lateInit.detach();
110 }
111 
SetInterval(int64_t intervalMsec)112 void ScreenSaverHandler::SetInterval(int64_t intervalMsec)
113 {
114     std::lock_guard<std::mutex> lock(mutex_);
115     if (intervalMsec_ == intervalMsec) {
116         return;
117     }
118     if (timer_ != nullptr) {
119         PowerMgrResetTimer(timer_, intervalMsec, intervalMsec);
120         if (enabled_) {
121             SetEnableLocked();
122         }
123     }
124     intervalMsec_ = intervalMsec;
125 }
126 
SetEnableLocked()127 bool ScreenSaverHandler::SetEnableLocked()
128 {
129     if (PowerMgrStartTimer(timer_, (void *)this) == FALSE) {
130         POWER_HILOGE("Failed to start timer");
131         return false;
132     }
133     if (!InputEventListenerProxy::GetInstance()->RegisterInputEventListener(this)) {
134         PowerMgrStopTimer(timer_);
135         POWER_HILOGE("Failed to register input event listener");
136         return false;
137     }
138     POWER_HILOGI("Succeed to enable screen saver");
139     return true;
140 }
141 
SetDisableLocked()142 bool ScreenSaverHandler::SetDisableLocked()
143 {
144     InputEventListenerProxy::GetInstance()->UnregisterInputEventListener();
145     PowerMgrStopTimer(timer_);
146     POWER_HILOGI("Succeed to disable screen saver");
147     return true;
148 }
149 
SetState(bool enable)150 int32_t ScreenSaverHandler::SetState(bool enable)
151 {
152     std::lock_guard<std::mutex> lock(mutex_);
153     if (timer_ == nullptr) {
154         return EC_FAILURE;
155     }
156     if (enabled_ == enable) {
157         return EC_SUCCESS;
158     }
159     bool ret = enable ? SetEnableLocked() : SetDisableLocked();
160     if (!ret) {
161         POWER_HILOGE("Failed to set state: %d", static_cast<int32_t>(enable));
162         return EC_FAILURE;
163     }
164     enabled_ = enable;
165     return EC_SUCCESS;
166 }
167 
OnRawEvent(const RawEvent & event)168 void ScreenSaverHandler::OnRawEvent(const RawEvent &event)
169 {
170     static int64_t lastUpdateTime = 0;
171     std::lock_guard<std::mutex> lock(mutex_);
172     screenSaverStarted_ = false;
173     int64_t currentTime = GetCurrentTimeMsec(CLOCK_MONOTONIC);
174     if ((currentTime - lastUpdateTime) > MSEC_PER_SEC) {
175         PowerMgrRestartTimer(timer_, (void *)this);
176         lastUpdateTime = currentTime;
177     }
178 }
179 } // namespace OHOS
180