| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | using System;using System.Net.Http;using System.Text.RegularExpressions;using System.Threading.Tasks;using Android.App;using Android.Content;using Android.Runtime;using Java.Net;using Xamarin.Android.Net;using Net = Android.Net;[assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.Android.Version_Android))]namespace InABox.Mobile.Android{    /// <summary>    /// <see cref="ILatestVersion"/> implementation for Android.    /// </summary>    [Preserve(AllMembers = true)]    public class Version_Android : IAppVersion    {        string _packageName = "";        string _versionName = "";        public Version_Android() : base()        {            try            {                _packageName = Application.Context.PackageName;                _versionName = Application.Context.PackageManager.GetPackageInfo(Application.Context.PackageName, 0).VersionName;            }            catch            {            }        }        /// <inheritdoc />        public string InstalledVersionNumber        {            get => _versionName;        }        /// <inheritdoc />        public async Task<bool> IsUsingLatestVersion()        {            try            {                var info = await GetLatestVersion(false);                return String.IsNullOrWhiteSpace(info.Version)                        || (Version.Parse(info.Version).CompareTo(Version.Parse(_versionName)) <= 0);            }            catch (Exception e)            {                MobileLogging.Log(e,"Version");            }            return true;        }                private static AppInfo _info;                public async Task<AppInfo> GetLatestVersion(bool force)        {                        if (force)                _info = null;                        if (_info != null)                return _info;                        _info = new AppInfo();                        // https://play.google.com/store/apps/details?id=comal.timesheets.Android&hl=en            var url = $"https://play.google.com/store/apps/details?id={_packageName}&hl=en";            try            {                using (var request = new HttpRequestMessage(HttpMethod.Get, url))                {                    using (var handler = new HttpClientHandler())                    {                        handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>                        {                            //bypass                            return true;                        };                        using (var client = new HttpClient(handler))                        {                            try                            {                                using (var responseMsg = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead))                                {                                    if (!responseMsg.IsSuccessStatusCode)                                    {                                        throw new LatestVersionException($"Error connecting to the Play Store. Url={url}.");                                    }                                    try                                    {                                        var content = responseMsg.Content == null ? null : await responseMsg.Content.ReadAsStringAsync();                                        var versionMatch = Regex.Match(content, "\\[\\[\\[\"(\\d+\\.\\d+\\.\\d+)").Groups[1];                                                                                if (versionMatch.Success)                                        {                                            _info.Version = versionMatch.Value.Trim();                                        }                                    }                                    catch (Exception e)                                    {                                        throw new LatestVersionException($"Error parsing content from the Play Store. Url={url}.", e);                                    }                                }                            }                            catch (Exception ex1)                            {                            }                        }                    }                }            }            catch (Exception ex)            {                MobileLogging.Log(ex,"PlayStore");            }            return _info;        }                public Task OpenAppInStore()        {            try            {                try                {                    var intent = new Intent(Intent.ActionView, Net.Uri.Parse($"market://details?id={_packageName}"));                    intent.SetPackage("com.android.vending");                    intent.SetFlags(ActivityFlags.NewTask);                    Application.Context.StartActivity(intent);                }                catch (ActivityNotFoundException)                {                    var intent = new Intent(Intent.ActionView, Net.Uri.Parse($"https://play.google.com/store/apps/details?id={_packageName}"));                    Application.Context.StartActivity(intent);                }            }            catch (Exception e)            {                MobileLogging.Log(e,"PlayStore");            }            return Task.FromResult(true);        }    }}
 |