• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "bundle_active_shell_command.h"
17 
18 #include <iostream>
19 #include <getopt.h>
20 
21 #include "iservice_registry.h"
22 #include "singleton.h"
23 
24 #include "bundle_active_client.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 namespace {
29     static constexpr int32_t MIN_BUNDLE_ACTIVE_DUMP_PARAMS_NUM = 4;
30 
31     static const struct option OPTIONS[] = {
32         {"help", no_argument, nullptr, 'h'},
33         {"all", no_argument, nullptr, 'A'},
34     };
35 
36     static const std::string HELP_MSG = "usage: deviceusagestats <command> [<options>]\n"
37                                     "These are common commands list:\n"
38                                     "  help                         list available commands\n"
39                                     "  dump                         dump the info of bundleactive\n";
40 
41     static const std::string DUMP_HELP_MSG =
42         "usage: bundleactive dump [<options>]\n"
43         "options list:\n"
44         "  -h                                                             help menu\n"
45         "  -A                                                                                    \n"
46         "      Events [beginTime] [endTime] [userId]                      get events for one user\n"
47         "      PackageUsage [intervalType] [beginTime] [endTime] [userId] get package usage for one user\n"
48         "      ModuleUsage [maxNum] [userId]                              get module usage for one user\n";
49 } // namespace
50 
BundleActiveShellCommand(int32_t argc,char * argv[])51 BundleActiveShellCommand::BundleActiveShellCommand(int32_t argc, char *argv[]) : ShellCommand(argc,
52     argv, "deviceusagestats")
53 {}
54 
CreateCommandMap()55 int32_t BundleActiveShellCommand::CreateCommandMap()
56 {
57     commandMap_ = {
58         {"help", std::bind(&BundleActiveShellCommand::RunAsHelpCommand, this)},
59         {"dump", std::bind(&BundleActiveShellCommand::RunAsDumpCommand, this)},
60     };
61     return 0;
62 }
63 
CreateMessageMap()64 int32_t BundleActiveShellCommand::CreateMessageMap()
65 {
66     messageMap_ = {};
67     return 0;
68 }
69 
init()70 int32_t BundleActiveShellCommand::init()
71 {
72     return 0;
73 }
74 
RunAsHelpCommand()75 int32_t BundleActiveShellCommand::RunAsHelpCommand()
76 {
77     resultReceiver_.append(HELP_MSG);
78     return 0;
79 }
80 
RunAsDumpCommand()81 int32_t BundleActiveShellCommand::RunAsDumpCommand()
82 {
83     int32_t ind = 0;
84     int32_t option = getopt_long(argc_, argv_, "hA", OPTIONS, &ind);
85     int32_t ret = 0;
86     std::vector<std::string> infos;
87     switch (option) {
88         case 'h':
89             resultReceiver_.append(DUMP_HELP_MSG);
90             break;
91         case 'A':
92             if (argc_ < MIN_BUNDLE_ACTIVE_DUMP_PARAMS_NUM) {
93                 resultReceiver_.append("Please input correct params.\n");
94                 resultReceiver_.append(DUMP_HELP_MSG);
95                 return -1;
96             }
97             ret = BundleActiveClient::GetInstance().ShellDump(argList_, infos);
98             break;
99         default:
100             resultReceiver_.append("Please input correct params.\n");
101             resultReceiver_.append(DUMP_HELP_MSG);
102             ret = -1;
103             break;
104     }
105     for (auto info : infos) {
106         resultReceiver_.append(info);
107     }
108     return ret;
109 }
110 }  // namespace DeviceUsageStats
111 }  // namespace OHOS
112 
113