2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 05:37:43 +00:00

Fixed PairingFragment and MaterialAct stealing the callback from each other

This commit is contained in:
Albert Vaca 2015-09-04 06:04:22 -07:00
parent 4d9f2d12b5
commit c535637b45
5 changed files with 35 additions and 23 deletions

View File

@ -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() {

View File

@ -154,7 +154,7 @@ public class MaterialActivity extends AppCompatActivity {
Collection<Device> devices = service.getDevices().values(); Collection<Device> devices = service.getDevices().values();
for (Device device : devices) { for (Device device : devices) {
if (device.isReachable() && device.isPaired()) { 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.setIcon(device.getIcon());
item.setCheckable(true); item.setCheckable(true);
item.setChecked(device.getDeviceId().equals(mCurrentDevice)); item.setChecked(device.getDeviceId().equals(mCurrentDevice));
@ -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();

View File

@ -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");
} }
}); });
} }

View File

@ -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();

View File

@ -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();