1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-12 13:09:24 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceHelper.java

81 lines
2.5 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge;
import android.bluetooth.BluetoothClass;
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;
}
}