1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "notification_shell_command.h"
17
18 #include <getopt.h>
19 #include <iostream>
20
21 #include "ans_inner_errors.h"
22 #include "singleton.h"
23
24 namespace OHOS {
25 namespace Notification {
26 namespace {
27
28 static const struct option OPTIONS[] = {
29 {"help", no_argument, nullptr, 'h'},
30 {"active", no_argument, nullptr, 'A'},
31 {"recent", no_argument, nullptr, 'R'},
32 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
33 {"distributed", no_argument, nullptr, 'D'},
34 #endif
35 {"setRecentCount", required_argument, nullptr, 0},
36 {0, 0, 0, 0},
37 };
38
39 static const std::string HELP_MSG = "usage: anm <command> [<options>]\n"
40 "These are common commands list:\n"
41 " help list available commands\n"
42 " dump dump the info of notification\n";
43 static const std::string DUMP_HELP_MSG =
44 "usage: anm dump [<options>]\n"
45 "options list:\n"
46 " --help, -h help menu\n"
47 " --active, -A list all active notifications\n"
48 " --recent, -R list recent notifications\n"
49 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
50 " --distributed, -D list all distributed notifications by remote device\n"
51 #endif
52 " --setRecentCount <N> set the max count of recent notification keeping in memory\n";
53 } // namespace
54
NotificationShellCommand(int argc,char * argv[])55 NotificationShellCommand::NotificationShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "anm_dump")
56 {}
57
CreateCommandMap()58 ErrCode NotificationShellCommand::CreateCommandMap()
59 {
60 commandMap_ = {
61 {"help", std::bind(&NotificationShellCommand::RunAsHelpCommand, this)},
62 {"dump", std::bind(&NotificationShellCommand::RunAsDumpCommand, this)},
63 };
64
65 return ERR_OK;
66 }
67
CreateMessageMap()68 ErrCode NotificationShellCommand::CreateMessageMap()
69 {
70 messageMap_ = {};
71
72 return ERR_OK;
73 }
74
init()75 ErrCode NotificationShellCommand::init()
76 {
77 ErrCode result = OHOS::ERR_OK;
78
79 if (!ans_) {
80 ans_ = DelayedSingleton<AnsNotification>::GetInstance();
81 }
82
83 if (!ans_) {
84 result = OHOS::ERR_INVALID_VALUE;
85 }
86
87 return result;
88 }
89
RunAsHelpCommand()90 ErrCode NotificationShellCommand::RunAsHelpCommand()
91 {
92 resultReceiver_.append(HELP_MSG);
93 return ERR_OK;
94 }
95
RunHelp()96 ErrCode NotificationShellCommand::RunHelp()
97 {
98 resultReceiver_.append(DUMP_HELP_MSG);
99 return ERR_OK;
100 }
101
RunActive(std::vector<std::string> & infos)102 ErrCode NotificationShellCommand::RunActive(std::vector<std::string> &infos)
103 {
104 ErrCode ret = ERR_OK;
105 if (ans_ != nullptr) {
106 ret = ans_->ShellDump("active", infos);
107 resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n");
108 } else {
109 ret = ERR_ANS_SERVICE_NOT_CONNECTED;
110 }
111 return ret;
112 }
113
RunRecent(std::vector<std::string> & infos)114 ErrCode NotificationShellCommand::RunRecent(std::vector<std::string> &infos)
115 {
116 ErrCode ret = ERR_OK;
117 if (ans_ != nullptr) {
118 ret = ans_->ShellDump("recent", infos);
119 resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n");
120 } else {
121 ret = ERR_ANS_SERVICE_NOT_CONNECTED;
122 }
123 return ret;
124 }
125
126 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
RunDistributed(std::vector<std::string> & infos)127 ErrCode NotificationShellCommand::RunDistributed(std::vector<std::string> &infos)
128 {
129 ErrCode ret = ERR_OK;
130 if (ans_ != nullptr) {
131 ret = ans_->ShellDump("distributed", infos);
132 resultReceiver_.append("Total:" + std::to_string(infos.size()) + "\n");
133 } else {
134 ret = ERR_ANS_SERVICE_NOT_CONNECTED;
135 }
136 return ret;
137 }
138 #endif
139
RunAsDumpCommand()140 ErrCode NotificationShellCommand::RunAsDumpCommand()
141 {
142 int ind = 0;
143 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
144 int option = getopt_long(argc_, argv_, "hARD", OPTIONS, &ind);
145 #else
146 int option = getopt_long(argc_, argv_, "hAR", OPTIONS, &ind);
147 #endif
148
149 ErrCode ret = ERR_OK;
150 std::vector<std::string> infos;
151
152 switch (option) {
153 case 'h':
154 ret = RunHelp();
155 break;
156 case 'A':
157 ret = RunActive(infos);
158 break;
159 case 'R':
160 ret = RunRecent(infos);
161 break;
162 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
163 case 'D':
164 ret = RunDistributed(infos);
165 break;
166 #endif
167 case 0:
168 if (ans_ != nullptr) {
169 ret = ans_->ShellDump(std::string(OPTIONS[ind].name) + " " + std::string(optarg), infos);
170 } else {
171 ret = ERR_ANS_SERVICE_NOT_CONNECTED;
172 }
173 break;
174 default:
175 resultReceiver_.append(DUMP_HELP_MSG);
176 break;
177 }
178
179 int index = 0;
180 for (auto info : infos) {
181 resultReceiver_.append("No." + std::to_string(index) + "\n");
182 resultReceiver_.append(info);
183 }
184
185 return ret;
186 }
187 } // namespace Notification
188 } // namespace OHOS
189