Skip to content

Commit 4ed0857

Browse files
committed
Added MauiAppBuilder extension for plugin initialization.
1 parent 0d2ce99 commit 4ed0857

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#if ANDROID
2+
using Android.OS;
3+
using Plugin.Maui.ScreenSecurity.Platforms.Android;
4+
#elif WINDOWS
5+
using Plugin.Maui.ScreenSecurity.Platforms.Windows;
6+
#endif
7+
using Microsoft.Maui.LifecycleEvents;
8+
using Plugin.Maui.ScreenSecurity.Handlers;
9+
10+
namespace Plugin.Maui.ScreenSecurity;
11+
12+
public static class MauiAppBuilderExtensions
13+
{
14+
public static MauiAppBuilder UseScreenSecurity(this MauiAppBuilder builder)
15+
{
16+
builder.ConfigureLifecycleEvents(life =>
17+
{
18+
#if ANDROID34_0_OR_GREATER
19+
life.AddAndroid(android =>
20+
{
21+
if (Build.VERSION.SdkInt >= BuildVersionCodes.UpsideDownCake)
22+
{
23+
CustomScreenCaptureCallback _customScreenCaptureCallback = new();
24+
MainThreadExecutor _mainThreadExecutor = new();
25+
26+
android.OnStart(activity =>
27+
{
28+
activity.RegisterScreenCaptureCallback(_mainThreadExecutor, _customScreenCaptureCallback);
29+
});
30+
31+
android.OnStop(activity =>
32+
{
33+
activity.UnregisterScreenCaptureCallback(_customScreenCaptureCallback);
34+
});
35+
}
36+
});
37+
#elif WINDOWS
38+
life.AddWindows(windows =>
39+
{
40+
windows.OnLaunched((app, args) =>
41+
{
42+
var mainWindow = Application.Current?.Windows[0];
43+
var nativeWindow = mainWindow?.Handler?.PlatformView as MauiWinUIWindow;
44+
45+
NativeMethods._hookID = NativeMethods.SetHook(NativeMethods._proc);
46+
47+
if (nativeWindow is not null)
48+
{
49+
nativeWindow.Content.KeyDown += (sender, e) =>
50+
{
51+
if (e.Key == Windows.System.VirtualKey.Snapshot)
52+
{
53+
ScreenCaptureEventHandler.RaiseScreenCaptured();
54+
}
55+
};
56+
}
57+
});
58+
59+
windows.OnClosed((app, args) =>
60+
{
61+
NativeMethods.UnhookWindowsHookEx(NativeMethods._hookID);
62+
});
63+
});
64+
#endif
65+
});
66+
67+
return builder;
68+
}
69+
}

0 commit comments

Comments
 (0)