1 /*
2  * Copyright (c) 2024 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 "ringtone_subscriber.h"
18 
19 #include "common_event_support.h"
20 #include "ringtone_log.h"
21 
22 namespace OHOS {
23 namespace Media {
24 using namespace OHOS::AAFwk;
25 const std::vector<std::string> RingtoneSubscriber::events_ = {
26     EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING,
27     EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING,
28     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF,
29     EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON,
30     EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED,
31     EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED
32 };
33 
RingtoneSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)34 RingtoneSubscriber::RingtoneSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
35     : EventFwk::CommonEventSubscriber(subscriberInfo)
36 {
37 }
38 
Subscribe(void)39 bool RingtoneSubscriber::Subscribe(void)
40 {
41     EventFwk::MatchingSkills matchingSkills;
42     for (auto event : events_) {
43         matchingSkills.AddEvent(event);
44     }
45     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
46 
47     std::shared_ptr<RingtoneSubscriber> subscriber = std::make_shared<RingtoneSubscriber>(subscribeInfo);
48     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
49 }
50 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)51 void RingtoneSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
52 {
53     const AAFwk::Want &want = eventData.GetWant();
54     std::string action = want.GetAction();
55     RINGTONE_INFO_LOG("OnReceiveEvent action:%{public}s.", action.c_str());
56     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) == 0) {
57         RINGTONE_INFO_LOG("recieve boot complete event.");
58     }
59 }
60 }  // namespace Media
61 }  // namespace OHOS
62