1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.commands.uiautomator;
18
19import android.os.Process;
20
21import java.util.Arrays;
22
23/**
24 * Entry point into the uiautomator command line
25 *
26 * This class maintains the list of sub commands, and redirect the control into it based on the
27 * command line arguments. It also prints out help arguments for each sub commands.
28 *
29 * To add a new sub command, implement {@link Command} and add an instance into COMMANDS array
30 */
31public class Launcher {
32
33    /**
34     * A simple abstraction class for supporting generic sub commands
35     */
36    public static abstract class Command {
37        private String mName;
38
39        public Command(String name) {
40            mName = name;
41        }
42
43        /**
44         * Returns the name of the sub command
45         * @return
46         */
47        public String name() {
48            return mName;
49        }
50
51        /**
52         * Returns a one-liner of the function of this command
53         * @return
54         */
55        public abstract String shortHelp();
56
57        /**
58         * Returns a detailed explanation of the command usage
59         *
60         * Usage may have multiple lines, indentation of 4 spaces recommended.
61         * @return
62         */
63        public abstract String detailedOptions();
64
65        /**
66         * Starts the command with the provided arguments
67         * @param args
68         */
69        public abstract void run(String args[]);
70    }
71
72    public static void main(String[] args) {
73        // show a meaningful process name in `ps`
74        Process.setArgV0("uiautomator");
75        if (args.length >= 1) {
76            Command command = findCommand(args[0]);
77            if (command != null) {
78                String[] args2 = {};
79                if (args.length > 1) {
80                    // consume the first arg
81                    args2 = Arrays.copyOfRange(args, 1, args.length);
82                }
83                command.run(args2);
84                return;
85            }
86        }
87        HELP_COMMAND.run(args);
88    }
89
90    private static Command findCommand(String name) {
91        for (Command command : COMMANDS) {
92            if (command.name().equals(name)) {
93                return command;
94            }
95        }
96        return null;
97    }
98
99    private static Command HELP_COMMAND = new Command("help") {
100        @Override
101        public void run(String[] args) {
102            System.err.println("Usage: uiautomator <subcommand> [options]\n");
103            System.err.println("Available subcommands:\n");
104            for (Command command : COMMANDS) {
105                String shortHelp = command.shortHelp();
106                String detailedOptions = command.detailedOptions();
107                if (shortHelp == null) {
108                    shortHelp = "";
109                }
110                if (detailedOptions == null) {
111                    detailedOptions = "";
112                }
113                System.err.println(String.format("%s: %s", command.name(), shortHelp));
114                System.err.println(detailedOptions);
115            }
116        }
117
118        @Override
119        public String detailedOptions() {
120            return null;
121        }
122
123        @Override
124        public String shortHelp() {
125            return "displays help message";
126        }
127    };
128
129    private static Command[] COMMANDS = new Command[] {
130        HELP_COMMAND,
131        new RunTestCommand(),
132        new DumpCommand(),
133        new EventsCommand(),
134    };
135}