Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Button actions and extra confirmation step for Amazfit BIP #925

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public enum Event {
OUTGOING,
REJECT,
START,
IGNORE,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Looper;
import android.widget.Toast;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,34 +33,48 @@
import java.nio.ByteOrder;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiWeatherConditions;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipFWHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert;
import nodomain.freeyourgadget.gadgetbridge.service.devices.amazfitbip.operations.AmazfitBipFetchLogsOperation;
import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBCallControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Version;

public class AmazfitBipSupport extends MiBand2Support {

private static final Logger LOG = LoggerFactory.getLogger(AmazfitBipSupport.class);

private boolean buttonActionConfirmationReceived = false;
private boolean buttonActionApproved = false;

public AmazfitBipSupport() {
super(LOG);
}
Expand Down Expand Up @@ -119,7 +137,90 @@ public void onFindDevice(boolean start) {

@Override
public void handleButtonEvent() {
// ignore
super.handleButtonEvent();
}

@Override
public void runButtonAction() {
final Prefs prefs = GBApplication.getPrefs();

if (currentButtonTimerActivationTime != currentButtonPressTime) {
return;
}

final String requiredButtonPressMessage = prefs.getString(MiBandConst.PREF_MIBAND_BUTTON_PRESS_BROADCAST,
this.getContext().getString(R.string.mi2_prefs_button_press_broadcast_default_value));

if (prefs.getBoolean("require_button_action_confirmazion", false)) {

buttonActionConfirmationReceived = false;
buttonActionApproved = false;
LOG.debug("ButtonAction - Ringing the device");
//ringing the device
onFindDevice(true);
//wating for accept or reject
final Timer buttonActionAckTimer = new Timer("Mi Band Button Action Timer");
buttonActionAckTimer.scheduleAtFixedRate(new TimerTask() {

int j = 0;
@Override
public void run() {
j++;
if (buttonActionConfirmationReceived) {
if (buttonActionApproved) {
executeButtonAction(requiredButtonPressMessage, prefs.getBoolean(MiBandConst.PREF_MIBAND_BUTTON_ACTION_VIBRATE, false));
}

//TODO: send a proper notification to the device
/*
NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.type = NotificationType.VIBER;
notificationSpec.sender = "ButtonAction1";
notificationSpec.sender = "ButtonAction2";
notificationSpec.subject = "ButtonAction3";
notificationSpec.body = buttonActionApproved ? "Button Action approved" : "Button Action rejected";
AmazfitBipSupport.super.onNotification(notificationSpec);
*/
buttonActionAckTimer.cancel();
} else if (j > 20) {
buttonActionAckTimer.cancel();
//stop ringing
onFindDevice(false);
} else {
}
}
}, 0, 500);
} else {
executeButtonAction(requiredButtonPressMessage, prefs.getBoolean(MiBandConst.PREF_MIBAND_BUTTON_ACTION_VIBRATE, false));
}

currentButtonActionId = 0;

currentButtonPressCount = 0;
currentButtonPressTime = System.currentTimeMillis();

}

@Override
public void processActionConfirmation(GBDeviceEventCallControl callEvent) {
LOG.info("ButtonAction - processActionConfirmation received: " + callEvent.event);
buttonActionConfirmationReceived = true;
if ((callEvent.event == GBDeviceEventCallControl.Event.IGNORE) || (callEvent.event == GBDeviceEventCallControl.Event.ACCEPT)) {
buttonActionApproved = true;
}
}

private void executeButtonAction(String requiredButtonPressMessage, boolean vibrate) {
LOG.info("ButtonAction - fire intent");
Intent in = new Intent();
in.setAction(requiredButtonPressMessage);
in.putExtra("button_id", currentButtonActionId);
LOG.info("Sending " + requiredButtonPressMessage + " with button_id " + currentButtonActionId);
this.getContext().getApplicationContext().sendBroadcast(in);
if (vibrate) {
performPreferredNotification(null, null, null, MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY, null);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@
public class MiBand2Support extends AbstractBTLEDeviceSupport {

// We introduce key press counter for notification purposes
private static int currentButtonActionId = 0;
private static int currentButtonPressCount = 0;
private static long currentButtonPressTime = 0;
private static long currentButtonTimerActivationTime = 0;
protected static int currentButtonActionId = 0;
protected static int currentButtonPressCount = 0;
protected static long currentButtonPressTime = 0;
protected static long currentButtonTimerActivationTime = 0;

private static final Logger LOG = LoggerFactory.getLogger(MiBand2Support.class);
private final DeviceInfoProfile<MiBand2Support> deviceInfoProfile;
Expand Down Expand Up @@ -922,11 +922,15 @@ public void handleDeviceEvent(byte[] value) {
case HuamiDeviceEvent.CALL_REJECT:
callCmd.event = GBDeviceEventCallControl.Event.REJECT;
evaluateGBDeviceEvent(callCmd);
// this event will be used to process button action confirmation
processActionConfirmation(callCmd);
break;
case HuamiDeviceEvent.CALL_IGNORE:
LOG.info("ignore call (not yet supported)");
//callCmd.event = GBDeviceEventCallControl.Event.IGNORE;
callCmd.event = GBDeviceEventCallControl.Event.IGNORE;
//evaluateGBDeviceEvent(callCmd);
// this event will be used to process button action confirmation
processActionConfirmation(callCmd);
break;
case HuamiDeviceEvent.BUTTON_PRESSED:
LOG.info("button pressed");
Expand Down Expand Up @@ -964,6 +968,10 @@ public void handleDeviceEvent(byte[] value) {
}
}

public void processActionConfirmation(GBDeviceEventCallControl callEvent) {
// Not supported for MiBand2, but needed for BIP
}

public void handleButtonEvent() {
///logMessageContent(value);

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@

<string name="pref_title_language">Language</string>

<string name="pref_title_require_button_action_confirmazion">Require conformation on screen after Button Action</string>
<string name="pref_summary_require_button_action_confirmazion">You can approve or reject the button action on the screen</string>

<string name="pref_title_minimize_priority">Hide the Gadgetbridge notification</string>
<string name="pref_summary_minimize_priority_off">The icon in the status bar and the notification in the lockscreen are shown</string>
<string name="pref_summary_minimize_priority_on">The icon in the status bar and the notification in the lockscreen are hidden</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
android:key="amazfitbip_language"
android:summary="%s"
android:title="@string/pref_title_language" />
<CheckBoxPreference
android:defaultValue="false"
android:key="require_button_action_confirmazion"
android:summary="@string/pref_summary_require_button_action_confirmazion"
android:title="@string/pref_title_require_button_action_confirmazion" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_device_pebble"
Expand Down