Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
350 lines (294 loc) · 11.7 KB
/
Copy pathMainActivity.java
File metadata and controls
350 lines (294 loc) · 11.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package io.left.hellomesh;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import io.left.rightmesh.android.AndroidMeshManager;
import io.left.rightmesh.android.MeshService;
import io.left.rightmesh.id.MeshID;
import io.left.rightmesh.mesh.MeshManager;
import io.left.rightmesh.mesh.MeshStateListener;
import io.left.rightmesh.util.MeshUtility;
import io.left.rightmesh.util.RightMeshException;
import io.reactivex.functions.Consumer;
import protobuf.HelloMessages;
import protobuf.HelloMessages.HelloMessage;
import protobuf.HelloMessages.MessageType;
import protobuf.HelloMessages.Message;
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;
public class MainActivity extends Activity implements MeshStateListener {
// Port to bind app to.
private static final int HELLO_PORT = 9879;
// 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<>();
TextView txtReceived;
/**
* Called when app first opens, initializes {@link AndroidMeshManager} reference (which will
* start the {@link MeshService} 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);
txtReceived = (TextView) findViewById(R.id.txtReceived);
mm = AndroidMeshManager.getInstance(MainActivity.this, MainActivity.this, "HM-AZIZ");
}
/**
* Called when activity is on screen.
*/
@Override
protected void onResume() {
try {
super.onResume();
mm.resume();
} catch (MeshService.ServiceDisconnectedException 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 (MeshService.ServiceDisconnectedException e) {
e.printStackTrace();
}
}
/**
* Called by the {@link MeshService} 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, new Consumer() {
@Override
public void accept(Object o) throws Exception {
handleDataReceived((MeshManager.RightMeshEvent) o);
}
});
mm.on(PEER_CHANGED, new Consumer() {
@Override
public void accept(Object o) throws Exception {
handlePeerChanged((MeshManager.RightMeshEvent) o);
}
});
// If you are using Java 8 or a lambda backport like RetroLambda, you can use
// a more concise syntax, like the following:
// mm.on(PEER_CHANGED, this::handlePeerChanged);
// mm.on(DATA_RECEIVED, this::dataReceived);
// Enable buttons now that mesh is connected.
Button btnConfigure = (Button) findViewById(R.id.btnConfigure);
Button btnSend = (Button) 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 = (TextView) 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() {
String status = "uuid: " + mm.getUuid().toString() + "\n\n\npeers:\n";
for (MeshID user : users) {
status += user.toString() + "\n";
}
TextView txtStatus = (TextView) findViewById(R.id.txtStatus);
txtStatus.setText(status);
}
/**
* Handles incoming data events from the mesh - toasts the contents of the data.
*
* @param e event object from mesh
*/
int i = 0;
private void handleDataReceived(MeshManager.RightMeshEvent e) {
final MeshManager.DataReceivedEvent event = (MeshManager.DataReceivedEvent) e;
try {
final HelloMessage messageWrapper = HelloMessage.parseFrom(event.data);
MessageType messageType = messageWrapper.getMessageType();
if (messageType == MessageType.MESSAGE) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Message message = messageWrapper.getMessage();
Toast.makeText(MainActivity.this, message.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
} catch (InvalidProtocolBufferException e1) {
e1.printStackTrace();
}
}
/**
* 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(MeshManager.RightMeshEvent e) {
// Update peer list.
MeshManager.PeerChangedEvent event = (MeshManager.PeerChangedEvent) e;
if (!mm.getUuid().equals(event.peerUuid)) {
if (event.state != REMOVED && !users.contains(event.peerUuid)) {
Log.e("Peer_state", "Peer Added =" + event.peerUuid);
users.add(event.peerUuid);
} else if (event.state == REMOVED) {
Log.e("Peer_state", "Peer Removed =" + event.peerUuid);
users.remove(event.peerUuid);
}
// Update display.
runOnUiThread(new Runnable() {
@Override
public void run() {
updateStatus();
}
});
} else {
Log.e("Peer_state", "Self Id =" + event.peerUuid);
}
}
/**
* Sends "hello" to all known peers.
*
* @param v calling view
*/
public void sendHello(View v) throws RightMeshException {
String msg = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
for (MeshID receiver : users) {
String new_msg = "Hello to: " + receiver + " from" + mm.getUuid() + msg;
txtReceived.setText("Send Message Length: " + msg.length());
HelloMessages.Message protoMsg = HelloMessages.Message.newBuilder()
.setMessage(new_msg)
.setMsgtime(System.currentTimeMillis())
.build();
HelloMessages.HelloMessage message = HelloMessages.HelloMessage.newBuilder()
.setMessageType(HelloMessages.MessageType.MESSAGE)
.setMessage(protoMsg)
.build();
sendDataReliable(receiver, message.toByteArray());
/* for (int i = 0; i < 100; i++) {
String new_msg2 = "Index: " + i + new_msg;
sendDataReliable(receiver, new_msg2.getBytes());
}*/
}
}
private void sendDataReliable(MeshID receiver, byte[] msg) {
try {
mm.sendDataReliable(receiver, HELLO_PORT, msg);
} catch (RightMeshException e) {
}
}
public void castData(HashSet<MeshID> meshIDs, byte[] bytes) {
mm.castData(meshIDs, HELLO_PORT, bytes, AndroidMeshManager.CastType.UNICAST, 0);
}
public void sendFile(View v) throws RightMeshException {
if (InvokPermission.getInstance().requestPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
openFileChooser();
}
}
private void sendFile2(String filePath) {
byte[] fileBytes = getFileByteBuffer(filePath);
if (fileBytes == null || fileBytes.length == 0) return;
txtReceived.setText("Send File Length: " + fileBytes.length);
for (MeshID receiver : users) {
for (int i = 0; i < 10; i++) {
sendDataReliable(receiver, fileBytes);
}
}
for (int i = 0; i <= 10; i++) {
//castData(users, fileBytes);
}
}
private void openFileChooser() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("*/*");
startActivityForResult(photoPickerIntent, 100);
}
private Cursor cursor = null;
private String fileSource;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("Request Code", "Code=" + requestCode);
if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
fileSource = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
System.out.println("File Path = " + fileSource);
//File sourceFile = new File(fileSource);
sendFile2(fileSource);
}
}
}
/**
* Open mesh settings screen.
*
* @param v calling view
*/
public void configure(View v) {
try {
mm.showSettingsActivity();
} catch (RightMeshException ex) {
MeshUtility.Log(this.getClass().getCanonicalName(), "Service not connected");
}
}
public static byte[] getFileByteBuffer(String fileUrl) {
try {
FileChannel fileChannel = new RandomAccessFile(fileUrl, "r").getChannel();
long fileSize = fileChannel.size();
ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
int readBytes = fileChannel.read(buffer, 0);
Log.e("Send_B", "Send byte = " + readBytes);
fileChannel.close();
return buffer.array();
} catch (IOException e) {
}
return null;
}
}