-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASAPExampleActivity.java
More file actions
249 lines (213 loc) · 10.2 KB
/
ASAPExampleActivity.java
File metadata and controls
249 lines (213 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package net.sharksystem.asap.android.example;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPStorage;
import net.sharksystem.asap.android.R;
// TODO do not work with internal classes in any example!!
import net.sharksystem.asap.android.apps.ASAPActivity;
import net.sharksystem.asap.engine.ASAPEngineFS;
import net.sharksystem.utils.fs.FSUtils;
import java.io.IOException;
import java.util.Set;
public class ASAPExampleActivity extends ASAPActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
}
public void onClick(View view) {
View startWifiButton = findViewById(R.id.startWifiDirect);
View stopWifiButton = findViewById(R.id.stopWifiDirect);
View startBTButton = findViewById(R.id.startBT);
View stopBTButton = findViewById(R.id.stopBT);
View startLoRaButton = findViewById(R.id.startLoRa);
View stopLoRaButton = findViewById(R.id.stopLoRa);
View startHubTester = findViewById(R.id.onASAPHubTesterButton);
View startHubPerformanceTest = findViewById(R.id.onASAPHubPerformanceTestButton);
View startTCPEncounterButton = findViewById(R.id.onASAPTCPEncounterButton);
if (view == startWifiButton) {
Log.d(this.getLogStart(), "start wifi button pressed - send message");
super.startWifiP2P();
} else if (view == stopWifiButton) {
Log.d(this.getLogStart(), "stop wifi button pressed - send message");
super.stopWifiP2P();
} else if (view == startBTButton) {
Log.d(this.getLogStart(), "start bt button pressed - ask service to start bt");
super.startBluetooth();
} else if (view == stopBTButton) {
Log.d(this.getLogStart(), "stop bt button pressed - send message");
super.stopBluetooth();
}
/*
else if(view == findViewById(R.id.startDiscoverable)) {
Log.d(this.getLogStart(), "start discoverable button pressed - send message");
this.startBluetoothDiscoverable();
}
else if(view == findViewById(R.id.startDiscovery)) {
Log.d(this.getLogStart(), "start discover button pressed - send message");
this.startBluetoothDiscovery();
}
*/
else if (view == findViewById(R.id.startDiscoverableAndDiscovery)) {
Log.d(this.getLogStart(),
"start discoverable and discover button pressed - send messages");
super.startBluetoothDiscovery();
super.startBluetoothDiscoverable();
} else if (view == startLoRaButton) {
Log.d(this.getLogStart(), "start LoRa button pressed - send message");
super.startLoRa();
} else if (view == stopLoRaButton) {
Log.d(this.getLogStart(), "stop LoRa button pressed - send message");
super.stopLoRa();
} else if (view == startHubTester) {
Log.d(this.getLogStart(), "start Hub Tester button pressed");
this.startActivity(new Intent(this, ASAPExampleHubTesterActivity.class));
} else if (view == startHubPerformanceTest) {
Log.d(this.getLogStart(), "start Hub Performance Test button pressed");
this.startActivity(new Intent(this, ASAPExamplePerformanceTestActivity.class));
}else if (view == startTCPEncounterButton) {
Log.d(this.getLogStart(), "start TCP Encounter button pressed");
this.startActivity(new Intent(this, ASAPExampleTCPEncounterActivity.class));
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// changes in active layer 2 connections //
///////////////////////////////////////////////////////////////////////////////////////////
public void asapNotifyOnlinePeersChanged(Set<CharSequence> onlinePeerList) {
super.asapNotifyOnlinePeersChanged(onlinePeerList);
TextView peerListTextView = this.findViewById(R.id.onlinePeersList);
if (onlinePeerList == null || onlinePeerList.size() < 1) {
peerListTextView.setText("no peers online");
} else {
StringBuilder sb = new StringBuilder();
sb.append("peers online:");
sb.append("\n");
for (CharSequence peerID : onlinePeerList) {
sb.append("id: ");
sb.append(peerID);
sb.append("\n");
}
peerListTextView.setText(sb.toString());
}
peerListTextView.refreshDrawableState();
}
///////////////////////////////////////////////////////////////////////////////////////////
// helps debugging //
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void asapNotifyBTDiscoverableStopped() {
super.asapNotifyBTDiscoverableStopped();
Log.d(this.getLogStart(), "got notified: discoverable stopped");
Toast.makeText(this, "discoverable stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void asapNotifyBTDiscoveryStopped() {
super.asapNotifyBTDiscoveryStopped();
Log.d(this.getLogStart(), "got notified: discovery stopped");
Toast.makeText(this, "discovery stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void asapNotifyBTDiscoveryStarted() {
super.asapNotifyBTDiscoveryStarted();
Log.d(this.getLogStart(), "got notified: discovery started");
Toast.makeText(this, "discovery started", Toast.LENGTH_SHORT).show();
}
@Override
public void asapNotifyBTDiscoverableStarted() {
super.asapNotifyBTDiscoverableStarted();
Log.d(this.getLogStart(), "got notified: discoverable started");
Toast.makeText(this, "discoverable started", Toast.LENGTH_SHORT).show();
}
@Override
public void asapNotifyBTEnvironmentStarted() {
super.asapNotifyBTEnvironmentStarted();
Log.d(this.getLogStart(), "got notified: bluetooth on");
Toast.makeText(this, "bluetooth on", Toast.LENGTH_SHORT).show();
}
@Override
public void asapNotifyBTEnvironmentStopped() {
super.asapNotifyBTEnvironmentStopped();
Log.d(this.getLogStart(), "got notified: bluetooth off");
Toast.makeText(this, "bluetooth off", Toast.LENGTH_SHORT).show();
}
///////////////////////////////////////////////////////////////////////////////////////////
// asap store test scenario(s) //
///////////////////////////////////////////////////////////////////////////////////////////
// TODO change that code - avoid internals interfaces!!
private final String URI = "sn://chat";
private final String MESSAGE = "Hi, that's a message";
private final byte[] BYTE_MESSAGE = MESSAGE.getBytes();
ASAPStorage asapStorage;
public void onSetupCleanASAPStorageClick(View view) {
try {
this.setupCleanASAPStorage();
} catch (IOException | ASAPException e) {
Log.d(this.getLogStart(), "exception: " + e.getLocalizedMessage());
} catch (RuntimeException e) {
Log.d(this.getLogStart(), "runtime exception: " + e.getLocalizedMessage());
}
}
public void onSwitch2ExchangeActivity(View view) {
this.startActivity(new Intent(this, ASAPExampleMessagingActivity.class));
}
private void setupCleanASAPStorage() throws IOException, ASAPException {
String absoluteFolderName = this.getASAPAndroidPeer().
getApplicationRootFolder(ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME);
Log.d(this.getLogStart(), "going to clean folder: " + absoluteFolderName);
FSUtils.removeFolder(absoluteFolderName);
Log.d(this.getLogStart(), "create asap storage with: "
+ this.getASAPAndroidPeer().getOwnerID()
+ " | "
+ this.getASAPAndroidPeer().getApplicationRootFolder(ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME)
+ " | "
+ ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME
);
this.asapStorage = ASAPEngineFS.getASAPStorage(
this.getASAPAndroidPeer().getOwnerID().toString(),
this.getASAPAndroidPeer().getApplicationRootFolder(ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME),
ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME);
}
public void onASAPHub(View view) {
Log.d(this.getLogStart(), "onASAPHub reached");
this.startActivity(new Intent(this, ASAPExampleHubManagementActivity.class));
/*
try {
this.checkStorage();
if(asapOnlineSender != null) {
Log.d(this.getLogStart(), "online message sender already set");
return;
}
this.asapOnlineSender = new ASAPOnlineMessageSenderAndroidUserSide(this.getASAPApplication());
this.asapStorage.attachASAPMessageAddListener(this.asapOnlineSender);
} catch (IOException | ASAPException e) {
Log.d(this.getLogStart(), "exception: " + e.getLocalizedMessage());
}
*/
}
public void onRemoveAddOnlineSenderClick(View view) {
Log.d(this.getLogStart(), "onRemoveAddOnlineSenderClick reached");
Log.d(this.getLogStart(), "onAddOnlineSenderClick reached");
/*
if(this.asapOnlineSender == null) {
Log.d(this.getLogStart(), "online message sender not set");
return;
}
if(this.asapStorage == null) {
Log.d(this.getLogStart(), "asap storage not set");
return;
}
this.asapStorage.detachASAPMessageAddListener();
*/
}
private void checkStorage() throws IOException, ASAPException {
if (this.asapStorage == null) {
Log.d(this.getLogStart(), "storage not yet initialized - clean and setup: " + MESSAGE);
this.setupCleanASAPStorage();
}
}
}