1
1
package com .dormmom .flutter_twilio_voice ;
2
2
3
- //import com.google.firebase.iid.FirebaseInstanceId;
4
- //import com.google.firebase.iid.InstanceIdResult;
5
-
6
3
import com .twilio .voice .Call ;
7
4
import com .twilio .voice .CallException ;
8
5
import com .twilio .voice .CallInvite ;
25
22
import android .media .AudioAttributes ;
26
23
import android .media .AudioFocusRequest ;
27
24
import android .media .AudioManager ;
28
- import android .media .SoundPool ;
29
25
import android .os .Build ;
30
26
import android .util .Log ;
31
27
import androidx .annotation .NonNull ;
@@ -56,7 +52,7 @@ public class FlutterTwilioVoicePlugin implements FlutterPlugin, MethodChannel.Me
56
52
private VoiceBroadcastReceiver voiceBroadcastReceiver ;
57
53
58
54
private NotificationManager notificationManager ;
59
- private SoundPoolManager soundPoolManager ;
55
+ // private SoundPoolManager soundPoolManager;
60
56
private CallInvite activeCallInvite ;
61
57
private Call activeCall ;
62
58
private int activeCallNotificationId ;
@@ -66,6 +62,8 @@ public class FlutterTwilioVoicePlugin implements FlutterPlugin, MethodChannel.Me
66
62
RegistrationListener registrationListener = registrationListener ();
67
63
UnregistrationListener unregistrationListener = unregistrationListener ();
68
64
Call .Listener callListener = callListener ();
65
+ private MethodChannel methodChannel ;
66
+ private EventChannel eventChannel ;
69
67
private EventChannel .EventSink eventSink ;
70
68
private String fcmToken ;
71
69
@@ -75,14 +73,14 @@ public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
75
73
}
76
74
77
75
private static void register (BinaryMessenger messenger , FlutterTwilioVoicePlugin plugin , Context context ) {
78
- final MethodChannel methodChannel = new MethodChannel (messenger , CHANNEL_NAME + "/messages" );
79
- methodChannel .setMethodCallHandler (plugin );
76
+ plugin . methodChannel = new MethodChannel (messenger , CHANNEL_NAME + "/messages" );
77
+ plugin . methodChannel .setMethodCallHandler (plugin );
80
78
81
- final EventChannel eventChannel = new EventChannel (messenger , CHANNEL_NAME + "/events" );
82
- eventChannel .setStreamHandler (plugin );
79
+ plugin . eventChannel = new EventChannel (messenger , CHANNEL_NAME + "/events" );
80
+ plugin . eventChannel .setStreamHandler (plugin );
83
81
84
82
plugin .context = context ;
85
- plugin .soundPoolManager = SoundPoolManager .getInstance (context );
83
+ // plugin.soundPoolManager = SoundPoolManager.getInstance(context);
86
84
87
85
plugin .notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
88
86
plugin .voiceBroadcastReceiver = new VoiceBroadcastReceiver (plugin );
@@ -116,6 +114,7 @@ public static void registerWith(PluginRegistry.Registrar registrar) {
116
114
private void handleIncomingCallIntent (Intent intent ) {
117
115
if (intent != null && intent .getAction () != null ) {
118
116
String action = intent .getAction ();
117
+ Log .d (TAG , "Handling incoming call intent for action " + action );
119
118
activeCallInvite = intent .getParcelableExtra (Constants .INCOMING_CALL_INVITE );
120
119
activeCallNotificationId = intent .getIntExtra (Constants .INCOMING_CALL_NOTIFICATION_ID , 0 );
121
120
@@ -147,6 +146,7 @@ private void showIncomingCallDialog() {
147
146
148
147
private void handleIncomingCall () {
149
148
this .eventSink .success ("Ringing" );
149
+ //soundPoolManager.playRinging();
150
150
/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
151
151
showIncomingCallDialog();
152
152
} else {
@@ -158,8 +158,8 @@ private void handleIncomingCall() {
158
158
159
159
private void handleCancel () {
160
160
//if (alertDialog != null && alertDialog.isShowing()) {
161
- soundPoolManager . stopRinging ( );
162
- this . eventSink . success ( "Canceled" );
161
+ this . eventSink . success ( "Call Ended" );
162
+ //soundPoolManager.stopRinging( );
163
163
//alertDialog.cancel();
164
164
//}
165
165
}
@@ -168,6 +168,7 @@ private void registerReceiver() {
168
168
if (!isReceiverRegistered ) {
169
169
IntentFilter intentFilter = new IntentFilter ();
170
170
intentFilter .addAction (Constants .ACTION_INCOMING_CALL );
171
+ intentFilter .addAction (Constants .ACTION_INCOMING_CALL_NOTIFICATION );
171
172
intentFilter .addAction (Constants .ACTION_CANCEL_CALL );
172
173
intentFilter .addAction (Constants .ACTION_FCM_TOKEN );
173
174
LocalBroadcastManager .getInstance (this .activity ).registerReceiver (
@@ -224,7 +225,9 @@ private VoiceBroadcastReceiver(FlutterTwilioVoicePlugin plugin) {
224
225
@ Override
225
226
public void onReceive (Context context , Intent intent ) {
226
227
String action = intent .getAction ();
227
- if (action .equals (Constants .ACTION_INCOMING_CALL ) || action .equals (Constants .ACTION_CANCEL_CALL )) {
228
+ Log .d (TAG , "Received broadcast for action " + action );
229
+ if (action != null && (action .equals (Constants .ACTION_INCOMING_CALL ) || action .equals (Constants .ACTION_CANCEL_CALL )
230
+ || action .equals (Constants .ACTION_INCOMING_CALL_NOTIFICATION ))) {
228
231
/*
229
232
* Handle the incoming or cancelled call invite
230
233
*/
@@ -245,22 +248,6 @@ public void onReceive(Context context, Intent intent) {
245
248
*
246
249
*/
247
250
private void registerForCallInvites () {
248
- /*FirebaseInstanceId.getInstance()
249
- .getInstanceId()
250
- .addOnCompleteListener(
251
- new OnCompleteListener<InstanceIdResult>() {
252
- @Override
253
- public void onComplete(@NonNull Task<InstanceIdResult> task) {
254
- if (!task.isSuccessful()) {
255
- Log.w(TAG, "getToken, error fetching instanceID: ", task.getException());
256
- result.success(null);
257
- return;
258
- }
259
-
260
- Log.i(TAG, "Registering with FCM");
261
- Voice.register(accessToken, Voice.RegistrationChannel.FCM, task.getResult().getToken(), registrationListener);
262
- }
263
- });*/
264
251
if (this .accessToken != null && this .fcmToken != null ) {
265
252
Log .i (TAG , "Registering with FCM" );
266
253
Voice .register (this .accessToken , Voice .RegistrationChannel .FCM , this .fcmToken , registrationListener );
@@ -276,53 +263,64 @@ private void unregisterForCallInvites() {
276
263
277
264
@ Override
278
265
public void onDetachedFromEngine (FlutterPluginBinding flutterPluginBinding ) {
279
- soundPoolManager .release ();
266
+ Log .d (TAG , "Detatched from Flutter engine" );
267
+ //soundPoolManager.release();
280
268
}
281
269
282
270
@ Override
283
271
public void onListen (Object o , EventChannel .EventSink eventSink ) {
272
+ Log .i (TAG , "Setting event sink" );
284
273
this .eventSink = eventSink ;
285
274
}
286
275
287
276
@ Override
288
277
public void onCancel (Object o ) {
278
+ Log .i (TAG , "Removing event sink" );
289
279
this .eventSink = null ;
290
280
}
291
281
292
282
@ Override
293
283
public void onMethodCall (MethodCall call , MethodChannel .Result result ) {
294
284
if (call .method .equals ("tokens" )) {
285
+ Log .d (TAG , "Setting up tokens" );
295
286
this .accessToken = call .argument ("accessToken" );
296
287
this .fcmToken = call .argument ("fcmToken" );
297
288
this .registerForCallInvites ();
298
289
result .success (true );
299
290
} else if (call .method .equals ("sendDigits" )) {
300
291
String digits = call .argument ("digits" );
301
292
if (this .activeCall != null ) {
293
+ Log .d (TAG , "Sending digits " + digits );
302
294
this .activeCall .sendDigits (digits );
303
295
}
304
296
result .success (true );
305
297
} else if (call .method .equals ("hangUp" )) {
298
+ Log .d (TAG , "Hanging up" );
306
299
this .disconnect ();
307
300
result .success (true );
308
301
} else if (call .method .equals ("toggleSpeaker" )) {
309
302
// nuthin
310
303
result .success (true );
311
304
} else if (call .method .equals ("muteCall" )) {
305
+ Log .d (TAG , "Muting call" );
312
306
this .mute ();
313
307
result .success (true );
314
308
} else if (call .method .equals ("isOnCall" )) {
309
+ Log .d (TAG , "Is on call invoked" );
315
310
result .success (this .activeCall != null );
316
311
} else if (call .method .equals ("holdCall" )) {
312
+ Log .d (TAG , "Hold call invoked" );
317
313
this .hold ();
318
314
result .success (true );
319
315
} else if (call .method .equals ("answer" )) {
316
+ Log .d (TAG , "Answering call" );
320
317
this .answer ();
321
318
result .success (true );
322
319
} else if (call .method .equals ("unregister" )) {
323
320
this .unregisterForCallInvites ();
324
321
result .success (true );
325
322
} else if (call .method .equals ("makeCall" )) {
323
+ Log .d (TAG , "Making new call" );
326
324
final HashMap <String , String > params = new HashMap <>();
327
325
params .put ("To" , call .argument ("to" ).toString ());
328
326
params .put ("From" , call .argument ("from" ).toString ());
@@ -340,7 +338,8 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
340
338
* Accept an incoming Call
341
339
*/
342
340
private void answer () {
343
- soundPoolManager .getInstance (this .context ).stopRinging ();
341
+ Log .d (TAG , "Answering call" );
342
+ SoundPoolManager .getInstance (this .context ).stopRinging ();
344
343
activeCallInvite .accept (this .activity , callListener );
345
344
notificationManager .cancel (activeCallNotificationId );
346
345
}
@@ -551,92 +550,3 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
551
550
}
552
551
*/
553
552
}
554
-
555
-
556
- class SoundPoolManager {
557
-
558
- private boolean playing = false ;
559
- private boolean loaded = false ;
560
- private boolean playingCalled = false ;
561
- private float actualVolume ;
562
- private float maxVolume ;
563
- private float volume ;
564
- private AudioManager audioManager ;
565
- private SoundPool soundPool ;
566
- private int ringingSoundId ;
567
- private int ringingStreamId ;
568
- private int disconnectSoundId ;
569
- private static SoundPoolManager instance ;
570
-
571
- private SoundPoolManager (Context context ) {
572
- // AudioManager audio settings for adjusting the volume
573
- audioManager = (AudioManager ) context .getSystemService (Context .AUDIO_SERVICE );
574
- actualVolume = (float ) audioManager .getStreamVolume (AudioManager .STREAM_MUSIC );
575
- maxVolume = (float ) audioManager .getStreamMaxVolume (AudioManager .STREAM_MUSIC );
576
- volume = actualVolume / maxVolume ;
577
-
578
- // Load the sounds
579
- int maxStreams = 1 ;
580
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
581
- soundPool = new SoundPool .Builder ()
582
- .setMaxStreams (maxStreams )
583
- .build ();
584
- } else {
585
- soundPool = new SoundPool (maxStreams , AudioManager .STREAM_MUSIC , 0 );
586
- }
587
-
588
- soundPool .setOnLoadCompleteListener (new SoundPool .OnLoadCompleteListener () {
589
- @ Override
590
- public void onLoadComplete (SoundPool soundPool , int sampleId , int status ) {
591
- loaded = true ;
592
- if (playingCalled ) {
593
- playRinging ();
594
- playingCalled = false ;
595
- }
596
- }
597
-
598
- });
599
- ringingSoundId = soundPool .load (context , R .raw .incoming , 1 );
600
- disconnectSoundId = soundPool .load (context , R .raw .disconnect , 1 );
601
- }
602
-
603
- public static SoundPoolManager getInstance (Context context ) {
604
- if (instance == null ) {
605
- instance = new SoundPoolManager (context );
606
- }
607
- return instance ;
608
- }
609
-
610
- public void playRinging () {
611
- if (loaded && !playing ) {
612
- ringingStreamId = soundPool .play (ringingSoundId , volume , volume , 1 , -1 , 1f );
613
- playing = true ;
614
- } else {
615
- playingCalled = true ;
616
- }
617
- }
618
-
619
- public void stopRinging () {
620
- if (playing ) {
621
- soundPool .stop (ringingStreamId );
622
- playing = false ;
623
- }
624
- }
625
-
626
- public void playDisconnect () {
627
- if (loaded && !playing ) {
628
- soundPool .play (disconnectSoundId , volume , volume , 1 , 0 , 1f );
629
- playing = false ;
630
- }
631
- }
632
-
633
- public void release () {
634
- if (soundPool != null ) {
635
- soundPool .unload (ringingSoundId );
636
- soundPool .unload (disconnectSoundId );
637
- soundPool .release ();
638
- soundPool = null ;
639
- }
640
- instance = null ;
641
- }
642
- }
0 commit comments