Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use preview3. #196

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {

dependencies {
testCompile 'junit:junit:4.12'
compile 'com.twilio:voice-android:3.0.0-preview2'
compile 'com.twilio:voice-android:3.0.0-preview3'
compile 'com.android.support:design:27.0.2'
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.squareup.retrofit:retrofit:1.9.0'
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/twilio/voice/quickstart/VoiceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ public class VoiceActivity extends AppCompatActivity {
private SoundPoolManager soundPoolManager;

public static final String INCOMING_CALL_INVITE = "INCOMING_CALL_INVITE";
public static final String CANCELLED_CALL_INVITE = "CANCELLED_CALL_INVITE";
public static final String INCOMING_CALL_NOTIFICATION_ID = "INCOMING_CALL_NOTIFICATION_ID";
public static final String ACTION_INCOMING_CALL = "ACTION_INCOMING_CALL";
public static final String ACTION_CANCEL_CALL = "ACTION_CANCEL_CALL";
public static final String ACTION_FCM_TOKEN = "ACTION_FCM_TOKEN";

private NotificationManager notificationManager;
Expand Down Expand Up @@ -279,7 +281,7 @@ private void handleIncomingCallIntent(Intent intent) {
if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals(ACTION_INCOMING_CALL)) {
activeCallInvite = intent.getParcelableExtra(INCOMING_CALL_INVITE);
if (activeCallInvite != null && (activeCallInvite.getState() == CallInvite.State.PENDING)) {
if (activeCallInvite != null) {
soundPoolManager.playRinging();
alertDialog = createIncomingCallDialog(VoiceActivity.this,
activeCallInvite,
Expand All @@ -288,10 +290,12 @@ private void handleIncomingCallIntent(Intent intent) {
alertDialog.show();
activeCallNotificationId = intent.getIntExtra(INCOMING_CALL_NOTIFICATION_ID, 0);
} else {
if (alertDialog != null && alertDialog.isShowing()) {
soundPoolManager.stopRinging();
alertDialog.cancel();
}

}
} else if (intent.getAction().equals(ACTION_CANCEL_CALL)) {
if (alertDialog != null && alertDialog.isShowing()) {
soundPoolManager.stopRinging();
alertDialog.cancel();
}
} else if (intent.getAction().equals(ACTION_FCM_TOKEN)) {
retrieveAccessToken();
Expand All @@ -303,6 +307,7 @@ private void registerReceiver() {
if (!isReceiverRegistered) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_INCOMING_CALL);
intentFilter.addAction(ACTION_CANCEL_CALL);
intentFilter.addAction(ACTION_FCM_TOKEN);
LocalBroadcastManager.getInstance(this).registerReceiver(
voiceBroadcastReceiver, intentFilter);
Expand All @@ -322,9 +327,9 @@ private class VoiceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_INCOMING_CALL)) {
if (action.equals(ACTION_INCOMING_CALL) || action.equals(ACTION_CANCEL_CALL)) {
/*
* Handle the incoming call invite
* Handle the incoming or cancelled call invite
*/
handleIncomingCallIntent(intent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.twilio.voice.CallInvite;
import com.twilio.voice.MessageException;
import com.twilio.voice.CancelledCallInvite;
import com.twilio.voice.MessageListener;
import com.twilio.voice.Voice;
import com.twilio.voice.quickstart.R;
Expand Down Expand Up @@ -56,96 +58,106 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
final int notificationId = (int) System.currentTimeMillis();
Voice.handleMessage(this, data, new MessageListener() {

boolean valid = Voice.handleMessage(this, remoteMessage.getData(), new MessageListener() {
@Override
public void onCallInvite(CallInvite callInvite) {
public void onCallInvite(@NonNull CallInvite callInvite) {
final int notificationId = (int) System.currentTimeMillis();
VoiceFirebaseMessagingService.this.notify(callInvite, notificationId);
VoiceFirebaseMessagingService.this.sendCallInviteToActivity(callInvite, notificationId);
}

@Override
public void onError(MessageException messageException) {
Log.e(TAG, messageException.getLocalizedMessage());
public void onCancelledCallInvite(@NonNull CancelledCallInvite cancelledCallInvite) {
VoiceFirebaseMessagingService.this.cancelNotification(cancelledCallInvite);
VoiceFirebaseMessagingService.this.sendCancelledCallInviteToActivity(
cancelledCallInvite);
}

});

if (!valid) {
Log.e(TAG, "The message was not a valid Twilio Voice SDK payload: " +
remoteMessage.getData());
}

}
}

private void notify(CallInvite callInvite, int notificationId) {
String callSid = callInvite.getCallSid();
Notification notification = null;

if (callInvite.getState() == CallInvite.State.PENDING) {
Intent intent = new Intent(this, VoiceActivity.class);
intent.setAction(VoiceActivity.ACTION_INCOMING_CALL);
intent.putExtra(VoiceActivity.INCOMING_CALL_NOTIFICATION_ID, notificationId);
intent.putExtra(VoiceActivity.INCOMING_CALL_INVITE, callInvite);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
Intent intent = new Intent(this, VoiceActivity.class);
intent.setAction(VoiceActivity.ACTION_INCOMING_CALL);
intent.putExtra(VoiceActivity.INCOMING_CALL_NOTIFICATION_ID, notificationId);
intent.putExtra(VoiceActivity.INCOMING_CALL_INVITE, callInvite);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
/*
* Pass the notification id and call sid to use as an identifier to cancel the
* notification later
*/
Bundle extras = new Bundle();
extras.putInt(NOTIFICATION_ID_KEY, notificationId);
extras.putString(CALL_SID_KEY, callInvite.getCallSid());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel callInviteChannel = new NotificationChannel(VOICE_CHANNEL,
"Primary Voice Channel", NotificationManager.IMPORTANCE_DEFAULT);
callInviteChannel.setLightColor(Color.GREEN);
callInviteChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(callInviteChannel);

Notification notification =
buildNotification(callInvite.getFrom() + " is calling.",
pendingIntent,
extras);
notificationManager.notify(notificationId, notification);
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_call_end_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(callInvite.getFrom() + " is calling.")
.setAutoCancel(true)
.setExtras(extras)
.setContentIntent(pendingIntent)
.setGroup("test_app_notification")
.setColor(Color.rgb(214, 10, 37));

notificationManager.notify(notificationId, notificationBuilder.build());
}
}

private void cancelNotification(CancelledCallInvite cancelledCallInvite) {
SoundPoolManager.getInstance((this)).stopRinging();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
/*
* Pass the notification id and call sid to use as an identifier to cancel the
* notification later
* If the incoming call was cancelled then remove the notification by matching
* it with the call sid from the list of notifications in the notification drawer.
*/
Bundle extras = new Bundle();
extras.putInt(NOTIFICATION_ID_KEY, notificationId);
extras.putString(CALL_SID_KEY, callSid);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel callInviteChannel = new NotificationChannel(VOICE_CHANNEL,
"Primary Voice Channel", NotificationManager.IMPORTANCE_DEFAULT);
callInviteChannel.setLightColor(Color.GREEN);
callInviteChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(callInviteChannel);

notification = buildNotification(callInvite.getFrom() + " is calling.", pendingIntent, extras);
notificationManager.notify(notificationId, notification);
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_call_end_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(callInvite.getFrom() + " is calling.")
.setAutoCancel(true)
.setExtras(extras)
.setContentIntent(pendingIntent)
.setGroup("test_app_notification")
.setColor(Color.rgb(214, 10, 37));

notificationManager.notify(notificationId, notificationBuilder.build());
}
} else {
SoundPoolManager.getInstance(this).stopRinging();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
/*
* If the incoming call was cancelled then remove the notification by matching
* it with the call sid from the list of notifications in the notification drawer.
*/
StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();
for (StatusBarNotification statusBarNotification : activeNotifications) {
notification = statusBarNotification.getNotification();
Bundle extras = notification.extras;
String notificationCallSid = extras.getString(CALL_SID_KEY);

if (callSid.equals(notificationCallSid)) {
notificationManager.cancel(extras.getInt(NOTIFICATION_ID_KEY));
} else {
sendCallInviteToActivity(callInvite, notificationId);
}
StatusBarNotification[] activeNotifications =
notificationManager.getActiveNotifications();
for (StatusBarNotification statusBarNotification : activeNotifications) {
Notification notification = statusBarNotification.getNotification();
Bundle extras = notification.extras;
String notificationCallSid = extras.getString(CALL_SID_KEY);

if (cancelledCallInvite.getCallSid().equals(notificationCallSid)) {
notificationManager.cancel(extras.getInt(NOTIFICATION_ID_KEY));
}
} else {
/*
* Prior to Android M the notification manager did not provide a list of
* active notifications so we lazily clear all the notifications when
* receiving a cancelled call.
*
* In order to properly cancel a notification using
* NotificationManager.cancel(notificationId) we should store the call sid &
* notification id of any incoming calls using shared preferences or some other form
* of persistent storage.
*/
notificationManager.cancelAll();
}
} else {
/*
* Prior to Android M the notification manager did not provide a list of
* active notifications so we lazily clear all the notifications when
* receiving a CancelledCallInvite.
*
* In order to properly cancel a notification using
* NotificationManager.cancel(notificationId) we should store the call sid &
* notification id of any incoming calls using shared preferences or some other form
* of persistent storage.
*/
notificationManager.cancelAll();
}
}

Expand All @@ -162,6 +174,15 @@ private void sendCallInviteToActivity(CallInvite callInvite, int notificationId)
this.startActivity(intent);
}

/*
* Send the CancelledCallInvite to the VoiceActivity
*/
private void sendCancelledCallInviteToActivity(CancelledCallInvite cancelledCallInvite) {
Intent intent = new Intent(VoiceActivity.ACTION_CANCEL_CALL);
intent.putExtra(VoiceActivity.CANCELLED_CALL_INVITE, cancelledCallInvite);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

/**
* Build a notification.
*
Expand Down