From b67649d2db25c0b112516c83dc6efd7d46a31c86 Mon Sep 17 00:00:00 2001 From: huangxiaolinabc123 Date: Fri, 10 Feb 2023 07:18:49 +0000 Subject: [PATCH] add common Signed-off-by: huangxiaolinabc123 Change-Id: I7c9af7b8247f8ea70f8b383e81fcf669e03f7db4 --- services/samgr/native/BUILD.gn | 3 + .../include/collect/common_event_collect.h | 70 +++++++++ .../collect/device_status_collect_manager.h | 2 +- .../source/collect/common_event_collect.cpp | 136 ++++++++++++++++++ .../collect/device_status_collect_manager.cpp | 3 + 5 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 services/samgr/native/include/collect/common_event_collect.h create mode 100644 services/samgr/native/source/collect/common_event_collect.cpp diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn index 460948fa..b1415def 100644 --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -42,6 +42,7 @@ ohos_executable("samgr") { "//foundation/systemabilitymgr/samgr/services/dfx/source/hisysevent_adapter.cpp", "//foundation/systemabilitymgr/samgr/services/lsamgr/src/local_ability_manager_proxy.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/ability_death_recipient.cpp", + "//foundation/systemabilitymgr/samgr/services/samgr/native/source/collect/common_event_collect.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/collect/device_status_collect_manager.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/collect/icollect_plugin.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/main.cpp", @@ -67,8 +68,10 @@ ohos_executable("samgr") { defines = [] if (is_standard_system) { external_deps = [ + "ability_base:want", "access_token:libaccesstoken_sdk", "c_utils:utils", + "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", diff --git a/services/samgr/native/include/collect/common_event_collect.h b/services/samgr/native/include/collect/common_event_collect.h new file mode 100644 index 00000000..b249ab31 --- /dev/null +++ b/services/samgr/native/include/collect/common_event_collect.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H +#define SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H + +#include + +#include "common_event_subscriber.h" +#include "device_status_collect_manager.h" +#include "icollect_plugin.h" +#include "event_handler.h" + +namespace OHOS { +class ScreenEventStatusCollect : public ICollectPlugin { +public: + explicit ScreenEventStatusCollect(const sptr& report, const sptr & deviceStatusCollectManager); + ~ScreenEventStatusCollect() = default; + + int32_t OnStart() override; + int32_t OnStop() override; + bool AddScreenChangeListener(); +private: + std::vector commonNames_; + sptr deviceStatusCollectManager_; + std::shared_ptr commonHandler_; + std::shared_ptr commonEventSubscriber = nullptr; + std::shared_ptr CreateScreenEventSubscriber(); + bool IsCesReady(); + bool IsCommonEvent(const OnDemandEvent& ev); +}; + +class CommonHandler : public AppExecFwk::EventHandler { + public: + CommonHandler(const std::shared_ptr& runner, + const sptr& collect) : AppExecFwk::EventHandler(runner), screenCollect_(collect) {} + ~CommonHandler() = default; + void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; + + private: + sptr screenCollect_; +}; + +class ScreenEventStatusSubscriber : public EventFwk::CommonEventSubscriber { +public: + ScreenEventStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, + const std::shared_ptr &collect); + ~ScreenEventStatusSubscriber() override = default; + /** + * @brief The OnReceiveEvent callback. + * + * @param data The data. + */ + void OnReceiveEvent(const EventFwk::CommonEventData &data) override; +private: + std::shared_ptr collect_; +}; +} // namespace OHOS +#endif // SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H \ No newline at end of file diff --git a/services/samgr/native/include/collect/device_status_collect_manager.h b/services/samgr/native/include/collect/device_status_collect_manager.h index cc722a23..3f6f7007 100644 --- a/services/samgr/native/include/collect/device_status_collect_manager.h +++ b/services/samgr/native/include/collect/device_status_collect_manager.h @@ -31,13 +31,13 @@ public: void UnInit(); void ReportEvent(const OnDemandEvent& event) override; void StartCollect(); + std::list onDemandSaProfiles_; private: void FilterOnDemandSaProfiles(const std::list& saProfiles); void GetSaControlListByEvent(const OnDemandEvent& event, std::list& saControlList); static bool IsSameEvent(const OnDemandEvent& ev1, const OnDemandEvent& ev2); std::map> collectPluginMap_; std::shared_ptr collectHandler_; - std::list onDemandSaProfiles_; }; } // namespace OHOS #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_STATUS_COLLECT_MANAGER_H \ No newline at end of file diff --git a/services/samgr/native/source/collect/common_event_collect.cpp b/services/samgr/native/source/collect/common_event_collect.cpp new file mode 100644 index 00000000..4e2a5f28 --- /dev/null +++ b/services/samgr/native/source/collect/common_event_collect.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_event_collect.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "matching_skills.h" +#include "want.h" +#include "sam_log.h" +#include "sa_profiles.h" +#include "system_ability_definition.h" +#include "system_ability_manager.h" + +using namespace OHOS::AppExecFwk; +namespace OHOS { +namespace { +const std::string SA_TAG_COMMONEVENT_EVENT = "commonevent"; +constexpr uint32_t INIT_EVENT = 10; +constexpr int64_t DELAY_TIME = 1000; +} +ScreenEventStatusSubscriber::ScreenEventStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, + const std::shared_ptr &collect) : EventFwk::CommonEventSubscriber(subscribeInfo), collect_(collect) {} + +void ScreenEventStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) +{ + const std::string action = data.GetWant().GetAction(); + HILOGD("OnReceiveEvent get action: %{public}s", action.c_str()); + OnDemandEvent event = { COMMON_EVENT, action, ""}; + collect_->ReportEvent(event); +} + +bool ScreenEventStatusCollect::IsCommonEvent(const OnDemandEvent& ev) +{ + return ev.eventId == COMMON_EVENT; +} + +bool ScreenEventStatusCollect::IsCesReady() +{ + auto cesProxy = SystemAbilityManager::GetInstance()->CheckSystemAbility( + COMMON_EVENT_SERVICE_ID); + if (cesProxy != nullptr) { + IPCObjectProxy* proxy = reinterpret_cast(cesProxy.GetRefPtr()); + // make sure the proxy is not dead + if (proxy != nullptr && !proxy->IsObjectDead()) { + return true; + } + } + return false; +} + +std::shared_ptr ScreenEventStatusCollect::CreateScreenEventSubscriber() +{ + HILOGI("CreateScreenEventSubscriber called"); + EventFwk::MatchingSkills skill = EventFwk::MatchingSkills(); + for (auto& commonName : commonNames_) { + skill.AddEvent(commonName); + } + EventFwk::CommonEventSubscribeInfo info(skill); + std::shared_ptr screenEventstatusCollect(this); + return std::make_shared(info, screenEventstatusCollect); +} + +ScreenEventStatusCollect::ScreenEventStatusCollect(const sptr& report, const sptr & deviceStatusCollectManager) + : ICollectPlugin(report), deviceStatusCollectManager_(deviceStatusCollectManager) +{ +} + +int32_t ScreenEventStatusCollect::OnStart() +{ + HILOGI("ScreenEventStatusCollect OnStart called"); + std::shared_ptr handler = EventHandler::Current(); + if (handler == nullptr) { + return ERR_INVALID_VALUE; + } + commonHandler_ = std::make_shared(handler->GetEventRunner(), this); + commonHandler_->SendEvent(INIT_EVENT); + return ERR_OK; +} + +int32_t ScreenEventStatusCollect::OnStop() +{ + if (commonHandler_ != nullptr) { + commonHandler_->SetEventRunner(nullptr); + commonHandler_ = nullptr; + } + return ERR_OK; +} + +bool ScreenEventStatusCollect::AddScreenChangeListener() +{ + HILOGI("ScreenEventStatusCollect AddScreenChangeListener called"); + if (IsCesReady()) { + HILOGI("Ces is Ready"); + for (auto& profile : deviceStatusCollectManager_->onDemandSaProfiles_) { + for (auto iterStart = profile.startOnDemand.begin(); iterStart != profile.startOnDemand.end(); iterStart++) { + if (IsCommonEvent(*iterStart)) { + commonNames_.push_back(iterStart->name); + } + } + } + this->commonEventSubscriber = CreateScreenEventSubscriber(); + return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber); + } + return false; +} + +void CommonHandler::ProcessEvent(const InnerEvent::Pointer& event) +{ + if (screenCollect_ == nullptr || event == nullptr) { + HILOGE("ScreenEventStatusCollect ProcessEvent collect or event is null!"); + return; + } + auto eventId = event->GetInnerEventId(); + if (eventId != INIT_EVENT) { + HILOGE("ScreenEventStatusCollect ProcessEvent error event code!"); + return; + } + if (!screenCollect_->AddScreenChangeListener()) { + HILOGW("ScreenEventStatusCollect AddScreenChangeListener retry"); + SendEvent(INIT_EVENT, DELAY_TIME); + } + HILOGI("ScreenEventStatusCollect AddScreenChangeListener success"); +} +} // namespace OHOS \ No newline at end of file diff --git a/services/samgr/native/source/collect/device_status_collect_manager.cpp b/services/samgr/native/source/collect/device_status_collect_manager.cpp index 83b2a06d..096803ec 100644 --- a/services/samgr/native/source/collect/device_status_collect_manager.cpp +++ b/services/samgr/native/source/collect/device_status_collect_manager.cpp @@ -19,6 +19,7 @@ #ifdef SUPPORT_DEVICE_MANAGER #include "device_networking_collect.h" #endif +#include "common_event_collect.h" #include "sam_log.h" #include "system_ability_manager.h" @@ -33,6 +34,8 @@ void DeviceStatusCollectManager::Init(const std::list& saProfiles) sptr networkingCollect = new DeviceNetworkingCollect(this); collectPluginMap_[DEVICE_ONLINE] = networkingCollect; #endif + sptr eventStatuscollect = new ScreenEventStatusCollect(this, this); + collectPluginMap_[COMMON_EVENT] = eventStatuscollect; StartCollect(); HILOGI("DeviceStatusCollectManager Init end"); } -- Gitee