From 88b3ef23fc477cdf947028a9a280ec7d3a99d2a9 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 16 Aug 2020 22:10:05 -0400 Subject: [PATCH] Android progress --- android/app/src/main/AndroidManifest.xml | 3 ++ .../java/com/rusefi/app/SerialService.java | 26 ++++++++++++ .../src/main/java/com/rusefi/app/rusEFI.java | 41 +++++++++++++++---- .../app/src/main/res/layout/activity_usb.xml | 6 +++ android/readme.md | 15 +++++++ .../com/rusefi/proxy/NetworkConnector.java | 18 ++++++-- 6 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 android/app/src/main/java/com/rusefi/app/SerialService.java create mode 100644 android/readme.md diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index f7af9838a9..4bc5dfd319 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -30,8 +30,11 @@ android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> + + + diff --git a/android/app/src/main/java/com/rusefi/app/SerialService.java b/android/app/src/main/java/com/rusefi/app/SerialService.java new file mode 100644 index 0000000000..ac30346146 --- /dev/null +++ b/android/app/src/main/java/com/rusefi/app/SerialService.java @@ -0,0 +1,26 @@ +package com.rusefi.app; + +import android.app.Service; +import android.content.Intent; +import android.os.Binder; +import android.os.IBinder; + +import androidx.annotation.Nullable; + +public class SerialService extends Service { + class SerialBinder extends Binder { +/* + SerialService getService() { + return SerialService.this; + } +*/ + } + + private final IBinder binder = new SerialBinder(); + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return binder; + } +} diff --git a/android/app/src/main/java/com/rusefi/app/rusEFI.java b/android/app/src/main/java/com/rusefi/app/rusEFI.java index ee2138f7b4..1a6a11f009 100644 --- a/android/app/src/main/java/com/rusefi/app/rusEFI.java +++ b/android/app/src/main/java/com/rusefi/app/rusEFI.java @@ -42,6 +42,7 @@ import com.devexperts.logging.Logging; import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.Snackbar; import com.rusefi.Callable; +import com.rusefi.Timeouts; import com.rusefi.app.serial.AndroidSerial; import com.rusefi.auth.AuthTokenUtil; import com.rusefi.dfu.DfuConnection; @@ -57,6 +58,8 @@ import com.rusefi.io.serial.StreamConnector; import com.rusefi.proxy.NetworkConnector; import com.rusefi.proxy.NetworkConnectorContext; +import java.util.Date; + public class rusEFI extends Activity { private final static Logging log = Logging.getLogging(rusEFI.class); private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; @@ -66,11 +69,12 @@ public class rusEFI extends Activity { // // protected static final int DFU_DETACH_TIMEOUT = 1000; - private static final String VERSION = "rusEFI app v0.0000007\n"; + private static final String VERSION = "rusEFI app v0.0000008\n"; /* UI elements */ private TextView mStatusView; private TextView mResultView; + private TextView broadcastStatus; private EditText authToken; private TextView authStatusMessage; private TextView authStatusClickableUrl; @@ -87,9 +91,16 @@ public class rusEFI extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_usb); + /** + * We need to make sure that WiFi is available for us, this might be related to screen on? + */ + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + findViewById(R.id.buttonSound).setVisibility(View.GONE); findViewById(R.id.buttonDfu).setVisibility(View.GONE); + broadcastStatus = findViewById(R.id.broadcastStatus); + usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); // turn on scree while ADB debugging idle phone @@ -244,6 +255,8 @@ public class rusEFI extends Activity { } else if (view.getId() == R.id.buttonSound) { soundBroadcast.start(); } else if (view.getId() == R.id.buttonBroadcast) { + startService(new Intent(this, SerialService.class)); + AndroidSerial serial = AndroidSerial.getAndroidSerial(mStatusView, mResultView, usbManager); if (serial == null) { // error already reported to mStatusView @@ -262,18 +275,30 @@ public class rusEFI extends Activity { linkManager.getConnector().connectAndReadConfiguration(new ConnectionStateListener() { @Override public void onConnectionEstablished() { - mResultView.post(() -> mResultView.append("On connection established\n")); + mResultView.post(() -> mResultView.append(new Date() + " On connection established\n")); NetworkConnectorContext context = new NetworkConnectorContext(); + NetworkConnector.ActivityListener oncePerSecondStatistics = new NetworkConnector.ActivityListener() { + long previousTime; + + @Override + public void onActivity(IoStream targetEcuSocket) { + long now = System.currentTimeMillis(); + if (now - previousTime < Timeouts.SECOND) { + return; + } + previousTime = now; + broadcastStatus.post(() -> broadcastStatus.setText(targetEcuSocket.getBytesIn() + "/" + targetEcuSocket.getBytesOut())); + } + }; NetworkConnector.NetworkConnectorResult result = new NetworkConnector().start(NetworkConnector.Implementation.Android, getAuthToken(), context, new NetworkConnector.ReconnectListener() { - @Override - public void onReconnect() { + @Override + public void onReconnect() { + } + }, linkManager, oncePerSecondStatistics); - } - }, linkManager); - - mResultView.post(() -> mResultView.append("Broadcast: " + result + "\n")); + mResultView.post(() -> mResultView.append(new Date() + " Broadcast: " + result + "\n")); } diff --git a/android/app/src/main/res/layout/activity_usb.xml b/android/app/src/main/res/layout/activity_usb.xml index a86674b034..2b634801d3 100644 --- a/android/app/src/main/res/layout/activity_usb.xml +++ b/android/app/src/main/res/layout/activity_usb.xml @@ -60,6 +60,12 @@ android:onClick="sendMessage" android:text="Broadcast" /> + + diff --git a/android/readme.md b/android/readme.md new file mode 100644 index 0000000000..3650f0bc91 --- /dev/null +++ b/android/readme.md @@ -0,0 +1,15 @@ + + +# Implementation Details + +USB serial +https://github.com/mik3y/usb-serial-for-android + +DFU implementation +https://github.com/rusefi/dfu_java + +Error reporting uses +https://github.com/ACRA/acra + +Hex reader based on +https://github.com/encedo/hex2dfu \ No newline at end of file diff --git a/java_console/io/src/main/java/com/rusefi/proxy/NetworkConnector.java b/java_console/io/src/main/java/com/rusefi/proxy/NetworkConnector.java index 239e913645..de78f3cfea 100644 --- a/java_console/io/src/main/java/com/rusefi/proxy/NetworkConnector.java +++ b/java_console/io/src/main/java/com/rusefi/proxy/NetworkConnector.java @@ -67,10 +67,10 @@ public class NetworkConnector implements Closeable { return NetworkConnectorResult.ERROR; } - return start(implementation, authToken, context, reconnectListener, controllerConnector); + return start(implementation, authToken, context, reconnectListener, controllerConnector, ActivityListener.VOID); } - public NetworkConnectorResult start(Implementation implementation, String authToken, NetworkConnectorContext context, ReconnectListener reconnectListener, LinkManager linkManager) { + public NetworkConnectorResult start(Implementation implementation, String authToken, NetworkConnectorContext context, ReconnectListener reconnectListener, LinkManager linkManager, ActivityListener activityListener) { ControllerInfo controllerInfo; try { controllerInfo = getControllerInfo(linkManager, linkManager.getConnector().getBinaryProtocol().getStream()); @@ -88,6 +88,7 @@ public class NetworkConnector implements Closeable { try { start(implementation, + activityListener, context.serverPortForControllers(), linkManager, authToken, (String message) -> { log.error(message + " Disconnect from proxy server detected, now sleeping " + context.reconnectDelay() + " seconds"); sleep(context.reconnectDelay() * Timeouts.SECOND); @@ -108,7 +109,7 @@ public class NetworkConnector implements Closeable { } @NotNull - private static SessionDetails start(Implementation implementation, int serverPortForControllers, LinkManager linkManager, String authToken, final TcpIoStream.DisconnectListener disconnectListener, int oneTimeToken, ControllerInfo controllerInfo, final NetworkConnectorContext context) throws IOException { + private static SessionDetails start(Implementation implementation, ActivityListener activityListener, int serverPortForControllers, LinkManager linkManager, String authToken, final TcpIoStream.DisconnectListener disconnectListener, int oneTimeToken, ControllerInfo controllerInfo, final NetworkConnectorContext context) throws IOException { IoStream targetEcuSocket = linkManager.getConnector().getBinaryProtocol().getStream(); SessionDetails deviceSessionDetails = new SessionDetails(implementation, controllerInfo, authToken, oneTimeToken, rusEFIVersion.CONSOLE_VERSION); @@ -146,6 +147,7 @@ public class NetworkConnector implements Closeable { BinaryProtocolServer.Packet response = targetEcuSocket.readPacket(); log.info("Relaying response to proxy size=" + response.getPacket().length); stream.sendPacket(response); + activityListener.onActivity(targetEcuSocket); } }; baseBroadcastingThread.start(); @@ -209,6 +211,16 @@ public class NetworkConnector implements Closeable { void onReconnect(); } + public interface ActivityListener { + ActivityListener VOID = new ActivityListener() { + @Override + public void onActivity(IoStream targetEcuSocket) { + + } + }; + void onActivity(IoStream targetEcuSocket); + } + public enum Implementation { Android, Plugin,