1
+ /*
2
+ * Adb.cs - Developed by Dan Wager for AndroidLib.dll
3
+ */
4
+
5
+ using System . IO ;
6
+
7
+ namespace RegawMOD . Android
8
+ {
9
+ /// <summary>
10
+ /// Holds formatted commands to execute through <see cref="Adb"/>
11
+ /// </summary>
12
+ /// <remarks><para>Can only be created with <c>Adb.FormAdbCommand()</c> or <c>Adb.FormAdbShellCommand()</c></para>
13
+ /// <para>Can only be executed with <c>Adb.ExecuteAdbCommand()</c> or <c>Adb.ExecuteAdbCommandNoReturn()</c></para></remarks>
14
+ public class AdbCommand
15
+ {
16
+ private string command ;
17
+ internal string Command { get { return this . command ; } }
18
+ internal AdbCommand ( string command ) { this . command = command ; }
19
+ }
20
+
21
+ /// <summary>
22
+ /// Controls all commands sent to the currently running Android Debug Bridge Server
23
+ /// </summary>
24
+ public static class Adb
25
+ {
26
+ internal const string ADB = "adb" ;
27
+ internal const string ADB_EXE = "adb.exe" ;
28
+
29
+ /// <summary>
30
+ /// Forms an <see cref="AdbCommand"/> that is passed to <c>Adb.ExecuteAdbCommand()</c>
31
+ /// </summary>
32
+ /// <remarks><para>This should only be used for non device-specific Adb commands, such as <c>adb devices</c> or <c>adb version</c>.</para>
33
+ /// <para>Never try to start/kill the running Adb Server, as the <see cref="AndroidController"/> type handles it internally.</para></remarks>
34
+ /// <param name="command">The command to run on the Adb Server</param>
35
+ /// <param name="args">Any arguments that need to be sent to <paramref name="command"/></param>
36
+ /// <returns><see cref="AdbCommand"/> that contains formatted command information</returns>
37
+ /// <example>This example demonstrates how to create an <see cref="AdbCommand"/>
38
+ /// <code>
39
+ /// //This example shows how to create an AdbCommand object to execute on the running server.
40
+ /// //The command we will create is "adb devices".
41
+ /// //Notice how in the formation, you don't supply the prefix "adb", because the method takes care of it for you.
42
+ ///
43
+ /// AdbCommand adbCmd = Adb.FormAdbCommand("devices");
44
+ ///
45
+ /// </code>
46
+ /// </example>
47
+ public static AdbCommand FormAdbCommand ( string command , params object [ ] args )
48
+ {
49
+ string adbCommand = ( args . Length > 0 ) ? command + " " : command ;
50
+
51
+ for ( int i = 0 ; i < args . Length ; i ++ )
52
+ adbCommand += args [ i ] + " " ;
53
+
54
+ return new AdbCommand ( adbCommand ) ;
55
+ }
56
+
57
+ /// <summary>
58
+ /// Forms an <see cref="AdbCommand"/> that is passed to <c>Adb.ExecuteAdbCommand()</c>
59
+ /// </summary>
60
+ /// <remarks>This should only be used for device-specific Adb commands, such as <c>adb push</c> or <c>adb pull</c>.</remarks>
61
+ /// <param name="device">Specific <see cref="Device"/> to run the command on</param>
62
+ /// <param name="command">The command to run on the Adb Server</param>
63
+ /// <param name="args">Any arguments that need to be sent to <paramref name="command"/></param>
64
+ /// <returns><see cref="AdbCommand"/> that contains formatted command information</returns>
65
+ /// <example>This example demonstrates how to create an <see cref="AdbCommand"/>
66
+ /// <code>//This example shows how to create an AdbCommand object to execute on the running server.
67
+ /// //The command we will create is "adb pull /system/app C:\".
68
+ /// //Notice how in the formation, you don't supply the prefix "adb", because the method takes care of it for you.
69
+ /// //This example also assumes you have a Device instance named device.
70
+ ///
71
+ /// AdbCommand adbCmd = Adb.FormAdbCommand(device, "pull", "/system/app", @"C:\");
72
+ ///
73
+ /// </code>
74
+ /// </example>
75
+ public static AdbCommand FormAdbCommand ( Device device , string command , params object [ ] args )
76
+ {
77
+ return FormAdbCommand ( "-s " + device . SerialNumber + " " + command , args ) ;
78
+ }
79
+
80
+ /// <summary>
81
+ /// Forms an <see cref="AdbCommand"/> that is passed to <c>Adb.ExecuteAdbCommand()</c>
82
+ /// </summary>
83
+ /// <param name="device">Specific <see cref="Device"/> to run the command on</param>
84
+ /// <param name="rootShell">Specifies if you need <paramref name="executable"/> to run in a root shell</param>
85
+ /// <param name="executable">Executable file on <paramref name="device"/> to execute</param>
86
+ /// <param name="args">Any arguments that need to be sent to <paramref name="executable"/></param>
87
+ /// <returns><see cref="AdbCommand"/> that contains formatted command information</returns>
88
+ /// <remarks>This should only be used for Adb Shell commands, such as <c>adb shell getprop</c> or <c>adb shell dumpsys</c>.</remarks>
89
+ /// <exception cref="DeviceHasNoRootException"> if <paramref name="device"/> does not have root</exception>
90
+ /// <example>This example demonstrates how to create an <see cref="AdbCommand"/>
91
+ /// <code>
92
+ /// //This example shows how to create an AdbCommand object to execute on the running server.
93
+ /// //The command we will create is "adb shell input keyevent KeyEventCode.HOME".
94
+ /// //Notice how in the formation, you don't supply the prefix "adb", because the method takes care of it for you.
95
+ /// //This example also assumes you have a Device instance named device.
96
+ ///
97
+ /// AdbCommand adbCmd = Adb.FormAdbCommand(device, "input", "keyevent", (int)KeyEventCode.HOME);
98
+ ///
99
+ /// </code>
100
+ /// </example>
101
+ public static AdbCommand FormAdbShellCommand ( Device device , bool rootShell , string executable , params object [ ] args )
102
+ {
103
+ if ( rootShell && ! device . HasRoot )
104
+ throw new DeviceHasNoRootException ( ) ;
105
+
106
+ string shellCommand = string . Format ( "-s {0} shell \" " , device . SerialNumber ) ;
107
+
108
+ if ( rootShell )
109
+ shellCommand += "su -c \" " ;
110
+
111
+ shellCommand += executable ;
112
+
113
+ for ( int i = 0 ; i < args . Length ; i ++ )
114
+ shellCommand += " " + args [ i ] ;
115
+
116
+ if ( rootShell )
117
+ shellCommand += "\" " ;
118
+
119
+ shellCommand += "\" " ;
120
+
121
+ return new AdbCommand ( shellCommand ) ;
122
+ }
123
+
124
+ /// <summary>
125
+ /// Opens Adb Shell and allows input to be typed directly to the shell. Experimental!
126
+ /// </summary>
127
+ /// <remarks>Added specifically for RegawMOD CDMA Hero Rooter. Always remember to pass "exit" as the last command or it will not return!</remarks>
128
+ /// <param name="device">Specific <see cref="Device"/> to run the command on</param>
129
+ /// <param name="inputLines">Lines of commands to send to shell</param>
130
+ public static void ExecuteAdbShellCommandInputString ( Device device , params string [ ] inputLines )
131
+ {
132
+ Command . RunProcessWriteInput ( AndroidController . Instance . ResourceDirectory + ADB_EXE , "shell" , inputLines ) ;
133
+ }
134
+
135
+ /// <summary>
136
+ /// Executes an <see cref="AdbCommand"/> on the running Adb Server
137
+ /// </summary>
138
+ /// <remarks>This should be used if you want the output of the command returned</remarks>
139
+ /// <param name="command">Instance of <see cref="AdbCommand"/></param>
140
+ /// <returns>Output of <paramref name="command"/> run on server</returns>
141
+ public static string ExecuteAdbCommand ( AdbCommand command )
142
+ {
143
+ return Command . RunProcessReturnOutput ( AndroidController . Instance . ResourceDirectory + ADB_EXE , command . Command ) ;
144
+ }
145
+
146
+ /// <summary>
147
+ /// Executes an <see cref="AdbCommand"/> on the running Adb Server
148
+ /// </summary>
149
+ /// <remarks>This should be used if you do not want the output of the command returned. Good for quick abd shell commands</remarks>
150
+ /// <param name="command">Instance of <see cref="AdbCommand"/></param>
151
+ /// <returns>Output of <paramref name="command"/> run on server</returns>
152
+ public static void ExecuteAdbCommandNoReturn ( AdbCommand command )
153
+ {
154
+ Command . RunProcessNoReturn ( AndroidController . Instance . ResourceDirectory + ADB_EXE , command . Command ) ;
155
+ }
156
+
157
+ /// <summary>
158
+ /// Gets a value indicating if an Android Debug Bridge Server is currently running.
159
+ /// </summary>
160
+ public static bool ServerRunning
161
+ {
162
+ get { return Command . IsProcessRunning ( Adb . ADB ) ; }
163
+ }
164
+
165
+ internal static void StartServer ( )
166
+ {
167
+ ExecuteAdbCommandNoReturn ( Adb . FormAdbCommand ( "start-server" ) ) ;
168
+ }
169
+
170
+ internal static void KillServer ( )
171
+ {
172
+ ExecuteAdbCommandNoReturn ( Adb . FormAdbCommand ( "kill-server" ) ) ;
173
+ }
174
+
175
+ /// <summary>
176
+ /// Forwards a port that remains until the current <see cref="AndroidController"/> instance is Disposed, or the device is unplugged
177
+ /// </summary>
178
+ /// <remarks>Only supports tcp: forward spec for now</remarks>
179
+ /// <param name="device">Instance of <see cref="Device"/> to apply port forwarding to</param>
180
+ /// <param name="localPort">Local port number</param>
181
+ /// <param name="remotePort">Remote port number</param>
182
+ /// <returns>True if successful, false if unsuccessful</returns>
183
+ public static bool PortForward ( Device device , int localPort , int remotePort )
184
+ {
185
+ bool success = false ;
186
+
187
+ AdbCommand adbCmd = Adb . FormAdbCommand ( device , "forward" , "tcp:" + localPort , "tcp:" + remotePort ) ;
188
+ using ( StringReader r = new StringReader ( ExecuteAdbCommand ( adbCmd ) ) )
189
+ {
190
+ if ( r . ReadToEnd ( ) . Trim ( ) == "" )
191
+ success = true ;
192
+ }
193
+
194
+ return success ;
195
+ }
196
+ }
197
+ }
0 commit comments