Skip to content

Commit

Permalink
Merge pull request #7 from FabriBertani/bug/#06_Android_Thread_Exception
Browse files Browse the repository at this point in the history
Bug/#6 Android thread exception
  • Loading branch information
FabriBertani authored Jul 3, 2023
2 parents 34a3e8a + 2a4d572 commit db5f24f
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Android.Views;
using Android.OS;
using Android.Views;

namespace Plugin.Maui.ScreenSecurity;

partial class ScreenSecurityImplementation : IScreenSecurity
{
private static volatile Handler? handler;

/// <summary>
/// Prevent screen content from being exposed when the app
/// is sent to <b>Background</b> or the <b>Recents screen</b>.
Expand All @@ -13,9 +16,15 @@ public void EnableScreenSecurityProtection()
{
try
{
var activity = Platform.CurrentActivity;
if (handler?.Looper != Looper.MainLooper)
handler = new Handler(Looper.MainLooper!);

handler?.Post(() =>
{
var activity = Platform.CurrentActivity;

activity?.Window?.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
activity?.Window?.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
});
}
catch (Exception ex)
{
Expand All @@ -33,9 +42,15 @@ public void DisableScreenSecurityProtection()
{
try
{
var activity = Platform.CurrentActivity;
if (handler?.Looper != Looper.MainLooper)
handler = new Handler(Looper.MainLooper!);

handler?.Post(() =>
{
var activity = Platform.CurrentActivity;

activity?.Window?.ClearFlags(WindowManagerFlags.Secure);
activity?.Window?.ClearFlags(WindowManagerFlags.Secure);
});
}
catch (Exception ex)
{
Expand Down

0 comments on commit db5f24f

Please sign in to comment.