2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2015-2021 Andreas Shimokawa, Carsten Pfeiffer, Christian
|
2018-08-29 21:30:23 +02:00
|
|
|
Fischer, Daniele Gobbetti, José Rebelo, Szymon Tomasz Stefanek
|
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
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-11-27 01:09:20 +01:00
|
|
|
import android.annotation.TargetApi;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.app.Activity;
|
2016-07-12 00:24:23 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2016-11-27 01:09:20 +01:00
|
|
|
import android.bluetooth.le.ScanFilter;
|
2015-08-06 02:17:38 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
2016-11-27 01:09:20 +01:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.ParcelUuid;
|
2015-05-07 23:51:03 +02:00
|
|
|
|
2021-05-27 23:11:00 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-11-27 01:09:20 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2016-10-24 20:02:44 +02:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-10-24 20:02:44 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
2019-06-05 12:48:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2019-10-05 22:35:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
|
2022-07-10 22:01:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
|
2015-08-06 02:17:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
2016-06-16 21:54:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
2016-06-18 01:26:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
2016-10-24 20:02:44 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
2016-02-02 17:33:24 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2016-04-25 23:39:03 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2021-05-27 23:11:00 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_NAME;
|
|
|
|
|
2022-07-10 22:01:20 +02:00
|
|
|
public class MiBandCoordinator extends AbstractBLEDeviceCoordinator {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MiBandCoordinator.class);
|
2015-07-27 23:49:53 +02:00
|
|
|
|
|
|
|
public MiBandCoordinator() {
|
|
|
|
}
|
2015-05-07 23:51:03 +02:00
|
|
|
|
2016-11-27 01:09:20 +01:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
public Collection<? extends ScanFilter> createBLEScanFilters() {
|
|
|
|
ParcelUuid mi1Service = new ParcelUuid(MiBandService.UUID_SERVICE_MIBAND_SERVICE);
|
|
|
|
ScanFilter filter = new ScanFilter.Builder().setServiceUuid(mi1Service).build();
|
|
|
|
return Collections.singletonList(filter);
|
|
|
|
}
|
|
|
|
|
2016-11-27 02:41:52 +01:00
|
|
|
@NonNull
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
2016-11-27 01:09:20 +01:00
|
|
|
public DeviceType getSupportedType(GBDeviceCandidate candidate) {
|
2016-01-06 23:38:17 +01:00
|
|
|
String macAddress = candidate.getMacAddress().toUpperCase();
|
2016-06-28 00:35:50 +02:00
|
|
|
if (macAddress.startsWith(MiBandService.MAC_ADDRESS_FILTER_1_1A)
|
|
|
|
|| macAddress.startsWith(MiBandService.MAC_ADDRESS_FILTER_1S)) {
|
2016-11-27 01:09:20 +01:00
|
|
|
return DeviceType.MIBAND;
|
2016-06-28 00:35:50 +02:00
|
|
|
}
|
2016-08-17 00:53:16 +02:00
|
|
|
if (candidate.supportsService(MiBandService.UUID_SERVICE_MIBAND_SERVICE)
|
|
|
|
&& !candidate.supportsService(MiBandService.UUID_SERVICE_MIBAND2_SERVICE)) {
|
2016-11-27 01:09:20 +01:00
|
|
|
return DeviceType.MIBAND;
|
2016-06-28 00:35:50 +02:00
|
|
|
}
|
|
|
|
// and a heuristic
|
|
|
|
try {
|
2016-07-12 00:24:23 +02:00
|
|
|
BluetoothDevice device = candidate.getDevice();
|
|
|
|
if (isHealthWearable(device)) {
|
|
|
|
String name = device.getName();
|
2016-11-27 01:09:20 +01:00
|
|
|
if (name != null && name.toUpperCase().startsWith(MiBandConst.MI_GENERAL_NAME_PREFIX.toUpperCase())) {
|
|
|
|
return DeviceType.MIBAND;
|
|
|
|
}
|
2016-06-28 00:35:50 +02:00
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.error("unable to check device support", ex);
|
|
|
|
}
|
2016-11-27 01:09:20 +01:00
|
|
|
return DeviceType.UNKNOWN;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 20:02:44 +02:00
|
|
|
@Override
|
|
|
|
protected void deleteDevice(GBDevice gbDevice, Device device, DaoSession session) throws GBException {
|
|
|
|
Long deviceId = device.getId();
|
|
|
|
QueryBuilder<?> qb = session.getMiBandActivitySampleDao().queryBuilder();
|
|
|
|
qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public DeviceType getDeviceType() {
|
|
|
|
return DeviceType.MIBAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<? extends Activity> getPairingActivity() {
|
|
|
|
return MiBandPairingActivity.class;
|
|
|
|
}
|
2015-05-07 23:51:03 +02:00
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
@Override
|
2016-06-27 20:41:20 +02:00
|
|
|
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
|
2016-11-29 22:22:54 +01:00
|
|
|
return new MiBandSampleProvider(device, session);
|
2015-07-27 23:49:53 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
@Override
|
|
|
|
public InstallHandler findInstallHandler(Uri uri, Context context) {
|
|
|
|
MiBandFWInstallHandler handler = new MiBandFWInstallHandler(uri, context);
|
|
|
|
return handler.isValid() ? handler : null;
|
|
|
|
}
|
|
|
|
|
2015-10-24 23:28:02 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsActivityDataFetching() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsScreenshots() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
@Override
|
|
|
|
public int getAlarmSlotCount() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:55:00 +01:00
|
|
|
@Override
|
|
|
|
public boolean supportsSmartWakeup(GBDevice device) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:02:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsActivityTracking() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
|
|
|
public String getManufacturer() {
|
|
|
|
return "Xiaomi";
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:39:11 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsAppsManagement() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<? extends Activity> getAppsManagementActivity() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-19 21:51:23 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsCalendarEvents() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:59:46 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsRealtimeData() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:09:37 +01:00
|
|
|
@Override
|
|
|
|
public boolean supportsWeather() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-26 21:57:12 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsFindDevice() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
public static boolean hasValidUserInfo() {
|
2016-01-06 23:38:17 +01:00
|
|
|
String dummyMacAddress = MiBandService.MAC_ADDRESS_FILTER_1_1A + ":00:00:00";
|
2015-05-07 23:51:03 +02:00
|
|
|
try {
|
|
|
|
UserInfo userInfo = getConfiguredUserInfo(dummyMacAddress);
|
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the configured user info, or, if that is not available or invalid,
|
|
|
|
* a default user info.
|
2015-05-18 20:56:19 +02:00
|
|
|
*
|
2015-05-07 23:51:03 +02:00
|
|
|
* @param miBandAddress
|
|
|
|
*/
|
|
|
|
public static UserInfo getAnyUserInfo(String miBandAddress) {
|
|
|
|
try {
|
|
|
|
return getConfiguredUserInfo(miBandAddress);
|
|
|
|
} catch (Exception ex) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Error creating user info from settings, using default user instead: " + ex);
|
2015-05-07 23:51:03 +02:00
|
|
|
return UserInfo.getDefault(miBandAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user info from the user configured data in the preferences.
|
2015-05-18 20:56:19 +02:00
|
|
|
*
|
2015-05-07 23:51:03 +02:00
|
|
|
* @param miBandAddress
|
|
|
|
* @throws IllegalArgumentException when the user info can not be created
|
|
|
|
*/
|
|
|
|
public static UserInfo getConfiguredUserInfo(String miBandAddress) throws IllegalArgumentException {
|
2016-02-02 17:33:24 +01:00
|
|
|
ActivityUser activityUser = new ActivityUser();
|
2016-04-25 23:39:03 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2016-02-02 17:33:24 +01:00
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
UserInfo info = UserInfo.create(
|
|
|
|
miBandAddress,
|
2021-05-27 23:11:00 +02:00
|
|
|
prefs.getString(PREF_USER_NAME, null),
|
2016-05-13 23:47:47 +02:00
|
|
|
activityUser.getGender(),
|
|
|
|
activityUser.getAge(),
|
|
|
|
activityUser.getHeightCm(),
|
|
|
|
activityUser.getWeightKg(),
|
2015-05-07 23:51:03 +02:00
|
|
|
0
|
|
|
|
);
|
|
|
|
return info;
|
|
|
|
}
|
2015-07-29 18:49:16 +02:00
|
|
|
|
2019-10-05 22:35:30 +02:00
|
|
|
public static int getWearLocation(String deviceAddress) throws IllegalArgumentException {
|
2015-07-31 13:48:11 +02:00
|
|
|
int location = 0; //left hand
|
2019-10-05 22:35:30 +02:00
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
|
|
|
if ("right".equals(prefs.getString(DeviceSettingsPreferenceConst.PREF_WEARLOCATION, "left"))) {
|
2015-07-31 13:48:11 +02:00
|
|
|
location = 1; // right hand
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:11:44 +02:00
|
|
|
public static int getDeviceTimeOffsetHours(String deviceAddress) throws IllegalArgumentException {
|
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
2016-06-03 04:43:12 +02:00
|
|
|
return prefs.getInt(MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, 0);
|
|
|
|
}
|
|
|
|
|
2022-05-15 23:15:10 +02:00
|
|
|
public static boolean getHeartrateSleepSupport(String deviceAddress) throws IllegalArgumentException {
|
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
|
|
|
return prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_HEARTRATE_USE_FOR_SLEEP_DETECTION, false);
|
2016-04-02 16:09:30 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 16:11:35 +01:00
|
|
|
public static int getReservedAlarmSlots(String miBandAddress) throws IllegalArgumentException {
|
2019-12-14 23:43:54 +01:00
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(miBandAddress));
|
|
|
|
return prefs.getInt(DeviceSettingsPreferenceConst.PREF_RESERVER_ALARMS_CALENDAR, 0);
|
2015-12-17 16:11:35 +01:00
|
|
|
}
|
2016-06-19 23:22:28 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsHeartRateMeasurement(GBDevice device) {
|
2016-08-26 20:57:59 +02:00
|
|
|
String hwVersion = device.getModel();
|
2016-06-19 23:22:28 +02:00
|
|
|
return isMi1S(hwVersion) || isMiPro(hwVersion);
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:48:33 +02:00
|
|
|
@Override
|
|
|
|
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
|
|
|
|
return new int[]{
|
2019-10-05 22:35:30 +02:00
|
|
|
R.xml.devicesettings_wearlocation,
|
2022-05-15 23:15:10 +02:00
|
|
|
R.xml.devicesettings_heartrate_sleep,
|
2019-06-05 14:11:44 +02:00
|
|
|
R.xml.devicesettings_lowlatency_fwupdate,
|
2019-12-14 23:43:54 +01:00
|
|
|
R.xml.devicesettings_reserve_alarms_calendar,
|
2019-06-05 14:11:44 +02:00
|
|
|
R.xml.devicesettings_fake_timeoffset
|
2019-06-05 12:48:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-19 23:22:28 +02:00
|
|
|
private boolean isMi1S(String hardwareVersion) {
|
|
|
|
return MiBandConst.MI_1S.equals(hardwareVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isMiPro(String hardwareVersion) {
|
|
|
|
return MiBandConst.MI_PRO.equals(hardwareVersion);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|