• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define MLOG_TAG "Subscribe"
16 
17 #include "medialibrary_subscriber.h"
18 
19 #include "appexecfwk_errors.h"
20 #include "bundle_info.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "want.h"
24 
25 #include "medialibrary_data_manager.h"
26 #include "medialibrary_errno.h"
27 #include "media_log.h"
28 #include "media_scanner_manager.h"
29 
30 using namespace OHOS::AAFwk;
31 
32 namespace OHOS {
33 namespace Media {
34 const std::vector<std::string> MedialibrarySubscriber::events_ = {
35     EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED,
36     EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED,
37     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF,
38     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON
39 };
40 
MedialibrarySubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)41 MedialibrarySubscriber::MedialibrarySubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
42     : EventFwk::CommonEventSubscriber(subscriberInfo)
43 {
44     isScreenOff_ = false;
45     isPowerConnected_ = false;
46 }
47 
Subscribe(void)48 bool MedialibrarySubscriber::Subscribe(void)
49 {
50     EventFwk::MatchingSkills matchingSkills;
51     for (auto event : events_) {
52         matchingSkills.AddEvent(event);
53     }
54     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
55 
56     std::shared_ptr<MedialibrarySubscriber> subscriber = std::make_shared<MedialibrarySubscriber>(subscribeInfo);
57     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
58 }
59 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)60 void MedialibrarySubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
61 {
62     const AAFwk::Want& want = eventData.GetWant();
63     std::string action = want.GetAction();
64     MEDIA_INFO_LOG("OnReceiveEvent action:%{public}s.", action.c_str());
65 
66     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) == 0) {
67         isScreenOff_ = true;
68         DoBackgroundOperation();
69     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) == 0) {
70         isPowerConnected_ = true;
71         DoBackgroundOperation();
72     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) == 0) {
73         isScreenOff_ = false;
74         StopBackgroundOperation();
75     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) == 0) {
76         isPowerConnected_ = false;
77         StopBackgroundOperation();
78     }
79 }
80 
DoBackgroundOperation()81 void MedialibrarySubscriber::DoBackgroundOperation()
82 {
83     if (isScreenOff_ && isPowerConnected_) {
84         std::shared_ptr<MediaLibraryDataManager> dataManager = MediaLibraryDataManager::GetInstance();
85         if (dataManager == nullptr) {
86             return;
87         }
88         auto result = dataManager->GenerateThumbnails();
89         if (result != E_OK) {
90             MEDIA_ERR_LOG("GenerateThumbnails faild");
91         }
92 
93         result = dataManager->DoAging();
94         if (result != E_OK) {
95             MEDIA_ERR_LOG("DoAging faild");
96         }
97     } else {
98         MEDIA_DEBUG_LOG("DoBackgroundOperation success isScreenOff_ %{public}d, isPowerConnected_ %{public}d",
99             isScreenOff_, isPowerConnected_);
100     }
101 }
102 
StopBackgroundOperation()103 void MedialibrarySubscriber::StopBackgroundOperation()
104 {
105     MediaLibraryDataManager::GetInstance()->InterruptBgworker();
106 }
107 }  // namespace Media
108 }  // namespace OHOS
109