Skip to content

Commit

Permalink
feat: Use Xamarin.Google.Android.Play.App.Update.Ktx(flexible updates…
Browse files Browse the repository at this point in the history
… not supported)
  • Loading branch information
HavenDV committed Mar 7, 2024
1 parent f5dd92a commit 9b544d3
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 103 deletions.
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.AppUpdateInfo.AddOnSuccessListener(Internal.Handler.AppUpdateSuccessListener!);
FakeAppUpdateManager.GetAppUpdateInfo().AddOnSuccessListener(Internal.Handler.AppUpdateSuccessListener!);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>1.0.4</Version>
<Version>1.1.0</Version>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Oscore</Authors>
Expand Down
11 changes: 11 additions & 0 deletions src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public class AndroidInAppUpdatesOptions
/// </summary>
public bool UseFakeAppUpdateManager { get; set; }

/// <summary>
/// By default, the Android system does not allow the automatic deletion of downloaded asset packs when the app is updated. <br/>
/// The default setting (false) is primarily chosen to prevent potential data loss. <br/>
/// Android strives to balance efficient storage management with the risk of inadvertently removing assets that might still be needed by the application. <br/>
/// By not automatically deleting asset packs upon an app update, the system errs on the side of caution—preserving any downloaded content that might not
/// necessarily be included in the updated version of the app but could still be important for its functionality or user data continuity. <br/>
/// This approach allows developers to manage their app’s assets more deliberately and ensures that users don’t lose access to critical resources
/// due to an automatic cleanup process. <br/>
/// </summary>
public bool AllowAssetPackDeletion { get; set; }

/// <summary>
/// This value is used to differentiate between multiple update or request processes within your app. <br/>
/// To avoid intersection with other libraries or request codes, choose a unique value within your application context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'" Label="Android In-app Updates">
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Jvm" Version="1.4.0.1" />
<!-- Now there issue with flexible updates: https://github.com/PatGet/XamarinPlayCoreUpdater/issues/17 -->
<!-- Waiting this PR: https://github.com/PatGet/XamarinPlayCoreUpdater/pull/20 -->
<!-- <PackageReference Include="Xamarin.Google.Android.Play.App.Update" Version="2.1.0.4" /> -->
<PackageReference Include="Xamarin.Google.Android.Play.Core" Version="1.10.3.9" />
<PackageReference Include="Xamarin.Google.Android.Play.App.Update.Ktx" Version="2.1.0.5" />
</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.Install.Model;
using Xamarin.Google.Android.Play.Core.Tasks;
using Activity = Android.App.Activity;
using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;

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

public void OnSuccess(Java.Lang.Object p0)
public void OnSuccess(Java.Lang.Object result)
{
if (p0 is not AppUpdateInfo info)
if (result is not AppUpdateInfo info)
{
return;
}

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(
info,
AppUpdateType.Immediate,
activity,
AppUpdateOptions
.NewBuilder(AppUpdateType.Immediate)
.SetAllowAssetPackDeletion(Handler.Options.AllowAssetPackDeletion)
.Build(),
updateRequest);
break;
}

case UpdateAvailability.UpdateAvailable or
UpdateAvailability.DeveloperTriggeredUpdateInProgress
when isFlexibleUpdatesAllowed:
{
InstallStateUpdatedListener ??= new InstallStateUpdatedListener();
appUpdateManager.RegisterListener(InstallStateUpdatedListener);

_ = appUpdateManager.StartUpdateFlowForResult(
info,
AppUpdateType.Flexible,
activity,
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.AppUpdateInfo.AddOnSuccessListener(AppUpdateSuccessListener);
AppUpdateManager.GetAppUpdateInfo().AddOnSuccessListener(AppUpdateSuccessListener);
}

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
using Xamarin.Google.Android.Play.Core.Install;
using Xamarin.Google.Android.Play.Core.Install.Model;
using Object = Java.Lang.Object;

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

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

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.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}");
// }
// }
// }
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.Install.Model;
using Xamarin.Google.Android.Play.Core.Tasks;
using Xamarin.Google.Android.Play.Core.AppUpdate.Install.Model;
using Activity = Android.App.Activity;

// ReSharper disable once CheckNamespace
namespace Maui.Android.InAppUpdates.Internal;
Expand All @@ -22,9 +22,9 @@ public class ResumeSuccessListener(
int updateRequest)
: Java.Lang.Object, IOnSuccessListener
{
public void OnSuccess(Java.Lang.Object p0)
public void OnSuccess(Java.Lang.Object result)
{
if (p0 is not AppUpdateInfo info)
if (result is not AppUpdateInfo info)
{
return;
}
Expand All @@ -41,8 +41,11 @@ public void OnSuccess(Java.Lang.Object p0)
// If an in-app update is already running, resume the update.
_ = appUpdateManager.StartUpdateFlowForResult(
info,
AppUpdateType.Immediate,
activity,
AppUpdateOptions
.NewBuilder(AppUpdateType.Immediate)
.SetAllowAssetPackDeletion(Handler.Options.AllowAssetPackDeletion)
.Build(),
updateRequest);
}
}
Expand Down

0 comments on commit 9b544d3

Please sign in to comment.