diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index 1242801a5..65f9514e0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -52,6 +52,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; @@ -64,6 +65,11 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PR import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DISPLAY_ITEMS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_ENABLE_TEXT_NOTIFICATIONS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_END; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_OFF; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_SCHEDULED; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_START; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_SLEEP_DURATION; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEPS_GOAL; @@ -87,6 +93,8 @@ public class SettingsActivity extends AbstractSettingsActivity { protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); + Prefs prefs = GBApplication.getPrefs(); + Preference pref = findPreference("notifications_generic"); pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { @@ -419,6 +427,60 @@ public class SettingsActivity extends AbstractSettingsActivity { } }); + String nightModeState = prefs.getString(MiBandConst.PREF_MI3_NIGHT_MODE, PREF_MI3_NIGHT_MODE_OFF); + boolean nightModeScheduled = nightModeState.equals(PREF_MI3_NIGHT_MODE_SCHEDULED); + + final Preference nightModeStart = findPreference(PREF_MI3_NIGHT_MODE_START); + nightModeStart.setEnabled(nightModeScheduled); + nightModeStart.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + invokeLater(new Runnable() { + @Override + public void run() { + GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE_START); + } + }); + return true; + } + }); + + + final Preference nightModeEnd = findPreference(PREF_MI3_NIGHT_MODE_END); + nightModeEnd.setEnabled(nightModeScheduled); + nightModeEnd.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + invokeLater(new Runnable() { + @Override + public void run() { + GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE_END); + } + }); + return true; + } + }); + + + final Preference nightMode = findPreference(PREF_MI3_NIGHT_MODE); + nightMode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + final boolean scheduled = PREF_MI3_NIGHT_MODE_SCHEDULED.equals(newVal.toString()); + + nightModeStart.setEnabled(scheduled); + nightModeEnd.setEnabled(scheduled); + + invokeLater(new Runnable() { + @Override + public void run() { + GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE); + } + }); + return true; + } + }); + final Preference corDisplayItems = findPreference("cor_display_items"); corDisplayItems.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java index 39035982b..4d48d0b5c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java @@ -24,6 +24,8 @@ import android.support.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; + import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; @@ -78,4 +80,18 @@ public class MiBand3Coordinator extends HuamiCoordinator { Prefs prefs = GBApplication.getPrefs(); return prefs.getBoolean(MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK, false); } + + public static String getNightMode() { + Prefs prefs = GBApplication.getPrefs(); + + return prefs.getString(MiBandConst.PREF_MI3_NIGHT_MODE, MiBandConst.PREF_MI3_NIGHT_MODE_OFF); + } + + public static Date getNightModeStart() { + return getTimePreference( MiBandConst.PREF_MI3_NIGHT_MODE_START, "16:00"); + } + + public static Date getNightModeEnd() { + return getTimePreference(MiBandConst.PREF_MI3_NIGHT_MODE_END, "07:00"); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java index 31cdf89ad..6ad375532 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java @@ -25,4 +25,7 @@ public class MiBand3Service { public static final byte[] COMMAND_CHANGE_SCREENS = new byte[]{ENDPOINT_DISPLAY_ITEMS, DISPLAY_ITEM_BIT_CLOCK, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00}; public static final byte[] COMMAND_ENABLE_BAND_SCREEN_UNLOCK = new byte[]{ENDPOINT_DISPLAY, 0x16, 0x00, 0x01}; public static final byte[] COMMAND_DISABLE_BAND_SCREEN_UNLOCK = new byte[]{ENDPOINT_DISPLAY, 0x16, 0x00, 0x00}; + public static final byte[] COMMAND_NIGHT_MODE_OFF = new byte[]{0x1a, 0x00}; + public static final byte[] COMMAND_NIGHT_MODE_SUNSET = new byte[]{0x1a, 0x02}; + public static final byte[] COMMAND_NIGHT_MODE_SCHEDULED = new byte[]{0x1a, 0x01, 0x10, 0x00, 0x07, 0x00}; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java index 0358dfb07..66cb6aa8f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java @@ -70,6 +70,12 @@ public final class MiBandConst { public static final String PREF_MIBAND_SETUP_BT_PAIRING = "mi_setup_bt_pairing"; public static final String PREF_MI3_BAND_SCREEN_UNLOCK = "mi3_band_screen_unlock"; + public static final String PREF_MI3_NIGHT_MODE = "mi3_night_mode"; + public static final String PREF_MI3_NIGHT_MODE_START = "mi3_night_mode_start"; + public static final String PREF_MI3_NIGHT_MODE_END = "mi3_night_mode_end"; + public static final String PREF_MI3_NIGHT_MODE_OFF = "off"; + public static final String PREF_MI3_NIGHT_MODE_SUNSET = "sunset"; + public static final String PREF_MI3_NIGHT_MODE_SCHEDULED = "scheduled"; public static final String ORIGIN_INCOMING_CALL = "incoming_call"; public static final String ORIGIN_ALARM_CLOCK = "alarm_clock"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java index b5ee908c6..599710e85 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java @@ -24,6 +24,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.Locale; import java.util.Set; @@ -94,6 +97,11 @@ public class MiBand3Support extends AmazfitBipSupport { case MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK: setBandScreenUnlock(builder); break; + case MiBandConst.PREF_MI3_NIGHT_MODE: + case MiBandConst.PREF_MI3_NIGHT_MODE_START: + case MiBandConst.PREF_MI3_NIGHT_MODE_END: + setNightMode(builder); + break; default: super.onSendConfiguration(config); return; @@ -140,11 +148,48 @@ public class MiBand3Support extends AmazfitBipSupport { return this; } + private MiBand3Support setNightMode(TransactionBuilder builder) { + String nightMode = MiBand3Coordinator.getNightMode(); + LOG.info("Setting night mode to " + nightMode); + + switch (nightMode) { + case MiBandConst.PREF_MI3_NIGHT_MODE_SUNSET: + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_NIGHT_MODE_SUNSET); + break; + case MiBandConst.PREF_MI3_NIGHT_MODE_OFF: + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_NIGHT_MODE_OFF); + break; + case MiBandConst.PREF_MI3_NIGHT_MODE_SCHEDULED: + byte[] cmd = MiBand3Service.COMMAND_NIGHT_MODE_SCHEDULED.clone(); + + Calendar calendar = GregorianCalendar.getInstance(); + + Date start = MiBand3Coordinator.getNightModeStart(); + calendar.setTime(start); + cmd[2] = (byte) calendar.get(Calendar.HOUR_OF_DAY); + cmd[3] = (byte) calendar.get(Calendar.MINUTE); + + Date end = MiBand3Coordinator.getNightModeEnd(); + calendar.setTime(end); + cmd[4] = (byte) calendar.get(Calendar.HOUR_OF_DAY); + cmd[5] = (byte) calendar.get(Calendar.MINUTE); + + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), cmd); + break; + default: + LOG.error("Invalid night mode: " + nightMode); + break; + } + + return this; + } + @Override public void phase2Initialize(TransactionBuilder builder) { super.phase2Initialize(builder); LOG.info("phase2Initialize..."); setBandScreenUnlock(builder); + setNightMode(builder); } @Override diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 39dfa2fc0..450218da8 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -170,6 +170,17 @@ @string/p_scheduled + + @string/off + @string/mi3_night_mode_sunset + @string/mi2_dnd_scheduled + + + @string/p_off + @string/p_sunset + @string/p_scheduled + + @string/chart_steps @string/distance diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 66e48cfc7..ea12ea521 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -440,6 +440,9 @@ End time Band screen unlock" Swipe up to unlock the band\'s screen + Night mode + Lower band screen brightness automatically at night + Automatic Simplified Chinese Traditional Chinese @@ -537,6 +540,7 @@ On Off Off + At sunset Automatic (sleep detection) Scheduled (time interval) Attempting to pair with %1$s diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 96d5d895c..570783715 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -35,6 +35,7 @@ off on + sunset automatic scheduled diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 6744c4e4b..d4ada4d19 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -251,6 +251,33 @@ android:key="miband3_display_items" android:summary="@string/mi2_prefs_display_items_summary" android:title="@string/mi2_prefs_display_items"/> + + + + + + + + + + + +