-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMainActivity.java
203 lines (180 loc) · 7.04 KB
/
MainActivity.java
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
package io.left.hellomesh;
import static io.left.rightmesh.mesh.MeshManager.DATA_RECEIVED;
import static io.left.rightmesh.mesh.MeshManager.PEER_CHANGED;
import static io.left.rightmesh.mesh.MeshManager.REMOVED;
import android.app.Activity;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import io.left.rightmesh.android.AndroidMeshManager;
import io.left.rightmesh.android.AndroidMeshService;
import io.left.rightmesh.id.MeshId;
import io.left.rightmesh.mesh.MeshManager;
import io.left.rightmesh.mesh.MeshManager.PeerChangedEvent;
import io.left.rightmesh.mesh.MeshManager.RightMeshEvent;
import io.left.rightmesh.mesh.MeshStateListener;
import io.left.rightmesh.util.Logger;
import io.left.rightmesh.util.RightMeshException;
import java.util.HashSet;
public class MainActivity extends Activity implements MeshStateListener {
// TODO: this port must match the port assigned, on developer.rightmesh.io, to your key
private static final int HELLO_PORT = 1357;
// MeshManager instance - interface to the mesh network.
AndroidMeshManager mm = null;
// Set to keep track of peers connected to the mesh.
HashSet<MeshId> users = new HashSet<>();
/**
* Called when app first opens, initializes {@link AndroidMeshManager} reference (which will
* start the {@link AndroidMeshService} if it isn't already running.
*
* @param savedInstanceState passed from operating system
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO: when testing, we suggest using rightmesh-library-dev in app:build.gradle,
// and specifying a pattern as the third argument to this call. This will isolate
// your devices so they won't try to connect to the network of the developer sitting
// beside you :D
mm = AndroidMeshManager.getInstance(
MainActivity.this,
MainActivity.this);
}
/**
* Called when activity is on screen.
*/
@Override
protected void onResume() {
try {
super.onResume();
mm.resume();
} catch (RightMeshException.RightMeshServiceDisconnectedException e) {
e.printStackTrace();
}
}
/**
* Called when the app is being closed (not just navigated away from). Shuts down
* the {@link AndroidMeshManager} instance.
*/
@Override
protected void onDestroy() {
try {
super.onDestroy();
mm.stop();
} catch (RightMeshException.RightMeshServiceDisconnectedException e) {
e.printStackTrace();
}
}
/**
* Called by the {@link AndroidMeshService} when the mesh state changes.
* Initializes mesh connection
* on first call.
*
* @param uuid our own user id on first detecting
* @param state state which indicates SUCCESS or an error code
*/
@Override
public void meshStateChanged(MeshId uuid, int state) {
if (state == MeshStateListener.SUCCESS) {
try {
// Binds this app to MESH_PORT.
// This app will now receive all events generated on that port.
mm.bind(HELLO_PORT);
// Subscribes handlers to receive events from the mesh.
mm.on(DATA_RECEIVED, this::handleDataReceived);
mm.on(PEER_CHANGED, this::handlePeerChanged);
// Enable buttons now that mesh is connected.
Button btnConfigure = findViewById(R.id.btnConfigure);
Button btnSend = findViewById(R.id.btnHello);
btnConfigure.setEnabled(true);
btnSend.setEnabled(true);
} catch (RightMeshException e) {
String status = "Error initializing the library" + e.toString();
Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
TextView txtStatus = findViewById(R.id.txtStatus);
txtStatus.setText(status);
return;
}
}
// Update display on successful calls (i.e. not FAILURE or DISABLED).
if (state == MeshStateListener.SUCCESS || state == MeshStateListener.RESUME) {
updateStatus();
}
}
/**
* Update the {@link TextView} with a list of all peers.
*/
private void updateStatus() {
StringBuilder status = new StringBuilder("uuid: " + mm.getUuid().toString() + "\npeers:\n");
for (MeshId user : users) {
status.append(user.toString()).append("\n");
}
TextView txtStatus = findViewById(R.id.txtStatus);
txtStatus.setText(status.toString());
}
/**
* Handles incoming data events from the mesh - toasts the contents of the data.
*
* @param e event object from mesh
*/
private void handleDataReceived(RightMeshEvent e) {
final MeshManager.DataReceivedEvent event = (MeshManager.DataReceivedEvent) e;
runOnUiThread(() -> {
// Toast data contents.
String message = new String(event.data);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
// Play a notification.
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(MainActivity.this, notification);
r.play();
});
}
/**
* Handles peer update events from the mesh - maintains a list of peers and updates the display.
*
* @param e event object from mesh
*/
private void handlePeerChanged(RightMeshEvent e) {
// Update peer list.
PeerChangedEvent event = (PeerChangedEvent) e;
if (event.state != REMOVED && !users.contains(event.peerUuid)) {
users.add(event.peerUuid);
} else if (event.state == REMOVED) {
users.remove(event.peerUuid);
}
// Update display.
runOnUiThread(this::updateStatus);
}
/**
* Sends "hello" to all known peers.
*
* @param v calling view
* @throws RightMeshException Throws exception when there's an error in the library
*/
public void sendHello(View v) throws RightMeshException {
for (MeshId receiver : users) {
String msg = "Hello to: " + receiver + " from" + mm.getUuid();
Logger.log(this.getClass().getCanonicalName(), "MSG: " + msg);
byte[] testData = msg.getBytes();
mm.sendDataReliable(receiver, HELLO_PORT, testData);
}
}
/**
* Open mesh settings screen.
*
* @param v calling view
*/
public void configure(View v) {
try {
mm.showSettingsActivity();
} catch (RightMeshException ex) {
Logger.log(this.getClass().getCanonicalName(), "Service not connected");
}
}
}