2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2016-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2019-11-23 21:52:46 +01:00
|
|
|
Gobbetti, José Rebelo, Nephiel
|
2017-10-23 14:46:08 +02: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/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.huami;
|
|
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.bluetooth.le.ScanFilter;
|
|
|
|
import android.content.Context;
|
2019-05-20 16:36:06 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-10-23 14:46:08 +02:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.ParcelUuid;
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2017-10-23 14:46:08 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
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.SettingsActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.DateTimeDisplay;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.DoNotDisturb;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2SampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPairingActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
|
|
|
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.util.Prefs;
|
|
|
|
|
|
|
|
public abstract class HuamiCoordinator extends AbstractDeviceCoordinator {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(HuamiCoordinator.class);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<? extends Activity> getPairingActivity() {
|
|
|
|
return MiBandPairingActivity.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
public Collection<? extends ScanFilter> createBLEScanFilters() {
|
|
|
|
ParcelUuid mi2Service = new ParcelUuid(MiBandService.UUID_SERVICE_MIBAND2_SERVICE);
|
|
|
|
ScanFilter filter = new ScanFilter.Builder().setServiceUuid(mi2Service).build();
|
|
|
|
return Collections.singletonList(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException {
|
|
|
|
Long deviceId = device.getId();
|
|
|
|
QueryBuilder<?> qb = session.getMiBandActivitySampleDao().queryBuilder();
|
|
|
|
qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getManufacturer() {
|
|
|
|
return "Huami";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsAppsManagement() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<? extends Activity> getAppsManagementActivity() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsCalendarEvents() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsRealtimeData() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
@Override
|
|
|
|
public int getAlarmSlotCount() {
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2017-10-23 14:46:08 +02:00
|
|
|
@Override
|
|
|
|
public boolean supportsActivityDataFetching() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsActivityTracking() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-02 22:40:28 +02:00
|
|
|
@Override
|
2019-05-22 00:42:22 +02:00
|
|
|
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
|
|
|
|
return new int[]{R.xml.devicesettings_pairingkey};
|
2019-05-02 22:40:28 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 14:46:08 +02:00
|
|
|
@Override
|
|
|
|
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
|
|
|
|
return new MiBand2SampleProvider(device, session);
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:33:35 +02:00
|
|
|
public static DateTimeDisplay getDateDisplay(Context context, String deviceAddress) throws IllegalArgumentException {
|
|
|
|
SharedPreferences sharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
String dateFormatTime = context.getString(R.string.p_dateformat_time);
|
2019-05-23 21:33:35 +02:00
|
|
|
if (dateFormatTime.equals(sharedPrefs.getString(MiBandConst.PREF_MI2_DATEFORMAT, dateFormatTime))) {
|
2017-10-23 14:46:08 +02:00
|
|
|
return DateTimeDisplay.TIME;
|
|
|
|
}
|
|
|
|
return DateTimeDisplay.DATE_TIME;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:00:18 +02:00
|
|
|
public static ActivateDisplayOnLift getActivateDisplayOnLiftWrist(Context context, String deviceAddress) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
2018-03-23 23:27:03 +01:00
|
|
|
|
|
|
|
String liftOff = context.getString(R.string.p_off);
|
|
|
|
String liftOn = context.getString(R.string.p_on);
|
|
|
|
String liftScheduled = context.getString(R.string.p_scheduled);
|
|
|
|
|
2019-02-13 13:06:42 +01:00
|
|
|
String pref = prefs.getString(HuamiConst.PREF_ACTIVATE_DISPLAY_ON_LIFT, liftOff);
|
2018-03-23 23:27:03 +01:00
|
|
|
|
|
|
|
if (liftOn.equals(pref)) {
|
|
|
|
return ActivateDisplayOnLift.ON;
|
|
|
|
} else if (liftScheduled.equals(pref)) {
|
|
|
|
return ActivateDisplayOnLift.SCHEDULED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ActivateDisplayOnLift.OFF;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:00:18 +02:00
|
|
|
public static Date getDisplayOnLiftStart(String deviceAddress) {
|
|
|
|
return getTimePreference(HuamiConst.PREF_DISPLAY_ON_LIFT_START, "00:00", deviceAddress);
|
2018-03-23 23:27:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:00:18 +02:00
|
|
|
public static Date getDisplayOnLiftEnd(String deviceAddress) {
|
|
|
|
return getTimePreference(HuamiConst.PREF_DISPLAY_ON_LIFT_END, "00:00", deviceAddress);
|
2019-02-13 13:06:42 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 00:42:22 +02:00
|
|
|
public static DisconnectNotificationSetting getDisconnectNotificationSetting(Context context, String deviceAddress) {
|
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
2019-02-13 13:06:42 +01:00
|
|
|
|
|
|
|
String liftOff = context.getString(R.string.p_off);
|
|
|
|
String liftOn = context.getString(R.string.p_on);
|
|
|
|
String liftScheduled = context.getString(R.string.p_scheduled);
|
|
|
|
|
|
|
|
String pref = prefs.getString(HuamiConst.PREF_DISCONNECT_NOTIFICATION, liftOff);
|
|
|
|
|
|
|
|
if (liftOn.equals(pref)) {
|
|
|
|
return DisconnectNotificationSetting.ON;
|
|
|
|
} else if (liftScheduled.equals(pref)) {
|
|
|
|
return DisconnectNotificationSetting.SCHEDULED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DisconnectNotificationSetting.OFF;
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:49:59 +02:00
|
|
|
public static Date getDisconnectNotificationStart(String deviceAddress) {
|
|
|
|
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_START, "00:00", deviceAddress);
|
2019-02-13 13:06:42 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:49:59 +02:00
|
|
|
public static Date getDisconnectNotificationEnd(String deviceAddress) {
|
|
|
|
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_END, "00:00", deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
public static Set<String> getDisplayItems(String deviceAddress) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
|
|
|
return prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, null);
|
2017-10-23 14:46:08 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 20:22:38 +02:00
|
|
|
public static boolean getUseCustomFont(String deviceAddress) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
|
|
|
return prefs.getBoolean(HuamiConst.PREF_USE_CUSTOM_FONT, false);
|
|
|
|
}
|
|
|
|
|
2017-10-23 14:46:08 +02:00
|
|
|
public static boolean getGoalNotification() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getBoolean(MiBandConst.PREF_MI2_GOAL_NOTIFICATION, false);
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:00:18 +02:00
|
|
|
public static boolean getRotateWristToSwitchInfo(String deviceAddress) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
return prefs.getBoolean(MiBandConst.PREF_MI2_ROTATE_WRIST_TO_SWITCH_INFO, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getInactivityWarnings() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getBoolean(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getInactivityWarningsThreshold() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getInt(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_THRESHOLD, 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getInactivityWarningsDnd() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getBoolean(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_DND, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date getInactivityWarningsStart() {
|
|
|
|
return getTimePreference(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_START, "06:00");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date getInactivityWarningsEnd() {
|
|
|
|
return getTimePreference(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_END, "22:00");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date getInactivityWarningsDndStart() {
|
|
|
|
return getTimePreference(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_DND_START, "12:00");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date getInactivityWarningsDndEnd() {
|
|
|
|
return getTimePreference(MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_DND_END, "14:00");
|
|
|
|
}
|
|
|
|
|
2019-05-24 23:46:20 +02:00
|
|
|
public static Date getDoNotDisturbStart(String deviceAddress) {
|
|
|
|
return getTimePreference(MiBandConst.PREF_DO_NOT_DISTURB_START, "01:00", deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 23:46:20 +02:00
|
|
|
public static Date getDoNotDisturbEnd(String deviceAddress) {
|
|
|
|
return getTimePreference(MiBandConst.PREF_DO_NOT_DISTURB_END, "06:00", deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 00:42:22 +02:00
|
|
|
public static boolean getBandScreenUnlock(String deviceAddress) {
|
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
|
|
|
return prefs.getBoolean(MiBandConst.PREF_SWIPE_UNLOCK, false);
|
|
|
|
}
|
|
|
|
|
2019-08-27 11:13:45 +02:00
|
|
|
public static boolean getExposeHRThirdParty(String deviceAddress) {
|
|
|
|
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
|
|
|
return prefs.getBoolean(HuamiConst.PREF_EXPOSE_HR_THIRDPARTY, false);
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:49:59 +02:00
|
|
|
protected static Date getTimePreference(String key, String defaultValue, String deviceAddress) {
|
|
|
|
Prefs prefs;
|
|
|
|
|
|
|
|
if (deviceAddress == null) {
|
|
|
|
prefs = GBApplication.getPrefs();
|
|
|
|
} else {
|
|
|
|
prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
|
|
|
|
}
|
2017-10-23 14:46:08 +02:00
|
|
|
String time = prefs.getString(key, defaultValue);
|
|
|
|
|
|
|
|
DateFormat df = new SimpleDateFormat("HH:mm");
|
|
|
|
try {
|
|
|
|
return df.parse(time);
|
2019-05-24 23:46:20 +02:00
|
|
|
} catch (Exception e) {
|
2017-10-23 14:46:08 +02:00
|
|
|
LOG.error("Unexpected exception in MiBand2Coordinator.getTime: " + e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date();
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:49:59 +02:00
|
|
|
protected static Date getTimePreference(String key, String defaultValue) {
|
|
|
|
return getTimePreference(key, defaultValue, null);
|
|
|
|
}
|
|
|
|
|
2017-10-23 14:46:08 +02:00
|
|
|
public static MiBandConst.DistanceUnit getDistanceUnit() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
String unit = prefs.getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
|
|
|
if (unit.equals(GBApplication.getContext().getString(R.string.p_unit_metric))) {
|
|
|
|
return MiBandConst.DistanceUnit.METRIC;
|
|
|
|
} else {
|
|
|
|
return MiBandConst.DistanceUnit.IMPERIAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 23:46:20 +02:00
|
|
|
public static DoNotDisturb getDoNotDisturb(String deviceAddress) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress);
|
2017-10-23 14:46:08 +02:00
|
|
|
|
2019-05-24 23:46:20 +02:00
|
|
|
String pref = prefs.getString(MiBandConst.PREF_DO_NOT_DISTURB, MiBandConst.PREF_DO_NOT_DISTURB_OFF);
|
2017-10-23 14:46:08 +02:00
|
|
|
|
2019-05-24 23:46:20 +02:00
|
|
|
if (MiBandConst.PREF_DO_NOT_DISTURB_AUTOMATIC.equals(pref)) {
|
2017-10-23 14:46:08 +02:00
|
|
|
return DoNotDisturb.AUTOMATIC;
|
2019-05-24 23:46:20 +02:00
|
|
|
} else if (MiBandConst.PREF_DO_NOT_DISTURB_SCHEDULED.equals(pref)) {
|
2017-10-23 14:46:08 +02:00
|
|
|
return DoNotDisturb.SCHEDULED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoNotDisturb.OFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsScreenshots() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsSmartWakeup(GBDevice device) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-26 21:57:12 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsFindDevice() {
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-13 08:08:54 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsAlarmSnoozing() {
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-23 14:46:08 +02:00
|
|
|
}
|