Skip to content

Commit 8c3ce55

Browse files
committed
bug fix. I disimproved (learned that word today :) ) the asap service message handler some versions ago. Anyway fixed and dealt with a race condition.
1 parent 061448f commit 8c3ce55

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

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/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,28 @@ public void startBluetoothDiscovery() {
219219
this.sendMessage2Service(ASAPServiceMethods.START_BLUETOOTH_DISCOVERY);
220220
}
221221

222+
/*
223+
There is a race condition:
224+
ASAPActivity a can launch ASAPActivity b. It happens (quite often actually) that a.onStop()
225+
is processed after b.onStart() or b.onResume(). In that case, startASAPBroadcast are called
226+
twice in the row (a.onStart() sometimes earlier, than b.onStart()) followed by
227+
stopASAPBroadcast issued by a.onStop(). b would not receive any broadcast - ASAP service
228+
was told to stop to send broadcasts.
229+
230+
Solution: We only stop broadcasting if there if the last activity signs off.
231+
*/
222232
public void startASAPEngineBroadcasts() {
223233
Log.d(this.getLogStart(), "send message to service: start ASAP Engine Broadcasts");
224234
this.sendMessage2Service(ASAPServiceMethods.START_BROADCASTS);
225235
}
226236

227237
public void stopASAPEngineBroadcasts() {
228-
Log.d(this.getLogStart(), "send message to service: stop ASAP Engine Broadcasts");
229-
this.sendMessage2Service(ASAPServiceMethods.STOP_BROADCASTS);
238+
if(this.getASAPApplication().getNumberASAPActivities() == 1) {
239+
Log.d(this.getLogStart(), "send message to service: stop ASAP Engine Broadcasts");
240+
this.sendMessage2Service(ASAPServiceMethods.STOP_BROADCASTS);
241+
} else {
242+
Log.d(this.getLogStart(), "don't stop broadcasts there are still listeners");
243+
}
230244
}
231245

232246
public void refreshProtocolStatus() {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class ASAPApplication extends BroadcastReceiver {
5252
private List<String> requiredPermissions;
5353
private List<String> grantedPermissions = new ArrayList<>();
5454
private List<String> deniedPermissions = new ArrayList<>();
55-
private int activityCount = 0;
55+
private int activityASAPActivities = 0;
5656
private List<CharSequence> onlinePeerList = new ArrayList<>();
5757

5858
/**
@@ -65,6 +65,10 @@ protected ASAPApplication(Collection<CharSequence> supportedFormats) {
6565
ASAPAndroid.ONLINE_EXCHANGE_DEFAULT);
6666
}
6767

68+
int getNumberASAPActivities() {
69+
return this.activityASAPActivities;
70+
}
71+
6872
/**
6973
* setup application without parameter. Use default for owner, root folder for asap storage
7074
* and online exchange behaviour. Don't setup any asap engine - take engines which are
@@ -206,15 +210,15 @@ public void activityCreated(ASAPActivity asapActivity, boolean initASAPApplicati
206210
this.setActivity(asapActivity);
207211
if(initASAPApplication) this.initialize();
208212

209-
this.activityCount++;
213+
this.activityASAPActivities++;
210214
Log.d(this.getLogStart(), "activity created. New activity count == "
211-
+ this.activityCount);
215+
+ this.activityASAPActivities);
212216
}
213217

214218
public void activityDestroyed(ASAPActivity asapActivity) {
215-
this.activityCount--;
219+
this.activityASAPActivities--;
216220
Log.d(this.getLogStart(), "activity destroyed. New activity count == "
217-
+ this.activityCount);
221+
+ this.activityASAPActivities);
218222
}
219223

220224
public ASAPActivity getActivity() {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public void handleMessage(Message msg) {
3838
this.handleCreateClosedChannel(msg);
3939
break;
4040

41+
case ASAPServiceMethods.START_BROADCASTS:
42+
this.asapService.resumeBroadcasts();
43+
break;
44+
45+
case ASAPServiceMethods.STOP_BROADCASTS:
46+
this.asapService.pauseBroadcasts();
47+
break;
48+
4149
case ASAPServiceMethods.START_WIFI_DIRECT:
4250
this.asapService.startWifiDirect();
4351
break;
@@ -74,10 +82,6 @@ public void handleMessage(Message msg) {
7482
this.asapService.startReconnectPairedDevices();
7583
break;
7684

77-
case ASAPServiceMethods.STOP_BROADCASTS:
78-
this.asapService.pauseBroadcasts();
79-
break;
80-
8185
default:
8286
super.handleMessage(msg);
8387
}

0 commit comments

Comments
 (0)