• 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 "hiview_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 constexpr char EVENT_INFO_TYPE[] = "type";
34 constexpr char EVENT_INFO_SUBTYPE[] = "subtype";
ParamEventManager()35 ParamEventManager::ParamEventManager() {}
36 
~ParamEventManager()37 ParamEventManager::~ParamEventManager()
38 {
39     UnSubscriberEvent();
40 }
41 
SubscriberEvent()42 void ParamEventManager::SubscriberEvent()
43 {
44     HIVIEW_LOGI("SubscriberEvent start.");
45     if (subscriber_) {
46         HIVIEW_LOGI("Common Event is already subscribered.");
47         return;
48     }
49 
50     EventFwk::MatchingSkills matchingSkills;
51     matchingSkills.AddEvent(CONFIG_UPDATED_ACTION);
52     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
53     subscribeInfo.SetPermission("ohos.permission.RECEIVE_UPDATE_MESSAGE");
54     subscriber_ = std::make_shared<ParamCommonEventSubscriber>(subscribeInfo, *this);
55 
56     bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
57     if (!subscribeResult) {
58         HIVIEW_LOGE("SubscriberEvent failed.");
59         subscriber_ = nullptr;
60         return;
61     }
62 };
63 
UnSubscriberEvent()64 void ParamEventManager::UnSubscriberEvent()
65 {
66     if (subscriber_) {
67         bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
68         HIVIEW_LOGI("subscribeResult = %{public}d", subscribeResult);
69         subscriber_ = nullptr;
70     }
71 };
72 
OnReceiveEvent(const AAFwk::Want & want)73 void ParamEventManager::OnReceiveEvent(const AAFwk::Want &want)
74 {
75     std::string action = want.GetAction();
76     std::string type = want.GetStringParam(EVENT_INFO_TYPE);
77     std::string subtype = want.GetStringParam(EVENT_INFO_SUBTYPE);
78     HIVIEW_LOGI("recive param update event: %{public}s, %{public}s, %{public}s", action.c_str(), type.c_str(),
79         subtype.c_str());
80     if (action != CONFIG_UPDATED_ACTION || type != CONFIG_TYPE) {
81         HIVIEW_LOGI("ignore other event.");
82         return;
83     }
84     ParamManager::InitParam();
85 };
86 } // namespace HiviewDFX
87 } // namespace OHOS
88