| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.sysui; |
| 18 |
|
| 19 |
import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_INIT; |
| 20 |
|
| 21 |
import com.android.internal.protolog.common.ProtoLog; |
| 22 |
|
| 23 |
import java.io.PrintWriter; |
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.TreeMap; |
| 26 |
import java.util.function.BiConsumer; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public final class ShellCommandHandler { |
| 34 |
private static final String TAG = ShellCommandHandler.class.getSimpleName(); |
| 35 |
|
| 36 |
|
| 37 |
private final TreeMap<String, BiConsumer<PrintWriter, String>> mDumpables = new TreeMap<>(); |
| 38 |
private final TreeMap<String, ShellCommandActionHandler> mCommands = new TreeMap<>(); |
| 39 |
|
| 40 |
public interface ShellCommandActionHandler { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
boolean onShellCommand(String[] args, PrintWriter pw); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void printShellCommandHelp(PrintWriter pw, String prefix); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public <T> void addDumpCallback(BiConsumer<PrintWriter, String> callback, T instance) { |
| 65 |
mDumpables.put(instance.getClass().getSimpleName(), callback); |
| 66 |
ProtoLog.v(WM_SHELL_INIT, "Adding dump callback for %s", |
| 67 |
instance.getClass().getSimpleName()); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public <T> void addCommandCallback(String commandClass, ShellCommandActionHandler actions, |
| 78 |
T instance) { |
| 79 |
mCommands.put(commandClass, actions); |
| 80 |
ProtoLog.v(WM_SHELL_INIT, "Adding command class %s for %s", commandClass, |
| 81 |
instance.getClass().getSimpleName()); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public void dump(PrintWriter pw) { |
| 86 |
for (String key : mDumpables.keySet()) { |
| 87 |
final BiConsumer<PrintWriter, String> r = mDumpables.get(key); |
| 88 |
r.accept(pw, ""); |
| 89 |
pw.println(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean handleCommand(final String[] args, PrintWriter pw) { |
| 96 |
if (args.length < 2) { |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
final String cmdClass = args[1]; |
| 102 |
if (cmdClass.toLowerCase().equals("help")) { |
| 103 |
return runHelp(pw); |
| 104 |
} |
| 105 |
if (!mCommands.containsKey(cmdClass)) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
final ShellCommandActionHandler actions = mCommands.get(args[1]); |
| 111 |
final String[] cmdClassArgs = Arrays.copyOfRange(args, 2, args.length); |
| 112 |
actions.onShellCommand(cmdClassArgs, pw); |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
private boolean runHelp(PrintWriter pw) { |
| 117 |
pw.println("Window Manager Shell commands:"); |
| 118 |
for (String commandClass : mCommands.keySet()) { |
| 119 |
pw.println(" " + commandClass); |
| 120 |
mCommands.get(commandClass).printShellCommandHelp(pw, " "); |
| 121 |
} |
| 122 |
pw.println(" help"); |
| 123 |
pw.println(" Print this help text."); |
| 124 |
pw.println(" <no arguments provided>"); |
| 125 |
pw.println(" Dump all Window Manager Shell internal state"); |
| 126 |
return true; |
| 127 |
} |
| 128 |
} |