Skip to content

Commit

Permalink
Merge pull request #34 from FabriBertani/feature/is_protection_enabled
Browse files Browse the repository at this point in the history
Feature: Add protection enabled detection
  • Loading branch information
FabriBertani authored May 25, 2024
2 parents 8ec29db + 6a26abf commit bd1de0f
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 18 deletions.
11 changes: 10 additions & 1 deletion Plugin.Maui.ScreenSecurity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plugin.Maui.ScreenSecurity"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{F0127D89-F3B5-4C88-9016-5CCA2DBF3F59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScreenSecuritySample", "samples\ScreenSecuritySample\ScreenSecuritySample.csproj", "{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScreenSecuritySample", "samples\ScreenSecuritySample\ScreenSecuritySample.csproj", "{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScreenSecurityBlazorSample", "samples\ScreenSecurityBlazorSample\ScreenSecurityBlazorSample.csproj", "{921C60FE-FBDF-4180-994E-13B6558FD0D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -25,12 +27,19 @@ Global
{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5}.Release|Any CPU.Build.0 = Release|Any CPU
{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5}.Release|Any CPU.Deploy.0 = Release|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Release|Any CPU.Build.0 = Release|Any CPU
{921C60FE-FBDF-4180-994E-13B6558FD0D9}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{479D2E8B-47FB-4F0D-B1AB-3DF4CC7D28A5} = {F0127D89-F3B5-4C88-9016-5CCA2DBF3F59}
{921C60FE-FBDF-4180-994E-13B6558FD0D9} = {F0127D89-F3B5-4C88-9016-5CCA2DBF3F59}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FB29A9B4-E0B3-4C0E-9E67-D98FC2F3E4EB}
Expand Down
5 changes: 5 additions & 0 deletions Plugin.Maui.ScreenSecurity/IScreenSecurity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public interface IScreenSecurity
/// Deactivates all screen security protection.
/// </summary>
void DeactivateScreenSecurityProtection();

/// <summary>
/// Checks if screen protection is enabled.
/// </summary>
bool IsProtectionEnabled { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ private void SetScreenSecurityProtection(bool enabled)
#pragma warning restore CA1416

if (enabled)
activity.Window?.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
{
activity.Window?.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);

IsProtectionEnabled = enabled;
}
else
activity.Window?.ClearFlags(WindowManagerFlags.Secure);
}
Expand All @@ -86,4 +90,9 @@ private void SetScreenSecurityProtection(bool enabled)
}
});
}

/// <summary>
/// Checks if screen protection is enabled.
/// </summary>
public bool IsProtectionEnabled { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void DeactivateScreenSecurityProtection()
SetScreenshotProtection(false);
}

/// <summary>
/// Checks if screen protection is enabled.
/// </summary>
public bool IsProtectionEnabled { get; private set; }

private void SetScreenshotProtection(bool enabled)
{
try
Expand All @@ -71,6 +76,8 @@ private void SetScreenshotProtection(bool enabled)

if (hwnd != IntPtr.Zero)
_ = NativeMethods.SetWindowDisplayAffinity(hwnd, enabled ? WDA_MONITOR : WDA_NONE);

IsProtectionEnabled = enabled;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public void ActivateScreenSecurityProtection(ScreenProtectionOptions screenProte
}

HandleScreenCaptureProtection(screenProtectionOptions.PreventScreenshot, screenProtectionOptions.PreventScreenRecording);

IsProtectionEnabled = true;
}

/// <summary>
Expand All @@ -94,8 +96,15 @@ public void DeactivateScreenSecurityProtection()
ScreenRecordingProtectionManager.HandleScreenRecordingProtection(false);

ScreenshotProtectionManager.HandleScreenshotProtection(false);

IsProtectionEnabled = false;
}

/// <summary>
/// Checks if screen protection is enabled.
/// </summary>
public bool IsProtectionEnabled { get; private set; }

private void HandleScreenCaptureProtection(bool preventScreenshot, bool preventScreenRecording)
{
ScreenshotProtectionManager.HandleScreenshotProtection(preventScreenshot, _window);
Expand Down
Binary file not shown.
Binary file not shown.
10 changes: 0 additions & 10 deletions Plugin.Maui.ScreenSecurity/Plugin.Maui.ScreenSecurity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
</ItemGroup>

<ItemGroup>
<None Remove="Platforms\iOS\black_bg.png" />
<None Remove="Platforms\iOS\white_bg.png" />
</ItemGroup>

<ItemGroup>
<MauiImage Include="Platforms\iOS\black_bg.png" />
<MauiImage Include="Platforms\iOS\white_bg.png" />
</ItemGroup>

<ItemGroup>
<None Include="..\Assets\plugin.maui.screensecurity_128x128.jpg">
<Pack>True</Pack>
Expand Down
2 changes: 2 additions & 0 deletions Plugin.Maui.ScreenSecurity/ScreenSecurity.net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public void DeactivateScreenSecurityProtection()
{
throw new NotImplementedException();
}

public bool IsProtectionEnabled { get; private set; }
}
4 changes: 1 addition & 3 deletions samples/ScreenSecuritySample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Maui;
using Plugin.Maui.ScreenSecurity;
using Plugin.Maui.ScreenSecurity;
using ScreenSecuritySample.Views;

namespace ScreenSecuritySample;
Expand All @@ -11,7 +10,6 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
3 changes: 1 addition & 2 deletions samples/ScreenSecuritySample/ScreenSecuritySample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 13 additions & 1 deletion samples/ScreenSecuritySample/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ScreenSecuritySample.Views.MainPage">
x:Class="ScreenSecuritySample.Views.MainPage"
x:Name="this">
<ScrollView
Padding="16, 0"
HorizontalScrollBarVisibility="Never">
Expand All @@ -19,6 +20,17 @@
FontSize="Large"
Text="Navigate to unprotected page"
Clicked="Button_Clicked" />
<Button
x:Name="activationBtn"
CornerRadius="15"
FontSize="Large"
Clicked="ActivationBtn_Clicked" />
<Label
x:Name="isEnabledLabel"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
FontSize="Medium"
Text="Screen protection enabled:"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
23 changes: 23 additions & 0 deletions samples/ScreenSecuritySample/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ protected override void OnAppearing()
// Activate the screen security protection with default settings
_screenSecurity.ActivateScreenSecurityProtection();

CheckStatusButton();

isEnabledLabel.Text = $"Screen protection enabled: {_screenSecurity.IsProtectionEnabled}";

/*
// For changing iOS options, follow one of the next examples:
Expand Down Expand Up @@ -54,4 +58,23 @@ private async void Button_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("unprotected_page", true);
}

private void ActivationBtn_Clicked(object sender, EventArgs e)
{
if (!_screenSecurity.IsProtectionEnabled)
_screenSecurity.ActivateScreenSecurityProtection();
else
_screenSecurity.DeactivateScreenSecurityProtection();

CheckStatusButton();

isEnabledLabel.Text = $"Screen protection enabled: {_screenSecurity.IsProtectionEnabled}";
}

private void CheckStatusButton()
{
activationBtn.Text = _screenSecurity.IsProtectionEnabled
? "Deactivate Screen Protection"
: "Activate Screen Protection";
}
}
6 changes: 6 additions & 0 deletions samples/ScreenSecuritySample/Views/SecondPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
FontSize="Large"
Text="Return to protected page"
Clicked="Button_Clicked" />
<Label
x:Name="isEnabledLabel"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
FontSize="Medium"
Text="Screen protection enabled:"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
2 changes: 2 additions & 0 deletions samples/ScreenSecuritySample/Views/SecondPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected override void OnAppearing()

// Deactivate the screen security protection.
_screenSecurity.DeactivateScreenSecurityProtection();

isEnabledLabel.Text = $"Screen protection enabled: {_screenSecurity.IsProtectionEnabled}";
}

private async void Button_Clicked(object sender, EventArgs e)
Expand Down

0 comments on commit bd1de0f

Please sign in to comment.