Skip to content

Commit 1817055

Browse files
committed
ASAPActivity.sendMessage actually sends a message - yes, that's a bug fix.
1 parent e738821 commit 1817055

File tree

10 files changed

+75
-25
lines changed

10 files changed

+75
-25
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ https://developer.android.com/studio/projects/android-library
33
*/
44

55
// choose one line either for an app or a lib
6-
//apply plugin: 'com.android.application'
7-
apply plugin: 'com.android.library'
6+
apply plugin: 'com.android.application'
7+
//apply plugin: 'com.android.library'
88

99
android {
1010
compileSdkVersion 28
1111
defaultConfig {
1212
// we produce a library by commenting out that line:
13-
// applicationId "net.sharksystem.asap.example"
13+
applicationId "net.sharksystem.asap.example"
1414
minSdkVersion 23
1515
targetSdkVersion 28
1616
versionCode 1

app/libs/ASAP_Engine_0.5.0.jar

14 Bytes
Binary file not shown.

app/src/main/java/net/sharksystem/asap/android/ASAP.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ASAP {
1111
public static final String USER = "user";
1212
public static final String FOLDER = "folder";
1313
public static final String RECIPIENT = "recipient";
14+
public static final String RECIPIENTS = "recipients";
1415

1516
public static final String ASAP_CHUNK_RECEIVED_ACTION = "net.sharksystem.asap.received";
1617

app/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestListener;
2424
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyBroadcastReceiver;
2525
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
26+
import net.sharksystem.asap.util.Helper;
2627

2728
import java.util.ArrayList;
2829
import java.util.Collection;
@@ -48,25 +49,30 @@ protected ASAPApplication getASAPApplication() {
4849
}
4950

5051
public void sendASAPMessage(CharSequence appName, CharSequence uri,
51-
Collection<CharSequence> recipients, byte[] message) throws ASAPException {
52+
Collection<CharSequence> recipients, byte[] message)
53+
throws ASAPException {
5254

5355
if(appName == null || appName.length() == 0
5456
|| uri == null || uri.length() == 0
55-
|| recipients == null || recipients.size() == 0
5657
|| message == null || message.length == 0
5758
) throw new ASAPException("parameter must not be null");
5859

59-
for(CharSequence recipient : recipients) {
60-
Message msg = Message.obtain(null, ASAPServiceMethods.SEND_MESSAGE, 0, 0);
61-
Bundle bundle = new Bundle();
62-
bundle.putString(ASAP.FORMAT, appName.toString());
63-
bundle.putString(ASAP.URI, uri.toString());
64-
bundle.putString(ASAP.RECIPIENT, recipient.toString());
65-
bundle.putByteArray(ASAP.MESSAGE_CONTENT, message); // important
66-
// bundle.putInt(ASAP.ERA, 0); // optional
67-
68-
this.sendMessage2Service(msg);
60+
// prepare message
61+
Message msg = Message.obtain(null, ASAPServiceMethods.SEND_MESSAGE, 0, 0);
62+
Bundle bundle = new Bundle();
63+
bundle.putString(ASAP.FORMAT, appName.toString());
64+
bundle.putString(ASAP.URI, uri.toString());
65+
bundle.putByteArray(ASAP.MESSAGE_CONTENT, message); // important
66+
// bundle.putInt(ASAP.ERA, 0); // optional
67+
68+
msg.setData(bundle);
69+
if(recipients != null && recipients.size() > 0) {
70+
String recipientsString = Helper.collection2String(recipients);
71+
bundle.putString(ASAP.RECIPIENTS, recipientsString);
6972
}
73+
74+
msg.setData(bundle);
75+
this.sendMessage2Service(msg);
7076
}
7177

7278
///////////////////////////////////////////////////////////////////////////////////////

app/src/main/java/net/sharksystem/asap/android/example/ASAPServiceTestActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void asapNotifyBTEnvironmentStopped() {
130130
private final String APPNAME = "ASAP_TEST_APP";
131131
private final String URI = "sn://chat";
132132
private final String MESSAGE = "Hi, that's a message";
133+
private final byte[] BYTE_MESSAGE = MESSAGE.getBytes();
133134

134135
ASAPStorage asapStorage;
135136

@@ -217,8 +218,15 @@ private void checkStorage() throws IOException, ASAPException {
217218
}
218219

219220
private void addMessage() throws IOException, ASAPException {
221+
// indirect - prefered way - send via ASAPService
222+
Log.d(this.getLogStart(), "ask asap service to deliver a message");
223+
this.sendASAPMessage(APPNAME, URI, null, BYTE_MESSAGE);
224+
225+
/*
226+
// direct approach - write into local file system
220227
this.checkStorage();
221228
Log.d(this.getLogStart(), "add message to storage: " + MESSAGE);
222229
this.asapStorage.add(URI, MESSAGE);
230+
*/
223231
}
224232
}

app/src/main/java/net/sharksystem/asap/android/service/ASAPMessageHandler.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import android.os.Message;
66
import android.util.Log;
77

8+
import net.sharksystem.asap.ASAPEngine;
9+
import net.sharksystem.asap.ASAPEngineFS;
810
import net.sharksystem.asap.ASAPException;
911
import net.sharksystem.asap.ASAPOnlineMessageSender;
12+
import net.sharksystem.asap.MultiASAPEngineFS;
1013
import net.sharksystem.asap.android.ASAP;
1114
import net.sharksystem.asap.android.ASAPServiceMethods;
15+
import net.sharksystem.asap.util.Helper;
1216

1317
import java.io.IOException;
1418
import java.util.ArrayList;
@@ -24,9 +28,11 @@ class ASAPMessageHandler extends Handler {
2428

2529
@Override
2630
public void handleMessage(Message msg) {
31+
Log.d(LOGSTART, "handleMessage called with what == " + msg.what);
2732
try {
2833
switch (msg.what) {
2934
case ASAPServiceMethods.SEND_MESSAGE:
35+
Log.d(LOGSTART, "handleMessage SEND_MESSAGE called");
3036
this.handleSendMessage(msg);
3137
break;
3238

@@ -81,8 +87,8 @@ private void handleSendMessage(Message msg) {
8187
if (msgData != null) {
8288
String format = msgData.getString(ASAP.FORMAT);
8389
String uri = msgData.getString(ASAP.URI);
84-
String recipient = msgData.getString(ASAP.RECIPIENT);
85-
byte[] content = msgData.getByteArray(ASAP.MESSAGE_CONTENT);
90+
String recipientsString = msgData.getString(ASAP.RECIPIENTS);
91+
byte[] message = msgData.getByteArray(ASAP.MESSAGE_CONTENT);
8692
int era = msgData.getInt(ASAP.ERA);
8793

8894
Log.d(LOGSTART, "received send message request");
@@ -91,8 +97,8 @@ private void handleSendMessage(Message msg) {
9197
return;
9298
}
9399

94-
if(content == null) {
95-
Log.e(LOGSTART, "format content must not be empty");
100+
if(message == null) {
101+
Log.e(LOGSTART, "message must not be empty");
96102
return;
97103
}
98104

@@ -101,6 +107,35 @@ private void handleSendMessage(Message msg) {
101107
return;
102108
}
103109

110+
MultiASAPEngineFS multiASAPEngine = asapService.getMultiASAPEngine();
111+
try {
112+
ASAPEngine asapEngine = null;
113+
try {
114+
asapEngine = multiASAPEngine.getEngineByFormat(format);
115+
}
116+
catch(ASAPException e ) {
117+
// engine does not yet exist
118+
Log.d(LOGSTART, " (TODO): asap engine for format not exists - going to create one - potential security leak:" + format);
119+
}
120+
121+
if(asapEngine == null) {
122+
// still null - create on - TODO security leak?
123+
asapEngine = multiASAPEngine.createEngineByFormat(format);
124+
}
125+
126+
if(recipientsString != null) {
127+
// extract recipients - and setup closed channel.
128+
asapEngine.createChannel(uri, Helper.string2CharSequenceSet(recipientsString));
129+
}
130+
131+
asapEngine.add(uri, message);
132+
133+
} catch (ASAPException | IOException e) {
134+
Log.w(LOGSTART, e.getLocalizedMessage());
135+
}
136+
137+
138+
/*
104139
try {
105140
StringBuilder sb = new StringBuilder();
106141
sb.append("going to send message over an open connection:");
@@ -128,11 +163,12 @@ private void handleSendMessage(Message msg) {
128163
}
129164
130165
asapOnlineMessageSender.sendASAPAssimilate(
131-
format, uri, recipient, content, era);
166+
format, uri, recipient, message, era);
132167
133168
} catch (Throwable e) {
134169
e.printStackTrace();
135170
}
171+
*/
136172
}
137173
}
138174
}

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ String getASAPRootFolderName() {
5454
return this.asapEngineRootFolderName;
5555
}
5656

57-
public MultiASAPEngineFS getASAPEngine() {
57+
public MultiASAPEngineFS getMultiASAPEngine() {
5858
if(this.asapMultiEngine == null) {
5959
Log.d(LOGSTART, "try to get asapMultiEngine");
6060

app/src/main/java/net/sharksystem/asap/android/service/MacLayerEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.util.Log;
55

66
import net.sharksystem.asap.ASAPException;
7-
import net.sharksystem.asap.MultiASAPEngineFS;
87
import net.sharksystem.asap.protocol.ASAPConnection;
98

109
import java.io.IOException;
@@ -131,7 +130,7 @@ protected void launchASAPConnection(
131130
// TestConnectionHandler testConnectionHandler = new TestConnectionHandler(this.is, this.os);
132131
// testConnectionHandler.start();
133132
this.asapConnections.put(address,
134-
this.getAsapService().getASAPEngine().handleConnection(inputStream, outputStream));
133+
this.getAsapService().getMultiASAPEngine().handleConnection(inputStream, outputStream));
135134

136135
} catch (IOException | ASAPException e) {
137136
Log.d(this.getLogStart(), "while lauching asap connection: " + e.getLocalizedMessage());

app/src/main/java/net/sharksystem/asap/android/wifidirect/WifiP2PEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void onConnectionInfoAvailable(WifiP2pInfo info) {
300300

301301
// create an ASAPSession with connection parameters
302302
ASAPConnectionLauncher ASAPConnectionLauncher = new ASAPConnectionLauncher(channelCreator,
303-
this.getAsapService().getASAPEngine());
303+
this.getAsapService().getMultiASAPEngine());
304304

305305
ASAPConnectionLauncher.start();
306306
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.5.0'
10+
classpath 'com.android.tools.build:gradle:3.5.2'
1111

1212

1313
// NOTE: Do not place your application dependencies here; they belong

0 commit comments

Comments
 (0)