Skip to content

Commit

Permalink
Android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Jun 8, 2022
1 parent 355e57e commit a508adb
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 29 deletions.
3 changes: 0 additions & 3 deletions src/Android/Avalonia.Android/AndroidPlatform.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Android;
using Avalonia.Android.Platform;
using Avalonia.Android.Platform.Input;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.OpenGL.Egl;
Expand Down Expand Up @@ -55,7 +53,6 @@ public static void Initialize(AndroidPlatformOptions options)
.Bind<IKeyboardDevice>().ToSingleton<AndroidKeyboardDevice>()
.Bind<IPlatformSettings>().ToConstant(Instance)
.Bind<IPlatformThreadingInterface>().ToConstant(new AndroidThreadingInterface())
.Bind<ISystemDialogImpl>().ToTransient<SystemDialogImpl>()
.Bind<IPlatformIconLoader>().ToSingleton<PlatformIconLoaderStub>()
.Bind<IRenderTimer>().ToConstant(new ChoreographerTimer())
.Bind<IRenderLoop>().ToConstant(new RenderLoop())
Expand Down
32 changes: 27 additions & 5 deletions src/Android/Avalonia.Android/AvaloniaActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
using AndroidX.Lifecycle;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls;
using Android.Runtime;
using Android.App;
using Android.Content;
using System;

namespace Avalonia.Android
{
public abstract class AvaloniaActivity<TApp> : AppCompatActivity where TApp : Application, new()
public abstract class AvaloniaActivity : AppCompatActivity
{
internal class SingleViewLifetime : ISingleViewApplicationLifetime
{
Expand All @@ -20,16 +24,15 @@ public Control MainView
}
}

internal Action<int, Result, Intent> ActivityResult;
internal AvaloniaView View;
internal AvaloniaViewModel _viewModel;

protected virtual AppBuilder CustomizeAppBuilder(AppBuilder builder) => builder.UseAndroid();
protected abstract AppBuilder CreateAppBuilder();

protected override void OnCreate(Bundle savedInstanceState)
{
var builder = AppBuilder.Configure<TApp>();

CustomizeAppBuilder(builder);
var builder = CreateAppBuilder();

View = new AvaloniaView(this);
SetContentView(View);
Expand Down Expand Up @@ -78,5 +81,24 @@ protected override void OnDestroy()

base.OnDestroy();
}

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);

ActivityResult?.Invoke(requestCode, resultCode, data);
}
}

public abstract class AvaloniaActivity<TApp> : AvaloniaActivity where TApp : Application, new()
{
protected virtual AppBuilder CustomizeAppBuilder(AppBuilder builder) => builder.UseAndroid();

protected override AppBuilder CreateAppBuilder()
{
var builder = AppBuilder.Configure<TApp>();

return CustomizeAppBuilder(builder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.Android.OpenGL;
using Avalonia.Android.Platform.Specific;
using Avalonia.Android.Platform.Specific.Helpers;
using Avalonia.Android.Storage;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Platform.Surfaces;
Expand All @@ -16,11 +17,13 @@
using Avalonia.OpenGL.Egl;
using Avalonia.OpenGL.Surfaces;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Avalonia.Rendering;

namespace Avalonia.Android.Platform.SkiaPlatform
{
class TopLevelImpl : IAndroidView, ITopLevelImpl, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo, ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost
class TopLevelImpl : IAndroidView, ITopLevelImpl, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo,
ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost, ITopLevelImplWithStorageProvider
{
private readonly IGlPlatformSurface _gl;
private readonly IFramebufferPlatformSurface _framebuffer;
Expand All @@ -46,6 +49,7 @@ public TopLevelImpl(AvaloniaView avaloniaView, bool placeOnTop = false)
_view.Resources.DisplayMetrics.HeightPixels).ToSize(RenderScaling);

NativeControlHost = new AndroidNativeControlHostImpl(avaloniaView);
StorageProvider = new AndroidStorageProvider((AvaloniaActivity)avaloniaView.Context);
}

public virtual Point GetAvaloniaPointFromEvent(MotionEvent e, int pointerIndex) =>
Expand Down Expand Up @@ -225,6 +229,8 @@ public sealed override IInputConnection OnCreateInputConnection(EditorInfo outAt
public ITextInputMethodImpl TextInputMethod => _textInputMethod;

public INativeControlHostImpl NativeControlHost { get; }

public IStorageProvider StorageProvider { get; }

public void SetTransparencyLevelHint(WindowTransparencyLevel transparencyLevel)
{
Expand Down
Loading

0 comments on commit a508adb

Please sign in to comment.