1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 02:46:50 +01:00

Include known devices from the database in CC

(as long as device-support is still available)
Fixes non-paired devices not being displayed
This commit is contained in:
cpfeiffer 2016-08-31 00:33:54 +02:00
parent 6340bcff15
commit cd535a0a45
3 changed files with 84 additions and 8 deletions

View File

@ -332,6 +332,15 @@ public class DBHelper {
return null;
}
/**
* Returns all active (that is, not old, archived ones) from the database.
* (currently the active handling is not available)
* @param daoSession
*/
public static List<Device> getActiveDevices(DaoSession daoSession) {
return daoSession.getDeviceDao().loadAll();
}
/**
* Looks up in the database the Device entity corresponding to the GBDevice. If a device
* exists already, it will be updated with the current preferences values. If no device exists

View File

@ -62,6 +62,7 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
return MiBandPairingActivity.class;
}
@Override
public Class<? extends Activity> getPrimaryActivity() {
return ChartsActivity.class;
}

View File

@ -3,24 +3,31 @@ package nodomain.freeyourgadget.gadgetbridge.util;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleCoordinator;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
@ -52,6 +59,18 @@ public class DeviceHelper {
return false;
}
public boolean isSupported(GBDevice device) {
if (coordinator != null && coordinator.supports(device)) {
return true;
}
for (DeviceCoordinator coordinator : getAllCoordinators()) {
if (coordinator.supports(device)) {
return true;
}
}
return false;
}
public GBDevice findAvailableDevice(String deviceAddress, Context context) {
Set<GBDevice> availableDevices = getAvailableDevices(context);
for (GBDevice availableDevice : availableDevices) {
@ -82,14 +101,10 @@ public class DeviceHelper {
} else if (!btAdapter.isEnabled()) {
GB.toast(context, context.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
} else {
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
DeviceHelper deviceHelper = DeviceHelper.getInstance();
for (BluetoothDevice pairedDevice : pairedDevices) {
GBDevice device = deviceHelper.toSupportedDevice(pairedDevice);
if (device != null) {
availableDevices.add(device);
}
}
List<GBDevice> dbDevices = getDatabaseDevices();
List<GBDevice> bondedDevices = getBondedDevices(btAdapter);
availableDevices.addAll(bondedDevices);
availableDevices.addAll(dbDevices);
Prefs prefs = GBApplication.getPrefs();
String miAddr = prefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
@ -168,4 +183,55 @@ public class DeviceHelper {
result.add(new PebbleCoordinator());
return result;
}
private List<GBDevice> getDatabaseDevices() {
List<GBDevice> result = new ArrayList<>();
try (DBHandler lockHandler = GBApplication.acquireDB()) {
List<Device> activeDevices = DBHelper.getActiveDevices(lockHandler.getDaoSession());
for (Device dbDevice : activeDevices) {
GBDevice gbDevice = toGBDevice(dbDevice);
if (gbDevice != null && DeviceHelper.getInstance().isSupported(gbDevice)) {
result.add(gbDevice);
}
}
return result;
} catch (Exception e) {
GB.toast("Error retrieving devices from database", Toast.LENGTH_SHORT, GB.ERROR);
return Collections.emptyList();
}
}
/**
* Converts a known device from the database to a GBDevice.
* Note: The device might not be supported anymore, so callers should verify that.
* @param dbDevice
* @return
*/
private GBDevice toGBDevice(Device dbDevice) {
DeviceType deviceType = DeviceType.fromKey(dbDevice.getType());
GBDevice gbDevice = new GBDevice(dbDevice.getIdentifier(), dbDevice.getName(), deviceType);
List<DeviceAttributes> deviceAttributesList = dbDevice.getDeviceAttributesList();
if (deviceAttributesList.size() > 0) {
gbDevice.setModel(dbDevice.getModel());
DeviceAttributes attrs = deviceAttributesList.get(0);
gbDevice.setFirmwareVersion(attrs.getFirmwareVersion1());
gbDevice.setFirmwareVersion2(attrs.getFirmwareVersion2());
}
return gbDevice;
}
private List<GBDevice> getBondedDevices(BluetoothAdapter btAdapter) {
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
List<GBDevice> result = new ArrayList<>(pairedDevices.size());
DeviceHelper deviceHelper = DeviceHelper.getInstance();
for (BluetoothDevice pairedDevice : pairedDevices) {
GBDevice device = deviceHelper.toSupportedDevice(pairedDevice);
if (device != null) {
result.add(device);
}
}
return result;
}
}