• 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_bundle_manager.h"
26 #include "medialibrary_data_manager.h"
27 #include "medialibrary_errno.h"
28 #include "media_log.h"
29 #include "media_scanner_manager.h"
30 #include "medialibrary_inotify.h"
31 #include "application_context.h"
32 #include "ability_manager_client.h"
33 using namespace OHOS::AAFwk;
34 
35 namespace OHOS {
36 namespace Media {
37 const std::vector<std::string> MedialibrarySubscriber::events_ = {
38     EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED,
39     EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED,
40     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF,
41     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON,
42     EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED
43 };
44 
MedialibrarySubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)45 MedialibrarySubscriber::MedialibrarySubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
46     : EventFwk::CommonEventSubscriber(subscriberInfo)
47 {
48     isScreenOff_ = false;
49     isPowerConnected_ = false;
50 }
51 
Subscribe(void)52 bool MedialibrarySubscriber::Subscribe(void)
53 {
54     EventFwk::MatchingSkills matchingSkills;
55     for (auto event : events_) {
56         matchingSkills.AddEvent(event);
57     }
58     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
59 
60     std::shared_ptr<MedialibrarySubscriber> subscriber = std::make_shared<MedialibrarySubscriber>(subscribeInfo);
61     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
62 }
63 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)64 void MedialibrarySubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
65 {
66     const AAFwk::Want& want = eventData.GetWant();
67     std::string action = want.GetAction();
68     MEDIA_INFO_LOG("OnReceiveEvent action:%{public}s.", action.c_str());
69     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) == 0) {
70         isScreenOff_ = true;
71         DoBackgroundOperation();
72     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) == 0) {
73         isPowerConnected_ = true;
74         DoBackgroundOperation();
75     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) == 0) {
76         isScreenOff_ = false;
77         StopBackgroundOperation();
78     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) == 0) {
79         isPowerConnected_ = false;
80         StopBackgroundOperation();
81     } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) == 0) {
82         string packageName = want.GetElement().GetBundleName();
83         RevertPendingByPackage(packageName);
84         MediaLibraryBundleManager::GetInstance()->Clear();
85     }
86 }
87 
DoBackgroundOperation()88 void MedialibrarySubscriber::DoBackgroundOperation()
89 {
90     if (isScreenOff_ && isPowerConnected_) {
91         std::shared_ptr<MediaLibraryDataManager> dataManager = MediaLibraryDataManager::GetInstance();
92         if (dataManager == nullptr) {
93             return;
94         }
95         auto result = dataManager->GenerateThumbnails();
96         if (result != E_OK) {
97             MEDIA_ERR_LOG("GenerateThumbnails faild");
98         }
99 
100         result = dataManager->DoAging();
101         if (result != E_OK) {
102             MEDIA_ERR_LOG("DoAging faild");
103         }
104 
105         result = dataManager->DoTrashAging();
106         if (result != E_OK) {
107             MEDIA_ERR_LOG("DoTrashAging faild");
108         }
109         auto watch = MediaLibraryInotify::GetInstance();
110         if (watch != nullptr) {
111             watch->DoAging();
112         }
113         auto scannerManager = MediaScannerManager::GetInstance();
114         if (scannerManager == nullptr) {
115             return;
116         }
117         scannerManager->ScanError();
118 
119         MEDIA_DEBUG_LOG("DoBackgroundOperation success isScreenOff_ %{public}d, isPowerConnected_ %{public}d",
120             isScreenOff_, isPowerConnected_);
121     }
122 }
123 
StopBackgroundOperation()124 void MedialibrarySubscriber::StopBackgroundOperation()
125 {
126     MediaLibraryDataManager::GetInstance()->InterruptBgworker();
127 }
128 
DoStartMtpService()129 void MedialibrarySubscriber::DoStartMtpService()
130 {
131     AAFwk::Want want;
132     want.SetElementName("com.ohos.medialibrary.medialibrarydata", "MtpService");
133     auto abilityContext = AbilityRuntime::Context::GetApplicationContext();
134     ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, abilityContext->GetToken(),
135         OHOS::AAFwk::DEFAULT_INVAL_VALUE);
136     MEDIA_INFO_LOG("MedialibrarySubscriber::DoStartMtpService. End calling StartAbility. ret=%{public}d", err);
137 }
138 
RevertPendingByPackage(const std::string & bundleName)139 void MedialibrarySubscriber::RevertPendingByPackage(const std::string &bundleName)
140 {
141     MediaLibraryDataManager::GetInstance()->RevertPendingByPackage(bundleName);
142 }
143 }  // namespace Media
144 }  // namespace OHOS
145