Skip to content

Commit 7a14612

Browse files
committed
Save android work
1 parent f0f46a9 commit 7a14612

15 files changed

+656
-214
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
.pub/
66

77
build/
8+
9+
.idea/
10+
*.iml

.idea/codeStyles/Project.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+183-86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/build.gradle

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.5.3'
11+
classpath 'com.android.tools.build:gradle:3.6.2'
1212
}
1313
}
1414

@@ -37,8 +37,13 @@ android {
3737

3838
dependencies {
3939
testImplementation 'junit:junit:4.12'
40-
implementation 'com.twilio:voice-android:5.0.2'
41-
//implementation 'com.google.firebase:firebase-messaging:17.6.0'
40+
implementation 'com.twilio:voice-android:5.1.1'
41+
implementation 'com.google.firebase:firebase-messaging:17.6.0'
42+
implementation 'com.android.support:support-v4:28.0.0'
43+
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
44+
implementation 'com.android.support:design:28.0.0'
45+
implementation 'com.android.support:animated-vector-drawable:28.0.0'
46+
implementation 'com.android.support:support-media-compat:28.0.0'
4247
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4348
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
4449
}

android/src/main/java/com/dormmom/flutter_twilio_voice/FlutterTwilioVoicePlugin.java

+30-120
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.dormmom.flutter_twilio_voice;
22

3-
//import com.google.firebase.iid.FirebaseInstanceId;
4-
//import com.google.firebase.iid.InstanceIdResult;
5-
63
import com.twilio.voice.Call;
74
import com.twilio.voice.CallException;
85
import com.twilio.voice.CallInvite;
@@ -25,7 +22,6 @@
2522
import android.media.AudioAttributes;
2623
import android.media.AudioFocusRequest;
2724
import android.media.AudioManager;
28-
import android.media.SoundPool;
2925
import android.os.Build;
3026
import android.util.Log;
3127
import androidx.annotation.NonNull;
@@ -56,7 +52,7 @@ public class FlutterTwilioVoicePlugin implements FlutterPlugin, MethodChannel.Me
5652
private VoiceBroadcastReceiver voiceBroadcastReceiver;
5753

5854
private NotificationManager notificationManager;
59-
private SoundPoolManager soundPoolManager;
55+
//private SoundPoolManager soundPoolManager;
6056
private CallInvite activeCallInvite;
6157
private Call activeCall;
6258
private int activeCallNotificationId;
@@ -66,6 +62,8 @@ public class FlutterTwilioVoicePlugin implements FlutterPlugin, MethodChannel.Me
6662
RegistrationListener registrationListener = registrationListener();
6763
UnregistrationListener unregistrationListener = unregistrationListener();
6864
Call.Listener callListener = callListener();
65+
private MethodChannel methodChannel;
66+
private EventChannel eventChannel;
6967
private EventChannel.EventSink eventSink;
7068
private String fcmToken;
7169

@@ -75,14 +73,14 @@ public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
7573
}
7674

7775
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);
8078

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);
8381

8482
plugin.context = context;
85-
plugin.soundPoolManager = SoundPoolManager.getInstance(context);
83+
//plugin.soundPoolManager = SoundPoolManager.getInstance(context);
8684

8785
plugin.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
8886
plugin.voiceBroadcastReceiver = new VoiceBroadcastReceiver(plugin);
@@ -116,6 +114,7 @@ public static void registerWith(PluginRegistry.Registrar registrar) {
116114
private void handleIncomingCallIntent(Intent intent) {
117115
if (intent != null && intent.getAction() != null) {
118116
String action = intent.getAction();
117+
Log.d(TAG, "Handling incoming call intent for action " + action);
119118
activeCallInvite = intent.getParcelableExtra(Constants.INCOMING_CALL_INVITE);
120119
activeCallNotificationId = intent.getIntExtra(Constants.INCOMING_CALL_NOTIFICATION_ID, 0);
121120

@@ -147,6 +146,7 @@ private void showIncomingCallDialog() {
147146

148147
private void handleIncomingCall() {
149148
this.eventSink.success("Ringing");
149+
//soundPoolManager.playRinging();
150150
/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
151151
showIncomingCallDialog();
152152
} else {
@@ -158,8 +158,8 @@ private void handleIncomingCall() {
158158

159159
private void handleCancel() {
160160
//if (alertDialog != null && alertDialog.isShowing()) {
161-
soundPoolManager.stopRinging();
162-
this.eventSink.success("Canceled");
161+
this.eventSink.success("Call Ended");
162+
//soundPoolManager.stopRinging();
163163
//alertDialog.cancel();
164164
//}
165165
}
@@ -168,6 +168,7 @@ private void registerReceiver() {
168168
if (!isReceiverRegistered) {
169169
IntentFilter intentFilter = new IntentFilter();
170170
intentFilter.addAction(Constants.ACTION_INCOMING_CALL);
171+
intentFilter.addAction(Constants.ACTION_INCOMING_CALL_NOTIFICATION);
171172
intentFilter.addAction(Constants.ACTION_CANCEL_CALL);
172173
intentFilter.addAction(Constants.ACTION_FCM_TOKEN);
173174
LocalBroadcastManager.getInstance(this.activity).registerReceiver(
@@ -224,7 +225,9 @@ private VoiceBroadcastReceiver(FlutterTwilioVoicePlugin plugin) {
224225
@Override
225226
public void onReceive(Context context, Intent intent) {
226227
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))) {
228231
/*
229232
* Handle the incoming or cancelled call invite
230233
*/
@@ -245,22 +248,6 @@ public void onReceive(Context context, Intent intent) {
245248
*
246249
*/
247250
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-
});*/
264251
if (this.accessToken != null && this.fcmToken != null) {
265252
Log.i(TAG, "Registering with FCM");
266253
Voice.register(this.accessToken, Voice.RegistrationChannel.FCM, this.fcmToken, registrationListener);
@@ -276,53 +263,64 @@ private void unregisterForCallInvites() {
276263

277264
@Override
278265
public void onDetachedFromEngine(FlutterPluginBinding flutterPluginBinding) {
279-
soundPoolManager.release();
266+
Log.d(TAG, "Detatched from Flutter engine");
267+
//soundPoolManager.release();
280268
}
281269

282270
@Override
283271
public void onListen(Object o, EventChannel.EventSink eventSink) {
272+
Log.i(TAG, "Setting event sink");
284273
this.eventSink = eventSink;
285274
}
286275

287276
@Override
288277
public void onCancel(Object o) {
278+
Log.i(TAG, "Removing event sink");
289279
this.eventSink = null;
290280
}
291281

292282
@Override
293283
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
294284
if (call.method.equals("tokens")) {
285+
Log.d(TAG, "Setting up tokens");
295286
this.accessToken = call.argument("accessToken");
296287
this.fcmToken = call.argument("fcmToken");
297288
this.registerForCallInvites();
298289
result.success(true);
299290
} else if (call.method.equals("sendDigits")) {
300291
String digits = call.argument("digits");
301292
if (this.activeCall != null) {
293+
Log.d(TAG, "Sending digits " + digits);
302294
this.activeCall.sendDigits(digits);
303295
}
304296
result.success(true);
305297
} else if (call.method.equals("hangUp")) {
298+
Log.d(TAG, "Hanging up");
306299
this.disconnect();
307300
result.success(true);
308301
} else if (call.method.equals("toggleSpeaker")) {
309302
// nuthin
310303
result.success(true);
311304
} else if (call.method.equals("muteCall")) {
305+
Log.d(TAG, "Muting call");
312306
this.mute();
313307
result.success(true);
314308
} else if (call.method.equals("isOnCall")) {
309+
Log.d(TAG, "Is on call invoked");
315310
result.success(this.activeCall != null);
316311
} else if (call.method.equals("holdCall")) {
312+
Log.d(TAG, "Hold call invoked");
317313
this.hold();
318314
result.success(true);
319315
} else if (call.method.equals("answer")) {
316+
Log.d(TAG, "Answering call");
320317
this.answer();
321318
result.success(true);
322319
} else if (call.method.equals("unregister")) {
323320
this.unregisterForCallInvites();
324321
result.success(true);
325322
} else if (call.method.equals("makeCall")) {
323+
Log.d(TAG, "Making new call");
326324
final HashMap<String, String> params = new HashMap<>();
327325
params.put("To", call.argument("to").toString());
328326
params.put("From", call.argument("from").toString());
@@ -340,7 +338,8 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
340338
* Accept an incoming Call
341339
*/
342340
private void answer() {
343-
soundPoolManager.getInstance(this.context).stopRinging();
341+
Log.d(TAG, "Answering call");
342+
SoundPoolManager.getInstance(this.context).stopRinging();
344343
activeCallInvite.accept(this.activity, callListener);
345344
notificationManager.cancel(activeCallNotificationId);
346345
}
@@ -551,92 +550,3 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
551550
}
552551
*/
553552
}
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

Comments
 (0)