diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 7c8360f1ca58e2973f7d0ac0859873e1b9990e81..c15a242cbab182fe24234e9ae69812350fb1c51f 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 954bb6af62fdcdd374e5ef33b249cd5f39dfa929..537f95d3084350d02e3fae2573fce576a1ae6b2c 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