JD2022-TU1/main/extern/UbiServices/custom/external/tools/androidLauncher/AndroidLauncher/DeviceConnectionInfo.cs

94 lines
3 KiB
C#

using System;
using System.Diagnostics;
namespace AndroidLauncher
{
public class DeviceConnectionInfo
{
public enum ConnectionState
{
Connected,
Unauthorized,
NoDevice
}
public string DeviceId;
public ConnectionState State;
public string Model;
public bool AreTestsRunning;
public AndroidADBProcess LaunchTestsProcess = null;
public DeviceConnectionInfo(string deviceId, string deviceState, string deviceModel)
{
DeviceId = deviceId;
switch (deviceState.ToLower())
{
case "connected":
State = ConnectionState.Connected;
break;
case "unauthorized":
State = ConnectionState.Unauthorized;
break;
case "nodevice":
State = ConnectionState.NoDevice;
break;
}
Model = deviceModel;
}
private bool IsAppRunning(string appPackageName)
{
int pid = Utils.FindPid(appPackageName, DeviceId);
if (pid != Utils.PidNotFound)
{
return true;
}
return false;
}
public bool IsValidToRun(string appPackageName)
{
return DeviceId.Length > 0 &&
State.Equals(ConnectionState.Connected) &&
!IsAppRunning(appPackageName);
}
public void PrepareDevice(string appPackageName, string apkLocation, Action onPrepareFailure)
{
Utils.StartAdbSyncWithTimeout("Uninstall the app if it was installed on the " + Model, "shell pm uninstall -k " + appPackageName, DeviceId);
Utils.StartAdbSyncWithTimeout("Install the application on the " + Model, "install -g " + apkLocation, DeviceId, null, onPrepareFailure);
Utils.StartAdbSyncWithTimeout("Clear the logs on the " + Model + " before starting the application.", "logcat -c", DeviceId, null, onPrepareFailure);
}
public void LaunchTests(string activityIntent, string activityIntentParameters)
{
LaunchTestsProcess = new AndroidADBProcess("shell am start -n " + activityIntent + " --es " + activityIntentParameters, DeviceId);
LaunchTestsProcess.OutputDataReceived += new DataReceivedEventHandler
(
delegate (object sender, DataReceivedEventArgs e)
{
if(e.Data != null && e.Data != "")
{
Console.WriteLine("[shell am start] {0}", e.Data);
}
}
);
LaunchTestsProcess.Start();
LaunchTestsProcess.BeginOutputReadLine();
}
public void KillLaunchedActivity()
{
if(LaunchTestsProcess != null)
{
LaunchTestsProcess.Kill();
}
}
}
}