1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceHelper.java
2015-05-12 20:09:35 +02:00

79 lines
2.4 KiB
Java

package nodomain.freeyourgadget.gadgetbridge;
import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.discovery.DeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.miband.MiBandCoordinator;
import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleCoordinator;
public class DeviceHelper {
private static DeviceHelper instance = new DeviceHelper();
public static DeviceHelper getInstance() {
return instance;
}
// lazily created
private List<DeviceCoordinator> coordinators;
// the current single coordinator (typically there's just one device connected
private DeviceCoordinator coordinator;
public boolean isSupported(DeviceCandidate candidate) {
if (coordinator != null && coordinator.supports(candidate)) {
return true;
}
for (DeviceCoordinator coordinator : getAllCoordinators()) {
if (coordinator.supports(candidate)) {
return true;
}
}
return false;
}
public DeviceCoordinator getCoordinator(DeviceCandidate device) {
if (coordinator != null && coordinator.supports(device)) {
return coordinator;
}
synchronized (this) {
for (DeviceCoordinator coord : getAllCoordinators()) {
if (coord.supports(device)) {
coordinator = coord;
return coordinator;
}
}
}
return new UnknownDeviceCoordinator();
}
public DeviceCoordinator getCoordinator(GBDevice device) {
if (coordinator != null && coordinator.supports(device)) {
return coordinator;
}
synchronized (this) {
for (DeviceCoordinator coord : getAllCoordinators()) {
if (coord.supports(device)) {
coordinator = coord;
return coordinator;
}
}
}
return new UnknownDeviceCoordinator();
}
public synchronized List<DeviceCoordinator> getAllCoordinators() {
if (coordinators == null) {
coordinators = createCoordinators();
}
return coordinators;
}
private List<DeviceCoordinator> createCoordinators() {
List<DeviceCoordinator> result = new ArrayList<>(2);
result.add(new MiBandCoordinator());
result.add(new PebbleCoordinator());
return result;
}
}