Support for button actions on Mi Band 2 device (#793)

Implement button actions for MiBand 2
This commit is contained in:
Michal Novotny 2017-09-05 22:37:41 +02:00 committed by Carsten Pfeiffer
parent 25fdf50525
commit fcf9be877a
5 changed files with 122 additions and 1 deletions

View File

@ -32,6 +32,11 @@ public final class MiBandConst {
public static final String PREF_MIBAND_ALARMS = "mi_alarms";
public static final String PREF_MIBAND_DONT_ACK_TRANSFER = "mi_dont_ack_transfer";
public static final String PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR = "mi_reserve_alarm_calendar";
public static final String PREF_MIBAND_BUTTON_ACTION_ENABLE = "mi2_enable_button_action";
public static final String PREF_MIBAND_BUTTON_ACTION_VIBRATE = "mi2_button_action_vibrate";
public static final String PREF_MIBAND_BUTTON_PRESS_COUNT = "mi_button_press_count";
public static final String PREF_MIBAND_BUTTON_PRESS_MAX_DELAY = "mi_button_press_count_max_delay";
public static final String PREF_MIBAND_BUTTON_PRESS_BROADCAST = "mi_button_press_broadcast";
public static final String PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION = "mi_hr_sleep_detection";
public static final String PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS = "mi_device_time_offset_hours";
public static final String PREF_MI2_DATEFORMAT = "mi2_dateformat";

View File

@ -24,7 +24,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.text.format.DateFormat;
import android.widget.Toast;
@ -123,6 +125,10 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ge
public class MiBand2Support extends AbstractBTLEDeviceSupport {
// We introduce key press counter for notification purposes
private static int currentButtonPressCount = 0;
private static long currentButtonPressTime = 0;
private static final Logger LOG = LoggerFactory.getLogger(MiBand2Support.class);
private final DeviceInfoProfile<MiBand2Support> deviceInfoProfile;
private final HeartRateProfile<MiBand2Support> heartRateProfile;
@ -809,8 +815,48 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
public void handleButtonPressed(byte[] value) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
LOG.info("Button pressed");
logMessageContent(value);
Prefs prefs = GBApplication.getPrefs();
// If disabled we return from function immediately
if (!prefs.getBoolean(MiBandConst.PREF_MIBAND_BUTTON_ACTION_ENABLE, false)) {
return;
}
int buttonPressMaxDelay = prefs.getInt(MiBandConst.PREF_MIBAND_BUTTON_PRESS_MAX_DELAY, 2000);
int requiredButtonPressCount = prefs.getInt(MiBandConst.PREF_MIBAND_BUTTON_PRESS_COUNT, 0);
String requiredButtonPressMessage = prefs.getString(MiBandConst.PREF_MIBAND_BUTTON_PRESS_BROADCAST,
this.getContext().getString(R.string.mi2_prefs_button_press_broadcast_default_value));
if (requiredButtonPressCount > 0) {
long timeSinceLastPress = System.currentTimeMillis() - currentButtonPressTime;
if ((currentButtonPressTime == 0) || (timeSinceLastPress < buttonPressMaxDelay)) {
currentButtonPressCount++;
}
else {
currentButtonPressCount = 0;
}
currentButtonPressTime = System.currentTimeMillis();
if (currentButtonPressCount >= requiredButtonPressCount) {
Intent in = new Intent();
in.setAction(requiredButtonPressMessage);
this.getContext().getApplicationContext().sendBroadcast(in);
currentButtonPressCount = 0;
currentButtonPressTime = System.currentTimeMillis();
if (prefs.getBoolean(MiBandConst.PREF_MIBAND_BUTTON_ACTION_VIBRATE, false)) {
performPreferredNotification(null, null, null, MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY, null);
}
}
}
}
private void handleUnknownCharacteristic(byte[] value) {

View File

@ -407,6 +407,19 @@
<string name="discovery_pair_question">Vyberte Párování pro spárování s vaším zaříením. Pokud to selže, tak to zkuste bez párování.</string>
<string name="discovery_yes_pair">Párování</string>
<string name="discovery_dont_pair">Nepárovat</string>
<string name="mi2_prefs_button_actions">Akce tlačítka</string>
<string name="mi2_prefs_button_actions_summary">Definujte akce při stisku tlačítka na Mi Band 2</string>
<string name="mi2_prefs_button_press_count">Počet stisků tlačítka</string>
<string name="mi2_prefs_button_press_count_summary">Definujte počet stisknutí tlačítka k vyslání broadcast zprávy</string>
<string name="mi2_prefs_button_press_broadcast">Zpráva k vyslání (broadcast)</string>
<string name="mi2_prefs_button_press_broadcast_summary">Zpráva k vyslání do systému pokud počtu stisknutí tlačítka (viz výše)</string>
<string name="mi2_prefs_button_press_broadcast_default_value">nodomain.freeyourgadget.gadgetbridge.mibandButtonPressed</string>
<string name="mi2_prefs_button_action">Povolit akci tlačítka</string>
<string name="mi2_prefs_button_action_summary">Povolit akci tlačítka na definovaný počet stisknutí</string>
<string name="mi2_prefs_button_action_vibrate">Povolit vibrace náramku</string>
<string name="mi2_prefs_button_action_vibrate_summary">Povolit vibrace náramku při vyslání broadcast zprávy</string>
<string name="mi2_prefs_button_press_count_max_delay">Maximální prodleva mezi stisky</string>
<string name="mi2_prefs_button_press_count_max_delay_summary">Maximální prodleva mezi stisky tlačítka v milisekundách</string>
<string name="fw_upgrade_notice_amazfitbip">Chystáte se nainstalovat firmware %s namísto toho, který je aktuálně na vašem Amazfit Bipu.
\n
\nUjistěte se, že jste nainstalovali firmware .gps, pak soubor .res a nakonec soubor .fw. Hodiny se po instalaci souboru .fw restartují.
@ -415,4 +428,4 @@
\n
\nEXPERIMENTÁLNÍ PROCES, DĚLÁTE NA VAŠE VLASTNÍ RIZIKO</string>
<string name="amazfitbip_firmware">"Firmware Amazfit Bipu %1$s"</string>
</resources>
</resources>

View File

@ -351,6 +351,19 @@
<string name="miband2_prefs_dateformat">Mi2: Date format</string>
<string name="dateformat_time">Time</string>
<string name="dateformat_date_time"><![CDATA[Time & date]]></string>
<string name="mi2_prefs_button_actions">Button actions</string>
<string name="mi2_prefs_button_actions_summary">Specify actions on Mi Band 2 button press</string>
<string name="mi2_prefs_button_press_count">Button press count</string>
<string name="mi2_prefs_button_press_count_summary">Number of button presses to trigger message broadcast</string>
<string name="mi2_prefs_button_press_broadcast">Broadcast message to send</string>
<string name="mi2_prefs_button_press_broadcast_summary">Broadcast message on defined number of button presses reached</string>
<string name="mi2_prefs_button_press_broadcast_default_value">nodomain.freeyourgadget.gadgetbridge.mibandButtonPressed</string>
<string name="mi2_prefs_button_action">Enable button action</string>
<string name="mi2_prefs_button_action_summary">Enable action on specified number of button presses</string>
<string name="mi2_prefs_button_action_vibrate">Enable band vibration</string>
<string name="mi2_prefs_button_action_vibrate_summary">Enable band vibration on button action triggered</string>
<string name="mi2_prefs_button_press_count_max_delay">Maximum delay between presses</string>
<string name="mi2_prefs_button_press_count_max_delay_summary">Maximum delay between button presses in milliseconds</string>
<string name="mi2_prefs_goal_notification">Goal notification</string>
<string name="mi2_prefs_goal_notification_summary">The band will vibrate when the daily steps goal is reached</string>
<string name="mi2_prefs_display_items">Display items</string>

View File

@ -30,6 +30,50 @@
android:summary="@string/mi2_prefs_goal_notification_summary"
android:title="@string/mi2_prefs_goal_notification" />
<PreferenceScreen
android:key="mi2_button_actions_key"
android:summary="@string/mi2_prefs_button_actions_summary"
android:title="@string/mi2_prefs_button_actions"
android:persistent="false">
<!-- workaround for missing toolbar -->
<PreferenceCategory
android:title="@string/mi2_prefs_button_action"
/>
<CheckBoxPreference
android:defaultValue="false"
android:key="mi2_enable_button_action"
android:summary="@string/mi2_prefs_button_action_summary"
android:title="@string/mi2_prefs_button_action" />
<CheckBoxPreference
android:defaultValue="false"
android:key="mi2_button_action_vibrate"
android:summary="@string/mi2_prefs_button_action_vibrate_summary"
android:title="@string/mi2_prefs_button_action_vibrate" />
<EditTextPreference
android:defaultValue="6"
android:inputType="number"
android:key="mi_button_press_count"
android:summary="@string/mi2_prefs_button_press_count_summary"
android:title="@string/mi2_prefs_button_press_count" />
<EditTextPreference
android:defaultValue="@string/mi2_prefs_button_press_broadcast_default_value"
android:key="mi_button_press_broadcast"
android:summary="@string/mi2_prefs_button_press_broadcast_summary"
android:title="@string/mi2_prefs_button_press_broadcast" />
<EditTextPreference
android:defaultValue="2000"
android:inputType="number"
android:key="mi_button_press_count_max_delay"
android:summary="@string/mi2_prefs_button_press_count_max_delay_summary"
android:title="@string/mi2_prefs_button_press_count_max_delay" />
</PreferenceScreen>
<EditTextPreference
android:defaultValue="0"
android:inputType="number"