package nodomain.freeyourgadget.gadgetbridge.devices.miband; import android.annotation.TargetApi; import android.app.Activity; import android.bluetooth.BluetoothDevice; import android.bluetooth.le.ScanFilter; import android.content.Context; import android.net.Uri; import android.os.Build; import android.os.ParcelUuid; import android.support.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; import java.util.Collections; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class MiBandCoordinator extends AbstractDeviceCoordinator { private static final Logger LOG = LoggerFactory.getLogger(MiBandCoordinator.class); public MiBandCoordinator() { } @NonNull @Override @TargetApi(Build.VERSION_CODES.LOLLIPOP) public Collection createBLEScanFilters() { ParcelUuid mi1Service = new ParcelUuid(MiBandService.UUID_SERVICE_MIBAND_SERVICE); ScanFilter filter = new ScanFilter.Builder().setServiceUuid(mi1Service).build(); return Collections.singletonList(filter); } @NonNull @Override public DeviceType getSupportedType(GBDeviceCandidate candidate) { String macAddress = candidate.getMacAddress().toUpperCase(); if (macAddress.startsWith(MiBandService.MAC_ADDRESS_FILTER_1_1A) || macAddress.startsWith(MiBandService.MAC_ADDRESS_FILTER_1S)) { return DeviceType.MIBAND; } if (candidate.supportsService(MiBandService.UUID_SERVICE_MIBAND_SERVICE) && !candidate.supportsService(MiBandService.UUID_SERVICE_MIBAND2_SERVICE)) { return DeviceType.MIBAND; } // and a heuristic try { BluetoothDevice device = candidate.getDevice(); if (isHealthWearable(device)) { String name = device.getName(); if (name != null && name.toUpperCase().startsWith(MiBandConst.MI_GENERAL_NAME_PREFIX.toUpperCase())) { return DeviceType.MIBAND; } } } catch (Exception ex) { LOG.error("unable to check device support", ex); } return DeviceType.UNKNOWN; } @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(); } @Override public DeviceType getDeviceType() { return DeviceType.MIBAND; } @Override public Class getPairingActivity() { return MiBandPairingActivity.class; } @Override public Class getPrimaryActivity() { return ChartsActivity.class; } @Override public SampleProvider getSampleProvider(GBDevice device, DaoSession session) { return new MiBandSampleProvider(device, session); } @Override public InstallHandler findInstallHandler(Uri uri, Context context) { MiBandFWInstallHandler handler = new MiBandFWInstallHandler(uri, context); return handler.isValid() ? handler : null; } @Override public boolean supportsActivityDataFetching() { return true; } @Override public boolean supportsScreenshots() { return false; } @Override public boolean supportsAlarmConfiguration() { return true; } @Override public boolean supportsSmartWakeup(GBDevice device) { return true; } @Override public boolean supportsActivityTracking() { return true; } @Override public int getTapString() { return R.string.tap_connected_device_for_activity; } @Override public String getManufacturer() { return "Xiaomi"; } @Override public boolean supportsAppsManagement() { return false; } @Override public Class getAppsManagementActivity() { return null; } public static boolean hasValidUserInfo() { String dummyMacAddress = MiBandService.MAC_ADDRESS_FILTER_1_1A + ":00:00: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. * * @param miBandAddress */ public static UserInfo getAnyUserInfo(String miBandAddress) { try { return getConfiguredUserInfo(miBandAddress); } catch (Exception ex) { LOG.error("Error creating user info from settings, using default user instead: " + ex); return UserInfo.getDefault(miBandAddress); } } /** * Returns the user info from the user configured data in the preferences. * * @param miBandAddress * @throws IllegalArgumentException when the user info can not be created */ public static UserInfo getConfiguredUserInfo(String miBandAddress) throws IllegalArgumentException { ActivityUser activityUser = new ActivityUser(); Prefs prefs = GBApplication.getPrefs(); UserInfo info = UserInfo.create( miBandAddress, prefs.getString(MiBandConst.PREF_USER_ALIAS, null), activityUser.getGender(), activityUser.getAge(), activityUser.getHeightCm(), activityUser.getWeightKg(), 0 ); return info; } public static int getWearLocation(String miBandAddress) throws IllegalArgumentException { int location = 0; //left hand Prefs prefs = GBApplication.getPrefs(); if ("right".equals(prefs.getString(MiBandConst.PREF_MIBAND_WEARSIDE, "left"))) { location = 1; // right hand } return location; } public static int getDeviceTimeOffsetHours() throws IllegalArgumentException { Prefs prefs = GBApplication.getPrefs(); return prefs.getInt(MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, 0); } public static boolean getHeartrateSleepSupport(String miBandAddress) throws IllegalArgumentException { Prefs prefs = GBApplication.getPrefs(); return prefs.getBoolean(MiBandConst.PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION, false); } public static int getFitnessGoal(String miBandAddress) throws IllegalArgumentException { Prefs prefs = GBApplication.getPrefs(); return prefs.getInt(MiBandConst.PREF_MIBAND_FITNESS_GOAL, 10000); } public static int getReservedAlarmSlots(String miBandAddress) throws IllegalArgumentException { Prefs prefs = GBApplication.getPrefs(); return prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, 0); } @Override public boolean supportsHeartRateMeasurement(GBDevice device) { String hwVersion = device.getModel(); 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); } }