Skip to content

Commit

Permalink
Improve DumpScreen error handling and add MIUI support
Browse files Browse the repository at this point in the history
  • Loading branch information
yungd1plomat committed Aug 14, 2023
1 parent 6ca02c3 commit 039e6bd
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Text;
using System.Threading;
using System.Xml;
using System.Text.RegularExpressions;

namespace AdvancedSharpAdbClient
{
Expand Down Expand Up @@ -764,18 +765,29 @@ public string DumpScreenString(DeviceData device)
socket.SendAdbRequest("shell:uiautomator dump /dev/tty");
AdbResponse response = socket.ReadAdbResponse();
using StreamReader reader = new(socket.GetShellStream(), Encoding);
string xmlString = reader.ReadToEnd().Replace("Events injected: 1\r\n", "").Replace("UI hierchary dumped to: /dev/tty", "").Trim();
return xmlString;
string xmlString = reader.ReadToEnd()
.Replace("Events injected: 1\r\n", string.Empty)
.Replace("UI hierchary dumped to: /dev/tty", string.Empty)
.Trim();
if (string.IsNullOrEmpty(xmlString))
return null;
if (xmlString.StartsWith("<?xml"))
return xmlString;
var xmlMatch = Regex.Match(xmlString, "<\\?xml(.?)*");
if (!xmlMatch.Success)
{
throw new XmlException("An error occurred while receiving xml: " + xmlString);
}
var cleanXml = xmlMatch.Value;
return cleanXml;
}

/// <inheritdoc/>
public XmlDocument DumpScreen(DeviceData device)
{
XmlDocument doc = new();
string xmlString = DumpScreenString(device);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR", StringComparison.OrdinalIgnoreCase)
&& !xmlString.StartsWith("java.lang.Exception"))
if (!string.IsNullOrEmpty(xmlString))
{
doc.LoadXml(xmlString);
return doc;
Expand All @@ -789,9 +801,7 @@ public Windows.Data.Xml.Dom.XmlDocument DumpScreenWinRT(DeviceData device)
{
Windows.Data.Xml.Dom.XmlDocument doc = new();
string xmlString = DumpScreenString(device);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR", StringComparison.OrdinalIgnoreCase)
&& !xmlString.StartsWith("java.lang.Exception"))
if (!string.IsNullOrEmpty(xmlString))
{
doc.LoadXml(xmlString);
return doc;
Expand Down

0 comments on commit 039e6bd

Please sign in to comment.