From ccaeebcd77fd493bd44ca3cc13e506cfeb5d07ff Mon Sep 17 00:00:00 2001 From: zero-cyc Date: Sat, 25 Jun 2022 18:49:56 +0800 Subject: [PATCH] add hidumper Signed-off-by: zero-cyc Change-Id: I84f8002cbacee61574ec17f1c52e2e1cf9069be9 --- .../include/advanced_notification_service.h | 2 + .../ans/src/advanced_notification_service.cpp | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 7c8360f1c..c15a242cb 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -814,6 +814,8 @@ private: sptr &targetBundle); bool PublishSlotChangeCommonEvent(const sptr &bundleOption); void ReportInfoToResourceSchedule(const int32_t userId, const std::string &bundleName); + int Dump(int fd, const std::vector &args) override; + void GetDumpInfo(const std::vector &args, std::string &result); private: static sptr instance_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 954bb6af6..537f95d30 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -61,6 +61,7 @@ namespace Notification { namespace { constexpr char ACTIVE_NOTIFICATION_OPTION[] = "active"; constexpr char RECENT_NOTIFICATION_OPTION[] = "recent"; +constexpr char HELP_NOTIFICATION_OPTION[] = "help"; #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED constexpr char DISTRIBUTED_NOTIFICATION_OPTION[] = "distributed"; #endif @@ -77,6 +78,24 @@ constexpr int32_t WINDOW_DEFAULT_WIDTH = 720; constexpr int32_t WINDOW_DEFAULT_HEIGHT = 1280; constexpr int32_t UI_HALF = 2; +constexpr char HIDUMPER_HELP_MSG[] = + "Usage:dump [options]\n" + "Description::\n" + " --active, -a list all active notifications\n" + " --recent, -r list recent notifications\n"; + +constexpr char HIDUMPER_ERR_MSG[] = + "error: unknown option.\nThe arguments are illegal and you can enter '-h' for help."; + +const std::unordered_map HIDUMPER_CMD_MAP = { + { "--help", HELP_NOTIFICATION_OPTION }, + { "--active", ACTIVE_NOTIFICATION_OPTION }, + { "--recent", RECENT_NOTIFICATION_OPTION }, + { "-h", HELP_NOTIFICATION_OPTION }, + { "-a", ACTIVE_NOTIFICATION_OPTION }, + { "-r", RECENT_NOTIFICATION_OPTION }, +}; + struct RecentNotification { sptr notification = nullptr; bool isActive = false; @@ -3594,5 +3613,47 @@ ErrCode AdvancedNotificationService::ShellDump(const std::string &cmd, const std })); return result; } + +int AdvancedNotificationService::Dump(int fd, const std::vector &args) +{ + ANS_LOGD("enter"); + std::string result; + GetDumpInfo(args, result); + int ret = dprintf(fd, "%s\n", result.c_str()); + if (ret < 0) { + ANS_LOGE("dprintf error"); + return ERR_ANS_INVALID_PARAM; + } + return ERR_OK; +} + +void AdvancedNotificationService::GetDumpInfo(const std::vector &args, std::string &result) +{ + if (args.size() != 1) { + result = HIDUMPER_ERR_MSG; + return; + } + std::vector dumpInfo; + std::string cmd = Str16ToStr8(args.front()); + if (HIDUMPER_CMD_MAP.find(cmd) == HIDUMPER_CMD_MAP.end()) { + result = HIDUMPER_ERR_MSG; + return; + } + std::string cmdValue = HIDUMPER_CMD_MAP.find(cmd)->second; + if (cmdValue == HELP_NOTIFICATION_OPTION) { + result = HIDUMPER_HELP_MSG; + } + ShellDump(cmdValue, "", SUBSCRIBE_USER_INIT, dumpInfo); + if (dumpInfo.empty()) { + result.append("no notification\n"); + return; + } + int32_t index = 0; + result.append("notification list:\n"); + for (const auto &info: dumpInfo) { + result.append("No." + std::to_string(++index) + "\n"); + result.append(info); + } +} } // namespace Notification } // namespace OHOS -- Gitee