Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
GH-1180 ; Don't launch new task on custom tabs. (#1183)
Browse files Browse the repository at this point in the history
* Don't launch new task on custom tabs.

* Android Set custom tabs flags for appcontext

* Added BrowserLaunchFlags

This can be a place to expose some platform specific isms.

For now on Android we have a `PrefereAdjacent`  and on iOS a `PreferModal`.

* Added present sheet options, sample

* Use flags in sample

* Cleanup API a little bit and add docs

Co-authored-by: Jonathan Dick <jodick@microsoft.com>
Co-authored-by: Jonathan Dick <jondick@gmail.com>
  • Loading branch information
3 people authored Mar 26, 2020
1 parent 351d96d commit 2bcb47d
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 26 deletions.
19 changes: 19 additions & 0 deletions Samples/Samples/View/BrowserPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@
ItemsSource="{Binding AllColors}"
SelectedIndex="{Binding ControlColor, Mode=TwoWay}" />

<Label Text="Flags:" />

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Label Text="Launch Adjacent (Android)" Grid.Column="0" Grid.Row="0" />
<Switch IsToggled="{Binding LaunchAdjacent, Mode=TwoWay}" Grid.Column="1" Grid.Row="0" />

<Label Text="Present as Form Sheet (iOS)" Grid.Column="0" Grid.Row="1" />
<Switch IsToggled="{Binding PresentAsFormSheet, Mode=TwoWay}" Grid.Column="1" Grid.Row="1" />

<Label Text="Present as Page Sheet (iOS)" Grid.Column="0" Grid.Row="2" />
<Switch IsToggled="{Binding PresentAsPageSheet, Mode=TwoWay}" Grid.Column="1" Grid.Row="2" />

</Grid>

<Label Text="{Binding BrowserStatus}" />
</StackLayout>
</ScrollView>
Expand Down
32 changes: 31 additions & 1 deletion Samples/Samples/ViewModel/BrowserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class BrowserViewModel : BaseViewModel
int browserTitleType = (int)BrowserTitleMode.Default;
int controlColor = 0;
int toolbarColor = 0;
bool presentAsFormSheet = false;
bool presentAsPageSheet = false;
bool launchAdjacent = false;

Dictionary<string, Color> colorDictionary;

Expand Down Expand Up @@ -89,6 +92,24 @@ public int ControlColor
set => SetProperty(ref controlColor, value);
}

public bool PresentAsFormSheet
{
get => presentAsFormSheet;
set => SetProperty(ref presentAsFormSheet, value);
}

public bool PresentAsPageSheet
{
get => presentAsPageSheet;
set => SetProperty(ref presentAsPageSheet, value);
}

public bool LaunchAdjacent
{
get => launchAdjacent;
set => SetProperty(ref launchAdjacent, value);
}

async void OpenUri()
{
if (IsBusy)
Expand All @@ -97,12 +118,21 @@ async void OpenUri()
IsBusy = true;
try
{
var flags = BrowserLaunchFlags.None;
if (PresentAsPageSheet)
flags |= BrowserLaunchFlags.PresentAsPageSheet;
if (PresentAsFormSheet)
flags |= BrowserLaunchFlags.PresentAsFormSheet;
if (LaunchAdjacent)
flags |= BrowserLaunchFlags.LaunchAdjacent;

await Browser.OpenAsync(uri, new BrowserLaunchOptions
{
LaunchMode = (BrowserLaunchMode)BrowserType,
TitleMode = (BrowserTitleMode)BrowserTitleType,
PreferredToolbarColor = GetColor(ToolbarColor),
PreferredControlColor = GetColor(ControlColor)
PreferredControlColor = GetColor(ControlColor),
Flags = flags
});
}
catch (Exception e)
Expand Down
38 changes: 27 additions & 11 deletions Xamarin.Essentials/Browser/Browser.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,40 @@ static Task<bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
tabsBuilder.SetShowTitle(options.TitleMode == BrowserTitleMode.Show);

var tabsIntent = tabsBuilder.Build();
var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
ActivityFlags? tabsFlags = null;

Context context = Platform.GetCurrentActivity(false);

if (context == null)
{
context = Platform.AppContext;

// If using ApplicationContext we need to set ClearTop/NewTask (See #225)
tabsFlags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
}

#if __ANDROID_24__
if (Platform.HasApiLevelN)
flags |= ActivityFlags.LaunchAdjacent;
if (Platform.HasApiLevelN && options.HasFlag(BrowserLaunchFlags.LaunchAdjacent))
{
if (tabsFlags.HasValue)
tabsFlags |= ActivityFlags.LaunchAdjacent | ActivityFlags.NewTask;
else
tabsFlags = ActivityFlags.LaunchAdjacent | ActivityFlags.NewTask;
}
#endif
tabsIntent.Intent.SetFlags(flags);

#if __ANDROID_25__
tabsIntent.LaunchUrl(Platform.AppContext, nativeUri);
#else
tabsIntent.LaunchUrl(Platform.GetCurrentActivity(true), nativeUri);
#endif
// Check if there's flags specified to use
if (tabsFlags.HasValue)
tabsIntent.Intent.SetFlags(tabsFlags.Value);

tabsIntent.LaunchUrl(context, nativeUri);

break;
case BrowserLaunchMode.External:
var intent = new Intent(Intent.ActionView, nativeUri);
flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
#if __ANDROID_24__
if (Platform.HasApiLevelN)
if (Platform.HasApiLevelN && options.HasFlag(BrowserLaunchFlags.LaunchAdjacent))
flags |= ActivityFlags.LaunchAdjacent;
#endif
intent.SetFlags(flags);
Expand Down
6 changes: 6 additions & 0 deletions Xamarin.Essentials/Browser/Browser.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ static async Task<bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
{
sfViewController.PopoverPresentationController.SourceView = vc.View;
}

if (options.HasFlag(BrowserLaunchFlags.PresentAsFormSheet))
sfViewController.ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
else if (options.HasFlag(BrowserLaunchFlags.PresentAsPageSheet))
sfViewController.ModalPresentationStyle = UIModalPresentationStyle.PageSheet;

await vc.PresentViewControllerAsync(sfViewController, true);
break;
case BrowserLaunchMode.External:
Expand Down
16 changes: 16 additions & 0 deletions Xamarin.Essentials/Browser/BrowserLaunchOptions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Drawing;
#endif

using System;

namespace Xamarin.Essentials
{
public class BrowserLaunchOptions
Expand All @@ -15,5 +17,19 @@ public class BrowserLaunchOptions
public BrowserLaunchMode LaunchMode { get; set; } = BrowserLaunchMode.SystemPreferred;

public BrowserTitleMode TitleMode { get; set; } = BrowserTitleMode.Default;

public BrowserLaunchFlags Flags { get; set; } = BrowserLaunchFlags.None;

internal bool HasFlag(BrowserLaunchFlags flag)
=> Flags.HasFlag(flag);
}

[Flags]
public enum BrowserLaunchFlags
{
None = 0,
LaunchAdjacent = 1,
PresentAsPageSheet = 2,
PresentAsFormSheet = 4
}
}
9 changes: 8 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -557,6 +564,7 @@
</Type>
<Type Name="Xamarin.Essentials.Permissions/Battery" Id="T:Xamarin.Essentials.Permissions.Battery">
<Member Id="M:Xamarin.Essentials.Permissions.Battery.#ctor" />
<Member Id="M:Xamarin.Essentials.Permissions.Battery.CheckStatusAsync" />
<Member Id="P:Xamarin.Essentials.Permissions.Battery.RequiredPermissions" />
</Type>
<Type Name="Xamarin.Essentials.Permissions/CalendarRead" Id="T:Xamarin.Essentials.Permissions.CalendarRead">
Expand Down Expand Up @@ -875,7 +883,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
8 changes: 7 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -871,7 +878,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
8 changes: 7 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-tvos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -833,7 +840,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
8 changes: 7 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -832,7 +839,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
8 changes: 7 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-watchos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -842,7 +849,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
8 changes: 7 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchMode)" />
<Member Id="M:Xamarin.Essentials.Browser.OpenAsync(System.Uri,Xamarin.Essentials.BrowserLaunchOptions)" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchFlags" Id="T:Xamarin.Essentials.BrowserLaunchFlags">
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.LaunchAdjacent" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.None" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsFormSheet" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchFlags.PresentAsPageSheet" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchMode" Id="T:Xamarin.Essentials.BrowserLaunchMode">
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.External" />
<Member Id="F:Xamarin.Essentials.BrowserLaunchMode.SystemPreferred" />
</Type>
<Type Name="Xamarin.Essentials.BrowserLaunchOptions" Id="T:Xamarin.Essentials.BrowserLaunchOptions">
<Member Id="M:Xamarin.Essentials.BrowserLaunchOptions.#ctor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.Flags" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.LaunchMode" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredControlColor" />
<Member Id="P:Xamarin.Essentials.BrowserLaunchOptions.PreferredToolbarColor" />
Expand Down Expand Up @@ -806,7 +813,6 @@
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshToken" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.RefreshTokenExpiresIn" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.Timestamp" />
<Member Id="P:Xamarin.Essentials.WebAuthenticatorResult.TokenType" />
</Type>
</Namespace>
</Framework>
Loading

0 comments on commit 2bcb47d

Please sign in to comment.