2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Christian
|
|
|
|
Fischer, Daniele Gobbetti, Szymon Tomasz Stefanek
|
|
|
|
|
|
|
|
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;
|
|
|
|
import android.support.annotation.NonNull;
|
2015-05-07 23:51:03 +02:00
|
|
|
|
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;
|
2016-05-04 12:31:29 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
|
2015-10-24 23:28:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
|
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
|
|
|
|
2015-10-24 23:28:02 +02:00
|
|
|
public class MiBandCoordinator extends AbstractDeviceCoordinator {
|
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
|
|
|
|
2016-08-31 00:33:54 +02:00
|
|
|
@Override
|
2015-07-20 23:20:24 +02:00
|
|
|
public Class<? extends Activity> getPrimaryActivity() {
|
|
|
|
return ChartsActivity.class;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:53:55 +01:00
|
|
|
@Override
|
|
|
|
public boolean supportsAlarmConfiguration() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
prefs.getString(MiBandConst.PREF_USER_ALIAS, 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
|
|
|
|
2015-07-31 13:48:11 +02:00
|
|
|
public static int getWearLocation(String miBandAddress) throws IllegalArgumentException {
|
|
|
|
int location = 0; //left hand
|
2016-04-25 23:39:03 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2015-08-21 15:23:48 +02:00
|
|
|
if ("right".equals(prefs.getString(MiBandConst.PREF_MIBAND_WEARSIDE, "left"))) {
|
2015-07-31 13:48:11 +02:00
|
|
|
location = 1; // right hand
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2016-06-03 04:43:12 +02:00
|
|
|
public static int getDeviceTimeOffsetHours() throws IllegalArgumentException {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getInt(MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, 0);
|
|
|
|
}
|
|
|
|
|
2016-04-02 16:09:30 +02:00
|
|
|
public static boolean getHeartrateSleepSupport(String miBandAddress) throws IllegalArgumentException {
|
2016-04-25 23:39:03 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2016-04-02 16:09:30 +02:00
|
|
|
return prefs.getBoolean(MiBandConst.PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION, false);
|
|
|
|
}
|
|
|
|
|
2015-12-17 16:11:35 +01:00
|
|
|
public static int getReservedAlarmSlots(String miBandAddress) throws IllegalArgumentException {
|
2016-04-25 23:39:03 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|