From b957c76d714b2a30aa34641397769b4c70af776e Mon Sep 17 00:00:00 2001 From: vanous Date: Sun, 2 Oct 2022 16:46:13 +0200 Subject: [PATCH] SuperCars: add blinking function and live battery status, fine-tune driving, race! --- .../devices/supercars/ControlActivity.java | 107 ++++++++++++++++-- .../devices/supercars/SuperCarsSupport.java | 13 ++- .../res/layout/activity_supercars_control.xml | 28 ++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 129 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java index 4a7f7fd56..6676aab57 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java @@ -1,10 +1,14 @@ package nodomain.freeyourgadget.gadgetbridge.devices.supercars; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.CheckBox; import android.widget.CompoundButton; +import android.widget.TextView; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -13,15 +17,20 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.service.devices.supercars.SuperCarsSupport; public class ControlActivity extends AbstractGBActivity implements JoystickView.JoystickListener { private static final Logger LOG = LoggerFactory.getLogger(ControlActivity.class); LocalBroadcastManager localBroadcastManager; - boolean lights = false; - boolean turbo = false; CountDownTimer periodicDataSenderRunner; + private GBDevice device; + TextView batteryPercentage; + boolean lights = false; + boolean blinking = false; + boolean turbo = false; + SuperCarsConstants.Direction direction = SuperCarsConstants.Direction.CENTER; SuperCarsConstants.Movement movement = SuperCarsConstants.Movement.IDLE; SuperCarsConstants.Speed speed = SuperCarsConstants.Speed.NORMAL; @@ -31,9 +40,26 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_supercars_control); - localBroadcastManager = LocalBroadcastManager.getInstance(this); + + Intent intent = getIntent(); + Bundle bundle = intent.getExtras(); + if (bundle != null) { + device = bundle.getParcelable(GBDevice.EXTRA_DEVICE); + } else { + throw new IllegalArgumentException("Must provide a device when invoking this activity"); + } + CheckBox turboMode = findViewById(R.id.turboMode); CheckBox lightsOn = findViewById(R.id.lightsOn); + CheckBox lightsBlinking = findViewById(R.id.lightsBlinking); + + batteryPercentage = findViewById(R.id.battery_percentage_label); + setBatteryLabel(); + + localBroadcastManager = LocalBroadcastManager.getInstance(ControlActivity.this); + IntentFilter filter = new IntentFilter(); + filter.addAction(GBDevice.ACTION_DEVICE_CHANGED); + localBroadcastManager.registerReceiver(commandReceiver, filter); turboMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @@ -45,23 +71,69 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. lightsOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + if (lightsBlinking.isChecked()) { + lightsBlinking.setChecked(false); + blinking = false; + } + } lights = isChecked; + setLights(); } }); - //when this activity is open, data is sent continuously every 200ms + lightsBlinking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + + if (isChecked) { + if (lightsOn.isChecked()) { + lightsOn.setChecked(false); + lights = false; + } + } + blinking = isChecked; + setLights(); + } + }); + + //when this activity is open, data is sent continuously every 100ms periodicDataSender(); periodicDataSenderRunner.start(); } + private void setLights() { + if (!blinking) { + if (lights) { + light = SuperCarsConstants.Light.ON; + } else { + light = SuperCarsConstants.Light.OFF; + } + } + } + + private void setBatteryLabel() { + String level = device.getBatteryLevel() > 0 ? String.format("%1s%%", device.getBatteryLevel()) : device.getName(); + batteryPercentage.setText(level); + } + private void sendLocalBroadcast(Intent intent) { localBroadcastManager.sendBroadcast(intent); } public void periodicDataSender() { - periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 200) { + periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 100) { public void onTick(long millisUntilFinished) { + + if (blinking) { + if (light.equals(SuperCarsConstants.Light.ON)) { + light = SuperCarsConstants.Light.OFF; + } else { + light = SuperCarsConstants.Light.ON; + } + } + create_intent_with_data(); } @@ -83,9 +155,9 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. @Override public void onJoystickMoved(float xPercent, float yPercent, int id) { - if (yPercent < 0) { + if (yPercent < 0.2 && yPercent != 0) { movement = SuperCarsConstants.Movement.UP; - } else if (yPercent > 0) { + } else if (yPercent > 0.2) { movement = SuperCarsConstants.Movement.DOWN; } else { movement = SuperCarsConstants.Movement.IDLE; @@ -99,11 +171,6 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. direction = SuperCarsConstants.Direction.CENTER; } - if (lights) { - light = SuperCarsConstants.Light.ON; - } else { - light = SuperCarsConstants.Light.OFF; - } if (turbo) { speed = SuperCarsConstants.Speed.TURBO; } else { @@ -115,6 +182,7 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. protected void onDestroy() { super.onDestroy(); periodicDataSenderRunner.cancel(); + LocalBroadcastManager.getInstance(this).unregisterReceiver(commandReceiver); } @Override @@ -128,4 +196,19 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. super.onPause(); periodicDataSenderRunner.cancel(); } + + BroadcastReceiver commandReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + LOG.debug("device receiver received " + intent.getAction()); + if (intent.getAction().equals(GBDevice.ACTION_DEVICE_CHANGED)) { + GBDevice newDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); + if (newDevice.equals(device)) { + device = newDevice; + setBatteryLabel(); + } + + } + } + }; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java index 7dc5ccc3f..c50ff7c35 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java @@ -67,7 +67,7 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(COMMAND_DRIVE_CONTROL)) { - send_data( + queueDataToBLE( (SuperCarsConstants.Speed) intent.getSerializableExtra(EXTRA_SPEED), (SuperCarsConstants.Movement) intent.getSerializableExtra(EXTRA_MOVEMENT), (SuperCarsConstants.Light) intent.getSerializableExtra(EXTRA_LIGHT), @@ -112,7 +112,8 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { if (decoded_data.length == 16) { GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); batteryEvent.state = BatteryState.BATTERY_NORMAL; - batteryEvent.level = decoded_data[4]; + int level = decoded_data[4]; + batteryEvent.level = level; evaluateGBDeviceEvent(batteryEvent); } } @@ -294,10 +295,10 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { return false; } - private void send_data(SuperCarsConstants.Speed speed, - SuperCarsConstants.Movement movement, - SuperCarsConstants.Light light, - SuperCarsConstants.Direction direction) { + private void queueDataToBLE(SuperCarsConstants.Speed speed, + SuperCarsConstants.Movement movement, + SuperCarsConstants.Light light, + SuperCarsConstants.Direction direction) { byte[] command = craft_packet(speed, direction, movement, light); TransactionBuilder builder = new TransactionBuilder("send data"); diff --git a/app/src/main/res/layout/activity_supercars_control.xml b/app/src/main/res/layout/activity_supercars_control.xml index 71cac1801..a948fd0ed 100644 --- a/app/src/main/res/layout/activity_supercars_control.xml +++ b/app/src/main/res/layout/activity_supercars_control.xml @@ -10,6 +10,17 @@ android:orientation="vertical" tools:ignore="MissingConstraints"> + + + android:height="48dp" + android:text="@string/supercars_turbo_speed_label" + tools:ignore="TouchTargetSizeCheck" /> + android:height="48dp" + android:text="@string/supercars_lights_label" + tools:ignore="TouchTargetSizeCheck" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5134e5c76..7a1233a92 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1854,4 +1854,5 @@ Shell Racing Turbo Speed Lights + Blinking