App.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using InABox.Rpc;
  10. using Xamarin.Essentials;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  14. namespace PRS.Mobile
  15. {
  16. public enum TransportStatus
  17. {
  18. BadConfiguration,
  19. NoConnection,
  20. OK,
  21. None
  22. }
  23. public partial class App : Application
  24. {
  25. public static string LaunchParameters { get; set; }
  26. public static IRpcClientTransport Transport { get; set; }
  27. public static TransportStatus ConnectTransport(string[] urls)
  28. {
  29. if (Transport?.IsConnected() == true)
  30. Transport.Disconnect();
  31. Transport = null;
  32. if (!urls.Any())
  33. return TransportStatus.BadConfiguration;
  34. var transport = new RpcClientSocketTransport(urls);
  35. transport.Connect();
  36. if (!transport.IsConnected())
  37. return TransportStatus.NoConnection;
  38. Transport = transport;
  39. ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, Transport);
  40. // Workaround for bug in RPCSocketServer where lrge uploads (ie photos)
  41. // actually crash the server. So to get something working, we'll
  42. // fall back to a Rest Client while still maintaining the RPC Transport stuff
  43. var info = new Client<User>().Info();
  44. if (info.RestPort != 0)
  45. {
  46. var resturl = $"{transport.ServerName()}:{info.RestPort}";
  47. ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.TimeBench,
  48. MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, resturl);
  49. }
  50. return TransportStatus.OK;
  51. }
  52. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  53. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  54. public static DataModel Data { get; } = new DataModel();
  55. public static string DeviceString
  56. {
  57. get
  58. {
  59. var result = Device.RuntimePlatform.Substring(0, 1).ToUpper();
  60. if (Device.Idiom == TargetIdiom.Phone)
  61. result = result.ToLower();
  62. return result;
  63. }
  64. }
  65. public static double GetXamarinWidth()
  66. {
  67. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  68. var width = mainDisplayInfo.Width;
  69. return width / mainDisplayInfo.Density;
  70. }
  71. public static bool IsInForeground { get; set; } = false;
  72. // this is set by PINLoginPage if DatabaseSettings.UserID is blank
  73. // Effective, this will enable the "back" (-> logout) button on the Main page
  74. // if the userid is not set.
  75. public static bool IsSharedDevice { get; set; }
  76. public const string MessageOnStart = "OnStart";
  77. public const string MessageOnSleep = "OnSleep";
  78. public const string MessageOnResume = "OnResume";
  79. public App()
  80. {
  81. InitializeComponent();
  82. // https://github.com/Baseflow/XF-Material-Library
  83. XF.Material.Forms.Material.Init(this);
  84. XF.Material.Forms.Material.Use("Material.Configuration");
  85. MobileUtils.Init();
  86. CoreUtils.RegisterClasses();
  87. ComalUtils.RegisterClasses();
  88. StartMonitoringGPS();
  89. StartMonitoringBlueTooth();
  90. // PINLoginPage is going to be the root navigation page.
  91. // If shown, it will rerun the validation process
  92. // So we should be able to get to this page from
  93. // 1. App Startup => Autologin
  94. // 2. SettingsPage.Save() => AutoLogin
  95. // 3. Main Window => Always manual login?
  96. var login = new PinLoginPage();
  97. var navigation = new NavigationPage(login);
  98. MainPage = navigation;
  99. }
  100. protected override void OnStart()
  101. {
  102. // Handle when your app starts
  103. }
  104. protected override void OnSleep()
  105. {
  106. // Handle when your app sleeps
  107. }
  108. protected override void OnResume()
  109. {
  110. // Handle when your app resumes
  111. }
  112. private void StartMonitoringGPS()
  113. {
  114. Task.Run(() =>
  115. {
  116. while (true)
  117. {
  118. if (App.IsInForeground)
  119. {
  120. try
  121. {
  122. GPS.GetLocation();
  123. }
  124. catch (Exception e)
  125. {
  126. // Need to Log this
  127. }
  128. }
  129. Thread.Sleep(TimeSpan.FromSeconds(30));
  130. }
  131. });
  132. }
  133. private void StartMonitoringBlueTooth()
  134. {
  135. Task.Run(() =>
  136. {
  137. while (true)
  138. {
  139. if (App.IsInForeground)
  140. {
  141. try
  142. {
  143. Bluetooth.ScanForDevices();
  144. }
  145. catch (Exception e)
  146. {
  147. // Need to Log this
  148. }
  149. }
  150. Thread.Sleep(TimeSpan.FromSeconds(30));
  151. }
  152. });
  153. }
  154. }
  155. }