WIP: more work, compile again

This commit is contained in:
cpfeiffer 2016-06-06 23:18:46 +02:00
parent 3b87966fe9
commit ae548d0806
6 changed files with 75 additions and 23 deletions

View File

@ -30,6 +30,7 @@ public class GBDaoGenerator {
public static final String VALID_TO_UTC = "validToUTC"; public static final String VALID_TO_UTC = "validToUTC";
private static final String MAIN_PACKAGE = "nodomain.freeyourgadget.gadgetbridge"; private static final String MAIN_PACKAGE = "nodomain.freeyourgadget.gadgetbridge";
private static final String MODEL_PACKAGE = MAIN_PACKAGE + ".model"; private static final String MODEL_PACKAGE = MAIN_PACKAGE + ".model";
private static final String VALID_BY_DATE = MODEL_PACKAGE + ".ValidByDate";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Schema schema = new Schema(7, MAIN_PACKAGE + ".entities"); Schema schema = new Schema(7, MAIN_PACKAGE + ".entities");
@ -89,12 +90,18 @@ public class GBDaoGenerator {
userAttributes.addIntProperty("weightKG").notNull(); userAttributes.addIntProperty("weightKG").notNull();
userAttributes.addIntProperty("sleepGoalHPD"); userAttributes.addIntProperty("sleepGoalHPD");
userAttributes.addIntProperty("stepsGoalSPD"); userAttributes.addIntProperty("stepsGoalSPD");
userAttributes.addDateProperty(VALID_FROM_UTC); addDateValidityTo(userAttributes);
userAttributes.addDateProperty(VALID_TO_UTC);
return userAttributes; return userAttributes;
} }
private static void addDateValidityTo(Entity entity) {
entity.addDateProperty(VALID_FROM_UTC);
entity.addDateProperty(VALID_TO_UTC);
entity.implementsInterface(VALID_BY_DATE);
}
private static Entity addDevice(Schema schema, Entity deviceAttributes) { private static Entity addDevice(Schema schema, Entity deviceAttributes) {
Entity device = addEntity(schema, "Device"); Entity device = addEntity(schema, "Device");
device.addIdProperty(); device.addIdProperty();
@ -114,8 +121,7 @@ public class GBDaoGenerator {
deviceAttributes.addIdProperty(); deviceAttributes.addIdProperty();
deviceAttributes.addStringProperty("firmwareVersion1").notNull(); deviceAttributes.addStringProperty("firmwareVersion1").notNull();
deviceAttributes.addStringProperty("firmwareVersion2"); deviceAttributes.addStringProperty("firmwareVersion2");
deviceAttributes.addDateProperty(VALID_FROM_UTC); addDateValidityTo(deviceAttributes);
deviceAttributes.addDateProperty(VALID_TO_UTC);
return deviceAttributes; return deviceAttributes;
} }

View File

@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -25,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao; import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.ValidByDate;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
@ -175,7 +177,7 @@ public class DBHelper {
private static boolean hasUpToDateUserAttributes(List<UserAttributes> userAttributes, ActivityUser prefsUser) { private static boolean hasUpToDateUserAttributes(List<UserAttributes> userAttributes, ActivityUser prefsUser) {
for (UserAttributes attr : userAttributes) { for (UserAttributes attr : userAttributes) {
if (!isActive(attr)) { if (!isValidNow(attr)) {
return false; return false;
} }
if (isEqual(attr, prefsUser)) { if (isEqual(attr, prefsUser)) {
@ -185,7 +187,38 @@ public class DBHelper {
return false; return false;
} }
private static boolean isValidNow(ValidByDate element) {
Calendar cal = DateTimeUtils.getCalendarUTC();
Date nowUTC = cal.getTime();
return isValid(element, nowUTC);
}
private static boolean isValid(ValidByDate element, Date nowUTC) {
Date validFromUTC = element.getValidFromUTC();
Date validToUTC = element.getValidToUTC();
if (nowUTC.before(validFromUTC)) {
return false;
}
if (validToUTC != null && nowUTC.after(validToUTC)) {
return false;
}
return true;
}
private static boolean isEqual(UserAttributes attr, ActivityUser prefsUser) { private static boolean isEqual(UserAttributes attr, ActivityUser prefsUser) {
if (prefsUser.getHeightCm() != attr.getHeightCM()) {
return false;
}
if (prefsUser.getWeightKg() != attr.getWeightKG()) {
return false;
}
if (prefsUser.getSleepDuration() != attr.getSleepGoalHPD()) {
return false;
}
if (prefsUser.getStepsGoal() != attr.getStepsGoalSPD()) {
return false;
}
return true;
} }
public static Device getDevice(GBDevice gbDevice, DaoSession session) { public static Device getDevice(GBDevice gbDevice, DaoSession session) {

View File

@ -3,9 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.model;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -15,11 +15,12 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class ActivityUser { public class ActivityUser {
private String activityUserName; private String activityUserName;
private Integer activityUserGender; private int activityUserGender;
private Integer activityUserYearOfBirth; private int activityUserYearOfBirth;
private Integer activityUserHeightCm; private int activityUserHeightCm;
private Integer activityUserWeightKg; private int activityUserWeightKg;
private Integer activityUserSleepDuration; private int activityUserSleepDuration;
private int activityUserStepsGoal;
private static final String defaultUserName = "gadgetbridge-user"; private static final String defaultUserName = "gadgetbridge-user";
public static final int defaultUserGender = 0; public static final int defaultUserGender = 0;
@ -28,6 +29,7 @@ public class ActivityUser {
public static final int defaultUserHeightCm = 175; public static final int defaultUserHeightCm = 175;
public static final int defaultUserWeightKg = 70; public static final int defaultUserWeightKg = 70;
public static final int defaultUserSleepDuration = 7; public static final int defaultUserSleepDuration = 7;
public static final int defaultUserStepsGoal = 8000;
public static final String PREF_USER_NAME = "mi_user_alias"; public static final String PREF_USER_NAME = "mi_user_alias";
public static final String PREF_USER_YEAR_OF_BIRTH = "activity_user_year_of_birth"; public static final String PREF_USER_YEAR_OF_BIRTH = "activity_user_year_of_birth";
@ -35,6 +37,7 @@ public class ActivityUser {
public static final String PREF_USER_HEIGHT_CM = "activity_user_height_cm"; public static final String PREF_USER_HEIGHT_CM = "activity_user_height_cm";
public static final String PREF_USER_WEIGHT_KG = "activity_user_weight_kg"; public static final String PREF_USER_WEIGHT_KG = "activity_user_weight_kg";
public static final String PREF_USER_SLEEP_DURATION = "activity_user_sleep_duration"; public static final String PREF_USER_SLEEP_DURATION = "activity_user_sleep_duration";
public static final String PREF_USER_STEPS_GOAL = MiBandConst.PREF_MIBAND_FITNESS_GOAL;
public ActivityUser() { public ActivityUser() {
fetchPreferences(); fetchPreferences();
@ -71,6 +74,13 @@ public class ActivityUser {
return activityUserSleepDuration; return activityUserSleepDuration;
} }
public int getStepsGoal() {
if (activityUserStepsGoal < 0) {
activityUserStepsGoal = defaultUserStepsGoal;
}
return activityUserStepsGoal;
}
public int getAge() { public int getAge() {
int userYear = getYearOfBirth(); int userYear = getYearOfBirth();
int age = 25; int age = 25;
@ -91,6 +101,7 @@ public class ActivityUser {
activityUserWeightKg = prefs.getInt(PREF_USER_WEIGHT_KG, defaultUserWeightKg); activityUserWeightKg = prefs.getInt(PREF_USER_WEIGHT_KG, defaultUserWeightKg);
activityUserYearOfBirth = prefs.getInt(PREF_USER_YEAR_OF_BIRTH, defaultUserYearOfBirth); activityUserYearOfBirth = prefs.getInt(PREF_USER_YEAR_OF_BIRTH, defaultUserYearOfBirth);
activityUserSleepDuration = prefs.getInt(PREF_USER_SLEEP_DURATION, defaultUserSleepDuration); activityUserSleepDuration = prefs.getInt(PREF_USER_SLEEP_DURATION, defaultUserSleepDuration);
activityUserStepsGoal = prefs.getInt(PREF_USER_STEPS_GOAL, defaultUserStepsGoal);
} }
public Date getUserBirthday() { public Date getUserBirthday() {

View File

@ -0,0 +1,8 @@
package nodomain.freeyourgadget.gadgetbridge.model;
import java.util.Date;
public interface ValidByDate {
Date getValidFromUTC();
Date getValidToUTC();
}

View File

@ -58,20 +58,18 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth {
} }
private boolean store(OverlayRecord[] overlayRecords) { private boolean store(OverlayRecord[] overlayRecords) {
DBHandler dbHandler = null; try (DBHandler dbHandler = GBApplication.acquireDB()) {
SampleProvider sampleProvider = new HealthSampleProvider(); SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
try { int latestTimestamp = sampleProvider.fetchLatestTimestamp();
dbHandler = GBApplication.acquireDB();
int latestTimestamp = dbHandler.fetchLatestTimestamp(sampleProvider);
for (OverlayRecord overlayRecord : overlayRecords) { for (OverlayRecord overlayRecord : overlayRecords) {
if (latestTimestamp < (overlayRecord.timestampStart + overlayRecord.durationSeconds)) if (latestTimestamp < (overlayRecord.timestampStart + overlayRecord.durationSeconds))
return false; return false;
switch (overlayRecord.type) { switch (overlayRecord.type) {
case 1: case 1:
dbHandler.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP), sampleProvider); sampleProvider.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP));
break; break;
case 2: case 2:
dbHandler.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP), sampleProvider); sampleProvider.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP));
break; break;
default: default:
//TODO: other values refer to unknown activity types. //TODO: other values refer to unknown activity types.
@ -79,10 +77,6 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth {
} }
} catch (Exception ex) { } catch (Exception ex) {
LOG.debug(ex.getMessage()); LOG.debug(ex.getMessage());
} finally {
if (dbHandler != null) {
dbHandler.release();
}
} }
return true; return true;
} }

View File

@ -1905,7 +1905,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", itemSize=" + item_size); LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", itemSize=" + item_size);
if (!mDatalogSessions.containsKey(id)) { if (!mDatalogSessions.containsKey(id)) {
if (uuid.equals(UUID_ZERO) && log_tag == 81) { if (uuid.equals(UUID_ZERO) && log_tag == 81) {
mDatalogSessions.put(id, new DatalogSessionHealthSteps(id, uuid, log_tag, item_type, item_size, getDevice()))); mDatalogSessions.put(id, new DatalogSessionHealthSteps(id, uuid, log_tag, item_type, item_size, getDevice()));
} else if (uuid.equals(UUID_ZERO) && log_tag == 83) { } else if (uuid.equals(UUID_ZERO) && log_tag == 83) {
mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, log_tag, item_type, item_size)); mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, log_tag, item_type, item_size));
} else if (uuid.equals(UUID_ZERO) && log_tag == 84) { } else if (uuid.equals(UUID_ZERO) && log_tag == 84) {