From 468a40c62edac35ceb66c532ab595c6f8529ad9d Mon Sep 17 00:00:00 2001 From: vanous Date: Sun, 5 Dec 2021 10:32:35 +0100 Subject: [PATCH] Activity Card: React to User settings. Unify step length --- .../gadgetbridge/SleepAlarmWidget.java | 2 +- .../freeyourgadget/gadgetbridge/Widget.java | 6 +- .../AboutUserPreferencesActivity.java | 6 ++ .../activities/AbstractSettingsActivity.java | 23 ++++++ .../activities/ControlCenterv2.java | 4 +- .../activities/WidgetAlarmsActivity.java | 4 +- .../charts/ActivityListingAdapter.java | 4 +- .../activities/charts/StepAnalysis.java | 11 +-- .../DeviceSpecificSettingsFragment.java | 4 +- .../adapter/GBDeviceAdapterv2.java | 6 +- .../gadgetbridge/database/DBHelper.java | 6 +- .../gadgetbridge/model/ActivityUser.java | 71 +++++++++++-------- .../operations/SetConfigurationOperation.java | 4 +- .../devices/zetime/ZeTimeDeviceSupport.java | 9 ++- 14 files changed, 97 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepAlarmWidget.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepAlarmWidget.java index 25ba6def5..11b054843 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepAlarmWidget.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepAlarmWidget.java @@ -90,7 +90,7 @@ public class SleepAlarmWidget extends AppWidgetProvider { public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (ACTION.equals(intent.getAction())) { - int userSleepDuration = new ActivityUser().getSleepDuration(); + int userSleepDuration = new ActivityUser().getSleepDurationGoal(); // current timestamp GregorianCalendar calendar = new GregorianCalendar(); // add preferred sleep duration diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java index 01ebde4ff..8dd4d32cc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java @@ -158,11 +158,11 @@ public class Widget extends AppWidgetProvider { int sleep = (int) dailyTotals[1]; ActivityUser activityUser = new ActivityUser(); int stepGoal = activityUser.getStepsGoal(); - int sleepGoal = activityUser.getSleepDuration(); + int sleepGoal = activityUser.getSleepDurationGoal(); int sleepGoalMinutes = sleepGoal * 60; - int distanceGoal = activityUser.getDistanceMeters() * 100; + int distanceGoal = activityUser.getDistanceGoalMeters() * 100; int stepLength = activityUser.getStepLengthCm(); - double distanceMeters = dailyTotals[0] * stepLength / 100; + double distanceMeters = dailyTotals[0] * stepLength * 0.01; double distanceFeet = distanceMeters * 3.28084f; double distanceFormatted = 0; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java index a7054377b..b945753e3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java @@ -47,6 +47,12 @@ public class AboutUserPreferencesActivity extends AbstractSettingsActivity { addPreferenceHandlerFor(PREF_USER_WEIGHT_KG); addPreferenceHandlerFor(PREF_USER_GENDER); addPreferenceHandlerFor(PREF_USER_STEPS_GOAL); + + addIntentNotificationListener(PREF_USER_STEPS_GOAL); + addIntentNotificationListener(PREF_USER_HEIGHT_CM); + addIntentNotificationListener(PREF_USER_SLEEP_DURATION); + addIntentNotificationListener(PREF_USER_STEP_LENGTH_CM); + addIntentNotificationListener(PREF_USER_DISTANCE_METERS); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java index afb41e865..9000aac48 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java @@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory; import java.util.Locale; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; /** @@ -243,4 +244,26 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi LOG.warn("Could not find preference " + preferenceKey); } } + + // Ensure that the Control center is re-rendered when user preferences change + protected void addIntentNotificationListener(final String preferenceKey) { + Preference pref = findPreference(preferenceKey); + + if (pref != null) { + pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); + return true; + } + }); + } else { + LOG.warn("Could not find preference " + preferenceKey); + } + } + + + + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java index 1a1e12835..2b2e82205 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java @@ -514,8 +514,8 @@ public class ControlCenterv2 extends AppCompatActivity for (GBDevice gbDevice : deviceList) { final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice); if (coordinator.supportsActivityTracking()) { - long[] steps = getSteps(gbDevice, db); - deviceActivityHashMap.put(gbDevice.getAddress(), steps); + long[] stepsAndSleepData = getSteps(gbDevice, db); + deviceActivityHashMap.put(gbDevice.getAddress(), stepsAndSleepData); } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/WidgetAlarmsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/WidgetAlarmsActivity.java index eddc42b4e..59ee7ff21 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/WidgetAlarmsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/WidgetAlarmsActivity.java @@ -68,7 +68,7 @@ public class WidgetAlarmsActivity extends Activity implements View.OnClickListen } else { setContentView(R.layout.widget_alarms_activity_list); - int userSleepDuration = new ActivityUser().getSleepDuration(); + int userSleepDuration = new ActivityUser().getSleepDurationGoal(); textView = findViewById(R.id.alarm5); if (userSleepDuration > 0) { Resources res = getResources(); @@ -117,7 +117,7 @@ public class WidgetAlarmsActivity extends Activity implements View.OnClickListen if (duration > 0) { calendar.add(Calendar.MINUTE, duration); } else { - int userSleepDuration = new ActivityUser().getSleepDuration(); + int userSleepDuration = new ActivityUser().getSleepDurationGoal(); // add preferred sleep duration if (userSleepDuration > 0) { calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java index e0a4b094a..26ec55322 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java @@ -41,8 +41,8 @@ public class ActivityListingAdapter extends AbstractActivityListingAdapter result = new ArrayList<>(); ActivityUser activityUser = new ActivityUser(); - double STEP_LENGTH_M; final int MIN_SESSION_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_min_session_length", 5); final int MAX_IDLE_PHASE_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_max_idle_phase_length", 5); final int MIN_STEPS_PER_MINUTE = GBApplication.getPrefs().getInt("chart_list_min_steps_per_minute", 40); int stepLengthCm = activityUser.getStepLengthCm(); - int heightCm = activityUser.getHeightCm(); - totalDailySteps = 0; - - if (stepLengthCm == 0 && heightCm != 0) { - STEP_LENGTH_M = heightCm * 0.43 * 0.01; - } else { - STEP_LENGTH_M = stepLengthCm * 0.01; - } + final double STEP_LENGTH_M = stepLengthCm * 0.01; final double MIN_SESSION_INTENSITY = Math.max(0, Math.min(1, MIN_STEPS_PER_MINUTE * 0.01)); + totalDailySteps = 0; ActivitySample previousSample = null; Date sessionStart = null; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java index 22baa0f79..2ed5f41bf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java @@ -525,8 +525,6 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat { addPreferenceHandlerFor(PREF_GALAXY_BUDS_TOUCH_RIGHT); addPreferenceHandlerFor(PREF_GALAXY_BUDS_LIVE_ANC); addPreferenceHandlerFor(PREF_GALAXY_BUDS_PRESSURE_RELIEF); - addPreferenceHandlerFor(PREFS_DEVICE_CHARTS_TABS); - addPreferenceHandlerFor(PREF_SONY_AMBIENT_SOUND_CONTROL); addPreferenceHandlerFor(PREF_SONY_FOCUS_VOICE); @@ -810,7 +808,7 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat { activityInDeviceSteps, activityInDeviceSleep, activityInDeviceDistance, - chartsTabsOrderSelection + chartsTabsOrderSelection, }; for (Preference preferenceInControlCenter : preferencesInControlCenter) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapterv2.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapterv2.java index d404a7267..55736a72d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapterv2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapterv2.java @@ -890,11 +890,11 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter 24) { - activityUserSleepDuration = defaultUserSleepDuration; + public int getSleepDurationGoal() { + if (activityUserSleepDurationGoal < 1 || activityUserSleepDurationGoal > 24) { + activityUserSleepDurationGoal = defaultUserSleepDurationGoal; } - return activityUserSleepDuration; + return activityUserSleepDurationGoal; } public int getStepsGoal() { @@ -141,11 +156,11 @@ public class ActivityUser { activityUserHeightCm = prefs.getInt(PREF_USER_HEIGHT_CM, defaultUserHeightCm); activityUserWeightKg = prefs.getInt(PREF_USER_WEIGHT_KG, defaultUserWeightKg); activityUserYearOfBirth = prefs.getInt(PREF_USER_YEAR_OF_BIRTH, defaultUserYearOfBirth); - activityUserSleepDuration = prefs.getInt(PREF_USER_SLEEP_DURATION, defaultUserSleepDuration); + activityUserSleepDurationGoal = prefs.getInt(PREF_USER_SLEEP_DURATION, defaultUserSleepDurationGoal); activityUserStepsGoal = prefs.getInt(PREF_USER_STEPS_GOAL, defaultUserStepsGoal); - activityUserCaloriesBurnt = prefs.getInt(PREF_USER_CALORIES_BURNT, defaultUserCaloriesBurnt); - activityUserDistanceMeters = prefs.getInt(PREF_USER_DISTANCE_METERS, defaultUserDistanceMeters); - activityUserActiveTimeMinutes = prefs.getInt(PREF_USER_ACTIVETIME_MINUTES, defaultUserActiveTimeMinutes); + activityUserCaloriesBurntGoal = prefs.getInt(PREF_USER_CALORIES_BURNT, defaultUserCaloriesBurntGoal); + activityUserDistanceGoalMeters = prefs.getInt(PREF_USER_DISTANCE_METERS, defaultUserDistanceGoalMeters); + activityUserActiveTimeGoalMinutes = prefs.getInt(PREF_USER_ACTIVETIME_MINUTES, defaultUserActiveTimeGoalMinutes); activityUserStepLengthCm = prefs.getInt(PREF_USER_STEP_LENGTH_CM, defaultUserStepLengthCm); } @@ -155,27 +170,27 @@ public class ActivityUser { return cal.getTime(); } - public int getCaloriesBurnt() + public int getCaloriesBurntGoal() { - if (activityUserCaloriesBurnt < 1) { - activityUserCaloriesBurnt = defaultUserCaloriesBurnt; + if (activityUserCaloriesBurntGoal < 1) { + activityUserCaloriesBurntGoal = defaultUserCaloriesBurntGoal; } - return activityUserCaloriesBurnt; + return activityUserCaloriesBurntGoal; } - public int getDistanceMeters() + public int getDistanceGoalMeters() { - if (activityUserDistanceMeters < 1) { - activityUserDistanceMeters = defaultUserDistanceMeters; + if (activityUserDistanceGoalMeters < 1) { + activityUserDistanceGoalMeters = defaultUserDistanceGoalMeters; } - return activityUserDistanceMeters; + return activityUserDistanceGoalMeters; } - public int getActiveTimeMinutes() + public int getActiveTimeGoalMinutes() { - if (activityUserActiveTimeMinutes < 1) { - activityUserActiveTimeMinutes = defaultUserActiveTimeMinutes; + if (activityUserActiveTimeGoalMinutes < 1) { + activityUserActiveTimeGoalMinutes = defaultUserActiveTimeGoalMinutes; } - return activityUserActiveTimeMinutes; + return activityUserActiveTimeGoalMinutes; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/operations/SetConfigurationOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/operations/SetConfigurationOperation.java index efa0cf7b4..88b3b48c9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/operations/SetConfigurationOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/operations/SetConfigurationOperation.java @@ -171,7 +171,7 @@ public class SetConfigurationOperation extends AbstractBTLEOperation> 8) & 0xff); @@ -179,7 +179,7 @@ public class SetConfigurationOperation extends AbstractBTLEOperation> 8) & 0xff); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java index be9b37373..46679b981 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java @@ -65,7 +65,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; -import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; @@ -1580,10 +1579,10 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { private void setUserGoals(TransactionBuilder builder) { ActivityUser activityUser = new ActivityUser(); int steps = activityUser.getStepsGoal() / 100; // ZeTime expect the steps in 100 increment - int calories = activityUser.getCaloriesBurnt(); - int distance = activityUser.getDistanceMeters() / 1000; // ZeTime only accepts km goals - int sleep = activityUser.getSleepDuration(); - int activeTime = activityUser.getActiveTimeMinutes(); + int calories = activityUser.getCaloriesBurntGoal(); + int distance = activityUser.getDistanceGoalMeters() / 1000; // ZeTime only accepts km goals + int sleep = activityUser.getSleepDurationGoal(); + int activeTime = activityUser.getActiveTimeGoalMinutes(); // set steps goal byte[] goal_steps = {ZeTimeConstants.CMD_PREAMBLE,