11using Android . App ;
22using Android . Content ;
33using Android . Media ;
4- using Android . OS ;
5- using AndroidX . Core . View ;
64using IXICore . Meta ;
75using SPIXI ;
86using SPIXI . Interfaces ;
@@ -11,6 +9,8 @@ namespace Spixi
119{
1210 public class SPlatformUtils
1311 {
12+ static object ringtoneLock = new object ( ) ;
13+
1414 static MediaPlayer ? ringtone = null ;
1515 static MediaPlayer ? dialtonePlayer = null ;
1616
@@ -43,68 +43,74 @@ public static string getHtmlPath()
4343
4444 public static void startRinging ( )
4545 {
46- if ( ringtone != null )
46+ lock ( ringtoneLock )
4747 {
48- return ;
49- }
50-
51- try
52- {
53- bool ring = true ;
54-
55- NotificationManager nm = ( NotificationManager ) MainActivity . Instance . GetSystemService ( Context . NotificationService ) ! ;
56- InterruptionFilter int_filter = nm . CurrentInterruptionFilter ;
57- if ( int_filter != InterruptionFilter . Priority && int_filter != InterruptionFilter . All )
48+ if ( ringtone != null )
5849 {
59- ring = false ;
50+ return ;
6051 }
61-
6252
63- AudioManager am = ( AudioManager ) MainActivity . Instance . GetSystemService ( Context . AudioService ) ! ;
64- if ( am . RingerMode != RingerMode . Normal )
53+ try
6554 {
66- ring = false ;
55+ bool ring = true ;
56+
57+ NotificationManager nm = ( NotificationManager ) MainActivity . Instance . GetSystemService ( Context . NotificationService ) ! ;
58+ InterruptionFilter int_filter = nm . CurrentInterruptionFilter ;
59+ if ( int_filter != InterruptionFilter . Priority && int_filter != InterruptionFilter . All )
60+ {
61+ ring = false ;
62+ }
63+
64+
65+ AudioManager am = ( AudioManager ) MainActivity . Instance . GetSystemService ( Context . AudioService ) ! ;
66+ if ( am . RingerMode != RingerMode . Normal )
67+ {
68+ ring = false ;
69+ }
70+
71+ MainActivity . Instance . VolumeControlStream = Android . Media . Stream . Ring ;
72+
73+ if ( ring )
74+ {
75+ ringtone = playSoundFromAssets ( "sounds/default_ringtone.mp3" ) ;
76+ ringtone . Looping = true ;
77+ ringtone . Start ( ) ;
78+ }
6779 }
68-
69- MainActivity . Instance . VolumeControlStream = Android . Media . Stream . Ring ;
70-
71- if ( ring )
80+ catch ( Exception e )
7281 {
73- ringtone = playSoundFromAssets ( "sounds/default_ringtone.mp3" ) ;
74- ringtone . Looping = true ;
75- ringtone . Start ( ) ;
82+ Logging . error ( "Exception occurred in startRinging: " + e ) ;
83+ ringtone = null ;
7684 }
7785 }
78- catch ( Exception e )
79- {
80- Logging . error ( "Exception occurred in startRinging: " + e ) ;
81- ringtone = null ;
82- }
8386 }
8487
8588 public static void stopRinging ( )
8689 {
87- if ( ringtone == null )
90+ lock ( ringtoneLock )
8891 {
89- return ;
90- }
92+ if ( ringtone == null )
93+ {
94+ return ;
95+ }
9196
92- try
93- {
94- if ( ringtone . IsPlaying )
97+ try
9598 {
96- ringtone . Stop ( ) ;
99+ if ( ringtone . IsPlaying )
100+ {
101+ ringtone . Stop ( ) ;
102+ }
103+ ringtone . Release ( ) ;
104+ }
105+ catch ( Exception e )
106+ {
107+ Logging . error ( "Exception occurred while stopping the ringtone: " + e ) ;
108+ }
109+ finally
110+ {
111+ ringtone = null ;
112+ MainActivity . Instance . VolumeControlStream = Android . Media . Stream . NotificationDefault ;
97113 }
98- ringtone . Release ( ) ;
99- }
100- catch ( Exception e )
101- {
102- Logging . error ( "Exception occurred while stopping the ringtone: " + e ) ;
103- }
104- finally
105- {
106- ringtone = null ;
107- MainActivity . Instance . VolumeControlStream = Android . Media . Stream . NotificationDefault ;
108114 }
109115 }
110116
0 commit comments