1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +01:00

Part 2: Add inactivity alert - complete now. Add checkbox to en-/disable the heart rate alarm and group this with the limits in its own screen.

This commit is contained in:
Sebastian Kranz 2018-09-17 08:10:49 +02:00
parent 828df4898f
commit ba0fdb8c96
5 changed files with 235 additions and 22 deletions

View File

@ -120,6 +120,7 @@ public class ZeTimeConstants {
public static final String PREF_ZETIME_DONT_DEL_ACTDATA = "zetime_dont_del_actdata";
public static final String PREF_ZETIME_HEARTRATE_ALARM = "zetime_heartrate_alarm_enable";
public static final String PREF_ZETIME_MAX_HEARTRATE = "alarm_max_heart_rate";
public static final String PREF_ZETIME_MIN_HEARTRATE = "alarm_min_heart_rate";
@ -127,6 +128,7 @@ public class ZeTimeConstants {
public static final String PREF_DO_NOT_DISTURB_START = "zetime_do_not_disturb_start";
public static final String PREF_DO_NOT_DISTURB_END = "zetime_do_not_disturb_end";
public static final String PREF_INACTIVITY_KEY = "zetime_inactivity_warning_key";
public static final String PREF_INACTIVITY_ENABLE = "zetime_inactivity_warnings";
public static final String PREF_INACTIVITY_START = "zetime_inactivity_warnings_start";
public static final String PREF_INACTIVITY_END = "zetime_inactivity_warnings_end";

View File

@ -130,6 +130,167 @@ public class ZeTimePreferenceActivity extends AbstractSettingsActivity {
return true;
}
});
final Preference inactivityAlert = findPreference(ZeTimeConstants.PREF_INACTIVITY_ENABLE);
inactivityAlert.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertStart = findPreference(ZeTimeConstants.PREF_INACTIVITY_START);
inactivityAlertStart.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertEnd = findPreference(ZeTimeConstants.PREF_INACTIVITY_END);
inactivityAlertEnd.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertThreshold = findPreference(ZeTimeConstants.PREF_INACTIVITY_THRESHOLD);
inactivityAlertThreshold.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertMo = findPreference(ZeTimeConstants.PREF_INACTIVITY_MO);
inactivityAlertMo.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertTu = findPreference(ZeTimeConstants.PREF_INACTIVITY_TU);
inactivityAlertTu.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertWe = findPreference(ZeTimeConstants.PREF_INACTIVITY_WE);
inactivityAlertWe.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertTh = findPreference(ZeTimeConstants.PREF_INACTIVITY_TH);
inactivityAlertTh.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertFr = findPreference(ZeTimeConstants.PREF_INACTIVITY_FR);
inactivityAlertFr.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertSa = findPreference(ZeTimeConstants.PREF_INACTIVITY_SA);
inactivityAlertSa.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
final Preference inactivityAlertSu = findPreference(ZeTimeConstants.PREF_INACTIVITY_SU);
inactivityAlertSu.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(ZeTimeConstants.PREF_INACTIVITY_KEY);
}
});
return true;
}
});
}
/**
* delayed execution so that the preferences are applied first
*/
private void invokeLater(Runnable runnable) {
getListView().post(runnable);
}
// private void addTryListeners() {

View File

@ -170,6 +170,9 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
case ZeTimeConstants.PREF_DATE_FORMAT:
setDateFormate(builder);
break;
case ZeTimeConstants.PREF_INACTIVITY_KEY:
setInactivityAlert(builder);
break;
}
builder.queue(getQueue());
} catch (IOException e) {
@ -1428,6 +1431,7 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
{
Prefs prefs = GBApplication.getPrefs();
boolean alarmEnabled = prefs.getBoolean(ZeTimeConstants.PREF_ZETIME_HEARTRATE_ALARM, false);
int maxHR = prefs.getInt(ZeTimeConstants.PREF_ZETIME_MAX_HEARTRATE, 180);
int minHR = prefs.getInt(ZeTimeConstants.PREF_ZETIME_MIN_HEARTRATE, 60);
@ -1438,7 +1442,7 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
(byte)0x0,
(byte)(maxHR & 0xff),
(byte)(minHR & 0xff),
(byte)0x1, // activate alarm
(byte)(alarmEnabled ? 1 : 0), // activate alarm
ZeTimeConstants.CMD_END};
sendMsgToWatch(builder, heartrateAlarm);
}
@ -1646,16 +1650,39 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
if(enabled)
{
String start = prefs.getString(ZeTimeConstants.PREF_INACTIVITY_START, "06:00");
String end = prefs.getString(ZeTimeConstants.PREF_INACTIVITY_END, "22:00");
DateFormat df_start = new SimpleDateFormat("HH:mm");
DateFormat df_end = new SimpleDateFormat("HH:mm");
Calendar calendar = GregorianCalendar.getInstance();
Calendar calendar_end = GregorianCalendar.getInstance();
int reps = (1 << 7); // set inactivity active: set bit 7
reps |= prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_MO, 0);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_TU, 0) << 1);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_WE, 0) << 2);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_TH, 0) << 3);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_FR, 0) << 4);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_SA, 0) << 5);
reps |= (prefs.getInt(ZeTimeConstants.PREF_INACTIVITY_SU, 0) << 6);
reps |= (prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_MO, false) ? 1 : 0);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_TU, false) ? 1 : 0) << 1);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_WE, false) ? 1 : 0) << 2);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_TH, false) ? 1 : 0) << 3);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_FR, false) ? 1 : 0) << 4);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_SA, false) ? 1 : 0) << 5);
reps |= ((prefs.getBoolean(ZeTimeConstants.PREF_INACTIVITY_SU, false) ? 1 : 0) << 6);
inactivity[5] = (byte)reps;
try {
calendar.setTime(df_start.parse(start));
try {
calendar_end.setTime(df_end.parse(end));
inactivity[7] = (byte)calendar.get(Calendar.HOUR_OF_DAY);
inactivity[8] = (byte)calendar.get(Calendar.MINUTE);
inactivity[9] = (byte)calendar_end.get(Calendar.HOUR_OF_DAY);
inactivity[10] = (byte)calendar_end.get(Calendar.MINUTE);
} catch(Exception e) {
LOG.error("Unexpected exception in ZeTimeDeviceSupport.setInactivityAlert: " + e.getMessage());
}
} catch(Exception e) {
LOG.error("Unexpected exception in ZeTimeDeviceSupport.setInactivityAlert: " + e.getMessage());
}
}
sendMsgToWatch(builder, inactivity);

View File

@ -209,8 +209,11 @@
<string name="zetime_title_settings">ZeTime settings</string>
<string name="zetime_title_heartrate">Heart rate settings</string>
<string name="zetime_title_screentime">Screen on duration in seconds</string>
<string name="activity_prefs_alarm_max_heart_rate">Max heart rate alarm</string>
<string name="activity_prefs_alarm_min_heart_rate">Min heart rate alarm</string>
<string name="zetime_title_heart_rate_alarm">Heart rate alarm</string>
<string name="zetime_title_heart_rate_alarm_summary">The watch will warn you when your heart rate exceeds the limits.</string>
<string name="zetime_heart_rate_alarm_enable">Enable the heart rate alarm</string>
<string name="activity_prefs_alarm_max_heart_rate">Max heart rate</string>
<string name="activity_prefs_alarm_min_heart_rate">Min heart rate</string>
<string name="zetime_analog_mode">Analog mode</string>
<string name="zetime_analog_mode_hands">Only hands</string>
<string name="zetime_analog_mode_handsandsteps">Hands and steps</string>

View File

@ -180,18 +180,38 @@
android:key="heartrate_measurement_interval"
android:summary="%s"
android:title="@string/prefs_title_heartrate_measurement_interval" />
<EditTextPreference
android:inputType="number"
android:key="alarm_max_heart_rate"
android:maxLength="3"
android:defaultValue="180"
android:title="@string/activity_prefs_alarm_max_heart_rate" />
<PreferenceScreen
android:key="zetime_heartrate_alarm_key"
android:summary="@string/zetime_title_heart_rate_alarm_summary"
android:title="@string/zetime_title_heart_rate_alarm"
android:persistent="false">
<EditTextPreference
android:inputType="number"
android:key="alarm_min_heart_rate"
android:maxLength="3"
android:defaultValue="60"
android:title="@string/activity_prefs_alarm_min_heart_rate" />
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/zetime_title_heart_rate_alarm"
/>
<CheckBoxPreference
android:defaultValue="false"
android:key="zetime_heartrate_alarm_enable"
android:summary="@string/zetime_title_heart_rate_alarm_summary"
android:title="@string/zetime_heart_rate_alarm_enable" />
<EditTextPreference
android:inputType="number"
android:dependency="zetime_heartrate_alarm_enable"
android:key="alarm_max_heart_rate"
android:maxLength="3"
android:defaultValue="180"
android:title="@string/activity_prefs_alarm_max_heart_rate" />
<EditTextPreference
android:inputType="number"
android:dependency="zetime_heartrate_alarm_enable"
android:key="alarm_min_heart_rate"
android:maxLength="3"
android:defaultValue="60"
android:title="@string/activity_prefs_alarm_min_heart_rate" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>