From c535637b45f8aebba0dc8a6d07b552d3ffc69bc7 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Fri, 4 Sep 2015 06:04:22 -0700 Subject: [PATCH] Fixed PairingFragment and MaterialAct stealing the callback from each other --- src/org/kde/kdeconnect/BackgroundService.java | 40 ++++++++++++------- .../NewUserInterface/MaterialActivity.java | 6 +-- .../NewUserInterface/PairingFragment.java | 4 +- .../Plugins/SharePlugin/ShareActivity.java | 4 +- .../UserInterface/MainActivity.java | 4 +- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index 06bda147..7b126633 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -34,40 +34,54 @@ import android.util.Log; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; +import org.kde.kdeconnect.Backends.LoopbackBackend.LoopbackLinkProvider; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BackgroundService extends Service { + public interface DeviceListChangedCallback { + void onDeviceListChanged(); + } + + private final ConcurrentHashMap deviceListChangedCallbacks = new ConcurrentHashMap<>(); + private final ArrayList linkProviders = new ArrayList<>(); - private final HashMap devices = new HashMap<>(); + private final ConcurrentHashMap devices = new ConcurrentHashMap<>(); private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() { @Override public void incomingRequest() { - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } @Override public void pairingSuccessful() { - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } @Override public void pairingFailed(String error) { - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } @Override public void unpaired() { - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } }; + private void onDeviceListChanged() { + for(DeviceListChangedCallback callback : deviceListChangedCallbacks.values()) { + callback.onDeviceListChanged(); + } + } + private void loadRememberedDevicesFromSettings() { //Log.e("BackgroundService", "Loading remembered trusted devices"); SharedPreferences preferences = getSharedPreferences("trusted_devices", Context.MODE_PRIVATE); @@ -120,7 +134,7 @@ public class BackgroundService extends Service { device.addPairingCallback(devicePairingCallback); } - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } @Override @@ -137,11 +151,11 @@ public class BackgroundService extends Service { } else { Log.e("KDE/onConnectionLost","Removing connection to unknown device, this should not happen"); } - if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged(); + onDeviceListChanged(); } }; - public HashMap getDevices() { + public ConcurrentHashMap getDevices() { return devices; } @@ -180,15 +194,13 @@ public class BackgroundService extends Service { } } - public interface DeviceListChangedCallback { - void onDeviceListChanged(); + public void addDeviceListChangedCallback(String key, DeviceListChangedCallback callback) { + deviceListChangedCallbacks.put(key, callback); } - private DeviceListChangedCallback deviceListChangedCallback = null; - public void setDeviceListChangedCallback(DeviceListChangedCallback callback) { - this.deviceListChangedCallback = callback; + public void removeDeviceListChangedCallback(String key) { + deviceListChangedCallbacks.remove(key); } - //This will called only once, even if we launch the service intent several times @Override public void onCreate() { diff --git a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java index 273110a2..798112ce 100644 --- a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java +++ b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java @@ -154,7 +154,7 @@ public class MaterialActivity extends AppCompatActivity { Collection devices = service.getDevices().values(); for (Device device : devices) { if (device.isReachable() && device.isPaired()) { - MenuItem item = menu.add(0,id++,0,device.getName()); + MenuItem item = menu.add(0, id++, 0, device.getName()); item.setIcon(device.getIcon()); item.setCheckable(true); item.setChecked(device.getDeviceId().equals(mCurrentDevice)); @@ -178,7 +178,7 @@ public class MaterialActivity extends AppCompatActivity { @Override public void onServiceStart(BackgroundService service) { service.onNetworkChange(); - service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() { + service.addDeviceListChangedCallback("MaterialActivity", new BackgroundService.DeviceListChangedCallback() { @Override public void onDeviceListChanged() { updateComputerList(); @@ -193,7 +193,7 @@ public class MaterialActivity extends AppCompatActivity { BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { - service.setDeviceListChangedCallback(null); + service.removeDeviceListChangedCallback("MaterialActivity"); } }); super.onStop(); diff --git a/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java b/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java index 5ab125d5..4ef87a33 100644 --- a/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java +++ b/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java @@ -133,7 +133,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { - service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() { + service.addDeviceListChangedCallback("PairingFragment", new BackgroundService.DeviceListChangedCallback() { @Override public void onDeviceListChanged() { updateComputerList(); @@ -150,7 +150,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { - service.setDeviceListChangedCallback(null); + service.removeDeviceListChangedCallback("PairingFragment"); } }); } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java index ab8154a2..a9a18047 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java @@ -412,7 +412,7 @@ public class ShareActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { service.onNetworkChange(); - service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() { + service.addDeviceListChangedCallback("ShareActivity", new BackgroundService.DeviceListChangedCallback() { @Override public void onDeviceListChanged() { updateComputerList(); @@ -427,7 +427,7 @@ public class ShareActivity extends ActionBarActivity { BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { - service.setDeviceListChangedCallback(null); + service.removeDeviceListChangedCallback("ShareActivity"); } }); super.onStop(); diff --git a/src/org/kde/kdeconnect/UserInterface/MainActivity.java b/src/org/kde/kdeconnect/UserInterface/MainActivity.java index 79d63097..69535f16 100644 --- a/src/org/kde/kdeconnect/UserInterface/MainActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/MainActivity.java @@ -181,7 +181,7 @@ public class MainActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { service.onNetworkChange(); - service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() { + service.addDeviceListChangedCallback("MainActivity", new BackgroundService.DeviceListChangedCallback() { @Override public void onDeviceListChanged() { updateComputerList(); @@ -196,7 +196,7 @@ public class MainActivity extends ActionBarActivity { BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { - service.setDeviceListChangedCallback(null); + service.removeDeviceListChangedCallback("MainActivity"); } }); super.onStop();