InstallStateUpdatedListener.onStateUpdate method mapping#20
Open
Alex1ch wants to merge 1 commit intoPatGet:masterfrom
Open
InstallStateUpdatedListener.onStateUpdate method mapping#20Alex1ch wants to merge 1 commit intoPatGet:masterfrom
Alex1ch wants to merge 1 commit intoPatGet:masterfrom
Conversation
Collaborator
|
@Alex1ch how can I test this to see if it works with the new changes? |
Author
|
@saamerm you can test sample from the issue #17, but you have to change parameter type: Or I can modify PlayCoreUpdateTest.Android later. |
Collaborator
|
Once these changes are made in your local environment, are you able to support flexible updates in your app? @Alex1ch |
|
I've tested the version from this PR and flexible updates seems to work fine. Main activity example: [Activity(MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : FormsAppCompatActivity
{
private ILogger<MainActivity> logger;
private IMessageBoxService messageBoxService;
private const int _Request_Update = 4711;
protected override void OnCreate(Bundle savedInstanceState)
{
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
base.OnCreate(savedInstanceState);
Popup.Init(this);
Platform.Init(this, savedInstanceState);
Forms.Init(this, savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
CachedImageRenderer.Init(true);
var application = new App.App();
LoadApplication(application);
var loggerFactory = application.DependencyInjectionService.Resolve<ILoggerFactory>();
logger = loggerFactory.CreateLogger<MainActivity>();
messageBoxService = application.DependencyInjectionService.Resolve<IMessageBoxService>();
#if !DEBUG
var appUpdateManager = AppUpdateManagerFactory.Create(this);
var appUpdateInfoTask = appUpdateManager.AppUpdateInfo;
appUpdateInfoTask.AddOnSuccessListener(new AppUpdateSuccessListener(appUpdateManager, this, _Request_Update, Intent, loggerFactory, messageBoxService));
#endif
}
public override void OnUserInteraction()
{
UserActivityMonitor.ReportActivity();
base.OnUserInteraction();
}
protected override void OnDestroy()
{
_ = BeforeApplicationExitActions.HandleThreatOfApplicationTerminationAsync();
base.OnDestroy();
}
public override void OnBackPressed()
{
Popup.SendBackPressed(base.OnBackPressed);
}
//protected override void OnResume()
//{
// var appUpdateManager = AppUpdateManagerFactory.Create(this);
// appUpdateManager.AppUpdateInfo.AddOnSuccessListener(new AppUpdateSuccessListener(appUpdateManager, this, _Request_Update, Intent));
// base.OnResume();
//}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (_Request_Update.Equals(requestCode))
{
HandleFlexibleUpdateActivityResult(resultCode);
}
}
private void HandleFlexibleUpdateActivityResult(Result resultCode)
{
switch (resultCode) // The switch block will be triggered only with flexible update since it returns the install result codes
{
case Result.Ok:
messageBoxService.ShowSuccessToast("Application update started");
break;
case Result.Canceled:
messageBoxService.ShowToast("Application update cancelled");
break;
case (Result) ActivityResult.ResultInAppUpdateFailed:
messageBoxService.ShowErrorToast("Application update failed");
break;
}
}
}
public class AppUpdateSuccessListener : Object, IOnSuccessListener
{
private readonly IAppUpdateManager appUpdateManager;
private readonly Activity mainActivity;
private readonly int updateRequest;
private readonly Intent intent;
private readonly ILoggerFactory loggerFactory;
private readonly IMessageBoxService messageBoxService;
private readonly ILogger<AppUpdateSuccessListener> logger;
public AppUpdateSuccessListener(IAppUpdateManager appUpdateManager, Activity mainActivity, int updateRequest, Intent intent, ILoggerFactory loggerFactory, IMessageBoxService messageBoxService)
{
this.appUpdateManager = appUpdateManager;
this.mainActivity = mainActivity;
this.updateRequest = updateRequest;
this.intent = intent;
this.loggerFactory = loggerFactory;
logger = loggerFactory.CreateLogger<AppUpdateSuccessListener>();
this.messageBoxService = messageBoxService;
}
public void OnSuccess(Object p0)
{
try
{
if (p0 is not AppUpdateInfo info)
{
return;
}
var availability = info.UpdateAvailability();
if (availability.Equals(UpdateAvailability.UpdateAvailable) ||
availability.Equals(UpdateAvailability.DeveloperTriggeredUpdateInProgress))
{
var availableVersionCode = info.AvailableVersionCode();
var currentVersionCode = int.Parse(AppInfo.BuildString);
var versionMismatchType = VersionLogic.GetVersionMismatchType(currentVersionCode, availableVersionCode);
if (versionMismatchType == VersionMismatchType.None)
{
return;
}
if (versionMismatchType == VersionMismatchType.Minor && info.IsUpdateTypeAllowed(AppUpdateType.Flexible))
{
appUpdateManager.RegisterListener(new InstallStateUpdatedListener(mainActivity, appUpdateManager, loggerFactory, messageBoxService));
appUpdateManager.StartUpdateFlowForResult(info, AppUpdateType.Flexible, mainActivity, updateRequest);
return;
}
if (versionMismatchType == VersionMismatchType.Minor && info.IsUpdateTypeAllowed(AppUpdateType.Immediate))
{
appUpdateManager.StartUpdateFlowForResult(info, AppUpdateType.Immediate, mainActivity, updateRequest);
return;
}
messageBoxService.ShowErrorToast($"In app update ({versionMismatchType}) is not allowed.");
}
}
catch (Exception e)
{
logger.LogError(e, "Failed to perform in app update.");
}
}
}
public class InstallStateUpdatedListener : Object, IInstallStateUpdatedListener
{
private readonly Activity activity;
private readonly IAppUpdateManager appUpdateManager;
private readonly IMessageBoxService messageBoxService;
private readonly ILogger<InstallStateUpdatedListener> logger;
public InstallStateUpdatedListener(Activity activity, IAppUpdateManager appUpdateManager, ILoggerFactory loggerFactory, IMessageBoxService messageBoxService)
{
this.activity = activity;
this.appUpdateManager = appUpdateManager;
logger = loggerFactory.CreateLogger<InstallStateUpdatedListener>();
this.messageBoxService = messageBoxService;
}
public void OnStateUpdate(InstallState installState)
{
try
{
var installStatus = installState.InstallStatus();
if (installStatus == InstallStatus.Downloading)
{
var completed = Math.Round((double) installState.BytesDownloaded() / installState.TotalBytesToDownload() * 100);
messageBoxService.ShowSuccessToast($"Downloaded {completed}%", TimeSpan.FromMilliseconds(500));
}
else if (installStatus == InstallStatus.Downloaded)
{
var dialog = new AlertDialog.Builder(activity);
var alert = dialog.Create();
alert?.SetTitle("Download completed");
alert?.SetMessage("Update is ready to be installed.");
alert?.SetButton((int) DialogButtonType.Positive, "Perform update", (o, args) => { appUpdateManager.CompleteUpdate(); });
alert?.SetCancelable(false);
alert?.Show();
}
else if (installStatus == InstallStatus.Failed)
{
messageBoxService.ShowErrorToast("Update download failed.");
}
}
catch (Exception e)
{
logger.LogError(e, "Error occurred during in app update status change.");
}
}
} |
saamerm
approved these changes
Oct 10, 2022
|
Would be great to see this get merged! |
|
i just downloaded the NuGet and i'm still facing this issue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've got an issue with implementing IInstallStateUpdatedListener interface. Java generated mappings for my class gave me an error that it didn't implement onStateUpdate(IstallState), because IInstallStateUpdatedListener maps osStateUpdate(Object).
So, to solve this problem I propose to remove IStateUpdatedListener from IInstallStateUpdatedListener and map onStateUpdate(IstallState) by hand.