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

STHApplication

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

公开
关注 0 Fork 0 Star 0
UTF-8
using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using JiDangJia.Services;

namespace JiDangJia;

[Activity(Theme="@android:style/Theme.Translucent.NoTitleBar",Exported=false)]
public sealed class OrderPhotoActivity : Activity
{
    private const int PhotoRequest=259;
    private static TaskCompletionSource<FileResult?>? pending;
    private static string pendingPath="";
    private string account="",photo="";
    public static Task<FileResult?> Capture(string account,string photo)
    {
        if(pending is not null)throw new InvalidOperationException("相机正在使用中");
        var completion=new TaskCompletionSource<FileResult?>(TaskCreationOptions.RunContinuationsAsynchronously);pending=completion;pendingPath=photo;
        try
        {
            var activity=Platform.CurrentActivity??throw new InvalidOperationException("当前页面无法打开相机");
            activity.StartActivity(new Intent(activity,typeof(OrderPhotoActivity)).PutExtra("account",account).PutExtra("photo",photo));
        }
        catch{pending=null;pendingPath="";throw;}
        return completion.Task;
    }
    protected override void OnCreate(Bundle? state)
    {
        base.OnCreate(state);account=Intent?.GetStringExtra("account")??"";photo=Intent?.GetStringExtra("photo")??"";
        if(!OrderCameraRecovery.OwnsPhoto(account,photo)){Finish();return;}
        if(state?.GetBoolean("camera-opened")==true)return;
        try
        {
            using var file=new Java.IO.File(photo);var uri=AndroidX.Core.Content.FileProvider.GetUriForFile(this,PackageName+".fileProvider",file);
            var capture=new Intent(MediaStore.ActionImageCapture).PutExtra(MediaStore.ExtraOutput,uri);
            capture.AddFlags(ActivityFlags.GrantReadUriPermission|ActivityFlags.GrantWriteUriPermission);capture.ClipData=ClipData.NewRawUri("contract photo",uri);
#pragma warning disable CS0618
            StartActivityForResult(capture,PhotoRequest);
#pragma warning restore CS0618
        }
        catch(Exception ex){OrderCameraRecovery.RecordResult(account,photo,false);FinishCapture(null,ex);}
    }
    protected override void OnSaveInstanceState(Bundle state){state.PutBoolean("camera-opened",true);base.OnSaveInstanceState(state);}
    protected override void OnActivityResult(int requestCode,Result resultCode,Intent? data)
    {
#pragma warning disable CS0618
        base.OnActivityResult(requestCode,resultCode,data);
#pragma warning restore CS0618
        if(requestCode!=PhotoRequest)return;
        bool accepted=resultCode==Result.Ok&&OrderCameraRecovery.OwnsPhoto(account,photo)&&File.Exists(photo)&&new FileInfo(photo).Length>0;
        OrderCameraRecovery.RecordResult(account,photo,accepted);FinishCapture(accepted?new FileResult(photo,"image/jpeg"):null);
    }
    private void FinishCapture(FileResult? result,Exception? error=null)
    {
        var completion=pendingPath==photo?pending:null;
        if(completion is not null){pending=null;pendingPath="";if(error is null)completion.TrySetResult(result);else completion.TrySetException(error);}
        else
        {
            // Android may have reclaimed the original .NET process while the external camera was open.
            // Relaunch the authenticated app; it restores this account's checkpoint after login/unlock.
            var launch=PackageManager?.GetLaunchIntentForPackage(PackageName??"");
            if(launch is not null){launch.AddFlags(ActivityFlags.ClearTop|ActivityFlags.SingleTop);StartActivity(launch);}
        }
        Finish();
    }
}