1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Text ;
4
+ using System . Threading ;
4
5
using Android . Content ;
5
6
using Android . OS ;
6
7
using Android . Telephony ;
@@ -20,11 +21,32 @@ static PhoneCallManager()
20
21
"PhoneCallManager was used too early in the application lifetime. " +
21
22
"Android app context needs to be available." ) ;
22
23
}
24
+
23
25
_telephonyManager = ( TelephonyManager ) ContextHelper . Current
24
26
. GetSystemService ( Context . TelephonyService ) ;
27
+
28
+ #pragma warning disable CS0618 // TelephonyManager is obsolete in API 31
29
+ #pragma warning disable CA1422 // Validate platform compatibility
30
+ if ( Build . VERSION . SdkInt < BuildVersionCodes . S )
31
+ {
32
+ _telephonyManager . Listen ( new CallStateListener ( ) , PhoneStateListenerFlags . CallState ) ;
33
+ }
34
+ else
35
+ {
36
+ RegisterTelephonyCallbackAsync ( ) ;
37
+ }
38
+ #pragma warning restore CA1422 // Validate platform compatibility
39
+ #pragma warning restore CS0618 // TelephonyManager is obsolete in API 31
40
+ }
41
+
42
+ private static async void RegisterTelephonyCallbackAsync ( )
43
+ {
25
44
#pragma warning disable CS0618 // TelephonyManager is obsolete in API 31
26
45
#pragma warning disable CA1422 // Validate platform compatibility
27
- _telephonyManager . Listen ( new CallStateListener ( ) , PhoneStateListenerFlags . CallState ) ;
46
+ if ( await Extensions . PermissionsHelper . TryGetPermission ( CancellationToken . None , Android . Manifest . Permission . ReadPhoneState ) )
47
+ {
48
+ _telephonyManager . RegisterTelephonyCallback ( ContextHelper . Current . MainExecutor , new CallCallback ( ) ) ;
49
+ }
28
50
#pragma warning restore CA1422 // Validate platform compatibility
29
51
#pragma warning restore CS0618 // TelephonyManager is obsolete in API 31
30
52
}
0 commit comments