1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-27 10:07:32 +01:00

Mi Band 3: Night Mode

This commit is contained in:
José Rebelo 2018-08-17 19:06:02 +01:00 committed by Andreas Shimokawa
parent 915ad99886
commit 9fda67b297
9 changed files with 175 additions and 0 deletions

View File

@ -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

View File

@ -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");
}
}

View File

@ -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};
}

View File

@ -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";

View File

@ -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

View File

@ -170,6 +170,17 @@
<item>@string/p_scheduled</item>
</string-array>
<string-array name="mi3_night_mode">
<item>@string/off</item>
<item>@string/mi3_night_mode_sunset</item>
<item>@string/mi2_dnd_scheduled</item>
</string-array>
<string-array name="mi3_night_mode_values">
<item>@string/p_off</item>
<item>@string/p_sunset</item>
<item>@string/p_scheduled</item>
</string-array>
<string-array name="pref_mi2_display_items">
<item>@string/chart_steps</item>
<item>@string/distance</item>

View File

@ -440,6 +440,9 @@
<string name="mi2_prefs_do_not_disturb_end">End time</string>
<string name="mi3_prefs_band_screen_unlock">Band screen unlock"</string>
<string name="mi3_prefs_band_screen_unlock_summary">Swipe up to unlock the band\'s screen</string>
<string name="mi3_prefs_night_mode">Night mode</string>
<string name="mi3_prefs_night_mode_summary">Lower band screen brightness automatically at night</string>
<string name="automatic">Automatic</string>
<string name="simplified_chinese">Simplified Chinese</string>
<string name="traditional_chinese">Traditional Chinese</string>
@ -537,6 +540,7 @@
<string name="on">On</string>
<string name="off">Off</string>
<string name="mi2_dnd_off">Off</string>
<string name="mi3_night_mode_sunset">At sunset</string>
<string name="mi2_dnd_automatic">Automatic (sleep detection)</string>
<string name="mi2_dnd_scheduled">Scheduled (time interval)</string>
<string name="discovery_attempting_to_pair">Attempting to pair with %1$s</string>

View File

@ -35,6 +35,7 @@
<item name="p_off" type="string">off</item>
<item name="p_on" type="string">on</item>
<item name="p_sunset" type="string">sunset</item>
<item name="p_automatic" type="string">automatic</item>
<item name="p_scheduled" type="string">scheduled</item>

View File

@ -251,6 +251,33 @@
android:key="miband3_display_items"
android:summary="@string/mi2_prefs_display_items_summary"
android:title="@string/mi2_prefs_display_items"/>
<PreferenceScreen
android:persistent="false"
android:title="@string/mi3_prefs_night_mode"
android:summary="@string/mi3_prefs_night_mode_summary">
<!-- workaround for missing toolbar -->
<PreferenceCategory android:title="@string/mi3_prefs_night_mode" />
<ListPreference
android:defaultValue="@string/p_off"
android:entries="@array/mi3_night_mode"
android:entryValues="@array/mi3_night_mode_values"
android:key="mi3_night_mode"
android:summary="%s"
android:title="@string/mi3_prefs_night_mode" />
<nodomain.freeyourgadget.gadgetbridge.util.TimePreference
android:defaultValue="16:00"
android:key="mi3_night_mode_start"
android:title="@string/mi2_prefs_do_not_disturb_start" />
<nodomain.freeyourgadget.gadgetbridge.util.TimePreference
android:defaultValue="7:00"
android:key="mi3_night_mode_end"
android:title="@string/mi2_prefs_do_not_disturb_end" />
</PreferenceScreen>
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_device_hplus"