• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "param_event_manager.h"
17 
18 #include <common_event_data.h>
19 #include <common_event_manager.h>
20 #include <common_event_support.h>
21 #include <memory>
22 #include <unistd.h>
23 
24 #include "common_event_subscriber.h"
25 #include "file_util.h"
26 #include "logger.h"
27 #include "param_manager.h"
28 #include "param_reader.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 DEFINE_LOG_TAG("Hiview-ParamUpdate");
33 const std::string EVENT_INFO_TYPE = "type";
34 const std::string EVENT_INFO_SUBTYPE = "subtype";
35 
~ParamEventManager()36 ParamEventManager::~ParamEventManager()
37 {
38     UnSubscriberEvent();
39 }
40 
SubscriberEvent()41 void ParamEventManager::SubscriberEvent()
42 {
43     HIVIEW_LOGI("SubscriberEvent start.");
44     if (subscriber_) {
45         HIVIEW_LOGI("Common Event is already subscribered.");
46         return;
47     }
48 
49     EventFwk::MatchingSkills matchingSkills;
50     matchingSkills.AddEvent(CONFIG_UPDATED_ACTION);
51     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
52     subscriber_ = std::make_shared<ParamCommonEventSubscriber>(subscribeInfo, *this);
53 
54     bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
55     if (!subscribeResult) {
56         HIVIEW_LOGE("SubscriberEvent failed.");
57         subscriber_ = nullptr;
58         return;
59     }
60 };
61 
UnSubscriberEvent()62 void ParamEventManager::UnSubscriberEvent()
63 {
64     if (subscriber_) {
65         bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
66         HIVIEW_LOGI("subscribeResult = %{public}d", subscribeResult);
67         subscriber_ = nullptr;
68     }
69 };
70 
OnReceiveEvent(const AAFwk::Want & want)71 void ParamEventManager::OnReceiveEvent(const AAFwk::Want &want)
72 {
73     std::string action = want.GetAction();
74     std::string type = want.GetStringParam(EVENT_INFO_TYPE);
75     std::string subtype = want.GetStringParam(EVENT_INFO_SUBTYPE);
76     HIVIEW_LOGI("recive param update event: %{public}s, %{public}s, %{public}s", action.c_str(), type.c_str(),
77         subtype.c_str());
78     if (action != CONFIG_UPDATED_ACTION || type != CONFIG_TYPE) {
79         HIVIEW_LOGI("ignore other event.");
80         return;
81     }
82     ParamManager::InitParam();
83 };
84 } // namespace HiviewDFX
85 } // namespace OHOS
86