From 7394b1cce3ff8d5115d9af7812d65c528ea966b6 Mon Sep 17 00:00:00 2001 From: vanous Date: Wed, 8 Jan 2020 19:58:31 +0100 Subject: [PATCH] Add-Buttons-Events-Config (#1761) --- .../GBDeviceEventMusicControl.java | 2 + .../amazfitbip/AmazfitBipCoordinator.java | 2 +- .../service/devices/huami/HuamiSupport.java | 76 ++++++++++++---- .../receivers/GBMusicControlReceiver.java | 6 ++ app/src/main/res/values/arrays.xml | 49 ++++++++++ app/src/main/res/values/strings.xml | 14 ++- .../res/xml/devicesettings_buttonactions.xml | 72 ++++++++++----- ...esettings_buttonactions_with_longpress.xml | 90 +++++++++++++++++++ 8 files changed, 265 insertions(+), 46 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_buttonactions_with_longpress.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventMusicControl.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventMusicControl.java index 9d9547a0e..85666f905 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventMusicControl.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventMusicControl.java @@ -29,5 +29,7 @@ public class GBDeviceEventMusicControl extends GBDeviceEvent { PREVIOUS, VOLUMEUP, VOLUMEDOWN, + FORWARD, + REWIND } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipCoordinator.java index ad2e788da..40b11d3fc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipCoordinator.java @@ -87,7 +87,7 @@ public class AmazfitBipCoordinator extends HuamiCoordinator { R.xml.devicesettings_liftwrist_display, R.xml.devicesettings_disconnectnotification, R.xml.devicesettings_expose_hr_thirdparty, - R.xml.devicesettings_buttonactions, + R.xml.devicesettings_buttonactions_with_longpress, R.xml.devicesettings_pairingkey }; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 09835e5cd..345d3996a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -152,6 +152,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { private static int currentButtonPressCount = 0; private static long currentButtonPressTime = 0; private static long currentButtonTimerActivationTime = 0; + private Timer buttonActionTimer = null; private static final Logger LOG = LoggerFactory.getLogger(HuamiSupport.class); private final DeviceInfoProfile deviceInfoProfile; @@ -1106,6 +1107,23 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { if (currentButtonTimerActivationTime != currentButtonPressTime) { return; } + //handle user events settings. 0 is long press, rest are button_id 1-3 + switch (currentButtonActionId) { + case 0: + handleMediaButton(prefs.getString("button_long_press_action_selection","UNKNOWN")); + break; + case 1: + handleMediaButton(prefs.getString("button_single_press_action_selection", "UNKNOWN")); + break; + case 2: + handleMediaButton(prefs.getString("button_double_press_action_selection", "UNKNOWN")); + break; + case 3: + handleMediaButton(prefs.getString("button_triple_press_action_selection", "UNKNOWN")); + break; + default: + break; + } String requiredButtonPressMessage = prefs.getString(HuamiConst.PREF_BUTTON_ACTION_BROADCAST, this.getContext().getString(R.string.mi2_prefs_button_press_broadcast_default_value)); @@ -1115,16 +1133,25 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { in.putExtra("button_id", currentButtonActionId); LOG.info("Sending " + requiredButtonPressMessage + " with button_id " + currentButtonActionId); this.getContext().getApplicationContext().sendBroadcast(in); + if (prefs.getBoolean(HuamiConst.PREF_BUTTON_ACTION_VIBRATE, false)) { vibrateOnce(); } currentButtonActionId = 0; - currentButtonPressCount = 0; currentButtonPressTime = System.currentTimeMillis(); } + private void handleMediaButton(String MediaAction) { + if (MediaAction.equals("UNKNOWN")) { + return; + } + GBDeviceEventMusicControl deviceEventMusicControl = new GBDeviceEventMusicControl(); + deviceEventMusicControl.event = GBDeviceEventMusicControl.Event.valueOf(MediaAction); + evaluateGBDeviceEvent(deviceEventMusicControl); + } + private void handleDeviceEvent(byte[] value) { if (value == null || value.length == 0) { return; @@ -1148,6 +1175,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { break; case HuamiDeviceEvent.BUTTON_PRESSED_LONG: LOG.info("button long-pressed "); + handleLongButtonEvent(); break; case HuamiDeviceEvent.START_NONWEAR: LOG.info("non-wear start detected"); @@ -1258,6 +1286,20 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } } + private void handleLongButtonEvent(){ + Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress())); + + if (!prefs.getBoolean(HuamiConst.PREF_BUTTON_ACTION_ENABLE, false)) { + return; + } + + currentButtonActionId = 0; + currentButtonPressTime = System.currentTimeMillis(); + currentButtonTimerActivationTime = currentButtonPressTime; + runButtonAction(); + + } + private void handleButtonEvent() { // If disabled we return from function immediately @@ -1267,7 +1309,6 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } int buttonPressMaxDelay = prefs.getInt(HuamiConst.PREF_BUTTON_ACTION_PRESS_MAX_INTERVAL, 2000); - int buttonActionDelay = prefs.getInt(HuamiConst.PREF_BUTTON_ACTION_BROADCAST_DELAY, 0); int requiredButtonPressCount = prefs.getInt(HuamiConst.PREF_BUTTON_ACTION_PRESS_COUNT, 0); if (requiredButtonPressCount > 0) { @@ -1280,31 +1321,32 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { currentButtonPressCount = 1; currentButtonActionId = 0; } + if (buttonActionTimer != null){ + buttonActionTimer.cancel(); + } currentButtonPressTime = System.currentTimeMillis(); if (currentButtonPressCount == requiredButtonPressCount) { currentButtonTimerActivationTime = currentButtonPressTime; - if (buttonActionDelay > 0) { - LOG.info("Activating timer"); - final Timer buttonActionTimer = new Timer("Huami Button Action Timer"); - buttonActionTimer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - runButtonAction(); - buttonActionTimer.cancel(); - } - }, buttonActionDelay, buttonActionDelay); - } - else { - LOG.info("Activating button action"); - runButtonAction(); - } + LOG.info("Activating button timer"); + buttonActionTimer = new Timer("Huami Button Action Timer"); + buttonActionTimer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + runButtonAction(); + buttonActionTimer.cancel(); + } + }, buttonPressMaxDelay, buttonPressMaxDelay); + currentButtonActionId++; currentButtonPressCount = 0; } } } + + + @Override public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java index 54120ddf6..19d12e861 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java @@ -64,6 +64,12 @@ public class GBMusicControlReceiver extends BroadcastReceiver { case PLAYPAUSE: keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE; break; + case REWIND: + keyCode = KeyEvent.KEYCODE_MEDIA_REWIND; + break; + case FORWARD: + keyCode = KeyEvent.KEYCODE_MEDIA_FAST_FORWARD; + break; case VOLUMEUP: // change default and fall through, :P volumeAdjust = AudioManager.ADJUST_RAISE; diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 6f3ffb9de..f0ade9285 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -604,4 +604,53 @@ 14 + + @string/pref_button_action_disabled + @string/pref_media_play + @string/pref_media_pause + @string/pref_media_playpause + @string/pref_media_next + @string/pref_media_previous + @string/pref_media_volumeup + @string/pref_media_volumedown + @string/pref_media_forward + @string/pref_media_rewind + + + Disabled + Media Play + Media Pause + Toggle playback + Next Track + Previous Track + Volume Up + Volume Down + Skip forward + Skip back + + + @string/pref_button_action_disabled_value + @string/pref_media_play_value + @string/pref_media_pause_value + @string/pref_media_playpause_value + @string/pref_media_next_value + @string/pref_media_previous_value + @string/pref_media_volumeup_value + @string/pref_media_volumedown_value + @string/pref_media_forward_value + @string/pref_media_rewind_value + + + + UNKNOWN + PLAY + PAUSE + PLAYPAUSE + NEXT + PREVIOUS + VOLUMEUP + VOLUMEDOWN + FORWARD + REWIND + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index be131d35a..52ea31585 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -463,9 +463,9 @@ Button actions Specify button press actions Button press count - Number of button presses to trigger message broadcast + Number of button presses to trigger an Event 1. Subsequent same amount of presses create Event 2 and so on. Broadcast message to send - Broadcast message on defined number of button presses reached + Broadcast message sent with the event. Parameter `button_id` is added automatically to each message. nodomain.freeyourgadget.gadgetbridge.ButtonPressed Enable button action Enable action on specified number of button presses @@ -473,8 +473,6 @@ Enable band vibration on button action triggered Maximum delay between presses Maximum delay between button presses in milliseconds - Delay after button action - Delay after one button action match (number is in button_id intent extra) or 0 for immediately Goal notification The band will vibrate when the daily steps goal is reached Display items @@ -773,6 +771,14 @@ offset timezone by change might take some seconds… offset time by + + Event 1 action + Event 2 action + Event 3 action + Detailed button press settings + Long press button action + + %d hour %d hours diff --git a/app/src/main/res/xml/devicesettings_buttonactions.xml b/app/src/main/res/xml/devicesettings_buttonactions.xml index 224b483c7..5a0e007ed 100644 --- a/app/src/main/res/xml/devicesettings_buttonactions.xml +++ b/app/src/main/res/xml/devicesettings_buttonactions.xml @@ -24,35 +24,59 @@ android:summary="@string/mi2_prefs_button_action_vibrate_summary" android:title="@string/mi2_prefs_button_action_vibrate" /> - + android:entries="@array/button_action_options" + android:entryValues="@array/button_action_values" + android:key="button_single_press_action_selection" + android:defaultValue="@string/pref_button_action_disabled_value" + android:summary="%s" + android:title="@string/huami_prefs_button_single_press_action_selection_title" /> - + android:entries="@array/button_action_options" + android:entryValues="@array/button_action_values" + android:key="button_double_press_action_selection" + android:summary="%s" + android:defaultValue="@string/pref_button_action_disabled_value" + android:title="@string/huami_prefs_button_double_press_action_selection_title" /> - + android:entries="@array/button_action_options" + android:entryValues="@array/button_action_values" + android:key="button_triple_press_action_selection" + android:summary="%s" + android:defaultValue="@string/pref_button_action_disabled_value" + android:title="@string/huami_prefs_button_triple_press_action_selection_title" /> + + + + + + + + + + - diff --git a/app/src/main/res/xml/devicesettings_buttonactions_with_longpress.xml b/app/src/main/res/xml/devicesettings_buttonactions_with_longpress.xml new file mode 100644 index 000000000..f8209efaa --- /dev/null +++ b/app/src/main/res/xml/devicesettings_buttonactions_with_longpress.xml @@ -0,0 +1,90 @@ + + + + + + + + public static final String PREF_BUTTON_ACTION_PRESS_DELAY = "button_action_press_count_delay"; + + + + + + + + + + + + + + + + + + + + + + + +