Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding flexible update support #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private async Task Downloading()

private static void AddOnSuccessListener()
{
FakeAppUpdateManager.GetAppUpdateInfo().AddOnSuccessListener(Internal.Handler.AppUpdateSuccessListener!);
FakeAppUpdateManager.AppUpdateInfo.AddOnSuccessListener(Internal.Handler.AppUpdateSuccessListener!);
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'" Label="Android In-app Updates">
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
<PackageReference Include="Xamarin.Google.Android.Play.App.Update.Ktx" Version="2.1.0.5" />
<PackageReference Include="Xamarin.Google.Android.Play.Core" Version="1.10.3.12" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('Xamarin.iOS')) != true AND $(TargetFramework.StartsWith('net8.0-ios')) != true AND $(TargetFramework.StartsWith('net8.0-maccatalyst')) != true ">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Android.App;
using Android.Gms.Tasks;
using Xamarin.Google.Android.Play.Core.AppUpdate;
using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;
using Xamarin.Google.Android.Play.Core.Install.Model;
using Xamarin.Google.Android.Play.Core.Tasks;
using Activity = Android.App.Activity;

// ReSharper disable once CheckNamespace
namespace Maui.Android.InAppUpdates.Internal;
Expand All @@ -12,7 +12,7 @@ public class AppUpdateSuccessListener(
int updateRequest)
: Java.Lang.Object, IOnSuccessListener
{
//public InstallStateUpdatedListener? InstallStateUpdatedListener { get; private set; }
private InstallStateUpdatedListener? InstallStateUpdatedListener { get; set; }

public void OnSuccess(Java.Lang.Object result)
{
Expand All @@ -24,14 +24,14 @@ public void OnSuccess(Java.Lang.Object result)
Handler.Options.DebugAction($"AVAILABLE VERSION CODE {info.AvailableVersionCode()}");

var updateAvailability = info.UpdateAvailability();
//var updatePriority = info.UpdatePriority();
var updatePriority = info.UpdatePriority();
var isImmediateUpdatesAllowed = info.IsUpdateTypeAllowed(AppUpdateType.Immediate);
//var isFlexibleUpdatesAllowed = info.IsUpdateTypeAllowed(AppUpdateType.Flexible);
var isFlexibleUpdatesAllowed = info.IsUpdateTypeAllowed(AppUpdateType.Flexible);
switch (updateAvailability)
{
case UpdateAvailability.UpdateAvailable or
UpdateAvailability.DeveloperTriggeredUpdateInProgress
when // updatePriority >= Handler.Options.ImmediateUpdatePriority &&
when updatePriority >= Handler.Options.ImmediateUpdatePriority &&
isImmediateUpdatesAllowed:
{
_ = appUpdateManager.StartUpdateFlowForResult(
Expand All @@ -45,23 +45,23 @@ public void OnSuccess(Java.Lang.Object result)
break;
}

// case UpdateAvailability.UpdateAvailable or
// UpdateAvailability.DeveloperTriggeredUpdateInProgress
// when isFlexibleUpdatesAllowed:
// {
// InstallStateUpdatedListener ??= new InstallStateUpdatedListener();
// appUpdateManager.RegisterListener(InstallStateUpdatedListener);
//
// _ = appUpdateManager.StartUpdateFlowForResult(
// info,
// activity,
// AppUpdateOptions
// .NewBuilder(AppUpdateType.Flexible)
// .SetAllowAssetPackDeletion(Handler.Options.AllowAssetPackDeletion)
// .Build(),
// updateRequest);
// break;
// }
case UpdateAvailability.UpdateAvailable or
UpdateAvailability.DeveloperTriggeredUpdateInProgress
when isFlexibleUpdatesAllowed:
{
InstallStateUpdatedListener ??= new InstallStateUpdatedListener();
appUpdateManager.RegisterListener(InstallStateUpdatedListener);

_ = appUpdateManager.StartUpdateFlowForResult(
info,
activity,
AppUpdateOptions
.NewBuilder(AppUpdateType.Flexible)
.SetAllowAssetPackDeletion(Handler.Options.AllowAssetPackDeletion)
.Build(),
updateRequest);
break;
}

case UpdateAvailability.UpdateNotAvailable:
case UpdateAvailability.Unknown:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Android.Content;
using Android.Runtime;
using Xamarin.Google.Android.Play.Core.AppUpdate;
using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;
using Xamarin.Google.Android.Play.Core.AppUpdate.Testing;
using Xamarin.Google.Android.Play.Core.Install.Model;
using Bundle = Android.OS.Bundle;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -44,7 +44,7 @@ public static void HandleCreate(Activity activity, Bundle? savedInstanceState)
appUpdateManager: AppUpdateManager,
activity: activity,
updateRequest: Options.RequestCode);
AppUpdateManager.GetAppUpdateInfo().AddOnSuccessListener(AppUpdateSuccessListener);
AppUpdateManager.AppUpdateInfo.AddOnSuccessListener(AppUpdateSuccessListener);
}

/// <summary>
Expand All @@ -62,7 +62,7 @@ public static void HandleResume(Activity activity)
appUpdateManager: AppUpdateManager,
activity: activity,
updateRequest: Options.RequestCode);
AppUpdateManager.GetAppUpdateInfo().AddOnSuccessListener(ResumeSuccessListener);
AppUpdateManager.AppUpdateInfo.AddOnSuccessListener(ResumeSuccessListener);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
// using Xamarin.Google.Android.Play.Core.AppUpdate.Install;
// using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;
//
// // ReSharper disable once CheckNamespace
// namespace Maui.Android.InAppUpdates.Internal;
//
// /// <summary>
// /// Listener to track request state updates.
// /// </summary>
// public class InstallStateUpdatedListener
// : Java.Lang.Object, IInstallStateUpdatedListener
// {
// /// <summary>
// /// This method will be triggered when the app update status is changed.
// /// </summary>
// /// <param name="p0"></param>
// /// <exception cref="ArgumentNullException"></exception>
// public void OnStateUpdate(Java.Lang.Object p0)
// {
// var state = p0 as InstallState ?? throw new ArgumentNullException(nameof(p0));
//
// try
// {
// var installStatus = state.InstallStatus();
// switch (installStatus)
// {
// case InstallStatus.Unknown:
// case InstallStatus.Pending:
// case InstallStatus.Installing:
// case InstallStatus.Installed:
// case InstallStatus.Canceled:
// break;
//
// case InstallStatus.Downloading
// when Handler.Options.ShowDownload:
// {
// var bytesDownloaded = state.BytesDownloaded();
// var totalBytesToDownload = state.TotalBytesToDownload() + 1;
// var percents = Math.Round(
// 100.0 * bytesDownloaded / totalBytesToDownload);
//
// Handler.Options.DownloadingAction(percents);
// break;
// }
//
// case InstallStatus.Downloaded:
// Handler.Options.CompleteUpdateAction();
// break;
//
// case InstallStatus.Failed:
// Handler.Options.DownloadFailedAction();
// break;
// }
// }
// catch (Exception e)
// {
// Handler.Options.DebugAction($"Error occurred during in app update status change: {e}");
// }
// }
// }
using Xamarin.Google.Android.Play.Core.Install;
using Xamarin.Google.Android.Play.Core.Install.Model;

// ReSharper disable once CheckNamespace
namespace Maui.Android.InAppUpdates.Internal;

/// <summary>
/// Listener to track request state updates.
/// </summary>
public class InstallStateUpdatedListener
: Java.Lang.Object, IInstallStateUpdatedListener
{
/// <summary>
/// This method will be triggered when the app update status is changed.
/// </summary>
/// <param name="p0"></param>
/// <exception cref="ArgumentNullException"></exception>
public void OnStateUpdate(InstallState state)
{
// var state = p0 as InstallState ?? throw new ArgumentNullException(nameof(p0));

try
{
var installStatus = state.InstallStatus();
switch (installStatus)
{
case InstallStatus.Unknown:
case InstallStatus.Pending:
case InstallStatus.Installing:
case InstallStatus.Installed:
case InstallStatus.Canceled:
break;

case InstallStatus.Downloading
when Handler.Options.ShowDownload:
{
var bytesDownloaded = state.BytesDownloaded();
var totalBytesToDownload = state.TotalBytesToDownload() + 1;
var percents = Math.Round(
100.0 * bytesDownloaded / totalBytesToDownload);

Handler.Options.DownloadingAction(percents);
break;
}

case InstallStatus.Downloaded:
Handler.Options.CompleteUpdateAction();
break;

case InstallStatus.Failed:
Handler.Options.DownloadFailedAction();
break;
}
}
catch (Exception e)
{
Handler.Options.DebugAction($"Error occurred during in app update status change: {e}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Android.Gms.Tasks;
using Android.App;
using Xamarin.Google.Android.Play.Core.AppUpdate;
using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;
using Activity = Android.App.Activity;
using Xamarin.Google.Android.Play.Core.Install.Model;
using Xamarin.Google.Android.Play.Core.Tasks;

// ReSharper disable once CheckNamespace
namespace Maui.Android.InAppUpdates.Internal;
Expand Down