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

[VBLOCKS-3454] Quickstart Refactor #629

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true" />
<service
android:name=".fcm.VoiceFirebaseMessagingService"
android:name=".IncomingMessageService"
android:stopWithTask="false"
android:exported="false">
<intent-filter>
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/twilio/voice/quickstart/Constants.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.twilio.voice.quickstart;

public class Constants {
public static final String CALL_SID_KEY = "CALL_SID";
public static final String CALL_SID = "CALL_SID";
public static final String VOICE_CHANNEL_LOW_IMPORTANCE = "notification-channel-low-importance";
public static final String VOICE_CHANNEL_HIGH_IMPORTANCE = "notification-channel-high-importance";
public static final String OUTGOING_CALL_RECIPIENT = "OUTGOING_CALL_RECIPIENT";
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_ACCEPT = "ACTION_ACCEPT";
public static final String ACTION_REJECT = "ACTION_REJECT";
public static final String FCM_TOKEN = "FCM_TOKEN";
public static final String ACTION_INCOMING_CALL_NOTIFICATION = "ACTION_INCOMING_CALL_NOTIFICATION";
public static final String ACTION_INCOMING_CALL = "ACTION_INCOMING_CALL";
public static final String ACTION_OUTGOING_CALL = "ACTION_OUTGOING_CALL";
public static final String ACTION_CANCEL_CALL = "ACTION_CANCEL_CALL";
public static final String ACTION_ACCEPT_CALL = "ACTION_ACCEPT";
public static final String ACTION_REJECT_CALL = "ACTION_REJECT";
public static final String ACTION_OUTGOING_CALL = "ACTION_OUTGOING_CALL";
public static final String ACTION_FCM_TOKEN = "ACTION_FCM_TOKEN";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.twilio.voice.quickstart;

import static com.twilio.voice.quickstart.Constants.ACTION_FCM_TOKEN;
import static com.twilio.voice.quickstart.Constants.ACTION_INCOMING_CALL;
import static com.twilio.voice.quickstart.Constants.CALL_SID;
import static com.twilio.voice.quickstart.Constants.FCM_TOKEN;
import static com.twilio.voice.quickstart.Constants.INCOMING_CALL_INVITE;
import static com.twilio.voice.quickstart.Constants.ACTION_CANCEL_CALL;
import static com.twilio.voice.quickstart.Constants.CANCELLED_CALL_INVITE;
import static java.lang.String.format;

import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.os.Parcelable;
import android.util.Pair;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.twilio.voice.CallException;
import com.twilio.voice.CallInvite;
import com.twilio.voice.CancelledCallInvite;
import com.twilio.voice.MessageListener;
import com.twilio.voice.Voice;

public class IncomingMessageService extends FirebaseMessagingService implements MessageListener {
private static final Logger log = new Logger(IncomingMessageService.class);

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
log.debug(format(
"Received firebase message\n\tmessage data: %s\n\tfrom: %s",
remoteMessage.getData(),
remoteMessage.getFrom()));

// Check if message contains a data payload.
if (!remoteMessage.getData().isEmpty() && !Voice.handleMessage(this, remoteMessage.getData(), this)) {
log.error(format("Received message was not a valid Twilio Voice SDK payload: %s", remoteMessage.getData()));
}
}

@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
startVoiceService(ACTION_FCM_TOKEN, new Pair<>(FCM_TOKEN, token));
}

@Override
public void onCallInvite(@NonNull CallInvite callInvite) {
startVoiceService(
ACTION_INCOMING_CALL,
new Pair<>(INCOMING_CALL_INVITE, callInvite));
}

@Override
public void onCancelledCallInvite(@NonNull CancelledCallInvite cancelledCallInvite,
@Nullable CallException callException) {
startVoiceService(
ACTION_CANCEL_CALL,
new Pair<>(CANCELLED_CALL_INVITE, cancelledCallInvite));
}

@SafeVarargs
private void startVoiceService(@NonNull final String action,
@NonNull final Pair<String, Object>...data) {
final Intent intent = new Intent(this, VoiceService.class);
intent.setAction(action);
for (Pair<String, Object> pair: data) {
if (pair.second instanceof String) {
intent.putExtra(pair.first, (String)pair.second);
} else if (pair.second instanceof Parcelable) {
intent.putExtra(pair.first, (Parcelable)pair.second);
}
}
startService(intent);
}
}
74 changes: 74 additions & 0 deletions app/src/main/java/com/twilio/voice/quickstart/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.twilio.voice.quickstart;

import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Vector;

class Logger extends OutputStream {
private final String logTag;
private final Vector<Character> logInfoBuffer = new Vector<>();
public Logger(Class<?> clazz) {
logTag = clazz.getSimpleName();
}

public void debug(final String message) {
if (BuildConfig.DEBUG) {
Log.d(logTag, message);
}
}

public void log(final String message) {
Log.i(logTag, message);
}

public void warning(final String message) {
try {
write(message.getBytes());
flush();
} catch (Exception ignore) {}
}

public void error(final String message) {
Log.e(logTag, message);
}

public void warning(final Exception e, final String message) {
PrintStream printStream = new PrintStream(this);
printStream.println(message);
e.printStackTrace(printStream);
printStream.flush();
}

@Override
public synchronized void write(int i) throws IOException {
logInfoBuffer.add((char)i);
}

@Override
public synchronized void write(byte[] b) throws IOException {
for (char c: (new String(b, Charset.defaultCharset()).toCharArray())) {
logInfoBuffer.add(c);
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
for (char c: (new String(b, off, len, Charset.defaultCharset()).toCharArray())) {
logInfoBuffer.add(c);
}
}

@Override
public synchronized void flush() throws IOException {
char [] output = new char[logInfoBuffer.size()];
for (int i = 0; i < logInfoBuffer.size(); ++i) {
output[i] = logInfoBuffer.get(i);
}
logInfoBuffer.clear();
Log.w(logTag, String.valueOf(output));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.Voice;

public class NotificationProxyActivity extends Activity {
@Override
Expand All @@ -24,7 +23,7 @@ private void handleIntent(Intent intent) {
final String action = intent.getAction();
if (action != null) {
final Intent serviceIntent =
(new Intent(intent)).setClass(this, IncomingCallNotificationService.class);
(new Intent(intent)).setClass(this, VoiceService.class);
final Intent appIntent =
(new Intent(intent)).setClass(this, VoiceActivity.class);
switch (action) {
Expand Down Expand Up @@ -52,7 +51,7 @@ private void launchMainActivity(Intent intent) {
}
private void launchService(Intent intent) {
Intent launchIntent = new Intent(intent);
launchIntent.setClass(this, IncomingCallNotificationService.class);
launchIntent.setClass(this, VoiceService.class);
startService(launchIntent);
}
}
Loading