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

Android Send message,Send typing indicator, Message status #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ compatible with RN 0.60+ (iOS and AndroidX)
## To do

- [x] Send SIP Messages (IM) iOS
- [ ] Send SIP Messages (IM) Android
- [x] Send SIP Messages (IM) Android
- [x] isTyping iOS
- [ ] isTyping Android
- [x] isTyping Android

## Installation

Expand Down Expand Up @@ -55,7 +55,10 @@ endpoint.on("connectivity_changed", (online) => {});
endpoint.on("call_received", (call) => {});
endpoint.on("call_changed", (call) => {});
endpoint.on("call_terminated", (call) => {});
endpoint.on("message_received", (message) => {});
endpoint.on("call_screen_locked", (call) => {}); // Android only
endpoint.on('typing_indication_received', (indication) => {}); // Android only
endpoint.on('message_status_received', (status) => {}); // Android only
```

Account creating is pretty strainghforward.
Expand Down
11 changes: 11 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1688747649134</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
6 changes: 3 additions & 3 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
arguments=
arguments=--init-script /var/folders/kr/twx13n9x0yx7gq8jz_hn_1j00000gn/T/d146c9752a26f79b52047fb6dc6ed385d064e120494f96f08ca63a317c41f94c.gradle --init-script /var/folders/kr/twx13n9x0yx7gq8jz_hn_1j00000gn/T/52cde0cfcf3e28b8b7510e992210d9614505e0911af0c190bd590d7158574963.gradle
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3))
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.4.2))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
java.home=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
Expand Down
65 changes: 50 additions & 15 deletions android/src/main/java/one/telefon/sip2/PjActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PjActions {

public static final String ACTION_START = "start";
public static final String ACTION_CREATE_ACCOUNT = "account_create";
public static final String ACTION_CHANGE_CODEC_SETTINGS= "change_codec_settings'";
public static final String ACTION_CHANGE_CODEC_SETTINGS = "change_codec_settings'";
public static final String ACTION_REGISTER_ACCOUNT = "account_register";
public static final String ACTION_DELETE_ACCOUNT = "account_delete";
public static final String ACTION_MAKE_CALL = "call_make";
Expand All @@ -47,6 +47,9 @@ public class PjActions {
public static final String ACTION_RINGING_CALL = "call_ringing";
public static final String ACTION_PROGRESS_CALL = "call_progress";

public static final String ACTION_SEND_MESSAGE = "one.telefon.message.send";
public static final String ACTION_SEND_TYPING_INDICATION = "one.telefon.typing.indication.send";


public static final String EVENT_STARTED = "one.telefon.account.started";
public static final String EVENT_ACCOUNT_CREATED = "one.telefon.account.created";
Expand All @@ -56,6 +59,8 @@ public class PjActions {
public static final String EVENT_CALL_RECEIVED = "one.telefon.call.received";
public static final String EVENT_CALL_SCREEN_LOCKED = "one.telefon.call.screen.locked";
public static final String EVENT_MESSAGE_RECEIVED = "one.telefon.message.received";
public static final String EVENT_TYPING_INDICATION_RECEIVED = "one.telefon.typing.indication.received";
public static final String EVENT_MESSAGE_STATUS_RECEIVED = "one.telefon.instant.message.status.received";
public static final String EVENT_HANDLED = "one.telefon.handled";

public static Intent createStartIntent(int callbackId, ReadableMap configuration, Context context) {
Expand All @@ -68,7 +73,8 @@ public static Intent createStartIntent(int callbackId, ReadableMap configuration
return intent;
}

public static Intent createSetServiceConfigurationIntent(int callbackId, ReadableMap configuration, Context context) {
public static Intent createSetServiceConfigurationIntent(int callbackId, ReadableMap configuration,
Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_SET_SERVICE_CONFIGURATION);
intent.putExtra("callback_id", callbackId);
Expand Down Expand Up @@ -107,7 +113,8 @@ public static Intent createAccountDeleteIntent(int callbackId, int accountId, Co
return intent;
}

public static Intent createMakeCallIntent(int callbackId, int accountId, String destination, ReadableMap settings, ReadableMap message, Context context) {
public static Intent createMakeCallIntent(int callbackId, int accountId, String destination, ReadableMap settings,
ReadableMap message, Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_MAKE_CALL);
intent.putExtra("callback_id", callbackId);
Expand Down Expand Up @@ -135,15 +142,16 @@ public static Intent createHangupCallIntent(int callbackId, int callId, Context
}

/*
public static Intent createActivateAudioSessionIntent(int callbackId, int callId, Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_ACTIVATEAUDIOSESSION_CALL);
intent.putExtra("callback_id", callbackId);
intent.putExtra("call_id", callId);

return intent;
}
*/
* public static Intent createActivateAudioSessionIntent(int callbackId, int
* callId, Context context) {
* Intent intent = new Intent(context, PjSipService.class);
* intent.setAction(PjActions.ACTION_ACTIVATEAUDIOSESSION_CALL);
* intent.putExtra("callback_id", callbackId);
* intent.putExtra("call_id", callId);
*
* return intent;
* }
*/

public static Intent createDeclineCallIntent(int callbackId, int callId, Context context) {
Intent intent = new Intent(context, PjSipService.class);
Expand Down Expand Up @@ -245,7 +253,8 @@ public static Intent createXFerCallIntent(int callbackId, int callId, String des
return intent;
}

public static Intent createXFerReplacesCallIntent(int callbackId, int callId, int destinationCallId, Context context) {
public static Intent createXFerReplacesCallIntent(int callbackId, int callId, int destinationCallId,
Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_XFER_REPLACES_CALL);
intent.putExtra("callback_id", callbackId);
Expand Down Expand Up @@ -285,6 +294,30 @@ public static Intent createChangeCodecSettingsIntent(int callbackId, ReadableMap
return intent;
}

public static Intent createSendMessageIntent(int callbackId, int accountId, String destination, String message,
Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_SEND_MESSAGE);
intent.putExtra("callback_id", callbackId);
intent.putExtra("account_id", accountId);
intent.putExtra("destination", destination);
intent.putExtra("message", message);

return intent;
}

public static Intent createImTypingIntent(int callbackId, int accountId, String destination, boolean isTyping,
Context context) {
Intent intent = new Intent(context, PjSipService.class);
intent.setAction(PjActions.ACTION_SEND_TYPING_INDICATION);
intent.putExtra("callback_id", callbackId);
intent.putExtra("account_id", accountId);
intent.putExtra("destination", destination);
intent.putExtra("isTyping", isTyping);

return intent;
}

private static void formatIntent(Intent intent, ReadableMap configuration) {
if (configuration == null) {
return;
Expand All @@ -311,7 +344,8 @@ private static void formatIntent(Intent intent, ReadableMap configuration) {
intent.putExtra(key, (Serializable) formatMap(configuration.getMap(key)));
break;
default:
Log.w(TAG, "Unable to put extra information for intent: unknown type \""+ configuration.getType(key) +"\"");
Log.w(TAG, "Unable to put extra information for intent: unknown type \""
+ configuration.getType(key) + "\"");
break;
}
}
Expand Down Expand Up @@ -344,7 +378,8 @@ private static Map<String, Object> formatMap(ReadableMap map) {
value.put(mapKey, formatMap(map.getMap(mapKey)));
break;
default:
Log.w(TAG, "Unable to put extra information for intent: unknown type \""+ map.getType(mapKey) +"\"");
Log.w(TAG,
"Unable to put extra information for intent: unknown type \"" + map.getType(mapKey) + "\"");
break;
}
}
Expand Down
49 changes: 48 additions & 1 deletion android/src/main/java/one/telefon/sip2/PjSipAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import org.pjsip.pjsua2.Account;
import org.pjsip.pjsua2.OnIncomingCallParam;
import org.pjsip.pjsua2.OnInstantMessageParam;
import org.pjsip.pjsua2.OnTypingIndicationParam;
import org.pjsip.pjsua2.OnInstantMessageStatusParam;
import org.pjsip.pjsua2.OnRegStateParam;
import org.pjsip.pjsua2.SendInstantMessageParam;
import org.pjsip.pjsua2.SendTypingIndicationParam;

public class PjSipAccount extends Account {

Expand Down Expand Up @@ -70,6 +74,47 @@ public void onInstantMessage(OnInstantMessageParam prm) {
service.emmitMessageReceived(this, message);
}

@Override
public void onTypingIndication(OnTypingIndicationParam prm) {
PjSipTypingIndication indication = new PjSipTypingIndication(this, prm);
service.emmitTypingIndication(this, indication);
}

@Override
public void onInstantMessageStatus(OnInstantMessageStatusParam prm) {
PjSipInstantMessageStatus status = new PjSipInstantMessageStatus(this, prm);
service.emmitInstantMessageStatus(this, status);
}

public void sendInstantMessage(String destination, String message) {
try {
SendInstantMessageParam prm = new SendInstantMessageParam();
prm.setContentType("text/plain");
prm.setContent(message);

PjSipBuddy buddy = new PjSipBuddy(this, destination);
buddy.sendInstantMessage(prm);
buddy.delete();
prm.delete();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public void sendTypingIndication(String destination, boolean isTyping) {
try {
SendTypingIndicationParam prm = new SendTypingIndicationParam();
prm.setIsTyping(isTyping);

PjSipBuddy buddy = new PjSipBuddy(this, destination);
buddy.sendTypingIndication(prm);
buddy.delete();
prm.delete();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public JSONObject toJson() {
JSONObject json = new JSONObject();

Expand All @@ -92,7 +137,9 @@ public JSONObject toJson() {
json.put("contactParams", configuration.getContactParams());
json.put("contactUriParams", configuration.getContactUriParams());

json.put("regServer", configuration.getRegServer());

json.put("regServer",
configuration.getRegServer());
json.put("regTimeout", configuration.isRegTimeoutNotEmpty() ? String.valueOf(configuration.getRegTimeout()) : "");
json.put("regContactParams", configuration.getRegContactParams());
json.put("regHeaders", configuration.getRegHeaders());
Expand Down
16 changes: 16 additions & 0 deletions android/src/main/java/one/telefon/sip2/PjSipBroadcastEmiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ public void fireMessageReceivedEvent(PjSipMessage message) {
context.sendBroadcast(intent);
}

public void fireTypingIndicationReceivedEvent(PjSipTypingIndication indication) {
Intent intent = new Intent();
intent.setAction(PjActions.EVENT_TYPING_INDICATION_RECEIVED);
intent.putExtra("data", indication.toJsonString());

context.sendBroadcast(intent);
}

public void fireInstantMessageStatusReceivedEvent(PjSipInstantMessageStatus status) {
Intent intent = new Intent();
intent.setAction(PjActions.EVENT_MESSAGE_STATUS_RECEIVED);
intent.putExtra("data", status.toJsonString());

context.sendBroadcast(intent);
}

public void fireCallReceivedEvent(PjSipCall call) {
Intent intent = new Intent();
intent.setAction(PjActions.EVENT_CALL_RECEIVED);
Expand Down
27 changes: 25 additions & 2 deletions android/src/main/java/one/telefon/sip2/PjSipBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public IntentFilter getFilter() {
filter.addAction(PjActions.EVENT_CALL_TERMINATED);
filter.addAction(PjActions.EVENT_CALL_SCREEN_LOCKED);
filter.addAction(PjActions.EVENT_MESSAGE_RECEIVED);
filter.addAction(PjActions.EVENT_TYPING_INDICATION_RECEIVED);
filter.addAction(PjActions.EVENT_MESSAGE_STATUS_RECEIVED);
filter.addAction(PjActions.EVENT_HANDLED);

return filter;
Expand All @@ -57,7 +59,8 @@ public IntentFilter getFilter() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

Log.d(TAG, "Received \""+ action +"\" response from service (" + ArgumentUtils.dumpIntentExtraParameters(intent) + ")");
Log.d(TAG, "Received \"" + action + "\" response from service ("
+ ArgumentUtils.dumpIntentExtraParameters(intent) + ")");

switch (action) {
case PjActions.EVENT_STARTED:
Expand All @@ -72,6 +75,12 @@ public void onReceive(Context context, Intent intent) {
case PjActions.EVENT_MESSAGE_RECEIVED:
onMessageReceived(intent);
break;
case PjActions.EVENT_TYPING_INDICATION_RECEIVED:
onTypingIndicationReceived(intent);
break;
case PjActions.EVENT_MESSAGE_STATUS_RECEIVED:
onInstantMessageStatusReceived(intent);
break;
case PjActions.EVENT_CALL_RECEIVED:
onCallReceived(intent);
break;
Expand Down Expand Up @@ -100,6 +109,20 @@ private void onMessageReceived(Intent intent) {
emit("pjSipMessageReceived", params);
}

private void onTypingIndicationReceived(Intent intent) {
String json = intent.getStringExtra("data");
Object params = ArgumentUtils.fromJson(json);

emit("pjSipTypingIndicationReceived", params);
}

private void onInstantMessageStatusReceived(Intent intent) {
String json = intent.getStringExtra("data");
Object params = ArgumentUtils.fromJson(json);

emit("pjSipMessageStatusReceived", params);
}

private void onCallReceived(Intent intent) {
String json = intent.getStringExtra("data");
Object params = ArgumentUtils.fromJson(json);
Expand Down Expand Up @@ -127,7 +150,7 @@ private void onCallback(Intent intent) {
if (callbacks.containsKey(id)) {
callback = callbacks.remove(id);
} else {
Log.w(TAG, "Callback with \""+ id +"\" identifier not found (\""+ intent.getAction() +"\")");
Log.w(TAG, "Callback with \"" + id + "\" identifier not found (\"" + intent.getAction() + "\")");
}
}

Expand Down
22 changes: 22 additions & 0 deletions android/src/main/java/one/telefon/sip2/PjSipBuddy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package one.telefon.sip2;

import org.pjsip.pjsua2.Buddy;
import org.pjsip.pjsua2.BuddyConfig;
import android.util.Log;

public class PjSipBuddy extends Buddy {
private static String TAG = "PjSipBuddy";

PjSipBuddy(PjSipAccount account, String destination) {
super();
BuddyConfig cfg = new BuddyConfig();
cfg.setUri(destination);
cfg.setSubscribe(false);

try {
create(account, cfg);
} catch (Exception e) {
Log.e(TAG, "Error while creating buddy", e);
}
}
}
Loading