From 998d96400307e8b8f5dba48e739d9b8a260cb1b0 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Tue, 6 Sep 2022 16:58:22 +0800 Subject: [PATCH] add anm set notification enabled Signed-off-by: fangJinliang1 Change-Id: Idce941b2417eea033406ede2433d38b44acd2ae7 Signed-off-by: fangJinliang1 --- tools/dump/BUILD.gn | 2 + .../dump/include/notification_shell_command.h | 2 + tools/dump/src/notification_shell_command.cpp | 71 ++++++++++++++++++- tools/test/unittest/dump/BUILD.gn | 2 + 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/tools/dump/BUILD.gn b/tools/dump/BUILD.gn index c13860b62..99d3ba405 100644 --- a/tools/dump/BUILD.gn +++ b/tools/dump/BUILD.gn @@ -49,6 +49,8 @@ ohos_executable("anm") { } external_deps = [ + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "multimedia_image_standard:image_native", diff --git a/tools/dump/include/notification_shell_command.h b/tools/dump/include/notification_shell_command.h index 4da8e2f5b..a295ce410 100644 --- a/tools/dump/include/notification_shell_command.h +++ b/tools/dump/include/notification_shell_command.h @@ -48,6 +48,8 @@ private: void SetDumpCmdInfo(std::string &cmd, std::string &bundle, int32_t &userId, ErrCode &ret); ErrCode RunDumpCmd(const std::string& cmd, const std::string& bundle, int32_t userId, std::vector &infos); + ErrCode RunSetEnableCmd(); + void SetNativeToken(); private: std::shared_ptr ans_; diff --git a/tools/dump/src/notification_shell_command.cpp b/tools/dump/src/notification_shell_command.cpp index 44dcb37f1..b135c15c5 100644 --- a/tools/dump/src/notification_shell_command.cpp +++ b/tools/dump/src/notification_shell_command.cpp @@ -19,6 +19,9 @@ #include #include "ans_inner_errors.h" +#include "nativetoken_kit.h" +#include "notification_bundle_option.h" +#include "token_setproc.h" #include "singleton.h" namespace OHOS { @@ -61,16 +64,18 @@ constexpr char DUMP_HELP_MSG[] = " --bundle, -b dump the info filter by the specified bundle name\n" " --user-id, -u dump the info filter by the specified userId\n"; -constexpr char SETTING_SHORT_OPTIONS[] = "c:"; +constexpr char SETTING_SHORT_OPTIONS[] = "c:e:"; const struct option SETTING_LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"recent-count", required_argument, nullptr, 'c'}, + {"enable-notification", required_argument, nullptr, 'e'}, }; constexpr char SETTING_HELP_MSG[] = "usage: anm setting []\n" "options list:\n" " --help, -h help menu\n" - " --recent-count -c set the max count of recent notifications keeping in memory\n"; + " --recent-count -c set the max count of recent notifications keeping in memory\n" + " --enable-notification -e set notification enabled for the bundle, eg: -e com.example:10100:1\n"; } // namespace NotificationShellCommand::NotificationShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "anm_dump") @@ -88,6 +93,7 @@ ErrCode NotificationShellCommand::CreateCommandMap() ErrCode NotificationShellCommand::Init() { + SetNativeToken(); ErrCode result = OHOS::ERR_OK; if (!ans_) { ans_ = DelayedSingleton::GetInstance(); @@ -98,6 +104,27 @@ ErrCode NotificationShellCommand::Init() return result; } +void NotificationShellCommand::SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.NOTIFICATION_CONTROLLER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_basic", + }; + + infoInstance.processName = "anm"; + tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + delete[] perms; +} + ErrCode NotificationShellCommand::RunAsHelpCommand() { resultReceiver_.append(HELP_MSG); @@ -222,10 +249,12 @@ ErrCode NotificationShellCommand::RunAsSettingCommand() if (option == '?') { if (optopt == 'c') { resultReceiver_.append("error: option 'c' requires a value.\n"); + } else if (optopt == 'e') { + resultReceiver_.append("error: option 'e' requires a value.\n"); } else { resultReceiver_.append("error: unknown option.\n"); } - resultReceiver_.append(DUMP_HELP_MSG); + resultReceiver_.append(SETTING_HELP_MSG); return ERR_INVALID_VALUE; } if (option == 'c') { @@ -239,8 +268,44 @@ ErrCode NotificationShellCommand::RunAsSettingCommand() cmd.append(" ").append(std::string(optarg)); return RunDumpCmd(cmd, "", SUBSCRIBE_USER_INIT, infos); } + if (option == 'e') { + return RunSetEnableCmd(); + } + resultReceiver_.append(SETTING_HELP_MSG); return ERR_INVALID_VALUE; } + +ErrCode NotificationShellCommand::RunSetEnableCmd() +{ + if (ans_ == nullptr) { + resultReceiver_.append("error: object is null\n"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + NotificationBundleOption bundleOption; + std::string info = std::string(optarg); + if (std::count(info.begin(), info.end(), ':') != 2) { // 2 (bundleName:uid:enable) + resultReceiver_.append("error: setting information error\n"); + resultReceiver_.append(SETTING_HELP_MSG); + return ERR_INVALID_VALUE; + } + + size_t pos = info.find(':'); + bundleOption.SetBundleName(info.substr(0, pos)); + info = info.substr(pos + 1); + pos = info.find(':'); + bundleOption.SetUid(atoi(info.substr(0, pos).c_str())); + bool enable = atoi(info.substr(pos + 1).c_str()); + + ErrCode ret = ans_->SetNotificationsEnabledForSpecifiedBundle(bundleOption, "", enable); + if (ret == ERR_OK) { + resultReceiver_.append("set notification enabled success\n"); + } else { + resultReceiver_.append("set notification enabled failed\n"); + } + return ret; + +} } // namespace Notification } // namespace OHOS diff --git a/tools/test/unittest/dump/BUILD.gn b/tools/test/unittest/dump/BUILD.gn index 314e2d4cd..0891923de 100644 --- a/tools/test/unittest/dump/BUILD.gn +++ b/tools/test/unittest/dump/BUILD.gn @@ -60,6 +60,8 @@ ohos_unittest("notification_shell_command_dump_test") { "ability_base:base", "ability_base:want", "ability_base:zuri", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", -- Gitee