Svc.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2008 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.svc;
18
19import android.app.ActivityManagerNative;
20import android.app.IActivityManager;
21import android.app.IInstrumentationWatcher;
22import android.content.ComponentName;
23import android.content.Intent;
24import android.net.Uri;
25import android.os.RemoteException;
26import android.os.Bundle;
27import android.os.ServiceManager;
28import android.view.IWindowManager;
29
30import java.util.Iterator;
31import java.util.Set;
32
33public class Svc {
34
35    public static abstract class Command {
36        private String mName;
37
38        public Command(String name) {
39            mName = name;
40        }
41
42        public String name() {
43            return mName;
44        }
45
46        public abstract String shortHelp();         // should fit on one short line
47        public abstract String longHelp();          // take as much space as you need, 75 col max
48        public abstract void run(String[] args);    // run the command
49    }
50
51    public static void main(String[] args) {
52        if (true) {
53            for (String a: args) {
54                System.err.print(a + " ");
55            }
56            System.err.println();
57        }
58
59        if (args.length >= 1) {
60            Command c = lookupCommand(args[0]);
61            if (c != null) {
62                c.run(args);
63                return;
64            }
65        }
66        COMMAND_HELP.run(args);
67    }
68
69    private static final Command lookupCommand(String name) {
70        final int N = COMMANDS.length;
71        for (int i=0; i<N; i++) {
72            Command c = COMMANDS[i];
73            if (c.name().equals(name)) {
74                return c;
75            }
76        }
77        return null;
78    }
79
80    public static final Command COMMAND_HELP = new Command("help") {
81        public String shortHelp() {
82            return "Show information about the subcommands";
83        }
84        public String longHelp() {
85            return shortHelp();
86        }
87        public void run(String[] args) {
88            if (args.length == 2) {
89                Command c = lookupCommand(args[1]);
90                if (c != null) {
91                    System.err.println(c.longHelp());
92                    return;
93                }
94            }
95
96            System.err.println("Available commands:");
97            final int N = COMMANDS.length;
98            int maxlen = 0;
99            for (int i=0; i<N; i++) {
100                Command c = COMMANDS[i];
101                int len = c.name().length();
102                if (maxlen < len) {
103                    maxlen = len;
104                }
105            }
106            String format = "    %-" + maxlen + "s    %s";
107            for (int i=0; i<N; i++) {
108                Command c = COMMANDS[i];
109                System.err.println(String.format(format, c.name(), c.shortHelp()));
110            }
111        }
112    };
113
114    public static final Command[] COMMANDS = new Command[] {
115        COMMAND_HELP,
116        new PowerCommand(),
117    };
118}
119