App.xaml.cs 5.3 KB

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