Skip to content

Commit

Permalink
Remove reference to concrete native host app (#16803)
Browse files Browse the repository at this point in the history
* fix: remove reference to concrete native host app

* chore: fixing null dereference

* chore: update to avoid obsolete code

* chore: removing obsolete hard references

* chore: use private property on Tizen

* chore: fix null dereference

* chore: normalize Exception for null services

* chore: use current Android Application Context without strong type

* chore: make fields nullable

* chore: fixing nullable references

* chore: add null check for Maui Context

* chore: fixing obsolete message
  • Loading branch information
dansiegel authored Sep 25, 2023
1 parent 34072d8 commit f09e3d5
Show file tree
Hide file tree
Showing 23 changed files with 186 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// base.OnCreate(savedInstanceState);

//#pragma warning disable CS0612 // Type or member is obsolete
// Forms.Init(new MauiContext(MauiApplication.Current.Services, this));
// Forms.Init(new MauiContext(IPlatformApplication.Current.Services, this));
//#pragma warning restore CS0612 // Type or member is obsolete
// LoadApplication(new Issue10182Application());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
// any UI has appeared.
// This creates a dummy MauiContext that wraps the Application.

var services = MauiApplication.Current.Services;
var services = IPlatformApplication.Current.Services;
var mauiContext = new MauiContext(services, app);
var state = new ActivationState(mauiContext);
#pragma warning disable CS0612 // Type or member is obsolete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void OnConfigureLifeCycle(ITizenLifecycleBuilder tizen)
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.

var services = MauiApplication.Current.Services;
var services = IPlatformApplication.Current.Services;
MauiContext mauiContext = new MauiContext(services);
ActivationState state = new ActivationState(mauiContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
// MauiContext inside Forms

var services = MauiWinUIApplication.Current.Services;
var services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services");
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext, args);
#pragma warning disable CS0612 // Type or member is obsolete
Expand Down
3 changes: 2 additions & 1 deletion src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public void Initialize(IServiceProvider services)
#if WINDOWS
var dispatcher =
services.GetService<IDispatcher>() ??
MauiWinUIApplication.Current.Services.GetRequiredService<IDispatcher>();
IPlatformApplication.Current?.Services.GetRequiredService<IDispatcher>() ??
throw new InvalidOperationException("Unable to find Application Services");

dispatcher.DispatchIfRequired(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS)
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.

var services = MauiUIApplicationDelegate.Current.Services;
var services = IPlatformApplication.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext);
#pragma warning disable CS0612 // Type or member is obsolete
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void Initialize(IServiceProvider services)
#if WINDOWS
var dispatcher =
services.GetService<IDispatcher>() ??
MauiWinUIApplication.Current.Services.GetRequiredService<IDispatcher>();
IPlatformApplication.Current?.Services.GetRequiredService<IDispatcher>();

dispatcher
.DispatchIfRequired(() =>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Hosting/EssentialsMauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal static MauiAppBuilder UseEssentials(this MauiAppBuilder builder)
builder.ConfigureLifecycleEvents(life =>
{
#if __ANDROID__
ApplicationModel.Platform.Init(MauiApplication.Current);
ApplicationModel.Platform.Init((Android.App.Application)Android.App.Application.Context);

life.AddAndroid(android => android
.OnCreate((activity, savedInstanceState) =>
Expand Down
18 changes: 9 additions & 9 deletions src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override void OnActivityResult(int requestCode, Result resultCode, Int
base.OnActivityResult(requestCode, resultCode, data);

ActivityResultCallbackRegistry.InvokeCallback(requestCode, resultCode, data);
MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnActivityResult>(del => del(this, requestCode, resultCode, data));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnActivityResult>(del => del(this, requestCode, resultCode, data));
}

// TODO: Investigate whether the new AndroidX way is actually useful:
Expand All @@ -27,7 +27,7 @@ public override void OnBackPressed()
#pragma warning restore 809
{
var preventBackPropagation = false;
MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnBackPressed>(del =>
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnBackPressed>(del =>
{
preventBackPropagation = del(this) || preventBackPropagation;
});
Expand All @@ -44,41 +44,41 @@ public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnConfigurationChanged>(del => del(this, newConfig));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnConfigurationChanged>(del => del(this, newConfig));
}

protected override void OnNewIntent(Intent? intent)
{
base.OnNewIntent(intent);

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnNewIntent>(del => del(this, intent));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnNewIntent>(del => del(this, intent));
}

protected override void OnPostCreate(Bundle? savedInstanceState)
{
base.OnPostCreate(savedInstanceState);

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPostCreate>(del => del(this, savedInstanceState));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPostCreate>(del => del(this, savedInstanceState));
}

protected override void OnPostResume()
{
base.OnPostResume();

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPostResume>(del => del(this));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPostResume>(del => del(this));
}

protected override void OnRestart()
{
base.OnRestart();

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRestart>(del => del(this));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRestart>(del => del(this));
}

[System.Runtime.Versioning.SupportedOSPlatform("android23.0")]
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRequestPermissionsResult>(del => del(this, requestCode, permissions, grantResults));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRequestPermissionsResult>(del => del(this, requestCode, permissions, grantResults));

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Expand All @@ -87,7 +87,7 @@ protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreInstanceState(savedInstanceState);

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRestoreInstanceState>(del => del(this, savedInstanceState));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRestoreInstanceState>(del => del(this, savedInstanceState));
}
}
}
5 changes: 4 additions & 1 deletion src/Core/src/Platform/Android/MauiAppCompatActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ protected override void OnCreate(Bundle? savedInstanceState)

base.OnCreate(savedInstanceState);

this.CreatePlatformWindow(MauiApplication.Current.Application, savedInstanceState);
if (IPlatformApplication.Current?.Application is not null)
{
this.CreatePlatformWindow(IPlatformApplication.Current.Application, savedInstanceState);
}
}

public override bool DispatchTouchEvent(MotionEvent? e)
Expand Down
53 changes: 36 additions & 17 deletions src/Core/src/Platform/Android/MauiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,68 +39,87 @@ public override void OnCreate()

var applicationContext = rootContext.MakeApplicationScope(this);

Services = applicationContext.Services;
_services = applicationContext.Services;

Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreating>(del => del(this));
_services.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreating>(del => del(this));

Application = Services.GetRequiredService<IApplication>();
_application = _services.GetRequiredService<IApplication>();

this.SetApplicationHandler(Application, applicationContext);
this.SetApplicationHandler(_application, applicationContext);

Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreate>(del => del(this));
_services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreate>(del => del(this));

base.OnCreate();
}

public override void OnLowMemory()
{
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationLowMemory>(del => del(this));
_services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationLowMemory>(del => del(this));

base.OnLowMemory();
}

public override void OnTrimMemory(TrimMemory level)
{
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationTrimMemory>(del => del(this, level));
_services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationTrimMemory>(del => del(this, level));

base.OnTrimMemory(level);
}

public override void OnConfigurationChanged(Configuration newConfig)
{
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationConfigurationChanged>(del => del(this, newConfig));
_services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationConfigurationChanged>(del => del(this, newConfig));

base.OnConfigurationChanged(newConfig);
}

public static MauiApplication Current { get; private set; } = null!;

public IServiceProvider Services { get; protected set; } = null!;
IServiceProvider? _services;

public IApplication Application { get; protected set; } = null!;
IApplication? _application;

// TODO: we should investigate throwing an exception or changing the public API
IServiceProvider IPlatformApplication.Services => _services!;

IApplication IPlatformApplication.Application => _application!;

[Obsolete("Use the IPlatformApplication.Current.Services instead.")]
public IServiceProvider Services
{
get => _services!;
protected set => _services = value;
}

[Obsolete("Use the IPlatformApplication.Current.Application instead.")]
public IApplication Application
{
get => _application!;
protected set => _application = value;
}

public class ActivityLifecycleCallbacks : Java.Lang.Object, IActivityLifecycleCallbacks
{
public void OnActivityCreated(Activity activity, Bundle? savedInstanceState) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnCreate>(del => del(activity, savedInstanceState));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnCreate>(del => del(activity, savedInstanceState));

public void OnActivityStarted(Activity activity) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnStart>(del => del(activity));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnStart>(del => del(activity));

public void OnActivityResumed(Activity activity) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnResume>(del => del(activity));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnResume>(del => del(activity));

public void OnActivityPaused(Activity activity) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPause>(del => del(activity));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnPause>(del => del(activity));

public void OnActivityStopped(Activity activity) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnStop>(del => del(activity));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnStop>(del => del(activity));

public void OnActivitySaveInstanceState(Activity activity, Bundle outState) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnSaveInstanceState>(del => del(activity, outState));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnSaveInstanceState>(del => del(activity, outState));

public void OnActivityDestroyed(Activity activity) =>
Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnDestroy>(del => del(activity));
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnDestroy>(del => del(activity));
}
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Android/MauiContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ internal static View ToPlatform(

internal static IServiceProvider GetApplicationServices(this IMauiContext mauiContext)
{
if (mauiContext.Context?.ApplicationContext is MauiApplication ma)
return ma.Services;
if (IPlatformApplication.Current?.Services is not null)
return IPlatformApplication.Current.Services;

throw new InvalidOperationException("Unable to find Application Services");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Tizen/CoreAppExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class CoreAppExtensions
{
public static IWindow GetWindow(this CoreApplication application)
{
foreach (var window in MauiApplication.Current.Application.Windows)
foreach (var window in IPlatformApplication.Current?.Application?.Windows ?? Array.Empty<IWindow>())
{
if (window?.Handler?.PlatformView is Window win && win == GetDefaultWindow())
return window;
Expand All @@ -23,7 +23,7 @@ public static IWindow GetWindow(this CoreApplication application)
if (platformWindow == null)
return null;

foreach (var window in MauiApplication.Current.Application.Windows)
foreach (var window in IPlatformApplication.Current?.Application?.Windows ?? Array.Empty<IWindow>())
{
if (window?.Handler?.PlatformView is Window win && win == platformWindow)
return window;
Expand Down
Loading

0 comments on commit f09e3d5

Please sign in to comment.