2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17: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.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<String, DeviceListChangedCallback> deviceListChangedCallbacks = new ConcurrentHashMap<>();
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() {
@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<String, Device> getDevices() {
public ConcurrentHashMap<String, Device> 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() {

View File

@ -154,7 +154,7 @@ public class MaterialActivity extends AppCompatActivity {
Collection<Device> 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();

View File

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

View File

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

View File

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