XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
namespace JiDangJia.Services;

// Only the awaited picker/camera owns this exception. Cold starts still require unlocking.
public sealed class ExternalPhotoScope : IDisposable
{
    private static readonly HashSet<ExternalPhotoScope> active=[];
    private readonly string account=AuthClient.Current.AccountId;
    private readonly DateTime expires=DateTime.UtcNow.AddMinutes(5);
    private bool disposed;
    public ExternalPhotoScope(){if(!AuthClient.Current.Locked)active.Add(this);}
    private static bool HasPending=>active.Any(s=>!s.disposed&&s.account==AuthClient.Current.AccountId);
    private static bool InProgress=>active.Any(s=>!s.disposed&&s.account==AuthClient.Current.AccountId&&s.expires>DateTime.UtcNow);
    private static bool DeviceLocked
    {
        get
        {
#if ANDROID
            var manager=Android.App.Application.Context.GetSystemService(Android.Content.Context.KeyguardService) as Android.App.KeyguardManager;
            return manager?.IsKeyguardLocked==true;
#else
            return false;
#endif
        }
    }
    public static void Background(Window window)
    {if(!InProgress||DeviceLocked)AppLock.Cover(window);}
    public static async Task Resumed(Window window)
    {
        if(DeviceLocked){AppLock.Cover(window);return;}
        if(!InProgress){if(HasPending)AppLock.Cover(window);return;}
        // A normal activity result completes the awaited picker before this check.
        // Manually reopening the app while its picker is still open requires unlocking.
        await Task.Delay(1000);
        if(InProgress&&ReferenceEquals(Application.Current?.Windows.FirstOrDefault(),window))AppLock.Cover(window);
    }
    public void Dispose(){disposed=true;active.Remove(this);}
}