mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
Fixed PairingFragment and MaterialAct stealing the callback from each other
This commit is contained in:
@@ -34,40 +34,54 @@ import android.util.Log;
|
|||||||
import org.kde.kdeconnect.Backends.BaseLink;
|
import org.kde.kdeconnect.Backends.BaseLink;
|
||||||
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
||||||
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
|
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
|
||||||
|
import org.kde.kdeconnect.Backends.LoopbackBackend.LoopbackLinkProvider;
|
||||||
|
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
import java.security.KeyPairGenerator;
|
import java.security.KeyPairGenerator;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class BackgroundService extends Service {
|
public class BackgroundService extends Service {
|
||||||
|
|
||||||
|
public interface DeviceListChangedCallback {
|
||||||
|
void onDeviceListChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final ConcurrentHashMap<String, DeviceListChangedCallback> deviceListChangedCallbacks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final ArrayList<BaseLinkProvider> linkProviders = new ArrayList<>();
|
private final ArrayList<BaseLinkProvider> linkProviders = new ArrayList<>();
|
||||||
|
|
||||||
private final HashMap<String, Device> devices = new HashMap<>();
|
private final ConcurrentHashMap<String, Device> devices = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() {
|
private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void incomingRequest() {
|
public void incomingRequest() {
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void pairingSuccessful() {
|
public void pairingSuccessful() {
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void pairingFailed(String error) {
|
public void pairingFailed(String error) {
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void unpaired() {
|
public void unpaired() {
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private void onDeviceListChanged() {
|
||||||
|
for(DeviceListChangedCallback callback : deviceListChangedCallbacks.values()) {
|
||||||
|
callback.onDeviceListChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void loadRememberedDevicesFromSettings() {
|
private void loadRememberedDevicesFromSettings() {
|
||||||
//Log.e("BackgroundService", "Loading remembered trusted devices");
|
//Log.e("BackgroundService", "Loading remembered trusted devices");
|
||||||
SharedPreferences preferences = getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
|
SharedPreferences preferences = getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
|
||||||
@@ -120,7 +134,7 @@ public class BackgroundService extends Service {
|
|||||||
device.addPairingCallback(devicePairingCallback);
|
device.addPairingCallback(devicePairingCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -137,11 +151,11 @@ public class BackgroundService extends Service {
|
|||||||
} else {
|
} else {
|
||||||
Log.e("KDE/onConnectionLost","Removing connection to unknown device, this should not happen");
|
Log.e("KDE/onConnectionLost","Removing connection to unknown device, this should not happen");
|
||||||
}
|
}
|
||||||
if (deviceListChangedCallback != null) deviceListChangedCallback.onDeviceListChanged();
|
onDeviceListChanged();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public HashMap<String, Device> getDevices() {
|
public ConcurrentHashMap<String, Device> getDevices() {
|
||||||
return devices;
|
return devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,15 +194,13 @@ public class BackgroundService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface DeviceListChangedCallback {
|
public void addDeviceListChangedCallback(String key, DeviceListChangedCallback callback) {
|
||||||
void onDeviceListChanged();
|
deviceListChangedCallbacks.put(key, callback);
|
||||||
}
|
}
|
||||||
private DeviceListChangedCallback deviceListChangedCallback = null;
|
public void removeDeviceListChangedCallback(String key) {
|
||||||
public void setDeviceListChangedCallback(DeviceListChangedCallback callback) {
|
deviceListChangedCallbacks.remove(key);
|
||||||
this.deviceListChangedCallback = callback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//This will called only once, even if we launch the service intent several times
|
//This will called only once, even if we launch the service intent several times
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
@@ -178,7 +178,7 @@ public class MaterialActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.onNetworkChange();
|
service.onNetworkChange();
|
||||||
service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() {
|
service.addDeviceListChangedCallback("MaterialActivity", new BackgroundService.DeviceListChangedCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onDeviceListChanged() {
|
public void onDeviceListChanged() {
|
||||||
updateComputerList();
|
updateComputerList();
|
||||||
@@ -193,7 +193,7 @@ public class MaterialActivity extends AppCompatActivity {
|
|||||||
BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.setDeviceListChangedCallback(null);
|
service.removeDeviceListChangedCallback("MaterialActivity");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
super.onStop();
|
super.onStop();
|
||||||
|
@@ -133,7 +133,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
|
|||||||
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() {
|
service.addDeviceListChangedCallback("PairingFragment", new BackgroundService.DeviceListChangedCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onDeviceListChanged() {
|
public void onDeviceListChanged() {
|
||||||
updateComputerList();
|
updateComputerList();
|
||||||
@@ -150,7 +150,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
|
|||||||
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.setDeviceListChangedCallback(null);
|
service.removeDeviceListChangedCallback("PairingFragment");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -412,7 +412,7 @@ public class ShareActivity extends ActionBarActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.onNetworkChange();
|
service.onNetworkChange();
|
||||||
service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() {
|
service.addDeviceListChangedCallback("ShareActivity", new BackgroundService.DeviceListChangedCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onDeviceListChanged() {
|
public void onDeviceListChanged() {
|
||||||
updateComputerList();
|
updateComputerList();
|
||||||
@@ -427,7 +427,7 @@ public class ShareActivity extends ActionBarActivity {
|
|||||||
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.setDeviceListChangedCallback(null);
|
service.removeDeviceListChangedCallback("ShareActivity");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
super.onStop();
|
super.onStop();
|
||||||
|
@@ -181,7 +181,7 @@ public class MainActivity extends ActionBarActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.onNetworkChange();
|
service.onNetworkChange();
|
||||||
service.setDeviceListChangedCallback(new BackgroundService.DeviceListChangedCallback() {
|
service.addDeviceListChangedCallback("MainActivity", new BackgroundService.DeviceListChangedCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onDeviceListChanged() {
|
public void onDeviceListChanged() {
|
||||||
updateComputerList();
|
updateComputerList();
|
||||||
@@ -196,7 +196,7 @@ public class MainActivity extends ActionBarActivity {
|
|||||||
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.setDeviceListChangedCallback(null);
|
service.removeDeviceListChangedCallback("MainActivity");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
super.onStop();
|
super.onStop();
|
||||||
|
Reference in New Issue
Block a user