2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleCoordinator;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
public boolean isSupported(GBDeviceCandidate candidate) {
|
2015-05-05 00:48:02 +02:00
|
|
|
if (coordinator != null && coordinator.supports(candidate)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (DeviceCoordinator coordinator : getAllCoordinators()) {
|
|
|
|
if (coordinator.supports(candidate)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
public DeviceCoordinator getCoordinator(GBDeviceCandidate device) {
|
2015-05-05 00:48:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|