-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADB_Clicker.java
231 lines (196 loc) · 6.44 KB
/
ADB_Clicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.JadbDevice;
public class ADB_Clicker
{
private CommandQueue commandQueue;
private JadbConnection jadb;
private JadbDevice device;
public static void main (String[] args)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ADB_Clicker clicker = new ADB_Clicker();
System.out.println("** Enter input: **");
for (String input = readLine(reader); !"exit".equals(input); input = readLine(reader))
{
if ("".equals(input)) continue;
String[] iStrings = input.split(" ");
String param = "";
for (int i = 1; i < iStrings.length; i++) param += iStrings[i] + (i == iStrings.length - 1 ? "" : " ");
switch (iStrings[0])
{
case "insert":
try { clicker.insert(param); }
catch (InvalidFormatException e) { System.out.println(e.getMessage()); }
break;
case "wait":
try { clicker.wait(param); }
catch (InvalidFormatException e) { System.out.println(e.getMessage()); }
break;
case "remove":
try { clicker.remove(param); }
catch (InvalidFormatException e) { System.out.println(e.getMessage()); }
break;
case "run":
clicker.runQueue();
break;
case "list":
try { clicker.list(param); }
catch (InvalidFormatException e) { System.out.println(e.getMessage()); }
break;
case "select":
try { clicker.select(param); }
catch (InvalidFormatException e) { System.out.println(e.getMessage()); }
break;
}
System.out.println("** Enter input: **");
}
System.out.println("Exiting");
}
private static String readLine (BufferedReader reader)
{
String ret;
try
{
ret = reader.readLine().toLowerCase();
}
catch (IOException e)
{
ret = "exit";
}
return ret;
}
public ADB_Clicker ()
{
commandQueue = new CommandQueue();
jadb = new JadbConnection();
}
public void insert (String param) throws InvalidFormatException
{
String[] parse = param.split(" ");
int[] params = new int[5];
if (parse.length != 5) throw new InvalidFormatException("Only 5 integer parameters are allowed.");
for (int i = 0; i < 5; i++)
{
try
{
params[i] = Integer.valueOf(parse[i]);
}
catch (NumberFormatException e)
{
throw new InvalidFormatException(String.format("Parameter %d is not an integer value", i+1));
}
}
SwipeShellCommand ssc = new SwipeShellCommand(params[0], params[1], params[2], params[3], params[4]);
commandQueue.insert(ssc);
System.out.println("Inserted: " + ssc);
}
public void wait (String param) throws InvalidFormatException
{
int parse;
try
{
parse = Integer.valueOf(param);
commandQueue.insert(new WaitCommand(parse));
}
catch (NumberFormatException e)
{
throw new InvalidFormatException("Invalid paramter value. Expected a single decimal number.");
}
}
public void remove (String param) throws InvalidFormatException
{
int parse;
try
{
parse = Integer.valueOf(param);
SwipeShellCommand ssc = commandQueue.remove(parse);
System.out.println("Removed: " + ssc);
}
catch (IndexOutOfBoundsException e)
{
throw new InvalidFormatException("An invalid index was given");
}
catch (NumberFormatException e)
{
throw new InvalidFormatException("expected format: \"remove [integer]\"");
}
}
public void list (String param) throws InvalidFormatException
{
switch (param)
{
case "devices":
list_devices();
break;
case "queue":
list_queue();
break;
default:
throw new InvalidFormatException("A parameter was not given or did not match \"devices\" or \"queue\"");
}
}
private void list_queue ()
{
System.out.println("".equals(commandQueue.toString()) ? "Empty queue." : commandQueue.toString());
}
private void list_devices () throws InvalidFormatException
{
try
{
List<JadbDevice> devices = jadb.getDevices();
for (int i = 0; i < devices.size(); i++)
{
System.out.println(String.format("%d: %s", i, devices.get(i).toString()));
}
}
catch (Exception e)
{
throw new InvalidFormatException("An error occured: " + e.getMessage());
}
}
public void select (String param) throws InvalidFormatException
{
try
{
int index = Integer.parseInt(param);
List<JadbDevice> list = jadb.getDevices();
device = list.get(index);
System.out.println(String.format("Selected device: %s", device.toString()));
}
catch (NumberFormatException e)
{
throw new InvalidFormatException("expected format: \"select [integer]\"");
}
catch (Exception e)
{
throw new InvalidFormatException("An error occured: " + e.getMessage());
}
}
public void runQueue ()
{
if (commandQueue.getSize() <= 0)
{
System.out.println("Add Clicks to Queue to Run Queue!");
}
else if (device == null)
{
System.out.println("Select a device");
}
else
{
Thread thread = commandQueue.runQueueThread(device, "Command Thread");
thread.run();
}
}
private class InvalidFormatException extends Exception
{
public InvalidFormatException (String m)
{
super(m);
}
}
}