2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Fixed some potential concurrent modification exceptions

# Conflicts:
#	src/org/kde/kdeconnect/Device.java
This commit is contained in:
Albert Vaca
2015-09-10 07:49:28 -07:00
parent b1ef93a90c
commit 35dd42f560
2 changed files with 36 additions and 10 deletions

View File

@@ -23,10 +23,11 @@ package org.kde.kdeconnect.Backends;
import org.kde.kdeconnect.NetworkPackage;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public abstract class BaseLinkProvider {
private final ArrayList<ConnectionReceiver> connectionReceivers = new ArrayList<>();
private final CopyOnWriteArrayList<ConnectionReceiver> connectionReceivers = new CopyOnWriteArrayList<>();
public interface ConnectionReceiver {
void onConnectionReceived(NetworkPackage identityPackage, BaseLink link);

View File

@@ -53,6 +53,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
public class Device implements BaseLink.PackageReceiver {
@@ -64,6 +65,37 @@ public class Device implements BaseLink.PackageReceiver {
private int notificationId;
private int protocolVersion;
private DeviceType deviceType;
private PairStatus pairStatus;
private final CopyOnWriteArrayList<PairingCallback> pairingCallback = new CopyOnWriteArrayList<>();
private Timer pairingTimer;
private final CopyOnWriteArrayList<BaseLink> links = new CopyOnWriteArrayList<>();
private ArrayList<String> incomingCapabilities;
private ArrayList<String> outgoingCapabilities;
private final HashMap<String, Plugin> plugins = new HashMap<>();
private final HashMap<String, Plugin> failedPlugins = new HashMap<>();
private ArrayList<String> unsupportedPlugins = new ArrayList<>();
private HashSet<String> supportedIncomingInterfaces = new HashSet<>();
HashMap<String, ArrayList<String>> pluginsByIncomingInterface;
HashMap<String, ArrayList<String>> pluginsByOutgoingInterface;
private final SharedPreferences settings;
public ArrayList<String> getUnsupportedPlugins() {
return unsupportedPlugins;
}
private final CopyOnWriteArrayList<PluginsChangedListener> pluginsChangedListeners = new CopyOnWriteArrayList<>();
public interface PluginsChangedListener {
void onPluginsChanged(Device device);
}
public enum PairStatus {
NotPaired,
Requested,
@@ -616,8 +648,7 @@ public class Device implements BaseLink.PackageReceiver {
boolean useEncryption = (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_PAIR) && isPaired());
//Make a copy to avoid concurrent modification exception if the original list changes
ArrayList<BaseLink> mLinks = new ArrayList<>(links);
for (final BaseLink link : mLinks) {
for (final BaseLink link : links) {
if (link == null) continue; //Since we made a copy, maybe somebody destroyed the link in the meanwhile
if (useEncryption) {
link.sendPackageEncrypted(np, callback, publicKey);
@@ -628,7 +659,7 @@ public class Device implements BaseLink.PackageReceiver {
}
if (!callback.success) {
Log.e("KDE/sendPackage", "No device link (of "+mLinks.size()+" available) could send the package "+np.getType()+". Package lost!");
Log.e("KDE/sendPackage", "No device link (of "+links.size()+" available) could send the package. Package "+np.getType()+" to " + name + " lost!");
backtrace.printStackTrace();
}
@@ -785,12 +816,6 @@ public class Device implements BaseLink.PackageReceiver {
return failedPlugins;
}
public interface PluginsChangedListener {
void onPluginsChanged(Device device);
}
private final ArrayList<PluginsChangedListener> pluginsChangedListeners = new ArrayList<>();
public void addPluginsChangedListener(PluginsChangedListener listener) {
pluginsChangedListeners.add(listener);
}