Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 46 additions & 93 deletions Appium Wizard/OpenDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ public async Task<bool> StartBackgroundTasks()
commonProgress.UpdateStepLabel(title, "Initializing...", 5);
if (OSType.Equals("Android"))
{
if (!MainScreen.useScrcpy)
{
await SetupAndroidScreenMirroringUsingUiAutomator();
}
else
{
await SetupAndroidScreenMirroringUsingScrcpy();
isScreenServerStarted = true; //use scrcpy
}
await SetupAndroidScreenMirroring();
}
else
{
Expand Down Expand Up @@ -521,7 +513,7 @@ await Task.Run(() =>
}

public static Dictionary<string, string> deviceSessionId = new Dictionary<string, string>();
private async Task SetupAndroidScreenMirroringUsingUiAutomator()
private async Task SetupAndroidScreenMirroring()
{
await Task.Run(() =>
{
Expand Down Expand Up @@ -552,8 +544,16 @@ await Task.Run(() =>
catch (Exception e)
{
Logger.Error(e, "Error installing uiautomator");
MessageBox.Show(e.Message, "Error installing UIAutomator", MessageBoxButtons.OK, MessageBoxIcon.Error);
isScreenServerStarted = false;
if (MainScreen.useScrcpy)
{
MessageBox.Show(e.Message, "Screen mirroring will work but Object spy won't work.", MessageBoxButtons.OK, MessageBoxIcon.Error);
isScreenServerStarted = true;
}
else
{
MessageBox.Show(e.Message, "Error installing UIAutomator, Please try again.", MessageBoxButtons.OK, MessageBoxIcon.Error);
isScreenServerStarted = false;
}
return;
}
try
Expand All @@ -569,17 +569,20 @@ await Task.Run(() =>
proxyPort = Common.GetFreePort(8221, 8299);
AndroidMethods.GetInstance().StartAndroidProxyServer(proxyPort, 6790, udid);
}
int forwardedScreenPort = AndroidMethods.GetInstance().GetForwardedPort(udid, 7810);
if (forwardedScreenPort != -1)
if (!MainScreen.useScrcpy)// Setup screen port only for UIAutomator (scrcpy handles screen mirroring directly)
{
screenServerPort = forwardedScreenPort;
}
else
{
commonProgress.UpdateStepLabel(title, "Setting up Proxy Server...", 60);
screenServerPort = Common.GetFreePort();
AndroidMethods.GetInstance().StartAndroidProxyServer(screenServerPort, 7810, udid);
}
int forwardedScreenPort = AndroidMethods.GetInstance().GetForwardedPort(udid, 7810);
if (forwardedScreenPort != -1)
{
screenServerPort = forwardedScreenPort;
}
else
{
commonProgress.UpdateStepLabel(title, "Setting up Proxy Server...", 60);
screenServerPort = Common.GetFreePort();
AndroidMethods.GetInstance().StartAndroidProxyServer(screenServerPort, 7810, udid);
}
}
commonProgress.UpdateStepLabel(title, "Checking UIAutomator running status...", 70);
bool IsUIAutomatorRunning = AndroidMethods.GetInstance().IsUIAutomatorRunning(udid);
if (!IsUIAutomatorRunning)
Expand Down Expand Up @@ -612,7 +615,7 @@ await Task.Run(() =>
isSessionCreated = false;
}
}
if (!isSessionCreated & !isItValidSession)
if (isSessionCreated && !isItValidSession)
{
commonProgress.UpdateStepLabel(title, "Restarting UIAutomator...", 80);
AndroidMethods.GetInstance().StopUIAutomator(udid);
Expand All @@ -633,93 +636,43 @@ await Task.Run(() =>
}
else
{
isScreenServerStarted = false;
if (MainScreen.useScrcpy)
{
MessageBox.Show("Screen mirroring will work but Object spy won't work.", "Screen mirroring", MessageBoxButtons.OK, MessageBoxIcon.Error);
isScreenServerStarted = true;
}
else
{
isScreenServerStarted = false;
}
}
if (isScreenServerStarted)
{
commonProgress.UpdateStepLabel(title, "Starting device screen streaming...", 85);
if (!deviceDetails.ContainsKey(udid))
{
keyValuePairs.Add("proxyPort", proxyPort);
keyValuePairs.Add("screenPort", screenServerPort);
if (!MainScreen.useScrcpy)
{
keyValuePairs.Add("screenPort", screenServerPort);
}
deviceDetails.Add(udid, keyValuePairs);
}
}
Thread.Sleep(2000);
}
catch (Exception e)
{
isScreenServerStarted = false;
MessageBox.Show(e.Message, "Failed starting screen server", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
});
}

private async Task SetupAndroidScreenMirroringUsingScrcpy()
{
await Task.Run(() =>
{
try
{
string message = "Please wait while setting up screen mirroring...";
try
{
MainScreen.udidScreenDensity[udid] = AndroidMethods.GetInstance().GetScreenDensity(udid);
}
catch (Exception)
{
}
commonProgress.UpdateStepLabel(title, message, 5);
proxyPort = AndroidMethods.GetInstance().GetForwardedPort(udid, 6790);
if (proxyPort == -1)
{
commonProgress.UpdateStepLabel(title, message, 10);
proxyPort = Common.GetFreePort(8221, 8299);
AndroidMethods.GetInstance().StartAndroidProxyServer(proxyPort, 6790, udid);
}
screenServerPort = AndroidMethods.GetInstance().GetForwardedPort(udid, 7810);
if (screenServerPort == -1)
if (MainScreen.useScrcpy)
{
commonProgress.UpdateStepLabel(title, message, 15);
screenServerPort = Common.GetFreePort(8221, 8299);
AndroidMethods.GetInstance().StartAndroidProxyServer(screenServerPort, 7810, udid);
MessageBox.Show("Screen mirroring will work but Object spy won't work.", "Screen mirroring", MessageBoxButtons.OK, MessageBoxIcon.Error);
isScreenServerStarted = true;
}
commonProgress.UpdateStepLabel(title, message, 20);
UIAutomatorSessionId = AndroidAPIMethods.GetSessionID(proxyPort);
if (UIAutomatorSessionId.Equals("nosession"))
else
{
commonProgress.UpdateStepLabel(title, message, 30);
bool isUIAutomatorInstalled = AndroidMethods.GetInstance().isUIAutomatorInstalled(udid, true, 10000);
Logger.Info("isUIAutomatorInstalled : " + isUIAutomatorInstalled);
if (!isUIAutomatorInstalled)
{
commonProgress.UpdateStepLabel(title, message, 40);
AndroidMethods.GetInstance().InstallUIAutomator(udid);
commonProgress.UpdateStepLabel(title, message, 50);
}
commonProgress.UpdateStepLabel(title, message, 60);
bool isRunning = AndroidMethods.GetInstance().IsUIAutomatorRunning(udid);
if (!isRunning)
{
commonProgress.UpdateStepLabel(title, message, 70);
AndroidAsyncMethods.GetInstance().StartUIAutomatorServer(udid);
Task.Delay(1000);
}
commonProgress.UpdateStepLabel(title, message, 80);
UIAutomatorSessionId = AndroidAPIMethods.GetSessionID(proxyPort);
if (UIAutomatorSessionId.Equals("nosession"))
{
UIAutomatorSessionId = AndroidAPIMethods.CreateSession(proxyPort);
}
}
commonProgress.UpdateStepLabel(title, message, 90);

}
catch (Exception e)
{
Logger.Error(e, "Error installing uiautomator");
MessageBox.Show(e.Message, "Screen mirroring will work but Object spy won't work.", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
isScreenServerStarted = false;
MessageBox.Show(e.Message, "Failed starting screen server", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
});
}
Expand Down
Loading