Somewhat hacky support for Mi2 notification icons #323

This commit is contained in:
cpfeiffer 2016-10-02 23:04:59 +02:00
parent 2a0d97b39a
commit 5bdc7933b3
7 changed files with 230 additions and 21 deletions

View File

@ -75,6 +75,10 @@ public class DebugActivity extends GBActivity {
}
}
};
private Button sendChatButton;
private Button sendTelegramButton;
private Button sendTwitterButton;
private Button sendFacebookButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -114,6 +118,58 @@ public class DebugActivity extends GBActivity {
GBApplication.deviceService().onNotification(notificationSpec);
}
});
sendChatButton = (Button) findViewById(R.id.sendChatButton);
sendChatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.sender = getResources().getText(R.string.app_name).toString();
notificationSpec.subject = editContent.getText().toString();
notificationSpec.body = editContent.getText().toString();
notificationSpec.type = NotificationType.CHAT;
notificationSpec.id = -1;
GBApplication.deviceService().onNotification(notificationSpec);
}
});
sendTelegramButton = (Button) findViewById(R.id.sendTelegramButton);
sendTelegramButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.sender = getResources().getText(R.string.app_name).toString();
notificationSpec.subject = editContent.getText().toString();
notificationSpec.body = editContent.getText().toString();
notificationSpec.type = NotificationType.TELEGRAM;
notificationSpec.id = -1;
GBApplication.deviceService().onNotification(notificationSpec);
}
});
sendTwitterButton = (Button) findViewById(R.id.sendTwitterButton);
sendTwitterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.sender = getResources().getText(R.string.app_name).toString();
notificationSpec.subject = editContent.getText().toString();
notificationSpec.body = editContent.getText().toString();
notificationSpec.type = NotificationType.TWITTER;
notificationSpec.id = -1;
GBApplication.deviceService().onNotification(notificationSpec);
}
});
sendFacebookButton = (Button) findViewById(R.id.sendFacebookButton);
sendFacebookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.sender = getResources().getText(R.string.app_name).toString();
notificationSpec.subject = editContent.getText().toString();
notificationSpec.body = editContent.getText().toString();
notificationSpec.type = NotificationType.FACEBOOK;
notificationSpec.id = -1;
GBApplication.deviceService().onNotification(notificationSpec);
}
});
incomingCallButton = (Button) findViewById(R.id.incomingCallButton);
incomingCallButton.setOnClickListener(new View.OnClickListener() {

View File

@ -26,6 +26,11 @@ public class MiBand2Service {
public static final UUID UUID_CHARACTERISTIC_AUTH = UUID.fromString("00000009-0000-3512-2118-0009af100700");
public static final UUID UUID_UNKNOWN_CHARACTERISTIC10 = UUID.fromString("00000010-0000-3512-2118-0009af100700");
public static final int ALERT_LEVEL_NONE = 0;
public static final int ALERT_LEVEL_MESSAGE = 1;
public static final int ALERT_LEVEL_PHONE_CALL = 2;
public static final int ALERT_LEVEL_VIBRATE_ONLY = 3;
// set metric distance
// set 12 hour time mode

View File

@ -4,6 +4,7 @@ import android.content.Context;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertLevel;
public class VibrationProfile {
public static final Context CONTEXT = GBApplication.getContext();
@ -42,6 +43,7 @@ public class VibrationProfile {
private final int[] onOffSequence;
private final short repeat;
private int alertLevel = AlertLevel.MildAlert.getId();
/**
* Creates a new profile instance.
@ -67,4 +69,12 @@ public class VibrationProfile {
public short getRepeat() {
return repeat;
}
public void setAlertLevel(int alertLevel) {
this.alertLevel = alertLevel;
}
public int getAlertLevel() {
return alertLevel;
}
}

View File

@ -54,7 +54,9 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbortTransactio
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ConditionalWriteAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertLevel;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.InitOperation;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -291,7 +293,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
private NotificationStrategy getNotificationStrategy() {
return new V2NotificationStrategy(this);
return new Mi2NotificationStrategy(this);
}
static final byte[] reboot = new byte[]{MiBandService.COMMAND_REBOOT};
@ -475,7 +477,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
}
private void performPreferredNotification(String task, String notificationOrigin, BtLEAction extraAction) {
private void performPreferredNotification(String task, String notificationOrigin, int alertLevel, BtLEAction extraAction) {
try {
TransactionBuilder builder = performInitialized(task);
Prefs prefs = GBApplication.getPrefs();
@ -483,6 +485,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
int vibratePause = getPreferredVibratePause(notificationOrigin, prefs);
short vibrateTimes = getPreferredVibrateCount(notificationOrigin, prefs);
VibrationProfile profile = getPreferredVibrateProfile(notificationOrigin, prefs, vibrateTimes);
profile.setAlertLevel(alertLevel);
int flashTimes = getPreferredFlashCount(notificationOrigin, prefs);
int flashColour = getPreferredFlashColour(notificationOrigin, prefs);
@ -553,20 +556,47 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
@Override
public void onNotification(NotificationSpec notificationSpec) {
// FIXME: these ORIGIN contants do not really make sense anymore
String task;
String origin;
int alertLevel;
switch (notificationSpec.type) {
case SMS:
performPreferredNotification("sms received", ORIGIN_SMS, null);
task = "sms received";
origin = ORIGIN_SMS;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case EMAIL:
performPreferredNotification("email received", ORIGIN_K9MAIL, null);
task = "email received";
origin = ORIGIN_K9MAIL;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case FACEBOOK:
task = "facebook message received";
origin = ORIGIN_GENERIC;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case TWITTER:
task = "twitter message received";
origin = ORIGIN_GENERIC;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case TELEGRAM:
task = "telegram message received";
origin = ORIGIN_GENERIC;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case CHAT:
performPreferredNotification("chat message received", ORIGIN_PEBBLEMSG, null);
task = "chat message received";
origin = ORIGIN_PEBBLEMSG;
alertLevel = MiBand2Service.ALERT_LEVEL_MESSAGE;
break;
case UNDEFINED:
default:
performPreferredNotification("generic notification received", ORIGIN_GENERIC, null);
task = "generic notification received";
origin = ORIGIN_GENERIC;
alertLevel = MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY;
}
performPreferredNotification(task, origin, alertLevel, null);
}
@Override
@ -624,8 +654,20 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
protected boolean shouldAbort() {
return !isTelephoneRinging();
}
@Override
public boolean run(BluetoothGatt gatt) {
if (!super.run(gatt)) {
// send a signal to stop the vibration
BluetoothGattCharacteristic characteristic = MiBand2Support.this.getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL);
characteristic.setValue(new byte[] {MiBand2Service.ALERT_LEVEL_NONE});
gatt.writeCharacteristic(characteristic);
return false;
}
return true;
}
};
performPreferredNotification("incoming call", MiBandConst.ORIGIN_INCOMING_CALL, abortAction);
performPreferredNotification("incoming call", MiBandConst.ORIGIN_INCOMING_CALL, MiBand2Service.ALERT_LEVEL_PHONE_CALL, abortAction);
} else if ((callSpec.command == CallSpec.CALL_START) || (callSpec.command == CallSpec.CALL_END)) {
telephoneRinging = false;
}

View File

@ -15,13 +15,17 @@ public class V2NotificationStrategy implements NotificationStrategy {
this.support = support;
}
protected AbstractBTLEDeviceSupport getSupport() {
return support;
}
@Override
public void sendDefaultNotification(TransactionBuilder builder, BtLEAction extraAction) {
VibrationProfile profile = VibrationProfile.getProfile(VibrationProfile.ID_MEDIUM, (short) 3);
sendCustomNotification(profile, extraAction, builder);
}
private void sendCustomNotification(VibrationProfile vibrationProfile, BtLEAction extraAction, TransactionBuilder builder) {
protected void sendCustomNotification(VibrationProfile vibrationProfile, BtLEAction extraAction, TransactionBuilder builder) {
//use the new alert characteristic
BluetoothGattCharacteristic alert = support.getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL);
for (short i = 0; i < vibrationProfile.getRepeat(); i++) {
@ -31,6 +35,14 @@ public class V2NotificationStrategy implements NotificationStrategy {
on = Math.min(500, on); // longer than 500ms is not possible
builder.write(alert, new byte[]{GattCharacteristic.MILD_ALERT}); //MILD_ALERT lights up GREEN leds, HIGH_ALERT lights up RED leds
builder.wait(on);
builder.write(alert, new byte[]{GattCharacteristic.HIGH_ALERT});
builder.wait(on);
builder.write(alert, new byte[]{0x3});
builder.wait(on);
builder.write(alert, new byte[]{0x4});
builder.wait(on);
builder.write(alert, new byte[]{0x5});
builder.wait(on);
builder.write(alert, new byte[]{GattCharacteristic.NO_ALERT});
if (++j < onOffSequence.length) {

View File

@ -0,0 +1,49 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband2;
import android.bluetooth.BluetoothGattCharacteristic;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.V2NotificationStrategy;
public class Mi2NotificationStrategy extends V2NotificationStrategy {
public Mi2NotificationStrategy(AbstractBTLEDeviceSupport support) {
super(support);
}
@Override
protected void sendCustomNotification(VibrationProfile vibrationProfile, BtLEAction extraAction, TransactionBuilder builder) {
//use the new alert characteristic
BluetoothGattCharacteristic alert = getSupport().getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL);
for (short i = 0; i < vibrationProfile.getRepeat(); i++) {
int[] onOffSequence = vibrationProfile.getOnOffSequence();
for (int j = 0; j < onOffSequence.length; j++) {
int on = onOffSequence[j];
on = Math.min(500, on); // longer than 500ms is not possible
builder.write(alert, new byte[]{(byte) vibrationProfile.getAlertLevel()});
// builder.wait(on);
// builder.write(alert, new byte[]{GattCharacteristic.NO_ALERT});
if (++j < onOffSequence.length) {
int off = Math.max(onOffSequence[j], 25); // wait at least 25ms
builder.wait(off);
}
if (extraAction != null) {
builder.add(extraAction);
}
}
}
}
@Override
public void sendCustomNotification(VibrationProfile vibrationProfile, int flashTimes, int flashColour, int originalColour, long flashDuration, BtLEAction extraAction, TransactionBuilder builder) {
// all other parameters are unfortunately not supported anymore ;-(
sendCustomNotification(vibrationProfile, extraAction, builder);
}
}

View File

@ -42,7 +42,7 @@
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="2"
android:text="send as SMS" />
android:text="SMS" />
<Button
android:id="@+id/sendEmailButton"
@ -51,8 +51,43 @@
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="2"
android:text="send as E-Mail"
android:layout_columnSpan="2" />
android:text="E-Mail"/>
<Button
android:id="@+id/sendChatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="fill_horizontal"
android:layout_row="2"
android:text="Chat"/>
<Button
android:id="@+id/sendTelegramButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:text="Telegram" />
<Button
android:id="@+id/sendTwitterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:text="Twitter"/>
<Button
android:id="@+id/sendFacebookButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:text="Facebook"/>
<EditText
android:id="@+id/editContent"
@ -73,7 +108,7 @@
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:layout_row="10"
android:layout_row="11"
android:text="create test notification" />
<Button
@ -82,7 +117,7 @@
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:layout_row="4"
android:text="incoming call" />
<Button
@ -91,7 +126,7 @@
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:layout_row="4"
android:text="outgoing call"
android:layout_columnSpan="2" />
@ -101,7 +136,7 @@
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="4"
android:layout_row="5"
android:text="start call" />
<Button
@ -110,7 +145,7 @@
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="4"
android:layout_row="5"
android:text="end call"
android:layout_columnSpan="2" />
@ -121,7 +156,7 @@
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:layout_row="5"
android:layout_row="6"
android:text="set time" />
<Button
@ -131,7 +166,7 @@
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_row="5"
android:layout_row="6"
android:text="set music info" />
<Button
android:id="@+id/HearRateButton"
@ -140,7 +175,7 @@
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:layout_row="8"
android:layout_row="9"
android:text="Heart Rate Test" />
<Button
android:id="@+id/rebootButton"
@ -149,7 +184,7 @@
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:layout_row="11"
android:layout_row="12"
android:text="reboot" />
</GridLayout>