|
| 1 | +// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using osu.Framework; |
| 9 | +using osu.Framework.Allocation; |
| 10 | +using osu.Framework.Graphics.Sprites; |
| 11 | +using osu.Framework.Platform; |
| 12 | +using osu.Game.Online.API; |
| 13 | +using osu.Game.Overlays.Notifications; |
| 14 | + |
| 15 | +namespace osu.Game.Updater |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// An update manager that shows notifications if a newer release is detected for mobile platforms. |
| 19 | + /// Installation is left up to the user. |
| 20 | + /// </summary> |
| 21 | + public partial class MobileUpdateNotifier : UpdateManager |
| 22 | + { |
| 23 | + private string version = null!; |
| 24 | + |
| 25 | + [Resolved] |
| 26 | + private GameHost host { get; set; } = null!; |
| 27 | + |
| 28 | + [BackgroundDependencyLoader] |
| 29 | + private void load(OsuGameBase game) |
| 30 | + { |
| 31 | + version = game.Version; |
| 32 | + } |
| 33 | + |
| 34 | + protected override async Task<bool> PerformUpdateCheck() |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + var releases = new OsuJsonWebRequest<GitHubRelease>("https://api.github.com/repos/ppy/osu/releases/latest"); |
| 39 | + |
| 40 | + await releases.PerformAsync().ConfigureAwait(false); |
| 41 | + |
| 42 | + var latest = releases.ResponseObject; |
| 43 | + |
| 44 | + // avoid any discrepancies due to build suffixes for now. |
| 45 | + // eventually we will want to support release streams and consider these. |
| 46 | + version = version.Split('-').First(); |
| 47 | + string latestTagName = latest.TagName.Split('-').First(); |
| 48 | + |
| 49 | + if (latestTagName != version && tryGetBestUrl(latest, out string? url)) |
| 50 | + { |
| 51 | + Notifications.Post(new SimpleNotification |
| 52 | + { |
| 53 | + Text = $"A newer release of osu! has been found ({version} → {latestTagName}).\n\n" |
| 54 | + + "Click here to download the new version, which can be installed over the top of your existing installation", |
| 55 | + Icon = FontAwesome.Solid.Download, |
| 56 | + Activated = () => |
| 57 | + { |
| 58 | + host.OpenUrlExternally(url); |
| 59 | + return true; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + catch |
| 67 | + { |
| 68 | + // we shouldn't crash on a web failure. or any failure for the matter. |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private bool tryGetBestUrl(GitHubRelease release, [NotNullWhen(true)] out string? url) |
| 76 | + { |
| 77 | + url = null; |
| 78 | + GitHubAsset? bestAsset = null; |
| 79 | + |
| 80 | + switch (RuntimeInfo.OS) |
| 81 | + { |
| 82 | + case RuntimeInfo.Platform.iOS: |
| 83 | + if (release.Assets?.Exists(f => f.Name.EndsWith(".ipa", StringComparison.Ordinal)) == true) |
| 84 | + // iOS releases are available via testflight. this link seems to work well enough for now. |
| 85 | + // see https://stackoverflow.com/a/32960501 |
| 86 | + url = "itms-beta://beta.itunes.apple.com/v1/app/1447765923"; |
| 87 | + |
| 88 | + break; |
| 89 | + |
| 90 | + case RuntimeInfo.Platform.Android: |
| 91 | + if (release.Assets?.Exists(f => f.Name.EndsWith(".apk", StringComparison.Ordinal)) == true) |
| 92 | + // on our testing device using the .apk URL causes the download to magically disappear. |
| 93 | + url = release.HtmlUrl; |
| 94 | + |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + url ??= bestAsset?.BrowserDownloadUrl; |
| 99 | + return url != null; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments