Skip to content

Commit

Permalink
fix: Ensure protocol activation is handled
Browse files Browse the repository at this point in the history
- Fixes #5076. In case the application activity is in SingleTask mode, OnNewIntent is called to set the intent. This occurs after Activity OnStarted, so we need to check whether the intent should be handled (by OnActivated for example)
  • Loading branch information
MartinZikmund committed Feb 1, 2021
1 parent 8c90b05 commit 5ab623d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/Uno.UI/UI/Xaml/ApplicationActivity.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
this.Intent = intent;
// In case this activity is in SingleTask mode, we try to handle
// the intent (for protocol activation scenarios).
(Application as NativeApplication)?.TryHandleIntent(intent);
}

/// <summary>
Expand Down
53 changes: 30 additions & 23 deletions src/Uno.UI/UI/Xaml/NativeApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,7 @@ private void OnActivityStarted(Activity activity)
{
_app.InitializationCompleted();

var handled = false;
if (_lastHandledIntent != activity.Intent)
{
_lastHandledIntent = activity.Intent;
if (activity.Intent?.Extras?.ContainsKey(JumpListItem.ArgumentsExtraKey) == true)
{
_app.OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, activity.Intent.GetStringExtra(JumpListItem.ArgumentsExtraKey)));
handled = true;
}
else if (activity.Intent.Data != null)
{
if (Uri.TryCreate(activity.Intent.Data.ToString(), UriKind.Absolute, out var uri))
{
_app.OnActivated(new ProtocolActivatedEventArgs(uri, _isRunning ? ApplicationExecutionState.Running : ApplicationExecutionState.NotRunning));
handled = true;
}
else
{
// log error and fall back to normal launch
this.Log().LogError($"Activation URI {activity.Intent.Data} could not be parsed");
}
}
}
var handled = TryHandleIntent(activity.Intent);

// default to normal launch
if (!handled && !_isRunning)
Expand All @@ -82,6 +60,35 @@ private void OnActivityStarted(Activity activity)
}
}

internal bool TryHandleIntent(Intent intent)
{
var handled = false;
if (_lastHandledIntent != intent)
{
_lastHandledIntent = intent;
if (intent?.Extras?.ContainsKey(JumpListItem.ArgumentsExtraKey) == true)
{
_app.OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, intent.GetStringExtra(JumpListItem.ArgumentsExtraKey)));
handled = true;
}
else if (intent.Data != null)
{
if (Uri.TryCreate(intent.Data.ToString(), UriKind.Absolute, out var uri))
{
_app.OnActivated(new ProtocolActivatedEventArgs(uri, _isRunning ? ApplicationExecutionState.Running : ApplicationExecutionState.NotRunning));
handled = true;
}
else
{
// log error and fall back to normal launch
this.Log().LogError($"Activation URI {intent.Data} could not be parsed");
}
}
}

return handled;
}

/// <summary>
/// This method is used by UI Test frameworks to get
/// the Xamarin compatible name for a control in Java.
Expand Down

0 comments on commit 5ab623d

Please sign in to comment.