1 /*
2  * Copyright (c) 2023 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 "sync_rule/screen_status_listener.h"
17 
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "screen_status.h"
21 #include "utils_log.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace CloudSync {
ScreenStatusSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,std::shared_ptr<ScreenStatusListener> listener)26 ScreenStatusSubscriber::ScreenStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
27                                                std::shared_ptr<ScreenStatusListener> listener)
28     : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener)
29 {
30 }
OnReceiveEvent(const EventFwk::CommonEventData & eventData)31 void ScreenStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
32 {
33     auto action = eventData.GetWant().GetAction();
34     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
35         LOGI("Screen On!");
36         ScreenStatus::SetScreenState(ScreenStatus::ScreenState::SCREEN_ON);
37     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
38         LOGI("Screen Off!");
39         ScreenStatus::SetScreenState(ScreenStatus::ScreenState::SCREEN_OFF);
40         listener_->ScreenOff();
41     }
42 }
43 
ScreenStatusListener(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)44 ScreenStatusListener::ScreenStatusListener(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
45 {
46     dataSyncManager_ = dataSyncManager;
47 }
48 
~ScreenStatusListener()49 ScreenStatusListener::~ScreenStatusListener()
50 {
51     Stop();
52 }
53 
ScreenOff()54 void ScreenStatusListener::ScreenOff()
55 {
56     dataSyncManager_->DownloadThumb();
57     dataSyncManager_->CacheVideo();
58 }
59 
Start()60 void ScreenStatusListener::Start()
61 {
62     /* subscribe Battery Okay Status and Charging status */
63     EventFwk::MatchingSkills matchingSkills;
64     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
65     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
66     EventFwk::CommonEventSubscribeInfo info(matchingSkills);
67     commonEventSubscriber_ = std::make_shared<ScreenStatusSubscriber>(info, shared_from_this());
68     auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
69     LOGI("Subscriber end, SubscribeResult = %{public}d", subRet);
70 }
71 
Stop()72 void ScreenStatusListener::Stop()
73 {
74     if (commonEventSubscriber_ != nullptr) {
75         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_);
76         commonEventSubscriber_ = nullptr;
77     }
78 }
79 } // namespace CloudSync
80 } // namespace FileManagement
81 } // namespace OHOS