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 #include "conditions/storage_listener.h"
16
17 #include "common_event_manager.h"
18 #include "common_event_support.h"
19 #include "matching_skills.h"
20 #include "want.h"
21 #include "work_sched_hilog.h"
22
23 namespace OHOS {
24 namespace WorkScheduler {
StorageEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,StorageListener & listener)25 StorageEventSubscriber::StorageEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
26 StorageListener &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {}
27
OnReceiveEvent(const EventFwk::CommonEventData & data)28 void StorageEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
29 {
30 const std::string action = data.GetWant().GetAction();
31 WS_HILOGD("OnReceiveEvent get action: %{public}s", action.c_str());
32 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_LOW) {
33 WS_HILOGI("Condition changed: STORAGE_LOW");
34 listener_.OnConditionChanged(WorkCondition::Type::STORAGE,
35 std::make_shared<DetectorValue>(WorkCondition::STORAGE_LEVEL_LOW, 0, 0, std::string()));
36 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_OK) {
37 WS_HILOGI("Condition changed: STORAGE_OK");
38 listener_.OnConditionChanged(WorkCondition::Type::STORAGE,
39 std::make_shared<DetectorValue>(WorkCondition::STORAGE_LEVEL_OKAY, 0, 0, std::string()));
40 } else {
41 WS_HILOGE("OnReceiveEvent action is invalid");
42 }
43 }
44
CreateStorageEventSubscriber(StorageListener & listener)45 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateStorageEventSubscriber(StorageListener &listener)
46 {
47 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
48 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_LOW);
49 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_OK);
50 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_FULL);
51 EventFwk::CommonEventSubscribeInfo info(skill);
52 return std::make_shared<StorageEventSubscriber>(info, listener);
53 }
54
StorageListener(std::shared_ptr<WorkQueueManager> workQueueManager)55 StorageListener::StorageListener(std::shared_ptr<WorkQueueManager> workQueueManager)
56 {
57 workQueueManager_ = workQueueManager;
58 }
59
~StorageListener()60 StorageListener::~StorageListener()
61 {
62 this->Stop();
63 }
64
Start()65 bool StorageListener::Start()
66 {
67 WS_HILOGI("Storage listener start");
68 this->commonEventSubscriber = CreateStorageEventSubscriber(*this);
69 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
70 }
71
Stop()72 bool StorageListener::Stop()
73 {
74 WS_HILOGI("Storage listener stop");
75 if (this->commonEventSubscriber != nullptr) {
76 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
77 if (result) {
78 this->commonEventSubscriber = nullptr;
79 }
80 return result;
81 }
82 return true;
83 }
84
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)85 void StorageListener::OnConditionChanged(WorkCondition::Type conditionType,
86 std::shared_ptr<DetectorValue> conditionVal)
87 {
88 if (workQueueManager_ != nullptr) {
89 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
90 } else {
91 WS_HILOGE("workQueueManager_ is nullptr.");
92 }
93 }
94 } // namespace WorkScheduler
95 } // namespace OHOS