Skip to content

Commit 72f63c5

Browse files
committed
integrating asap online message delivery
1 parent 05ba943 commit 72f63c5

18 files changed

Lines changed: 519 additions & 265 deletions

app/build.gradle

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

5-
// we produce a library by commenting out that line:
6-
//apply plugin: 'com.android.application'
7-
apply plugin: 'com.android.library'
5+
// choose one line either for an app or a lib
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

7.57 KB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
1212

13-
<activity android:name="net.sharksystem.asap.android.example.MainActivity">
13+
<activity android:name="net.sharksystem.asap.android.example.ASAPServiceTestActivity">
1414
<intent-filter>
1515
<action android:name="android.intent.action.MAIN" />
1616

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ASAP {
1010

1111
public static final String USER = "user";
1212
public static final String FOLDER = "folder";
13+
public static final String RECIPIENT = "recipient";
1314

1415
public static final String ASAP_RECEIVED_ACTION = "net.sharksystem.asap.received";
1516

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.os.Message;
55

66
public class ASAPServiceMethods {
7-
public static final int ADD_MESSAGE = 0;
7+
public static final int SEND_MESSAGE = 0;
88
public static final int ASK_PROTOCOL_STATUS = 9;
99

1010
public static final int START_WIFI_DIRECT = 1;
@@ -17,17 +17,4 @@ public class ASAPServiceMethods {
1717

1818
public static final int START_BROADCASTS = 7;
1919
public static final int STOP_BROADCASTS = 8;
20-
21-
public static Message createAddASAPMessageMessage(CharSequence format, CharSequence uri,
22-
byte[] asapMessage) {
23-
24-
Message msg = Message.obtain(null, ASAPServiceMethods.ADD_MESSAGE, 0, 0);
25-
Bundle msgData = new Bundle();
26-
msgData.putCharSequence(ASAP.FORMAT, format);
27-
msgData.putCharSequence(ASAP.URI, uri);
28-
msgData.putByteArray(ASAP.MESSAGE_CONTENT, asapMessage);
29-
msg.setData(msgData);
30-
31-
return msg;
32-
}
3320
}

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ASAPActivity extends AppCompatActivity implements
3030
ASAPServiceRequestListener, ASAPServiceNotificationListener {
3131

3232
private static final int MY_REQUEST_2ENABLE_BT = 1;
33+
private static final int MY_REQUEST_SET_BT_DISCOVERABLE = 2;
3334

3435
private Messenger mService;
3536
private boolean mBound;
@@ -40,6 +41,10 @@ public ASAPActivity(ASAPApplication asapApplication) {
4041
this.asapApplication = asapApplication;
4142
}
4243

44+
protected ASAPApplication getASAPApplication() {
45+
return this.asapApplication;
46+
}
47+
4348
///////////////////////////////////////////////////////////////////////////////////////
4449
// asap service requests //
4550
///////////////////////////////////////////////////////////////////////////////////////
@@ -83,26 +88,27 @@ public void asapSrcRq_startBTDiscoverable(int time) {
8388
this.visibilityTime);
8489

8590
// ask user to confirm - result is passed to onActivityResult
86-
this.startActivity(discoverableIntent);
91+
this.startActivityForResult(discoverableIntent, MY_REQUEST_SET_BT_DISCOVERABLE);
8792
}
8893

8994
public void onActivityResult(int requestCode, int resultCode, Intent data) {
9095
Log.d(this.getLogStart(),
91-
"requestCode == " + requestCode +
96+
".onActivityResult(): requestCode == " + requestCode +
9297
" / resultCode == " + resultCode);
9398

9499
if(requestCode == MY_REQUEST_2ENABLE_BT && resultCode == RESULT_OK) {
95100
Log.d(this.getLogStart(), "Bluetooth now enabled - ask service to start BT");
96101
this.sendMessage2Service(ASAPServiceMethods.START_BLUETOOTH);
97102
}
98103

99-
if(resultCode == this.visibilityTime) {
100-
Log.d(this.getLogStart(), "user allowed BT discoverability for seconds: "
101-
+ this.visibilityTime);
104+
if(requestCode == MY_REQUEST_SET_BT_DISCOVERABLE) {
105+
Log.d(this.getLogStart(),
106+
"user allowed BT discoverability for seconds: "
107+
+ resultCode);
102108

103109
// notify
104-
Log.d(this.getLogStart(), "call aspNotifyBTDiscoverableStarted()");
105-
this.aspNotifyBTDiscoverableStarted();
110+
Log.d(this.getLogStart(), "call asapNotifyBTDiscoverableStarted()");
111+
this.asapNotifyBTDiscoverableStarted();
106112
}
107113
}
108114

@@ -266,6 +272,7 @@ protected void onStart() {
266272
// Bind to the service
267273
super.onStart();
268274
Log.d(this.getLogStart(), "onStart");
275+
this.asapApplication.setActivity(this);
269276
this.setupASAPServiceNotificationBroadcastReceiver();
270277
this.setupASAPReceivedBroadcastReceiver();
271278
this.bindServices();
@@ -349,7 +356,7 @@ public void onServiceConnected(ComponentName className, IBinder service) {
349356
mService = new Messenger(service);
350357
mBound = true;
351358

352-
Log.d(getLogStart(), "connection established");
359+
Log.d(getLogStart(), "asap activity got connected to asap service");
353360
if(messageStorage != null && messageStorage.size() > 0) {
354361
Log.d(getLogStart(), "send stored messages | #msg = " + messageStorage.size());
355362
for(Message msg : messageStorage) {
@@ -369,7 +376,7 @@ public void onServiceDisconnected(ComponentName className) {
369376
};
370377

371378
@Override
372-
public void aspNotifyBTDiscoverableStarted() {
379+
public void asapNotifyBTDiscoverableStarted() {
373380
this.asapApplication.setBTDiscoverable(true);
374381
}
375382

@@ -379,22 +386,27 @@ public void asapNotifyBTDiscoverableStopped() {
379386
}
380387

381388
@Override
382-
public void aspNotifyBTEnvironmentStarted() {
389+
public void asapNotifyBTEnvironmentStarted() {
383390
this.asapApplication.setBTEnvironmentRunning(true);
384391
}
385392

386393
@Override
387-
public void aspNotifyBTEnvironmentStopped() {
394+
public void asapNotifyBTEnvironmentStopped() {
388395
this.asapApplication.setBTEnvironmentRunning(false);
389396
}
390397

391398
@Override
392-
public void aspNotifyBTDiscoveryStarted() {
399+
public void asapNotifyOnlinePeersChanged(List<CharSequence> peerList) {
400+
this.asapApplication.setOnlinePeersList(peerList);
401+
}
402+
403+
@Override
404+
public void asapNotifyBTDiscoveryStarted() {
393405
this.asapApplication.setBTDiscovery(true);
394406
}
395407

396408
@Override
397-
public void aspNotifyBTDiscoveryStopped() {
409+
public void asapNotifyBTDiscoveryStopped() {
398410
this.asapApplication.setBTDiscovery(false);
399411
}
400412

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyBroadcastReceiver;
2121
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
2222

23+
import java.lang.reflect.Array;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -39,12 +40,13 @@ public class ASAPApplication extends BroadcastReceiver {
3940
private boolean btDisoveryOn = false;
4041
private boolean btEnvironmentOn = false;
4142

42-
private Activity activity;
43+
private ASAPActivity activity;
4344

4445
private List<String> requiredPermissions;
4546
private List<String> grantedPermissions = new ArrayList<>();
4647
private List<String> deniedPermissions = new ArrayList<>();
4748
private int activityCount = 0;
49+
private List<CharSequence> onlinePeerList = new ArrayList<>();
4850

4951
/**
5052
* setup application by calling getASAPOwner(), getFolderName(), getASAPOnlineExchange().
@@ -145,6 +147,9 @@ public static ASAPApplication getASAPApplication() {
145147
return ASAPApplication.singleton;
146148
}
147149

150+
public String getApplicationRootFolder(String appName) {
151+
return this.getASAPRootFolder() + "/" + appName;
152+
}
148153

149154
public void activityCreated(ASAPActivity asapActivity) {
150155
this.setActivity(asapActivity);
@@ -161,11 +166,11 @@ public void activityDestroyed(ASAPActivity asapActivity) {
161166
+ this.activityCount);
162167
}
163168

164-
protected Activity getActivity() {
169+
public ASAPActivity getActivity() {
165170
return this.activity;
166171
}
167172

168-
void setActivity(Activity activity) {
173+
void setActivity(ASAPActivity activity) {
169174
Log.d(this.getLogStart(), "activity set");
170175
this.activity = activity;
171176
}
@@ -285,4 +290,22 @@ public void chunkReceived(String sender, String uri, String foldername, int era)
285290

286291
Log.d(this.getLogStart(), "should inform apps about it");
287292
}
293+
294+
public List<CharSequence> getOnlinePeerList() {
295+
return this.onlinePeerList;
296+
}
297+
298+
public void setOnlinePeersList(List<CharSequence> peerList) {
299+
StringBuilder sb = new StringBuilder();
300+
sb.append(this.getLogStart());
301+
sb.append("#online peers: ");
302+
sb.append(peerList.size());
303+
for(CharSequence peerName : peerList) {
304+
sb.append(" | ");
305+
sb.append(peerName);
306+
}
307+
308+
Log.d(this.getLogStart(), sb.toString());
309+
this.onlinePeerList = peerList;
310+
}
288311
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package net.sharksystem.asap.android.apps;
2+
3+
import android.os.Bundle;
4+
import android.os.Message;
5+
import android.util.Log;
6+
7+
import net.sharksystem.asap.ASAPAbstractOnlineMessageSender;
8+
import net.sharksystem.asap.ASAPException;
9+
import net.sharksystem.asap.ASAPStorage;
10+
import net.sharksystem.asap.android.ASAP;
11+
import net.sharksystem.asap.android.ASAPServiceMethods;
12+
import net.sharksystem.asap.android.Util;
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
17+
public class ASAPOnlineMessageSenderUserSide extends ASAPAbstractOnlineMessageSender {
18+
private final ASAPApplication asapApplication;
19+
20+
public ASAPOnlineMessageSenderUserSide(ASAPApplication asapApplication) {
21+
this.asapApplication = asapApplication;
22+
}
23+
24+
@Override
25+
public void sendASAPAssimilate(CharSequence format, CharSequence uri,
26+
List<CharSequence> recipients, byte[] messageAsBytes,
27+
int era) throws IOException, ASAPException {
28+
29+
Message msg;
30+
31+
Log.d(this.getLogStart(), "sendASAPAssimilate called");
32+
if(recipients == null || recipients.size() < 1) {
33+
Log.w(this.getLogStart(), "send to anonymous recipients");
34+
if(this.asapApplication.getOnlinePeerList().size() < 1) {
35+
Log.d(this.getLogStart(), "no online peers at all - send nothing");
36+
} else {
37+
msg = this.createSendASAPMessageMessage(
38+
format, uri, null, messageAsBytes, era);
39+
40+
this.asapApplication.getActivity().sendMessage2Service(msg);
41+
}
42+
} else {
43+
for (CharSequence recipient : recipients) {
44+
if(this.isOnline(recipient)) {
45+
msg = this.createSendASAPMessageMessage(format, uri, recipient, messageAsBytes, era);
46+
this.asapApplication.getActivity().sendMessage2Service(msg);
47+
} else {
48+
Log.d(this.getLogStart(), "no message sent / peer not online: " + recipient);
49+
}
50+
}
51+
}
52+
}
53+
54+
private boolean isOnline(CharSequence peer) {
55+
// TODO: need string comparision a la equalsIgnoreCase?
56+
Log.d(this.getLogStart(), "TODO: need string comparision a la equalsIgnoreCase?");
57+
return this.asapApplication.getOnlinePeerList().contains(peer);
58+
}
59+
60+
@Override
61+
public void sendASAPAssimilate(CharSequence format, CharSequence uri, byte[] messageAsBytes, int era)
62+
throws IOException, ASAPException {
63+
64+
this.sendASAPAssimilate(format, uri, null, messageAsBytes, era);
65+
}
66+
67+
private Message createSendASAPMessageMessage(CharSequence format, CharSequence uri,
68+
CharSequence recipient, byte[] asapMessage,
69+
int era) throws ASAPException {
70+
71+
if(format == null || format.length() < 1
72+
|| uri == null || uri.length() < 1
73+
|| asapMessage == null || asapMessage.length < 1
74+
) {
75+
throw new ASAPException("format, uri, message cannot be null or empty");
76+
}
77+
78+
Message msg = Message.obtain(null, ASAPServiceMethods.SEND_MESSAGE, 0, 0);
79+
Bundle msgData = new Bundle();
80+
msgData.putCharSequence(ASAP.FORMAT, format);
81+
if(recipient != null) { msgData.putCharSequence(ASAP.RECIPIENT, recipient); }
82+
msgData.putCharSequence(ASAP.URI, uri);
83+
msgData.putByteArray(ASAP.MESSAGE_CONTENT, asapMessage);
84+
msg.setData(msgData);
85+
86+
return msg;
87+
}
88+
89+
private String getLogStart() {
90+
return Util.getLogStart(this);
91+
}
92+
}

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
2525
import static android.bluetooth.BluetoothDevice.ACTION_FOUND;
2626

27+
//https://developer.android.com/guide/topics/connectivity/bluetooth#java
28+
2729
public class BluetoothEngine extends MacLayerEngine {
2830
private static BluetoothEngine engine = null;
2931
private BluetoothAdapter mBluetoothAdapter;
@@ -78,8 +80,6 @@ public void stop() {
7880
// do the real bluetooth stuff //
7981
/////////////////////////////////////////////////////////////////////////////////
8082

81-
//https://developer.android.com/guide/topics/connectivity/bluetooth#java
82-
8383
/**
8484
* Setup Bluetooth environment. Note: Only the environment is created.
8585
* Discovery and discoverability is not initiated.

app/src/main/java/net/sharksystem/asap/android/bluetooth/ScanModeChangedBroadcastReceiver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void onReceive(Context context, Intent intent) {
3636

3737
// check if discovery is gone
3838
if(mode == SCAN_MODE_NONE || mode == SCAN_MODE_CONNECTABLE) {
39+
Log.d(this.getLogStart(), "seems that discoverable stopped: ");
3940
this.context.sendBroadcast(
4041
new ASAPServiceRequestNotifyIntent(
4142
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_DISCOVERABLE_STOPPED

0 commit comments

Comments
 (0)