Skip to content

Commit 179a948

Browse files
authored
fix: Remove AsyncTask in the send function (it's deprecated) (#197)
1 parent 76c4586 commit 179a948

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

android/src/main/java/com/tradle/react/UdpSenderTask.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
package com.tradle.react;
99

10-
import android.os.AsyncTask;
11-
1210
import java.io.IOException;
1311
import java.lang.ref.WeakReference;
1412
import java.net.DatagramPacket;
@@ -18,24 +16,32 @@
1816
/**
1917
* Specialized AsyncTask that transmits data in the background, and notifies listeners of the result.
2018
*/
21-
public class UdpSenderTask extends AsyncTask<UdpSenderTask.SenderPacket, Void, Void> {
19+
public class UdpSenderTask implements Runnable {
2220
private static final String TAG = "UdpSenderTask";
2321

24-
private DatagramSocket mSocket;
25-
private WeakReference<OnDataSentListener> mListener;
22+
private final DatagramSocket mSocket;
23+
private final WeakReference<OnDataSentListener> mListener;
24+
25+
private SocketAddress mSocketAddress;
26+
private byte[] mData;
2627

27-
public UdpSenderTask(DatagramSocket socket, OnDataSentListener listener) {
28+
public UdpSenderTask(DatagramSocket socket, OnDataSentListener listener, SocketAddress socketAddress, byte[] data) {
2829
this.mSocket = socket;
29-
this.mListener = new WeakReference<OnDataSentListener>(listener);
30+
this.mListener = new WeakReference<>(listener);
31+
this.mSocketAddress = socketAddress;
32+
this.mData = data;
3033
}
3134

3235
@Override
33-
protected Void doInBackground(SenderPacket... params) {
36+
public void run() {
3437
OnDataSentListener listener = mListener.get();
3538

3639
try {
37-
SenderPacket packet = params[0];
38-
mSocket.send(new DatagramPacket(packet.data, packet.data.length, packet.socketAddress));
40+
if (mSocket == null) {
41+
return;
42+
}
43+
44+
mSocket.send(new DatagramPacket(mData, mData.length, mSocketAddress));
3945

4046
if (listener != null) {
4147
listener.onDataSent(this);
@@ -49,16 +55,6 @@ protected Void doInBackground(SenderPacket... params) {
4955
listener.onDataSentRuntimeException(this, rte);
5056
}
5157
}
52-
53-
return null;
54-
}
55-
56-
/**
57-
* Simple class to marshall outgoing data across to this AsyncTask
58-
*/
59-
public static class SenderPacket {
60-
SocketAddress socketAddress;
61-
byte[] data;
6258
}
6359

6460
/**

android/src/main/java/com/tradle/react/UdpSocketClient.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import java.net.UnknownHostException;
1616
import java.util.Map;
1717
import java.util.concurrent.ConcurrentHashMap;
18+
import java.util.concurrent.ExecutorService;
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.Future;
1821

1922
import static com.tradle.react.UdpSenderTask.OnDataSentListener;
2023

@@ -25,13 +28,15 @@ public final class UdpSocketClient implements UdpReceiverTask.OnDataReceivedList
2528
private final OnDataReceivedListener mReceiverListener;
2629
private final OnRuntimeExceptionListener mExceptionListener;
2730

31+
private ExecutorService executor = Executors.newSingleThreadExecutor();
32+
2833
private UdpReceiverTask mReceiverTask;
2934

3035
private final Map<UdpSenderTask, Callback> mPendingSends;
3136
private DatagramSocket mSocket;
3237
private boolean mIsMulticastSocket = false;
3338

34-
public UdpSocketClient(OnDataReceivedListener receiverListener, OnRuntimeExceptionListener exceptionListener) {
39+
public UdpSocketClient(OnDataReceivedListener receiverListener, OnRuntimeExceptionListener exceptionListener) {
3540
this.mReceiverListener = receiverListener;
3641
this.mExceptionListener = exceptionListener;
3742
this.mPendingSends = new ConcurrentHashMap<>();
@@ -125,18 +130,15 @@ public void send(String base64String, Integer port, String address, @Nullable Ca
125130

126131
byte[] data = Base64.decode(base64String, Base64.NO_WRAP);
127132

128-
UdpSenderTask task = new UdpSenderTask(mSocket, this);
129-
UdpSenderTask.SenderPacket packet = new UdpSenderTask.SenderPacket();
130-
packet.data = data;
131-
packet.socketAddress = new InetSocketAddress(InetAddress.getByName(address), port);
133+
UdpSenderTask task = new UdpSenderTask(mSocket, this, new InetSocketAddress(InetAddress.getByName(address), port), data);
132134

133135
if (callback != null) {
134136
synchronized (mPendingSends) {
135137
mPendingSends.put(task, callback);
136138
}
137139
}
138140

139-
task.execute(packet);
141+
executor.submit(task);
140142
}
141143

142144
/**
@@ -157,6 +159,9 @@ public void close() {
157159
mReceiverTask.terminate();
158160
}
159161

162+
// stop pending send tasks
163+
executor.shutdownNow();
164+
160165
// close the socket
161166
if (mSocket != null && !mSocket.isClosed()) {
162167
mSocket.close();

android/src/main/java/com/tradle/react/UdpSockets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void run() {
6666
}
6767

6868
/**
69-
* Private method to retrieve clients. Must be called from a GuardedAsyncTask for thread-safety.
69+
* Private method to retrieve clients.
7070
*/
7171
private UdpSocketClient findClient(final Integer cId, final Callback callback) {
7272
final UdpSocketClient client = mClients.get(cId);

0 commit comments

Comments
 (0)