2015-08-04 01:01:14 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
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;
|
2017-01-23 01:08:36 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2016-11-20 21:23:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.liveview.LiveviewSupport;
|
2016-12-14 00:28:35 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport;
|
2016-09-19 12:37:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.vibratissimo.VibratissimoSupport;
|
2016-12-21 13:51:25 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.HPlusSupport;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class DeviceSupportFactory {
|
|
|
|
private final BluetoothAdapter mBtAdapter;
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Context mContext;
|
2015-08-04 01:01:14 +02:00
|
|
|
|
|
|
|
public DeviceSupportFactory(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
public synchronized DeviceSupport createDeviceSupport(GBDevice device) throws GBException {
|
|
|
|
DeviceSupport deviceSupport = null;
|
|
|
|
String deviceAddress = device.getAddress();
|
2015-08-22 01:08:46 +02:00
|
|
|
int indexFirstColon = deviceAddress.indexOf(":");
|
|
|
|
if (indexFirstColon > 0) {
|
|
|
|
if (indexFirstColon == deviceAddress.lastIndexOf(":")) { // only one colon
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createTCPDeviceSupport(device);
|
2015-08-22 01:08:46 +02:00
|
|
|
} else {
|
|
|
|
// multiple colons -- bt?
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createBTDeviceSupport(device);
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
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?
|
2015-12-13 00:43:07 +01:00
|
|
|
deviceSupport = createClassNameDeviceSupport(device);
|
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-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createClassNameDeviceSupport(GBDevice device) throws GBException {
|
|
|
|
String className = device.getAddress();
|
2015-08-22 01:08:46 +02:00
|
|
|
try {
|
|
|
|
Class<?> deviceSupportClass = Class.forName(className);
|
|
|
|
Constructor<?> constructor = deviceSupportClass.getConstructor();
|
|
|
|
DeviceSupport support = (DeviceSupport) constructor.newInstance();
|
|
|
|
// has to create the device itself
|
2015-12-13 00:43:07 +01:00
|
|
|
support.setContext(device, null, mContext);
|
2015-08-22 01:08:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createBTDeviceSupport(GBDevice gbDevice) throws GBException {
|
2015-08-04 01:01:14 +02:00
|
|
|
if (mBtAdapter != null && mBtAdapter.isEnabled()) {
|
|
|
|
DeviceSupport deviceSupport = null;
|
|
|
|
|
|
|
|
try {
|
2015-12-13 00:43:07 +01:00
|
|
|
switch (gbDevice.getType()) {
|
|
|
|
case PEBBLE:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
|
|
|
case MIBAND:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new MiBandSupport(), EnumSet.of(ServiceDeviceSupport.Flags.THROTTLING, ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
2016-08-14 23:24:24 +02:00
|
|
|
case MIBAND2:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new MiBand2Support(), EnumSet.of(ServiceDeviceSupport.Flags.THROTTLING, ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
2016-09-19 12:37:41 +02:00
|
|
|
case VIBRATISSIMO:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new VibratissimoSupport(), EnumSet.of(ServiceDeviceSupport.Flags.THROTTLING, ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
2016-11-20 21:23:47 +01:00
|
|
|
case LIVEVIEW:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new LiveviewSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
2016-12-21 13:51:25 +01:00
|
|
|
case HPLUS:
|
2017-01-23 01:08:36 +01:00
|
|
|
deviceSupport = new ServiceDeviceSupport(new HPlusSupport(DeviceType.HPLUS), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
break;
|
|
|
|
case MAKIBESF68:
|
|
|
|
deviceSupport = new ServiceDeviceSupport(new HPlusSupport(DeviceType.MAKIBESF68), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
2016-12-21 13:51:25 +01:00
|
|
|
break;
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|
|
|
|
if (deviceSupport != null) {
|
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2016-07-28 22:42:16 +02:00
|
|
|
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e);
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-19 00:03:52 +02:00
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
private DeviceSupport createTCPDeviceSupport(GBDevice gbDevice) throws GBException {
|
2015-08-19 00:03:52 +02:00
|
|
|
try {
|
|
|
|
DeviceSupport deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
|
|
|
|
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
|
|
|
|
return deviceSupport;
|
|
|
|
} catch (Exception e) {
|
2015-12-13 00:43:07 +01:00
|
|
|
throw new GBException("cannot connect to " + gbDevice, e); // FIXME: localize
|
2015-08-19 00:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
}
|