From 1b6b7b6980537a9975802f87f8a8658806d76825 Mon Sep 17 00:00:00 2001 From: njupthan Date: Wed, 19 Jan 2022 10:03:58 +0800 Subject: [PATCH 1/2] Dump distributed notifications Signed-off-by: njupthan --- .../include/advanced_notification_service.h | 3 ++ .../ans/src/advanced_notification_service.cpp | 35 +++++++++++++++++++ tools/dump/BUILD.gn | 5 +++ tools/dump/src/notification_shell_command.cpp | 20 +++++++++++ 4 files changed, 63 insertions(+) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index bab0e6840..a73798659 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -166,6 +166,9 @@ private: int64_t GetNowSysTime(); ErrCode ActiveNotificationDump(std::vector &dumpInfo); ErrCode RecentNotificationDump(std::vector &dumpInfo); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + ErrCode DistributedNotificationDump(std::vector &dumpInfo); +#endif ErrCode SetRecentNotificationCount(const std::string arg); void UpdateRecentNotification(sptr ¬ification, bool isDelete, int reason); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index f4bc69ca9..12271ec9f 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -45,6 +45,9 @@ namespace Notification { namespace { static const std::string ACTIVE_NOTIFICATION_OPTION = "active"; static const std::string RECENT_NOTIFICATION_OPTION = "recent"; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED +static const std::string DISTRIBUTED_NOTIFICATION_OPTION = "distributed"; +#endif static const std::string SET_RECENT_COUNT_OPTION = "setRecentCount"; static const int32_t NOTIFICATION_MIN_COUNT = 0; @@ -1448,6 +1451,10 @@ ErrCode AdvancedNotificationService::ShellDump(const std::string &dumpOption, st result = ActiveNotificationDump(dumpInfo); } else if (dumpOption == RECENT_NOTIFICATION_OPTION) { result = RecentNotificationDump(dumpInfo); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + } else if (dumpOption == DISTRIBUTED_NOTIFICATION_OPTION) { + result = DistributedNotificationDump(dumpInfo); +#endif } else if (dumpOption.substr(0, dumpOption.find_first_of(" ", 0)) == SET_RECENT_COUNT_OPTION) { result = SetRecentNotificationCount(dumpOption.substr(dumpOption.find_first_of(" ", 0) + 1)); } else { @@ -1644,6 +1651,34 @@ ErrCode AdvancedNotificationService::RecentNotificationDump(std::vector &dumpInfo) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::stringstream stream; + for (auto record : notificationList_) { + if (record->deviceId.empty()) { + continue; + } + stream.clear(); + stream << "\tDeviceId: " << record->deviceId << "\n"; + stream << "\tBundleName: " << record->notification->GetBundleName() << "\n"; + + stream << "\tCreateTime: " << TimeToString(record->notification->GetNotificationRequest().GetCreateTime()) + << "\n"; + + stream << "\tNotification:\n"; + stream << "\t\tId: " << record->notification->GetId() << "\n"; + stream << "\t\tLabel: " << record->notification->GetLabel() << "\n"; + stream << "\t\tClassification: " << record->notification->GetNotificationRequest().GetClassification() << "\n"; + + dumpInfo.push_back(stream.str()); + } + + return ERR_OK; +} +#endif + ErrCode AdvancedNotificationService::SetRecentNotificationCount(const std::string arg) { ANS_LOGD("%{public}s arg = %{public}s", __FUNCTION__, arg.c_str()); diff --git a/tools/dump/BUILD.gn b/tools/dump/BUILD.gn index a8c2312c0..f10cb9a18 100644 --- a/tools/dump/BUILD.gn +++ b/tools/dump/BUILD.gn @@ -44,6 +44,11 @@ ohos_executable("anm") { "${aafwk_path}/services/common:common_config", ] + defines = [] + if (distributed_notification_supported) { + defines += [ "DISTRIBUTED_NOTIFICATION_SUPPORTED" ] + } + external_deps = [] external_deps += ans_standard_external_deps diff --git a/tools/dump/src/notification_shell_command.cpp b/tools/dump/src/notification_shell_command.cpp index 7e534d838..e50c04ada 100644 --- a/tools/dump/src/notification_shell_command.cpp +++ b/tools/dump/src/notification_shell_command.cpp @@ -31,6 +31,9 @@ static const struct option OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"active", no_argument, nullptr, 'A'}, {"recent", no_argument, nullptr, 'R'}, +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + {"distributed", no_argument, nullptr, 'D'}, +#endif {"setRecentCount", required_argument, nullptr, 0}, {0, 0, 0, 0}, }; @@ -45,6 +48,9 @@ static const std::string DUMP_HELP_MSG = " --help, -h help menu\n" " --active, -A list all active notifications\n" " --recent, -R list recent notifications\n" +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + " --distributed, -D list all distributed notifications by remote device\n" +#endif " --setRecentCount set the max count of recent notification keeping in memory\n"; } // namespace @@ -92,7 +98,11 @@ ErrCode NotificationShellCommand::RunAsHelpCommand() ErrCode NotificationShellCommand::RunAsDumpCommand() { int ind = 0; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + int option = getopt_long(argc_, argv_, "hARD", OPTIONS, &ind); +#else int option = getopt_long(argc_, argv_, "hAR", OPTIONS, &ind); +#endif ErrCode ret = ERR_OK; std::vector infos; @@ -117,6 +127,16 @@ ErrCode NotificationShellCommand::RunAsDumpCommand() ret = ERR_ANS_SERVICE_NOT_CONNECTED; } break; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + case 'D': + if (ans_ != nullptr) { + ret = ans_->ShellDump("distributed", infos); + resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); + } else { + ret = ERR_ANS_SERVICE_NOT_CONNECTED; + } + break; +#endif case 0: if (ans_ != nullptr) { ret = ans_->ShellDump(std::string(OPTIONS[ind].name) + " " + std::string(optarg), infos); -- Gitee From f5e10aa4729467a57f261ff118905182b8698132 Mon Sep 17 00:00:00 2001 From: njupthan Date: Wed, 19 Jan 2022 11:05:51 +0800 Subject: [PATCH 2/2] Fix codeCheck Signed-off-by: njupthan --- .../dump/include/notification_shell_command.h | 8 ++ tools/dump/src/notification_shell_command.cpp | 73 +++++++++++++------ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/tools/dump/include/notification_shell_command.h b/tools/dump/include/notification_shell_command.h index ae1b2f8dc..4644b7926 100644 --- a/tools/dump/include/notification_shell_command.h +++ b/tools/dump/include/notification_shell_command.h @@ -35,6 +35,14 @@ private: ErrCode RunAsHelpCommand(); ErrCode RunAsDumpCommand(); + ErrCode RunHelp(); + ErrCode RunActive(); + ErrCode RunRecent(); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + ErrCode RunDistributed(); +#endif + ErrCode RunSetRecentCount(); + private: std::shared_ptr ans_; }; diff --git a/tools/dump/src/notification_shell_command.cpp b/tools/dump/src/notification_shell_command.cpp index e50c04ada..d796c3813 100644 --- a/tools/dump/src/notification_shell_command.cpp +++ b/tools/dump/src/notification_shell_command.cpp @@ -95,6 +95,53 @@ ErrCode NotificationShellCommand::RunAsHelpCommand() return ERR_OK; } +ErrCode NotificationShellCommand::RunHelp() +{ + resultReceiver_.append(DUMP_HELP_MSG); + return ERR_OK; +} + +ErrCode NotificationShellCommand::RunActive() +{ + ErrCode ret = ERR_OK; + std::vector infos; + if (ans_ != nullptr) { + ret = ans_->ShellDump("active", infos); + resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); + } else { + ret = ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ret; +} + +ErrCode NotificationShellCommand::RunRecent() +{ + ErrCode ret = ERR_OK; + std::vector infos; + if (ans_ != nullptr) { + ret = ans_->ShellDump("recent", infos); + resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); + } else { + ret = ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ret; +} + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED +ErrCode NotificationShellCommand::RunDistributed() +{ + ErrCode ret = ERR_OK; + std::vector infos; + if (ans_ != nullptr) { + ret = ans_->ShellDump("distributed", infos); + resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); + } else { + ret = ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ret; +} +#endif + ErrCode NotificationShellCommand::RunAsDumpCommand() { int ind = 0; @@ -109,32 +156,17 @@ ErrCode NotificationShellCommand::RunAsDumpCommand() switch (option) { case 'h': - resultReceiver_.append(DUMP_HELP_MSG); + ret = RunHelp(); break; case 'A': - if (ans_ != nullptr) { - ret = ans_->ShellDump("active", infos); - resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); - } else { - ret = ERR_ANS_SERVICE_NOT_CONNECTED; - } + ret = RunActive(); break; case 'R': - if (ans_ != nullptr) { - ret = ans_->ShellDump("recent", infos); - resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); - } else { - ret = ERR_ANS_SERVICE_NOT_CONNECTED; - } + ret = RunRecent(); break; #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED case 'D': - if (ans_ != nullptr) { - ret = ans_->ShellDump("distributed", infos); - resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n"); - } else { - ret = ERR_ANS_SERVICE_NOT_CONNECTED; - } + ret = RunDistributed(); break; #endif case 0: @@ -145,9 +177,6 @@ ErrCode NotificationShellCommand::RunAsDumpCommand() } break; default: - std::cout << __LINE__ << " " << option << " " << ind << " " << std::endl; - if (optarg) - std::cout << optarg << std::endl; resultReceiver_.append(DUMP_HELP_MSG); break; } -- Gitee