22using Android . Content ;
33using Android . Content . PM ;
44using Android . OS ;
5- using Android . Runtime ;
6- using Android . Util ;
75using Avalonia ;
86using Avalonia . Android ;
97using Avalonia . ReactiveUI ;
@@ -25,14 +23,6 @@ namespace Vocup.Android;
2523 DataPathPattern = ".*\\ .vhf" ) ]
2624public class MainActivity : AvaloniaMainActivity < App >
2725{
28- private const string Tag = "Vocup.Android.MainActivity" ;
29- private const int FilePickerRequestCode = 3147 ;
30-
31- protected override AppBuilder CreateAppBuilder ( )
32- {
33- return AppBuilder . Configure ( ( ) => new App ( this ) ) . UseAndroid ( ) ;
34- }
35-
3626 protected override AppBuilder CustomizeAppBuilder ( AppBuilder builder )
3727 {
3828 return base . CustomizeAppBuilder ( builder )
@@ -52,59 +42,15 @@ protected override void OnNewIntent(Intent? intent)
5242 HandleIntent ( intent ) ;
5343 }
5444
55- public void ShowFilePicker ( )
56- {
57- var intent = new Intent ( Intent . ActionGetContent ) ;
58- intent . SetType ( "application/octet-stream" ) ;
59- intent . AddCategory ( Intent . CategoryOpenable ) ;
60- intent . AddFlags ( ActivityFlags . GrantReadUriPermission ) ;
61- StartActivityForResult ( Intent . CreateChooser ( intent , "Select a file" ) , FilePickerRequestCode ) ;
62- }
63-
64- protected override void OnActivityResult ( int requestCode , [ GeneratedEnum ] Result resultCode , Intent ? data )
65- {
66- base . OnActivityResult ( requestCode , resultCode , data ) ;
67-
68- if ( requestCode == FilePickerRequestCode && resultCode == Result . Ok && data ? . Data != null )
69- {
70- using System . IO . Stream ? stream = ContentResolver ? . OpenInputStream ( data . Data ) ;
71-
72- if ( stream == null )
73- {
74- Log . Error ( Tag , "Stream is null in OnActivityResult" ) ;
75- return ;
76- }
77- if ( Avalonia . Application . Current is not App app )
78- {
79- Log . Error ( Tag , "App is null in OnActivityResult" ) ;
80- return ;
81- }
82- Log . Debug ( Tag , $ "File size is { stream . Length } bytes") ;
83- app . OpenFile ( stream ) ;
84- }
85- }
86-
87- private void HandleIntent ( Intent ? intent )
45+ private static void HandleIntent ( Intent ? intent )
8846 {
89- if ( intent ? . Action == Intent . ActionView && intent . Data != null )
47+ if ( intent ? . Action == Intent . ActionView && intent . Data != null && Avalonia . Application . Current is App app )
9048 {
91- using System . IO . Stream ? stream = ContentResolver ? . OpenInputStream ( intent . Data ) ;
92-
93- if ( stream == null )
49+ string ? uri = intent . Data . ToString ( ) ;
50+ if ( uri != null )
9451 {
95- Log . Error ( Tag , "Stream is null in HandleIntent" ) ;
96- return ;
52+ app . OpenFile ( new ( uri ) ) ;
9753 }
98-
99- if ( Avalonia . Application . Current is not App app )
100- {
101- Log . Error ( Tag , "App is null in HandleIntent" ) ;
102- return ;
103- }
104-
105- Log . Debug ( Tag , $ "File size is { stream . Length } bytes") ;
106-
107- app . OpenFile ( stream ) ;
10854 }
10955 }
11056}
0 commit comments