Merge "Preload2: Add sequence UI"

am: 56ddac7fd7

Change-Id: I6ca6e1ea6c6721a24f7b1d3d7fb4715bd51aa0d9
This commit is contained in:
Andreas Gampe
2016-11-30 00:49:44 +00:00
committed by android-build-merger
3 changed files with 255 additions and 4 deletions

View File

@@ -34,4 +34,4 @@ PROG_NAME="$(follow_links)"
PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
ANDROID_ROOT=$PROG_DIR/..
java -cp $ANDROID_ROOT/framework/preload2.jar com.android.preload.Main
java -cp $ANDROID_ROOT/framework/preload2.jar com.android.preload.Main $@

View File

@@ -33,10 +33,13 @@ import com.android.preload.classdataretrieval.ClassDataRetriever;
import com.android.preload.classdataretrieval.hprof.Hprof;
import com.android.preload.classdataretrieval.jdwp.JDWPClassDataRetriever;
import com.android.preload.ui.IUI;
import com.android.preload.ui.SequenceUI;
import com.android.preload.ui.SwingUI;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -90,9 +93,14 @@ public class Main {
* @param args
*/
public static void main(String[] args) {
Main m = new Main(new SwingUI());
top = m;
Main m;
if (args.length > 0 && args[0].equals("--seq")) {
m = createSequencedMain(args);
} else {
m = new Main(new SwingUI());
}
top = m;
m.startUp();
}
@@ -130,6 +138,27 @@ public class Main {
ui.prepare(clientListModel, dataTableModel, actions);
}
/**
* @param args
* @return
*/
private static Main createSequencedMain(String[] args) {
SequenceUI ui = new SequenceUI();
Main main = new Main(ui);
Iterator<String> it = Arrays.asList(args).iterator();
it.next(); // --seq
ui.choice("#" + it.next()); // Device.
ui.confirmNo(); // Prepare: no.
ui.action(ScanPackageAction.class); // Take hprof dump.
ui.client("system_process"); // Select system server.
ui.action(ExportAction.class); // Export data.
ui.output(new File("/tmp/system_server.data")); // Write to file.
return main;
}
public static IUI getUI() {
return top.ui;
}

View File

@@ -0,0 +1,222 @@
package com.android.preload.ui;
import com.android.ddmlib.Client;
import com.android.ddmlib.ClientData;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Action;
import javax.swing.ListModel;
import javax.swing.table.TableModel;
public class SequenceUI implements IUI {
private ListModel<Client> clientListModel;
@SuppressWarnings("unused")
private TableModel dataTableModel;
private List<Action> actions;
private List<Object> sequence = new LinkedList<>();
public SequenceUI() {
}
@Override
public boolean isSingleThreaded() {
return true;
}
@Override
public void prepare(ListModel<Client> clientListModel, TableModel dataTableModel,
List<Action> actions) {
this.clientListModel = clientListModel;
this.dataTableModel = dataTableModel;
this.actions = actions;
}
public SequenceUI action(Action a) {
sequence.add(a);
return this;
}
public SequenceUI action(Class<? extends Action> actionClass) {
for (Action a : actions) {
if (actionClass.equals(a.getClass())) {
sequence.add(a);
return this;
}
}
throw new IllegalArgumentException("No action of class " + actionClass + " found.");
}
public SequenceUI confirmYes() {
sequence.add(Boolean.TRUE);
return this;
}
public SequenceUI confirmNo() {
sequence.add(Boolean.FALSE);
return this;
}
public SequenceUI input(String input) {
sequence.add(input);
return this;
}
public SequenceUI input(File... f) {
sequence.add(f);
return this;
}
public SequenceUI output(File f) {
sequence.add(f);
return this;
}
public SequenceUI tableRow(int i) {
sequence.add(i);
return this;
}
private class ClientSelector {
private String pkg;
public ClientSelector(String pkg) {
this.pkg = pkg;
}
public Client getClient() {
for (int i = 0; i < clientListModel.getSize(); i++) {
ClientData cd = clientListModel.getElementAt(i).getClientData();
if (cd != null) {
String s = cd.getClientDescription();
if (pkg.equals(s)) {
return clientListModel.getElementAt(i);
}
}
}
throw new RuntimeException("Didn't find client " + pkg);
}
}
public SequenceUI client(String pkg) {
sequence.add(new ClientSelector(pkg));
return this;
}
public SequenceUI choice(String pattern) {
sequence.add(pattern);
return this;
}
@Override
public void ready() {
// Run the actions.
// No iterator or foreach loop as the sequence will be emptied while running.
try {
while (!sequence.isEmpty()) {
Object next = sequence.remove(0);
if (next instanceof Action) {
((Action)next).actionPerformed(null);
} else {
throw new IllegalStateException("Didn't expect a non-action: " + next);
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
// Now shut down.
System.exit(0);
}
@Override
public Client getSelectedClient() {
Object next = sequence.remove(0);
if (next instanceof ClientSelector) {
return ((ClientSelector)next).getClient();
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public int getSelectedDataTableRow() {
Object next = sequence.remove(0);
if (next instanceof Integer) {
return ((Integer)next).intValue();
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public void showWaitDialog() {
}
@Override
public void updateWaitDialog(String s) {
System.out.println(s);
}
@Override
public void hideWaitDialog() {
}
@Override
public void showMessageDialog(String s) {
System.out.println(s);
}
@Override
public boolean showConfirmDialog(String title, String message) {
Object next = sequence.remove(0);
if (next instanceof Boolean) {
return ((Boolean)next).booleanValue();
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public String showInputDialog(String message) {
Object next = sequence.remove(0);
if (next instanceof String) {
return (String)next;
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public <T> T showChoiceDialog(String title, String message, T[] choices) {
Object next = sequence.remove(0);
if (next instanceof String) {
String s = (String)next;
for (T t : choices) {
if (t.toString().contains(s)) {
return t;
}
}
return null;
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public File showSaveDialog() {
Object next = sequence.remove(0);
if (next instanceof File) {
System.out.println(next);
return (File)next;
}
throw new IllegalStateException("Unexpected: " + next);
}
@Override
public File[] showOpenDialog(boolean multi) {
Object next = sequence.remove(0);
if (next instanceof File[]) {
return (File[])next;
}
throw new IllegalStateException("Unexpected: " + next);
}
}