• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_->TriggerRecoverySync(SyncTriggerType::SCREEN_OFF_TRIGGER);
57     dataSyncManager_->DownloadThumb();
58     dataSyncManager_->CacheVideo();
59 }
60 
Start()61 void ScreenStatusListener::Start()
62 {
63     /* subscribe Battery Okay Status and Charging status */
64     EventFwk::MatchingSkills matchingSkills;
65     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
66     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
67     EventFwk::CommonEventSubscribeInfo info(matchingSkills);
68     commonEventSubscriber_ = std::make_shared<ScreenStatusSubscriber>(info, shared_from_this());
69     auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
70     LOGI("Subscriber end, SubscribeResult = %{public}d", subRet);
71 }
72 
Stop()73 void ScreenStatusListener::Stop()
74 {
75     if (commonEventSubscriber_ != nullptr) {
76         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_);
77         commonEventSubscriber_ = nullptr;
78     }
79 }
80 } // namespace CloudSync
81 } // namespace FileManagement
82 } // namespace OHOS