2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 115ek, akasaka / Genjitsu Labs, Andreas Böhler,
|
|
|
|
Andreas Shimokawa, Andrew Watkins, angelpup, Arjan Schrijver, Carsten Pfeiffer,
|
|
|
|
Cre3per, DanialHanif, Daniel Dakhno, Daniele Gobbetti, Daniel Thompson, Da Pa,
|
|
|
|
Dmytro Bielik, Frank Ertl, GeekosaurusR3x, Gordon Williams, Jean-François
|
|
|
|
Greffier, jfgreffier, jhey, João Paulo Barraca, Jochen S, Johannes Krude,
|
|
|
|
José Rebelo, ksiwczynski, ladbsoft, Lesur Frederic, Maciej Kuśnierz, mamucho,
|
|
|
|
Manuel Ruß, maxirnilian, mkusnierz, narektor, Noodlez, odavo32nof, opavlov,
|
|
|
|
pangwalla, Pavel Elagin, Petr Kadlec, Petr Vaněk, protomors, Quallenauge,
|
|
|
|
Quang Ngô, Raghd Hamzeh, Sami Alaoui, Sebastian Kranz, sedy89, Sophanimus,
|
|
|
|
Stefan Bora, Taavi Eomäe, thermatk, tiparega, Vadim Kaushan, x29a, xaos,
|
|
|
|
Yukai Li
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
2015-10-18 01:01:13 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-12-13 00:43:07 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.widget.Toast;
|
2015-10-18 01:01:13 +02:00
|
|
|
|
2016-05-07 21:46:20 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-09-30 22:57:16 +02:00
|
|
|
import java.lang.reflect.Method;
|
2015-05-05 00:48:02 +02:00
|
|
|
import java.util.ArrayList;
|
2023-09-27 23:11:02 +02:00
|
|
|
import java.util.Arrays;
|
2016-08-31 00:33:54 +02:00
|
|
|
import java.util.Collections;
|
2023-10-28 10:49:16 +02:00
|
|
|
import java.util.HashMap;
|
2015-12-13 00:43:07 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2015-05-05 00:48:02 +02:00
|
|
|
import java.util.List;
|
2015-12-13 00:43:07 +01:00
|
|
|
import java.util.Set;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-09-30 23:07:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2016-08-31 00:33:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
2016-08-31 00:33:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
2021-10-31 17:36:04 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public class DeviceHelper {
|
2016-05-07 21:46:20 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DeviceHelper.class);
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private static final DeviceHelper instance = new DeviceHelper();
|
2023-09-27 23:11:02 +02:00
|
|
|
|
|
|
|
private DeviceType[] orderedDeviceTypes = null;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public static DeviceHelper getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-10-28 10:49:16 +02:00
|
|
|
private final HashMap<String, DeviceType> deviceTypeCache = new HashMap<>();
|
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
public GBDevice findAvailableDevice(String deviceAddress, Context context) {
|
|
|
|
Set<GBDevice> availableDevices = getAvailableDevices(context);
|
|
|
|
for (GBDevice availableDevice : availableDevices) {
|
|
|
|
if (deviceAddress.equals(availableDevice.getAddress())) {
|
|
|
|
return availableDevice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:40:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the list of all available devices that are supported by Gadgetbridge.
|
|
|
|
* Note that no state is known about the returned devices. Even if one of those
|
|
|
|
* devices is connected, it will report the default not-connected state.
|
|
|
|
*
|
|
|
|
* Clients interested in the "live" devices being managed should use the class
|
|
|
|
* DeviceManager.
|
|
|
|
* @param context
|
|
|
|
* @return
|
|
|
|
*/
|
2015-12-13 00:43:07 +01:00
|
|
|
public Set<GBDevice> getAvailableDevices(Context context) {
|
|
|
|
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
|
|
|
|
if (btAdapter == null) {
|
|
|
|
GB.toast(context, context.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
|
|
|
|
} else if (!btAdapter.isEnabled()) {
|
|
|
|
GB.toast(context, context.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
|
2016-09-02 10:54:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 04:28:03 +02:00
|
|
|
Set<GBDevice> availableDevices = new LinkedHashSet<>(getDatabaseDevices());
|
2016-09-02 10:54:32 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2020-08-26 04:28:03 +02:00
|
|
|
String miAddress = prefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
|
|
|
|
if (miAddress.length() > 0) {
|
2022-06-23 23:12:08 +02:00
|
|
|
GBDevice miDevice = new GBDevice(miAddress, "MI", null, null, DeviceType.MIBAND);
|
2016-09-29 22:40:16 +02:00
|
|
|
availableDevices.add(miDevice);
|
2016-09-02 10:54:32 +02:00
|
|
|
}
|
2015-12-13 00:43:07 +01:00
|
|
|
|
2016-09-02 10:54:32 +02:00
|
|
|
String pebbleEmuAddr = prefs.getString("pebble_emu_addr", "");
|
|
|
|
String pebbleEmuPort = prefs.getString("pebble_emu_port", "");
|
|
|
|
if (pebbleEmuAddr.length() >= 7 && pebbleEmuPort.length() > 0) {
|
2022-06-23 23:12:08 +02:00
|
|
|
GBDevice pebbleEmuDevice = new GBDevice(pebbleEmuAddr + ":" + pebbleEmuPort, "Pebble qemu", "", null, DeviceType.PEBBLE);
|
2016-09-02 10:54:32 +02:00
|
|
|
availableDevices.add(pebbleEmuDevice);
|
2015-12-13 00:43:07 +01:00
|
|
|
}
|
|
|
|
return availableDevices;
|
|
|
|
}
|
|
|
|
|
2015-10-18 01:01:13 +02:00
|
|
|
public GBDevice toSupportedDevice(BluetoothDevice device) {
|
2016-11-27 02:41:52 +01:00
|
|
|
GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN, device.getUuids());
|
2023-08-31 23:23:10 +02:00
|
|
|
candidate.refreshNameIfUnknown();
|
2017-01-26 00:11:52 +01:00
|
|
|
return toSupportedDevice(candidate);
|
|
|
|
}
|
2016-05-07 21:46:20 +02:00
|
|
|
|
2017-01-26 00:11:52 +01:00
|
|
|
public GBDevice toSupportedDevice(GBDeviceCandidate candidate) {
|
2023-09-27 23:11:02 +02:00
|
|
|
DeviceType resolvedType = resolveDeviceType(candidate);
|
|
|
|
return resolvedType.getDeviceCoordinator().createDevice(candidate, resolvedType);
|
2015-10-18 01:01:13 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 23:11:02 +02:00
|
|
|
private DeviceType[] getOrderedDeviceTypes(){
|
|
|
|
if(orderedDeviceTypes == null){
|
|
|
|
ArrayList<DeviceType> orderedDevices = new ArrayList<>(Arrays.asList(DeviceType.values()));
|
|
|
|
Collections.sort(orderedDevices, (dc1, dc2) -> dc1.getDeviceCoordinator().getOrderPriority() -
|
|
|
|
dc2.getDeviceCoordinator().getOrderPriority());
|
|
|
|
orderedDeviceTypes = orderedDevices.toArray(new DeviceType[0]);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2023-09-27 23:11:02 +02:00
|
|
|
|
|
|
|
return orderedDeviceTypes;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2023-10-28 10:49:16 +02:00
|
|
|
public DeviceType resolveDeviceType(GBDeviceCandidate deviceCandidate) {
|
|
|
|
return resolveDeviceType(deviceCandidate, true);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2023-10-28 10:49:16 +02:00
|
|
|
public DeviceType resolveDeviceType(GBDeviceCandidate deviceCandidate, boolean useCache){
|
2015-05-05 00:48:02 +02:00
|
|
|
synchronized (this) {
|
2023-10-28 10:49:16 +02:00
|
|
|
if(useCache) {
|
|
|
|
DeviceType cachedType =
|
|
|
|
deviceTypeCache.get(deviceCandidate.getMacAddress().toLowerCase());
|
|
|
|
if (cachedType != null) {
|
|
|
|
return cachedType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 23:11:02 +02:00
|
|
|
for (DeviceType type : getOrderedDeviceTypes()) {
|
|
|
|
if (type.getDeviceCoordinator().supports(deviceCandidate)) {
|
2023-10-28 10:49:16 +02:00
|
|
|
deviceTypeCache.put(deviceCandidate.getMacAddress().toLowerCase(), type);
|
2023-09-27 23:11:02 +02:00
|
|
|
return type;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-28 10:49:16 +02:00
|
|
|
deviceTypeCache.put(deviceCandidate.getMacAddress().toLowerCase(), DeviceType.UNKNOWN);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2023-09-27 23:11:02 +02:00
|
|
|
return DeviceType.UNKNOWN;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 23:11:02 +02:00
|
|
|
public DeviceCoordinator resolveCoordinator(GBDeviceCandidate device) {
|
|
|
|
return resolveDeviceType(device).getDeviceCoordinator();
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2016-08-31 00:33:54 +02:00
|
|
|
|
|
|
|
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);
|
2023-09-27 23:11:02 +02:00
|
|
|
if (gbDevice != null && gbDevice.getType().isSupported()) {
|
2016-08-31 00:33:54 +02:00
|
|
|
result.add(gbDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2020-08-02 10:55:06 +02:00
|
|
|
GB.toast(GBApplication.getContext().getString(R.string.error_retrieving_devices_database), Toast.LENGTH_SHORT, GB.ERROR, e);
|
2016-08-31 00:33:54 +02:00
|
|
|
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
|
|
|
|
*/
|
2016-11-27 09:49:28 +01:00
|
|
|
public GBDevice toGBDevice(Device dbDevice) {
|
2023-10-22 01:33:08 +02:00
|
|
|
DeviceType deviceType = DeviceType.fromName(dbDevice.getTypeName());
|
2022-06-23 23:12:08 +02:00
|
|
|
GBDevice gbDevice = new GBDevice(dbDevice.getIdentifier(), dbDevice.getName(), dbDevice.getAlias(), dbDevice.getParentFolder(), deviceType);
|
2023-09-27 23:11:02 +02:00
|
|
|
DeviceCoordinator coordinator = gbDevice.getDeviceCoordinator();
|
2021-10-31 17:36:04 +01:00
|
|
|
for (BatteryConfig batteryConfig : coordinator.getBatteryConfig()) {
|
2021-11-02 18:16:41 +01:00
|
|
|
gbDevice.setBatteryIcon(batteryConfig.getBatteryIcon(), batteryConfig.getBatteryIndex());
|
|
|
|
gbDevice.setBatteryLabel(batteryConfig.getBatteryLabel(), batteryConfig.getBatteryIndex());
|
2021-10-31 17:36:04 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:33:54 +02:00
|
|
|
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());
|
2016-11-27 09:49:28 +01:00
|
|
|
gbDevice.setVolatileAddress(attrs.getVolatileIdentifier());
|
2016-08-31 00:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return gbDevice;
|
|
|
|
}
|
|
|
|
|
2016-09-30 22:57:16 +02:00
|
|
|
/**
|
|
|
|
* Attempts to removing the bonding with the given device. Returns true
|
|
|
|
* if bonding was supposedly successful and false if anything went wrong
|
|
|
|
* @param device
|
|
|
|
* @return
|
|
|
|
*/
|
2016-09-30 23:07:30 +02:00
|
|
|
public boolean removeBond(GBDevice device) throws GBException {
|
2016-09-30 22:57:16 +02:00
|
|
|
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
if (defaultAdapter != null) {
|
|
|
|
BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress());
|
|
|
|
if (remoteDevice != null) {
|
|
|
|
try {
|
|
|
|
Method method = BluetoothDevice.class.getMethod("removeBond", (Class[]) null);
|
|
|
|
Object result = method.invoke(remoteDevice, (Object[]) null);
|
|
|
|
return Boolean.TRUE.equals(result);
|
|
|
|
} catch (Exception e) {
|
2016-09-30 23:07:30 +02:00
|
|
|
throw new GBException("Error removing bond to device: " + device, e);
|
2016-09-30 22:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-25 22:02:40 +01:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|