Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autorun will choose correct device when launching application #193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
78 changes: 78 additions & 0 deletions com.unity.mobile.android-logcat/Editor/AndroidLogcatAutoSelect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using UnityEditor;
namespace Unity.Android.Logcat
{
internal class AutoSelect
{
private const int kMillisecondsBetweenConsecutiveAutoConnectChecks = 1000;
private const int kMillisecondsMaxAutoconnectTimeOut = 5000;

public enum State
{
Idle,
InProgress
}

private State m_State;
private DateTime m_TimeOfLastAutoConnectUpdate;
private DateTime m_TimeOfLastAutoConnectStart;

public string PackageName { private set; get; }
public string DeviceId { private set; get; }

public State GetState()
{
return m_State;
}

public AutoSelect()
{
Finish();
}

public void Start(string deviceId, string package)
{
m_TimeOfLastAutoConnectStart = DateTime.Now;
m_State = State.InProgress;
DeviceId = deviceId;
PackageName = package;
}

public bool ShouldTick()
{
// This is for AutoRun triggered by "Build And Run".
if ((DateTime.Now - m_TimeOfLastAutoConnectUpdate).TotalMilliseconds < kMillisecondsBetweenConsecutiveAutoConnectChecks)
return false;
AndroidLogcatInternalLog.Log($"Waiting for {PackageName} launch, elapsed {(DateTime.Now - m_TimeOfLastAutoConnectStart).Seconds} seconds");
m_TimeOfLastAutoConnectUpdate = DateTime.Now;
return true;
}

public double CheckTimeout()
{
var timeoutMS = (DateTime.Now - m_TimeOfLastAutoConnectStart).TotalMilliseconds;
if (timeoutMS > kMillisecondsMaxAutoconnectTimeOut)
return timeoutMS;
return -1;
}

public void Finish()
{
m_State = State.Idle;
DeviceId = string.Empty;
PackageName = string.Empty;
m_TimeOfLastAutoConnectUpdate = DateTime.MinValue;
m_TimeOfLastAutoConnectStart = DateTime.MinValue;
}

public void Reset()
{
Finish();
}

public override string ToString()
{
return $"State: {m_State}, DeviceId: {DeviceId}, Pacakge: {PackageName}";
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion com.unity.mobile.android-logcat/Editor/AndroidLogcatCallbacks.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
using UnityEditor;
using UnityEditor.Android;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace Unity.Android.Logcat
{
#if UNITY_6000_1_OR_NEWER
internal class AndroidLogcatRunCallbacks : IPostprocessLaunch
{
public int callbackOrder => 0;

public void OnPostprocessLaunch(ILaunchReport launchReport)
{
if (!AndroidLogcatConsoleWindow.ShowDuringBuildRun)
return;

if (launchReport.buildTarget != NamedBuildTarget.Android)
return;

#if UNITY_ANDROID
var androidReport = launchReport.AsAndroidReport();
if (androidReport != null)
{
var wnd = AndroidLogcatConsoleWindow.ShowNewOrExisting();
foreach (var l in androidReport.Launches)
{
if (!l.Success)
continue;
wnd.SetAutoSelect(l.DeviceId, l.PackageName);
break;
}
}
#endif
}
}
#else
internal class AndroidLogcatCallbacks : IPostprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
Expand All @@ -13,7 +45,12 @@ public void OnPostprocessBuild(BuildReport report)
if ((report.summary.options & BuildOptions.AutoRunPlayer) != 0 &&
report.summary.platform == BuildTarget.Android &&
AndroidLogcatConsoleWindow.ShowDuringBuildRun)
AndroidLogcatConsoleWindow.ShowNewOrExisting(true);
{
var wnd = AndroidLogcatConsoleWindow.ShowNewOrExisting();
wnd.SetAutoSelect(string.Empty, string.Empty);
}
}
}
#endif

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ private IReadOnlyList<ProcessInformation> ProcessesForSelectedDevice
private AndroidLogcatRuntimeBase m_Runtime;
private AndroidLogcat m_Logcat;
private AndroidLogcatStatusBar m_StatusBar;
private DateTime m_TimeOfLastAutoConnectUpdate;
private DateTime m_TimeOfLastAutoConnectStart;
private const byte kSpace = 3;
private const int kMillisecondsBetweenConsecutiveDeviceChecks = 1000;
private const int kMillisecondsBetweenConsecutiveAutoConnectChecks = 1000;
private const int kMillisecondsMaxAutoconnectTimeOut = 5000;

private bool m_AutoSelectProcess;
private bool m_FinishedAutoselectingProcess;
private AutoSelect m_AutoSelect = new AutoSelect();

private bool m_ApplySettings;

private AndroidLogcatMemoryViewer m_MemoryViewer;
Expand All @@ -58,21 +54,12 @@ private ProcessInformation SelectedProcess
}
}

public bool AutoSelectProcess
{
set
{
m_AutoSelectProcess = value;
m_FinishedAutoselectingProcess = false;
m_TimeOfLastAutoConnectStart = DateTime.Now;
if (m_StatusBar != null && m_AutoSelectProcess)
m_StatusBar.Message = "Waiting for '" + PlayerSettings.applicationIdentifier + "'";
}

get
{
return m_AutoSelectProcess;
}
public void SetAutoSelect(string deviceId, string packageName)
{
m_AutoSelect.Start(deviceId, packageName);
if (m_StatusBar != null)
m_StatusBar.Message = $"Waiting for '{packageName}' on device '{deviceId}'";
}

private bool IsLogcatConnected => m_Logcat != null && m_Logcat.IsConnected;
Expand All @@ -94,12 +81,9 @@ protected void OnEnableInternal(AndroidLogcatRuntimeBase runtime)
m_SearchField = new SearchField();

m_Runtime.UserSettings.Tags.TagSelectionChanged += TagSelectionChanged;

m_TimeOfLastAutoConnectStart = DateTime.Now;
m_Runtime.Update += OnUpdate;

m_FinishedAutoselectingProcess = false;
AndroidLogcatInternalLog.Log("Package: {0}, Auto select: {1}", PlayerSettings.applicationIdentifier, AutoSelectProcess);
AndroidLogcatInternalLog.Log($"AutoSelect: {m_AutoSelect}");

m_StatusBar = new AndroidLogcatStatusBar();

Expand Down Expand Up @@ -140,7 +124,7 @@ internal void OnDisable()
StopLogCat();

m_Runtime.Update -= OnUpdate;
AndroidLogcatInternalLog.Log("OnDisable, Auto select: {0}", m_AutoSelectProcess);
AndroidLogcatInternalLog.Log($"OnDisable, Auto select: {m_AutoSelect}");
m_Runtime = null;
}

Expand Down Expand Up @@ -197,42 +181,53 @@ private void OnUpdate()
if (deviceQuery.FirstConnectedDevice == null)
deviceQuery.UpdateConnectedDevicesList(false);

if (deviceQuery.FirstConnectedDevice == null)
var firstDevice = deviceQuery.FirstConnectedDevice;
if (firstDevice == null)
return;

if (m_AutoSelectProcess && !m_FinishedAutoselectingProcess)
if (m_AutoSelect.GetState() == AutoSelect.State.InProgress)
{
// This is for AutoRun triggered by "Build And Run".
if ((DateTime.Now - m_TimeOfLastAutoConnectUpdate).TotalMilliseconds < kMillisecondsBetweenConsecutiveAutoConnectChecks)
if (!m_AutoSelect.ShouldTick())
return;
AndroidLogcatInternalLog.Log("Waiting for {0} launch, elapsed {1} seconds", PlayerSettings.applicationIdentifier, (DateTime.Now - m_TimeOfLastAutoConnectStart).Seconds);
m_TimeOfLastAutoConnectUpdate = DateTime.Now;

var firstDevice = deviceQuery.FirstConnectedDevice;
ResetProcesses(firstDevice);
IAndroidLogcatDevice targetDevice;

if (string.IsNullOrEmpty(m_AutoSelect.DeviceId))
{
targetDevice = firstDevice;
}
else
{
targetDevice = deviceQuery.GetDevice(m_AutoSelect.DeviceId);
if (targetDevice == null)
targetDevice = firstDevice;
}

var targetPackageName = string.IsNullOrEmpty(m_AutoSelect.PackageName) ? PlayerSettings.applicationIdentifier : m_AutoSelect.PackageName;
ResetProcesses(targetDevice);

int projectApplicationPid = GetPidFromPackageName(null, PlayerSettings.applicationIdentifier, firstDevice);
var process = m_Runtime.UserSettings.CreateProcessInformation(PlayerSettings.applicationIdentifier, projectApplicationPid, firstDevice);
int projectApplicationPid = GetPidFromPackageName(null, targetPackageName, targetDevice);
var process = m_Runtime.UserSettings.CreateProcessInformation(targetPackageName, projectApplicationPid, targetDevice);
if (process != null)
{
AndroidLogcatInternalLog.Log("Auto selecting process {0}", PlayerSettings.applicationIdentifier);
AndroidLogcatInternalLog.Log($"Auto selecting process {targetPackageName} on device '{targetDevice.Id}'");
// Note: Don't call SelectPackage as that will reset m_AutoselectPackage
SetProcess(process);
deviceQuery.SelectDevice(firstDevice, false);
deviceQuery.SelectDevice(targetDevice, false);

RestartLogCat();
m_FinishedAutoselectingProcess = true;
m_AutoSelect.Finish();
UpdateStatusBar();
}
else
{
var timeoutMS = (DateTime.Now - m_TimeOfLastAutoConnectStart).TotalMilliseconds;
if (timeoutMS > kMillisecondsMaxAutoconnectTimeOut)
var timeOut = m_AutoSelect.CheckTimeout();
if (timeOut > 0)
{
var msg = string.Format("Timeout {0} ms while waiting for '{1}' to launch.", timeoutMS, PlayerSettings.applicationIdentifier);
var msg = $"Timeout {timeOut} ms while waiting for '{targetPackageName}' to launch on device '{targetDevice}'.";
UpdateStatusBar(msg);
AndroidLogcatInternalLog.Log(msg);
m_FinishedAutoselectingProcess = true;
m_AutoSelect.Finish();
}
}
}
Expand All @@ -244,7 +239,7 @@ private void OnUpdate()
ProcessInformation selectedProcess;
GetDeviceAndProcessFromSavedState(out selectedDevice, out selectedProcess);
if (selectedDevice == null || selectedDevice.State != IAndroidLogcatDevice.DeviceState.Connected)
selectedDevice = deviceQuery.FirstConnectedDevice;
selectedDevice = firstDevice;
if (selectedDevice != null)
{
SelectedProcess = null;
Expand Down Expand Up @@ -467,9 +462,13 @@ private void DoDebuggingGUI()
}


if (GUILayout.Button("AutoSelect " + AutoSelectProcess.ToString(), AndroidLogcatStyles.toolbarButton))
if (GUILayout.Button("AutoSelect " + m_AutoSelect.ToString(), AndroidLogcatStyles.toolbarButton))
{
AutoSelectProcess = true;
var selectedDevice = m_Runtime.DeviceQuery.SelectedDevice;
if (selectedDevice != null && SelectedProcess != null)
m_AutoSelect.Start(selectedDevice.Id, SelectedProcess.DisplayName);
else
Debug.Log("Cannot auto select");
}

m_Logcat?.DoDebuggingGUI();
Expand Down Expand Up @@ -558,7 +557,7 @@ private void SelectProcess(ProcessInformation newProcess)
(newProcess != null && SelectedProcess != null && newProcess.name == SelectedProcess.name && newProcess.processId == SelectedProcess.processId))
return;

m_AutoSelectProcess = false;
m_AutoSelect.Finish();

AndroidLogcatInternalLog.Log("Selecting process {0}", newProcess == null ? "<null>" : newProcess.DisplayName);

Expand Down Expand Up @@ -869,10 +868,10 @@ public void UpdateStatusBar(string message)
[MenuItem("Window/Analysis/Android Logcat &6")]
internal static AndroidLogcatConsoleWindow ShowWindow()
{
return ShowNewOrExisting(false);
return ShowNewOrExisting();
}

internal static AndroidLogcatConsoleWindow ShowNewOrExisting(bool autoSelectPackage)
internal static AndroidLogcatConsoleWindow ShowNewOrExisting()
{
var wnd = GetWindow<AndroidLogcatConsoleWindow>();
if (wnd == null)
Expand All @@ -881,10 +880,8 @@ internal static AndroidLogcatConsoleWindow ShowNewOrExisting(bool autoSelectPack
}

wnd.titleContent = new GUIContent("Android Logcat");
wnd.AutoSelectProcess = autoSelectPackage;
wnd.Show();
wnd.Focus();

return wnd;
}
}
Expand Down