From 9b544d361ec5e675f2875ac84a394bd363340027 Mon Sep 17 00:00:00 2001 From: HavenDV Date: Fri, 8 Mar 2024 02:21:22 +0400 Subject: [PATCH] feat: Use Xamarin.Google.Android.Play.App.Update.Ktx(flexible updates not supported) --- sample/MainPage.xaml.cs | 2 +- src/libs/Directory.Build.props | 2 +- .../AndroidInAppUpdatesOptions.cs | 11 ++ .../Maui.Android.InAppUpdates.csproj | 8 +- .../Android/AppUpdateSuccessListener.cs | 54 ++++---- .../Platforms/Android/Handler.cs | 6 +- .../Android/InstallStateUpdatedListener.cs | 121 +++++++++--------- .../Android/ResumeSuccessListener.cs | 15 ++- 8 files changed, 116 insertions(+), 103 deletions(-) diff --git a/sample/MainPage.xaml.cs b/sample/MainPage.xaml.cs index e09ee91..7214aea 100644 --- a/sample/MainPage.xaml.cs +++ b/sample/MainPage.xaml.cs @@ -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 } diff --git a/src/libs/Directory.Build.props b/src/libs/Directory.Build.props index d190507..02d76a8 100755 --- a/src/libs/Directory.Build.props +++ b/src/libs/Directory.Build.props @@ -9,7 +9,7 @@ - 1.0.4 + 1.1.0 true true Oscore diff --git a/src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs b/src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs index e262cdb..a3a5a0a 100644 --- a/src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs +++ b/src/libs/Maui.Android.InAppUpdates/AndroidInAppUpdatesOptions.cs @@ -27,6 +27,17 @@ public class AndroidInAppUpdatesOptions /// public bool UseFakeAppUpdateManager { get; set; } + /// + /// By default, the Android system does not allow the automatic deletion of downloaded asset packs when the app is updated.
+ /// The default setting (false) is primarily chosen to prevent potential data loss.
+ /// Android strives to balance efficient storage management with the risk of inadvertently removing assets that might still be needed by the application.
+ /// 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.
+ /// 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.
+ ///
+ public bool AllowAssetPackDeletion { get; set; } + /// /// This value is used to differentiate between multiple update or request processes within your app.
/// To avoid intersection with other libraries or request codes, choose a unique value within your application context. diff --git a/src/libs/Maui.Android.InAppUpdates/Maui.Android.InAppUpdates.csproj b/src/libs/Maui.Android.InAppUpdates/Maui.Android.InAppUpdates.csproj index d87dff4..a4d9bf2 100644 --- a/src/libs/Maui.Android.InAppUpdates/Maui.Android.InAppUpdates.csproj +++ b/src/libs/Maui.Android.InAppUpdates/Maui.Android.InAppUpdates.csproj @@ -30,15 +30,9 @@ - - - - - - - + diff --git a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/AppUpdateSuccessListener.cs b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/AppUpdateSuccessListener.cs index aad9003..2b00c32 100644 --- a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/AppUpdateSuccessListener.cs +++ b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/AppUpdateSuccessListener.cs @@ -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; @@ -12,11 +12,11 @@ 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; } @@ -24,38 +24,44 @@ public void OnSuccess(Java.Lang.Object p0) 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: diff --git a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/Handler.cs b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/Handler.cs index 889acec..c645c4a 100644 --- a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/Handler.cs +++ b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/Handler.cs @@ -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 @@ -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); } /// @@ -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); } /// diff --git a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/InstallStateUpdatedListener.cs b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/InstallStateUpdatedListener.cs index 6bf0bc8..de70059 100644 --- a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/InstallStateUpdatedListener.cs +++ b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/InstallStateUpdatedListener.cs @@ -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; - -/// -/// Listener to track request state updates. -/// -public class InstallStateUpdatedListener - : Object, IInstallStateUpdatedListener -{ - /// - /// This method will be triggered when the app update status is changed. - /// - /// - /// - 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}"); - } - } -} \ No newline at end of file +// 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; +// +// /// +// /// Listener to track request state updates. +// /// +// public class InstallStateUpdatedListener +// : Java.Lang.Object, IInstallStateUpdatedListener +// { +// /// +// /// This method will be triggered when the app update status is changed. +// /// +// /// +// /// +// 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}"); +// } +// } +// } \ No newline at end of file diff --git a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/ResumeSuccessListener.cs b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/ResumeSuccessListener.cs index 1387e87..e797eb9 100644 --- a/src/libs/Maui.Android.InAppUpdates/Platforms/Android/ResumeSuccessListener.cs +++ b/src/libs/Maui.Android.InAppUpdates/Platforms/Android/ResumeSuccessListener.cs @@ -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; @@ -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; } @@ -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); } }