b15
b15

Reputation: 2361

Looking up version and forcing an update in xamarin

There are many posts about doing this in java, but I found that NSoup (the port of the JSoup library) doesn't work for me, so I failed to port it to c#/Xamarin. For multiplayer functions of a game I'm working on, I need to make sure clients are synced before starting multiplayer matchmaking. This means I have to force the user to update the app if there's a new version available before they're allowed to invite other players to matches, join quick matches, etc..

So when a user presses the "quick match" button, for example, I need to:

  1. Check for the version name (im incrementing version name, not code, for breaking changes)
  2. Compare the version name from that to the current version name installed

    3.

-If the newer version name is greater than the current one, I need to give the user the option to update their app, and send them to the google play store page for my app if they choose 'yes'. Then I'll just let them update from there and our work is done.

-If the versions are the same, allow whatever the button's functionality (i.e sending them to the waiting room for matchmaking) to proceed.

Upvotes: 2

Views: 7578

Answers (1)

b15
b15

Reputation: 2361

Create the methods necessary to check for updates and act accordingly:

private void CheckUpdate(Action doIfUpToDate)
    {
        if(NeedUpdate())
        {
            Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
            alert.SetTitle("New Update");
            alert.SetMessage("You must download the newest version of this to play multiplayer.  Would you like to now?");
            alert.SetCancelable(false);
            alert.SetPositiveButton("Yes", new EventHandler<DialogClickEventArgs>((object sender, DialogClickEventArgs e) => GetUpdate()));
            alert.SetNegativeButton("No", delegate{});
            alert.Show();
        }
        else
        {
            doIfUpToDate.Invoke();
        }
    }

private bool NeedUpdate()
    {
        try
        {
            var curVersion = PackageManager.GetPackageInfo(PackageName, 0).VersionName;
            var newVersion = curVersion;

            string htmlCode;
            //probably better to do in a background thread
            using (WebClient client = new WebClient())
            {
                htmlCode = client.DownloadString("https://play.google.com/store/apps/details?id=" + PackageName + "&hl=en");
            }

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlCode);

            newVersion = doc.DocumentNode.SelectNodes("//div[@itemprop='softwareVersion']")
                              .Select(p => p.InnerText)
                              .ToList()
                              .First()
                              .Trim();

            return String.Compare(curVersion, newVersion) < 0;
        }
        catch (Exception e)
        {
            Log.Error(TAG, e.Message);
            Toast.MakeText(this, "Trouble validating app version for multiplayer gameplay.. Check your internet connection", ToastLength.Long).Show();
            return true;
        }
    }

private void GetUpdate()
    {
        try
        {
            StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + PackageName)));
        }
        catch (ActivityNotFoundException e)
        {
            //Default to the the actual web page in case google play store app is not installed
            StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + PackageName)));
        }
    }

And then from a given button that could start a multiplayer game:

var quickMatchButton = FindViewById<Button>(Resource.Id.button_quick_game);
quickMatchButton.Click += new EventHandler((object sender, EventArgs e) => CheckUpdate(() => startQuickMatch()));

Upvotes: 4

Related Questions