Skip to content

Commit ae1144f

Browse files
committed
Merge pull request regaw-leinad#1 from regaw-leinad/master
Update to latest version
2 parents b58445a + 1dab93f commit ae1144f

File tree

9 files changed

+121
-149
lines changed

9 files changed

+121
-149
lines changed

AndroidLib/Classes/AndroidController/Adb.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ namespace RegawMOD.Android
1414
public class AdbCommand
1515
{
1616
private string command;
17+
private int timeout;
1718
internal string Command { get { return this.command; } }
18-
internal AdbCommand(string command) { this.command = command; }
19+
internal int Timeout { get { return this.timeout; } }
20+
internal AdbCommand(string command) { this.command = command; this.timeout = RegawMOD.Command.DEFAULT_TIMEOUT; }
21+
22+
/// <summary>
23+
/// Sets the timeout for the AdbCommand
24+
/// </summary>
25+
/// <param name="timeout">The timeout for the command in milliseconds</param>
26+
public AdbCommand WithTimeout(int timeout) { this.timeout = timeout; return this; }
1927
}
2028

2129
/// <summary>
@@ -26,6 +34,7 @@ public static class Adb
2634
private static object _lock = "U is lawked";
2735
internal const string ADB = "adb";
2836
internal const string ADB_EXE = "adb.exe";
37+
internal const string ADB_VERSION = "1.0.31";
2938

3039
/// <summary>
3140
/// Forms an <see cref="AdbCommand"/> that is passed to <c>Adb.ExecuteAdbCommand()</c>
@@ -149,7 +158,7 @@ public static string ExecuteAdbCommand(AdbCommand command, bool forceRegular = f
149158

150159
lock (_lock)
151160
{
152-
result = Command.RunProcessReturnOutput(AndroidController.Instance.ResourceDirectory + ADB_EXE, command.Command, forceRegular);
161+
result = Command.RunProcessReturnOutput(AndroidController.Instance.ResourceDirectory + ADB_EXE, command.Command, forceRegular, command.Timeout);
153162
}
154163

155164
return result;
@@ -180,7 +189,7 @@ public static int ExecuteAdbCommandReturnExitCode(AdbCommand command)
180189

181190
lock (_lock)
182191
{
183-
result = Command.RunProcessReturnExitCode(AndroidController.Instance.ResourceDirectory + ADB_EXE, command.Command);
192+
result = Command.RunProcessReturnExitCode(AndroidController.Instance.ResourceDirectory + ADB_EXE, command.Command, command.Timeout);
184193
}
185194

186195
return result;

AndroidLib/Classes/AndroidController/AndroidController.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public sealed class AndroidController
7474
#region MEMBER VARIABLES
7575
private string resourceDirectory;
7676
private List<string> connectedDevices;
77+
private bool Extract_Resources = false;
7778
#endregion
7879

7980
#region PROPERTIES
@@ -126,25 +127,24 @@ private AndroidController()
126127
#region PRIVATE METHODS
127128
private void CreateResourceDirectories()
128129
{
129-
if (!ResourceFolderManager.Register(ANDROID_CONTROLLER_TMP_FOLDER))
130+
if (!Adb.ExecuteAdbCommand(new AdbCommand("version")).Contains(Adb.ADB_VERSION))
130131
{
131-
if (File.Exists(this.resourceDirectory + Adb.ADB_EXE) && Adb.ServerRunning)
132-
{
133-
Adb.KillServer();
134-
Thread.Sleep(1000);
135-
}
136-
132+
Adb.KillServer();
133+
Thread.Sleep(1000);
137134
ResourceFolderManager.Unregister(ANDROID_CONTROLLER_TMP_FOLDER);
135+
Extract_Resources = true;
138136
}
139-
140137
ResourceFolderManager.Register(ANDROID_CONTROLLER_TMP_FOLDER);
141138
}
142139

143140
private void ExtractResources()
144141
{
145-
string[] res = new string[RESOURCES.Count];
146-
RESOURCES.Keys.CopyTo(res, 0);
147-
Extract.Resources(this, this.resourceDirectory, "Resources.AndroidController", res);
142+
if (this.Extract_Resources)
143+
{
144+
string[] res = new string[RESOURCES.Count];
145+
RESOURCES.Keys.CopyTo(res, 0);
146+
Extract.Resources(this, this.resourceDirectory, "Resources.AndroidController", res);
147+
}
148148
}
149149
#endregion
150150

@@ -160,8 +160,6 @@ public void Dispose()
160160
Adb.KillServer();
161161
Thread.Sleep(1000);
162162
}
163-
164-
ResourceFolderManager.Unregister(ANDROID_CONTROLLER_TMP_FOLDER);
165163
AndroidController.instance = null;
166164
}
167165

@@ -300,19 +298,34 @@ public void UpdateDeviceList()
300298
}
301299
}
302300

301+
private bool _CancelRequest;
302+
/// <summary>
303+
/// Set to true to cancel a WaitForDevice() method call
304+
/// </summary>
305+
public bool CancelWait
306+
{
307+
get { return _CancelRequest; }
308+
set { _CancelRequest = value; }
309+
}
310+
303311
/// <summary>
304312
/// Pauses thread until 1 or more Android devices are connected
305313
/// </summary>
306314
/// <remarks>Do Not Use in Windows Forms applications, as this method pauses the current thread. Works fine in Console Applications</remarks>
307315
public void WaitForDevice()
308316
{
309-
/* Entering an endless loop will exaust CPU.
310-
* Since this method must be called in a child thread in Windows Presentation Foundation (WPF) or Windows Form Apps,
311-
* sleeping thread for 250 miliSecond (1/4 of a second)
312-
* will be more friendly to the CPU. Nontheless checking 4 times for a connected device in each second is more than enough,
313-
* and will not result in late response from the app if a device gets connected.
314-
*/
315-
while (!this.HasConnectedDevices) { Thread.Sleep(250); }
317+
/* Entering an endless loop will exhaust CPU.
318+
* Since this method must be called in a child thread in Windows Presentation Foundation (WPF) or Windows Form Apps,
319+
* sleeping thread for 250 miliSecond (1/4 of a second)
320+
* will be more friendly to the CPU. Nonetheless checking 4 times for a connected device in each second is more than enough,
321+
* and will not result in late response from the app if a device gets connected.
322+
*/
323+
while (!this.HasConnectedDevices && !this.CancelWait)
324+
{
325+
Thread.Sleep(250);
326+
}
327+
328+
this.CancelWait = false;
316329
}
317330
#endregion
318331
}

AndroidLib/Classes/AndroidController/BuildProp.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* BuildProp.cs - Developed by Dan Wager for AndroidLib.dll
33
*/
44

5-
using RegawMOD.Util;
65
using System;
76
using System.Collections.Generic;
87
using System.IO;

AndroidLib/Classes/AndroidController/BusyBox.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public void Update()
5454

5555
if (!this.device.HasRoot || this.device.State != DeviceState.ONLINE)
5656
{
57-
this.isInstalled = false;
58-
this.version = null;
57+
SetNoBusybox();
5958
return;
6059
}
6160

@@ -66,22 +65,34 @@ public void Update()
6665

6766
if (check.Contains(string.Format("{0}: not found", EXECUTABLE)))
6867
{
69-
this.isInstalled = false;
70-
this.version = null;
68+
SetNoBusybox();
7169
return;
7270
}
7371

7472
this.isInstalled = true;
7573

7674
this.version = check.Split(' ')[1].Substring(1);
7775

78-
while (s.ReadLine() != "Currently defined functions:") { }
76+
while (s.Peek() != -1 && s.ReadLine() != "Currently defined functions:") { }
7977

8078
string[] cmds = s.ReadToEnd().Replace(" ", "").Replace("\r\r\n\t", "").Trim('\t', '\r', '\n').Split(',');
8179

82-
foreach (string cmd in cmds)
83-
this.commands.Add(cmd);
80+
if (cmds.Length.Equals(0))
81+
{
82+
SetNoBusybox();
83+
}
84+
else
85+
{
86+
foreach (string cmd in cmds)
87+
this.commands.Add(cmd);
88+
}
8489
}
8590
}
91+
92+
private void SetNoBusybox()
93+
{
94+
this.isInstalled = false;
95+
this.version = null;
96+
}
8697
}
8798
}

0 commit comments

Comments
 (0)