Version_Android.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Net.Http;
  3. using System.Text.RegularExpressions;
  4. using System.Threading.Tasks;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.Runtime;
  8. using Java.Net;
  9. using Xamarin.Android.Net;
  10. using Net = Android.Net;
  11. [assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.Android.Version_Android))]
  12. namespace InABox.Mobile.Android
  13. {
  14. /// <summary>
  15. /// <see cref="ILatestVersion"/> implementation for Android.
  16. /// </summary>
  17. [Preserve(AllMembers = true)]
  18. public class Version_Android : IAppVersion
  19. {
  20. string _packageName = "";
  21. string _versionName = "";
  22. public Version_Android() : base()
  23. {
  24. try
  25. {
  26. _packageName = Application.Context.PackageName;
  27. _versionName = Application.Context.PackageManager.GetPackageInfo(Application.Context.PackageName, 0).VersionName;
  28. }
  29. catch
  30. {
  31. }
  32. }
  33. /// <inheritdoc />
  34. public string InstalledVersionNumber
  35. {
  36. get => _versionName;
  37. }
  38. /// <inheritdoc />
  39. public async Task<bool> IsUsingLatestVersion()
  40. {
  41. try
  42. {
  43. var info = await GetLatestVersion(false);
  44. return String.IsNullOrWhiteSpace(info.Version)
  45. || (Version.Parse(info.Version).CompareTo(Version.Parse(_versionName)) <= 0);
  46. }
  47. catch (Exception e)
  48. {
  49. MobileLogging.Log(e,"Version");
  50. }
  51. return true;
  52. }
  53. private static AppInfo _info;
  54. public async Task<AppInfo> GetLatestVersion(bool force)
  55. {
  56. if (force)
  57. _info = null;
  58. if (_info != null)
  59. return _info;
  60. _info = new AppInfo();
  61. // https://play.google.com/store/apps/details?id=comal.timesheets.Android&hl=en
  62. var url = $"https://play.google.com/store/apps/details?id={_packageName}&hl=en";
  63. try
  64. {
  65. using (var request = new HttpRequestMessage(HttpMethod.Get, url))
  66. {
  67. using (var handler = new HttpClientHandler())
  68. {
  69. handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
  70. {
  71. //bypass
  72. return true;
  73. };
  74. using (var client = new HttpClient(handler))
  75. {
  76. try
  77. {
  78. using (var responseMsg = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead))
  79. {
  80. if (!responseMsg.IsSuccessStatusCode)
  81. {
  82. throw new LatestVersionException($"Error connecting to the Play Store. Url={url}.");
  83. }
  84. try
  85. {
  86. var content = responseMsg.Content == null ? null : await responseMsg.Content.ReadAsStringAsync();
  87. var versionMatch = Regex.Match(content, "\\[\\[\\[\"(\\d+\\.\\d+\\.\\d+)").Groups[1];
  88. if (versionMatch.Success)
  89. {
  90. _info.Version = versionMatch.Value.Trim();
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. throw new LatestVersionException($"Error parsing content from the Play Store. Url={url}.", e);
  96. }
  97. }
  98. }
  99. catch (Exception ex1)
  100. {
  101. }
  102. }
  103. }
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. MobileLogging.Log(ex,"PlayStore");
  109. }
  110. return _info;
  111. }
  112. public Task OpenAppInStore()
  113. {
  114. try
  115. {
  116. try
  117. {
  118. var intent = new Intent(Intent.ActionView, Net.Uri.Parse($"market://details?id={_packageName}"));
  119. intent.SetPackage("com.android.vending");
  120. intent.SetFlags(ActivityFlags.NewTask);
  121. Application.Context.StartActivity(intent);
  122. }
  123. catch (ActivityNotFoundException)
  124. {
  125. var intent = new Intent(Intent.ActionView, Net.Uri.Parse($"https://play.google.com/store/apps/details?id={_packageName}"));
  126. Application.Context.StartActivity(intent);
  127. }
  128. }
  129. catch (Exception e)
  130. {
  131. MobileLogging.Log(e,"PlayStore");
  132. }
  133. return Task.FromResult(true);
  134. }
  135. }
  136. }