Skip to content

Commit

Permalink
feat(Android): Provide access to BaseActivity events from SpanningRec…
Browse files Browse the repository at this point in the history
…tsExtension
  • Loading branch information
jeromelaban committed Oct 14, 2021
1 parent 981ab0d commit 23cca70
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 42 deletions.
26 changes: 25 additions & 1 deletion doc/articles/android-activities.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,28 @@
[Understand the Activity Lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle)

## Creating/Using Android Activities
At the root of every Android Uno app, lies a `BaseActivity` class that extends from `Android.Support.V7.App.AppCompatActivity` which is part of the [Android v7 AppCompat Support Library](https://developer.android.com/topic/libraries/support-library/features.html#v7-appcompat). If you ever need to create a new Activity within your app or within Uno you must be sure to extend `BaseActivity` and, if you need to apply a Theme to the activity, ensure that the Theme you set is a `Theme.AppCompat` theme (or descendant).
At the root of every Android Uno app, lies a `BaseActivity` class that extends from `Android.Support.V7.App.AppCompatActivity` which is part of the [Android v7 AppCompat Support Library](https://developer.android.com/topic/libraries/support-library/features.html#v7-appcompat). If you ever need to create a new Activity within your app or within Uno you must be sure to extend `BaseActivity` and, if you need to apply a Theme to the activity, ensure that the Theme you set is a `Theme.AppCompat` theme (or descendant).

## Accessing Android main activity events

Uno Platform provides an API to get access to the events/overrides invoked in the main activity (commonly inheriting from `UI.Xaml.ApplicationActivity`) outside of the activity class.

In order to get access to these events, you can write the following:

```csharp
using Uno.UI.ViewManagement;

App()
{
// ...
ApplicationViewHelper.GetBaseActivityEvents().Create += OnCreateEvent;
// ...
}

private void OnCreateEvent(Android.OS.Bundle savedInstanceState)
{

}
```

Note that some events are raised early during the application lifecyle and may need to be registered from the `App` constructor.
3 changes: 3 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.Toolkit.Wasm.csproj</_AdjustedOutputProjects>
<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.Toolkit.Skia.csproj</_AdjustedOutputProjects>
<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.Toolkit.net6.csproj</_AdjustedOutputProjects>

<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.DualScreen.net6.csproj</_AdjustedOutputProjects>

<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.FluentTheme.Wasm.csproj</_AdjustedOutputProjects>
<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.FluentTheme.Skia.csproj</_AdjustedOutputProjects>
<_AdjustedOutputProjects>$(_AdjustedOutputProjects);Uno.UI.FluentTheme.net6.csproj</_AdjustedOutputProjects>
Expand Down
23 changes: 23 additions & 0 deletions src/SamplesApp/SamplesApp.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Microsoft.Identity.Client;
using Uno.UI.ViewManagement;

namespace SamplesApp.Droid
{
Expand All @@ -25,6 +26,28 @@ namespace SamplesApp.Droid
DataScheme = "uno-samples-test")]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity
{
private bool _onCreateEventInvoked = false;

public MainActivity()
{
ApplicationViewHelper.GetBaseActivityEvents().Create += OnCreateEvent;
}

private void OnCreateEvent(Android.OS.Bundle savedInstanceState)
{
_onCreateEventInvoked = true;
}

protected override void OnStart()
{
if(!_onCreateEventInvoked)
{
throw new InvalidOperationException($"Invalid startup sequence to initialize BaseActivityEvents");
}

base.OnStart();
}

[Export("RunTest")]
public string RunTest(string metadataName) => App.RunTest(metadataName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
<ProjectReference Include="..\..\Uno.UI.RuntimeTests\Uno.UI.RuntimeTests.net6.csproj" />
<ProjectReference Include="..\..\Uno.UI.FluentTheme\Uno.UI.FluentTheme.net6.csproj" />
<ProjectReference Include="..\..\AddIns\Uno.UI.MSAL\Uno.UI.MSAL.net6.csproj" />
<ProjectReference Include="..\..\Uno.UI.DualScreen\Uno.UI.DualScreen.net6.csproj"/>

<!--
<ProjectReference Include="..\..\Uno.UI.DualScreen\Uno.UI.DualScreen.net6.csproj"/>
<ProjectReference Include="..\..\Uno.UI.Maps\Uno.UI.Maps.net6.csproj"/>
<ProjectReference Include="..\..\Uno.UI.RemoteControl\Uno.UI.RemoteControl.net6.csproj"/>
-->
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI.DualScreen/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
using Uno.Foundation.Extensibility;
using Uno.UI.DualScreen;

#if __ANDROID__
[assembly: ApiExtension(typeof(IApplicationViewSpanningRects), typeof(DuoApplicationViewSpanningRects))]
[assembly: ApiExtension(typeof(INativeHingeAngleSensor), typeof(DuoHingeAngleSensor))]
#endif
71 changes: 71 additions & 0 deletions src/Uno.UI.DualScreen/Uno.UI.DualScreen.net6.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<Project Sdk="MSBuild.Sdk.Extras" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>net6.0-android;net6.0-ios</TargetFrameworks>
<TargetFrameworksCI>net6.0-android;net6.0-ios</TargetFrameworksCI>
<AssemblyName>Uno.UI.DualScreen</AssemblyName>

<TargetFrameworks>$(TargetFrameworks);net6.0-maccatalyst;net6.0-macos</TargetFrameworks>
<TargetFrameworksCI>$(TargetFrameworks);net6.0-maccatalyst;net6.0-macos</TargetFrameworksCI>
</PropertyGroup>

<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId Condition="'$(UNO_UWP_BUILD)'!='true'">Uno.WinUI.DualScreen</PackageId>

<NoWarn>$(NoWarn);NU1701</NoWarn>

<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<Deterministic>true</Deterministic>
</PropertyGroup>

<PropertyGroup Condition="'$(UnoTargetFrameworkOverride)'!=''">
<TargetFrameworks>$(UnoTargetFrameworkOverride)</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<Authors>nventive</Authors>
<PackageProjectUrl>https://github.com/unoplatform/uno</PackageProjectUrl>
<PackageIconUrl>https://nv-assets.azurewebsites.net/logos/uno.png</PackageIconUrl>
<RepositoryUrl>https://github.com/unoplatform/uno</RepositoryUrl>
<Description>This package provides the ability for Uno Platform based apps to use dual screen devices such as the Surface Duo or Neo</Description>
<Copyright>Copyright (C) 2015-2020 nventive inc. - all rights reserved</Copyright>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Uno.UI\Uno.UI.net6.csproj" />
<ProjectReference Include="..\Uno.UWP\Uno.net6.csproj" TreatAsPackageReference="false" PrivateAssets="all" />
<ProjectReference Include="..\Uno.Foundation\Uno.Foundation.net6.csproj" TreatAsPackageReference="false" PrivateAssets="all" />
<ProjectReference Include="..\Uno.UI.BindingHelper.Android\Uno.UI.BindingHelper.Android.net6.csproj" Condition="$(IsMonoAndroid)" TreatAsPackageReference="false" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Uno.SourceGenerationTasks" />
<PackageReference Include="Uno.Core" />
<PackageReference Include="Uno.Core.Build" />
</ItemGroup>

<Import Project="..\Uno.CrossTargetting.props" />

<ItemGroup Condition="'$(TargetFramework)'=='net6.0-android'">
<PackageReference Include="Xamarin.DuoSdk" />
</ItemGroup>

<Import Project="..\SourceGenerators\Uno.UI.SourceGenerators\Content\Uno.UI.SourceGenerators.props" />

<Target Name="_UnoDualScreenOverrideNuget" AfterTargets="AfterBuild" DependsOnTargets="BuiltProjectOutputGroup" Condition="'$(UnoNugetOverrideVersion)'!=''">

<PropertyGroup>
<_OverrideTargetFramework>$(TargetFramework)</_OverrideTargetFramework>
<_TargetNugetFolder>$(USERPROFILE)\.nuget\packages\Uno.UI.DualScreen\$(UnoNugetOverrideVersion)\lib\$(_OverrideTargetFramework)</_TargetNugetFolder>
</PropertyGroup>
<ItemGroup>
<_OutputFiles Include="$(TargetDir)**" />
</ItemGroup>
<MakeDir Directories="$(_TargetNugetFolder)" />

<Message Importance="high" Text="OVERRIDING NUGET PACKAGE CACHE: $(_TargetNugetFolder)" />

<Copy SourceFiles="@(_OutputFiles)" DestinationFiles="@(_OutputFiles->'$(_TargetNugetFolder)\%(RecursiveDir)%(Filename)%(Extension)')" />
<Copy SourceFiles="@(_OutputFilesPDB)" DestinationFiles="@(_OutputFilesPDB->'$(_TargetNugetFolder)\%(RecursiveDir)%(Filename).pdb')" />
</Target>
</Project>
45 changes: 45 additions & 0 deletions src/Uno.UI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.UI.FluentTheme.v2.Skia"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.UI.FluentTheme.v2.Wasm", "Uno.UI.FluentTheme.v2\Uno.UI.FluentTheme.v2.Wasm.csproj", "{AD35E340-34DF-4FA9-B545-B9FBB72D3A2E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.UI.DualScreen.net6", "Uno.UI.DualScreen\Uno.UI.DualScreen.net6.csproj", "{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
SamplesApp\SamplesApp.UnitTests.Shared\SamplesApp.UnitTests.Shared.projitems*{04b1b5eb-d42e-47de-ada0-eb863e5574fd}*SharedItemsImports = 13
Expand Down Expand Up @@ -4845,6 +4847,48 @@ Global
{AD35E340-34DF-4FA9-B545-B9FBB72D3A2E}.Release|x64.Build.0 = Release|Any CPU
{AD35E340-34DF-4FA9-B545-B9FBB72D3A2E}.Release|x86.ActiveCfg = Release|Any CPU
{AD35E340-34DF-4FA9-B545-B9FBB72D3A2E}.Release|x86.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|ARM.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|ARM.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|ARM64.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|iPhone.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|x64.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|x64.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|x86.ActiveCfg = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Debug|x86.Build.0 = Debug|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|Any CPU.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|Any CPU.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|ARM.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|ARM.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|ARM64.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|ARM64.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|iPhone.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|iPhone.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|iPhoneSimulator.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|iPhoneSimulator.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|x64.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|x64.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|x86.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release_NoSamples|x86.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|Any CPU.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|ARM.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|ARM.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|ARM64.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|ARM64.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|iPhone.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|iPhone.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|x64.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|x64.Build.0 = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|x86.ActiveCfg = Release|Any CPU
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -4967,6 +5011,7 @@ Global
{CDE9C243-5CDC-4DF6-B82F-B9AF4B35A284} = {416684CF-A4E3-4079-B380-3FF0B00E433C}
{54E27C52-4157-4585-B03F-184D8FBCAE3F} = {416684CF-A4E3-4079-B380-3FF0B00E433C}
{AD35E340-34DF-4FA9-B545-B9FBB72D3A2E} = {416684CF-A4E3-4079-B380-3FF0B00E433C}
{CC0AF5E6-FA87-4A09-958A-95ADE1E5B946} = {416684CF-A4E3-4079-B380-3FF0B00E433C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9B2608F4-D82B-4B72-B399-33E822DF01D0}
Expand Down
14 changes: 12 additions & 2 deletions src/Uno.UI/BaseActivity.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Uno.Logging;
using Windows.UI.Xaml;
using Android.OS;
using Windows.UI.ViewManagement;

namespace Uno.UI
{
Expand Down Expand Up @@ -130,7 +131,7 @@ public BaseActivity(IntPtr handle, JniHandleOwnership transfer)
{
InitializeBinder();
ContextHelper.Current = this;
NotifyCreatingInstance();
Initialize();

#if !IS_UNO
Performance.Increment(CreatedTotalBindableActivityCounter);
Expand All @@ -142,14 +143,23 @@ public BaseActivity()
{
InitializeBinder();
ContextHelper.Current = this;
NotifyCreatingInstance();
Initialize();

#if !IS_UNO
Performance.Increment(CreatedTotalBindableActivityCounter);
Performance.Increment(ActiveBindableActivityCounter);
#endif
}

private void Initialize()
{
// Eagerly create the ApplicationView instance for IBaseActivityEvents
// to be useable (specifically for the Create event)
ApplicationView.GetForCurrentView();

NotifyCreatingInstance();
}

partial void InnerAttachedToWindow() => BinderAttachedToWindow();

partial void InnerDetachedFromWindow() => BinderDetachedFromWindow();
Expand Down
Loading

0 comments on commit 23cca70

Please sign in to comment.