2015-05-05 00:48:02 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.miband;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2015-05-07 23:51:03 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.DeviceType;
|
2015-05-07 23:51:03 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
|
2015-07-20 23:20:24 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity;
|
2015-05-05 00:48:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.discovery.DeviceCandidate;
|
2015-07-27 23:49:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.SampleProvider;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public class MiBandCoordinator implements DeviceCoordinator {
|
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
|
|
|
private final MiBandSampleProvider sampleProvider;
|
|
|
|
|
|
|
|
public MiBandCoordinator() {
|
|
|
|
sampleProvider = new MiBandSampleProvider();
|
|
|
|
}
|
2015-05-07 23:51:03 +02:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public boolean supports(DeviceCandidate candidate) {
|
|
|
|
return candidate.getMacAddress().toUpperCase().startsWith(MiBandService.MAC_ADDRESS_FILTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supports(GBDevice device) {
|
|
|
|
return getDeviceType().equals(device.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-20 23:20:24 +02:00
|
|
|
public Class<? extends Activity> getPrimaryActivity() {
|
|
|
|
return ChartsActivity.class;
|
|
|
|
}
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
@Override
|
|
|
|
public SampleProvider getSampleProvider() {
|
|
|
|
return sampleProvider;
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
public static boolean hasValidUserInfo() {
|
|
|
|
String dummyMacAddress = MiBandService.MAC_ADDRESS_FILTER + ":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.
|
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 {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
|
|
|
|
int userYear = Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_YEAR_OF_BIRTH, "0"));
|
|
|
|
int age = 25;
|
|
|
|
if (userYear > 1900) {
|
|
|
|
age = Calendar.getInstance().get(Calendar.YEAR) - userYear;
|
|
|
|
if (age <= 0) {
|
|
|
|
age = 25;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UserInfo info = UserInfo.create(
|
|
|
|
miBandAddress,
|
|
|
|
prefs.getString(MiBandConst.PREF_USER_ALIAS, null),
|
|
|
|
("male".equals(prefs.getString(MiBandConst.PREF_USER_GENDER, null)) ? 1 : 0),
|
|
|
|
age,
|
|
|
|
Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_HEIGHT_CM, "175")),
|
|
|
|
Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_WEIGHT_KG, "70")),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
return info;
|
|
|
|
}
|
2015-07-29 18:49:16 +02:00
|
|
|
|
|
|
|
public static int getFitnessGoal(String miBandAddress) throws IllegalArgumentException {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
|
|
|
|
return Integer.parseInt(prefs.getString(MiBandConst.PREF_MIBAND_FITNESS_GOAL, "10000"));
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|