2015-08-04 01:01:14 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-08-22 01:08:46 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2015-08-17 21:30:37 +02:00
|
|
|
import java.util.EnumSet;
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class DeviceSupportFactory {
|
|
|
|
private final BluetoothAdapter mBtAdapter;
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
public DeviceSupportFactory(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized DeviceSupport createDeviceSupport(String deviceAddress) throws GBException {
|
2015-08-19 00:03:52 +02:00
|
|
|
DeviceSupport deviceSupport;
|
2015-08-22 01:08:46 +02:00
|
|
|
int indexFirstColon = deviceAddress.indexOf(":");
|
|
|
|
if (indexFirstColon > 0) {
|
|
|
|
if (indexFirstColon == deviceAddress.lastIndexOf(":")) { // only one colon
|
|
|
|
deviceSupport = createTCPDeviceSupport(deviceAddress);
|
|
|
|
} else {
|
|
|
|
// multiple colons -- bt?
|
|
|
|
deviceSupport = createBTDeviceSupport(deviceAddress);
|
|
|
|
}
|
2015-08-19 00:03:52 +02:00
|
|
|
} else {
|
2015-08-22 01:08:46 +02:00
|
|
|
// no colon at all, maybe a class name?
|
|
|
|
deviceSupport = createClassNameDeviceSupport(deviceAddress);
|
2015-08-19 00:03:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
if (deviceSupport != null) {
|
|
|
|
return deviceSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no device found, check transport availability and warn
|
|
|
|
checkBtAvailability();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-08-22 01:08:46 +02:00
|
|
|
private DeviceSupport createClassNameDeviceSupport(String className) throws GBException {
|
|
|
|
try {
|
|
|
|
Class<?> deviceSupportClass = Class.forName(className);
|
|
|
|
Constructor<?> constructor = deviceSupportClass.getConstructor();
|
|
|
|
DeviceSupport support = (DeviceSupport) constructor.newInstance();
|
|
|
|
// has to create the device itself
|
|
|
|
support.setContext(null, null, mContext);
|
|
|
|
return support;
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
return null; // not a class, or not known at least
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new GBException("Error creating DeviceSupport instance for " + className, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
private void checkBtAvailability() {
|
|
|
|
if (mBtAdapter == null) {
|
|
|
|
GB.toast(mContext.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
} else if (!mBtAdapter.isEnabled()) {
|
|
|
|
GB.toast(mContext.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DeviceSupport createBTDeviceSupport(String deviceAddress) throws GBException {
|
|
|
|
if (mBtAdapter != null && mBtAdapter.isEnabled()) {
|
|
|
|
GBDevice gbDevice = null;
|
|
|
|
DeviceSupport deviceSupport = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(deviceAddress);
|
2015-10-18 08:20:18 +02:00
|
|
|
if (btDevice.getName() == null || btDevice.getName().startsWith("MI")) { //FIXME: workaround for Miband not being paired
|
2015-08-04 01:01:14 +02:00
|
|
|
gbDevice = new GBDevice(deviceAddress, "MI", DeviceType.MIBAND);
|
2015-08-17 21:30:37 +02:00
|
|
|
deviceSupport = new ServiceDeviceSupport(new MiBandSupport(), EnumSet.of(ServiceDeviceSupport.Flags.THROTTLING, ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
2015-08-04 01:01:14 +02:00
|
|
|
} else if (btDevice.getName().indexOf("Pebble") == 0) {
|
|
|
|
gbDevice = new GBDevice(deviceAddress, btDevice.getName(), DeviceType.PEBBLE);
|
2015-08-17 21:30:37 +02:00
|
|
|
deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|
|
|
|
if (deviceSupport != null) {
|
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_, e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-19 00:03:52 +02:00
|
|
|
|
|
|
|
private DeviceSupport createTCPDeviceSupport(String deviceAddress) throws GBException {
|
|
|
|
try {
|
|
|
|
GBDevice gbDevice = new GBDevice(deviceAddress, "Pebble qemu", DeviceType.PEBBLE); //FIXME, do not hardcode
|
|
|
|
DeviceSupport deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new GBException("cannot connect to " + deviceAddress, e); // FIXME: localize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|