From dcb54ebf4aa6a8ecf0526cf5e0ea86e97f2ab3b9 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Thu, 10 Sep 2015 08:05:23 -0700 Subject: [PATCH 1/2] Fixed some potential concurrent modification exceptions --- .../kdeconnect/Backends/BaseLinkProvider.java | 3 +- src/org/kde/kdeconnect/Device.java | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/org/kde/kdeconnect/Backends/BaseLinkProvider.java b/src/org/kde/kdeconnect/Backends/BaseLinkProvider.java index a95df464..78dae16e 100644 --- a/src/org/kde/kdeconnect/Backends/BaseLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/BaseLinkProvider.java @@ -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 connectionReceivers = new ArrayList<>(); + private final CopyOnWriteArrayList connectionReceivers = new CopyOnWriteArrayList<>(); public interface ConnectionReceiver { void onConnectionReceived(NetworkPackage identityPackage, BaseLink link); diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index 2af8dd84..6341b77b 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -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,24 @@ public class Device implements BaseLink.PackageReceiver { private int notificationId; private int protocolVersion; + private DeviceType deviceType; + private PairStatus pairStatus; + private final CopyOnWriteArrayList pairingCallback = new CopyOnWriteArrayList<>(); + private Timer pairingTimer; + + private final CopyOnWriteArrayList links = new CopyOnWriteArrayList<>(); + + private final HashMap plugins = new HashMap<>(); + private final HashMap failedPlugins = new HashMap<>(); + + private final SharedPreferences settings; + + private final CopyOnWriteArrayList pluginsChangedListeners = new CopyOnWriteArrayList<>(); + + public interface PluginsChangedListener { + void onPluginsChanged(Device device); + } + public enum PairStatus { NotPaired, Requested, @@ -97,17 +116,6 @@ public class Device implements BaseLink.PackageReceiver { void unpaired(); } - private DeviceType deviceType; - private PairStatus pairStatus; - private final ArrayList pairingCallback = new ArrayList<>(); - private Timer pairingTimer; - - private final ArrayList links = new ArrayList<>(); - private final HashMap plugins = new HashMap<>(); - private final HashMap failedPlugins = new HashMap<>(); - - private final SharedPreferences settings; - //Remembered trusted device, we need to wait for a incoming devicelink to communicate Device(Context context, String deviceId) { settings = context.getSharedPreferences(deviceId, Context.MODE_PRIVATE); @@ -616,8 +624,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 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 +635,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 +792,6 @@ public class Device implements BaseLink.PackageReceiver { return failedPlugins; } - public interface PluginsChangedListener { - void onPluginsChanged(Device device); - } - - private final ArrayList pluginsChangedListeners = new ArrayList<>(); - public void addPluginsChangedListener(PluginsChangedListener listener) { pluginsChangedListeners.add(listener); } From bde4a06fe0018be0bd5ddc823b98aaaa766191da Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Thu, 10 Sep 2015 08:07:06 -0700 Subject: [PATCH 2/2] Increased the version number to beta-release --- AndroidManifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4e439bba..a947a3bf 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,7 +1,7 @@