This commit is contained in:
rusefi 2020-08-16 00:32:10 -04:00
parent c8cab80b79
commit 5e34753d5f
9 changed files with 44 additions and 29 deletions

View File

@ -31,6 +31,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.util.Linkify;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
@ -41,7 +42,7 @@ import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.rusefi.Callable; import com.rusefi.Callable;
import com.rusefi.app.serial.AndroidSerial; import com.rusefi.app.serial.AndroidSerial;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.dfu.DfuConnection; import com.rusefi.dfu.DfuConnection;
import com.rusefi.dfu.DfuImage; import com.rusefi.dfu.DfuImage;
import com.rusefi.dfu.DfuLogic; import com.rusefi.dfu.DfuLogic;
@ -69,6 +70,7 @@ public class rusEFI extends Activity {
private TextView mStatusView; private TextView mStatusView;
private TextView mResultView; private TextView mResultView;
private EditText authToken; private EditText authToken;
private TextView myClickableUrl;
private UsbManager usbManager; private UsbManager usbManager;
private DfuUpload dfuUpload; private DfuUpload dfuUpload;
@ -102,15 +104,21 @@ public class rusEFI extends Activity {
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
String text = authToken.getText().toString(); String text = authToken.getText().toString();
if (AutoTokenUtil.isToken(text)) { if (AuthTokenUtil.isToken(text)) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this);
SharedPreferences.Editor editor = preferences.edit(); SharedPreferences.Editor editor = preferences.edit();
editor.putString(AutoTokenUtil.AUTH_TOKEN, text); editor.putString(AuthTokenUtil.AUTH_TOKEN, text);
editor.commit(); editor.commit();
myClickableUrl.setVisibility(View.GONE);
} }
} }
}); });
myClickableUrl = (TextView) findViewById(R.id.authStatus);
myClickableUrl.setText(AuthTokenUtil.TOKEN_PROFILE_URL);
Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter); registerReceiver(mUsbReceiver, filter);
@ -119,7 +127,9 @@ public class rusEFI extends Activity {
dfuUpload = new DfuUpload(this); dfuUpload = new DfuUpload(this);
dfuUpload.fileOperation(mResultView); dfuUpload.fileOperation(mResultView);
authToken.setText(getAuthToken()); String authToken = getAuthToken();
this.authToken.setText(authToken);
myClickableUrl.setVisibility(AuthTokenUtil.isToken(authToken) ? View.VISIBLE : View.GONE);
// switchOrProgramDfu(); // switchOrProgramDfu();
@ -128,7 +138,7 @@ public class rusEFI extends Activity {
private String getAuthToken() { private String getAuthToken() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this);
return preferences.getString(AutoTokenUtil.AUTH_TOKEN, AutoTokenUtil.TOKEN_WARNING); return preferences.getString(AuthTokenUtil.AUTH_TOKEN, "");
} }
@Override @Override

View File

@ -2,12 +2,12 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myCoordinatorLayout" android:id="@+id/myCoordinatorLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -34,6 +34,12 @@
android:inputType="text" android:inputType="text"
android:text="Auth Token" /> android:text="Auth Token" />
<TextView
android:id="@+id/authStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button <Button
android:id="@+id/buttonSound" android:id="@+id/buttonSound"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -1,6 +1,6 @@
package com.rusefi.auth; package com.rusefi.auth;
public class AutoTokenUtil { public class AuthTokenUtil {
public static final int TOKEN_LENGTH = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12; public static final int TOKEN_LENGTH = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12;
public static final String TOKEN_PROFILE_URL = "https://rusefi.com/forum/ucp.php?i=254"; public static final String TOKEN_PROFILE_URL = "https://rusefi.com/forum/ucp.php?i=254";

View File

@ -1,6 +1,6 @@
package com.rusefi.ui.test; package com.rusefi.ui.test;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -9,11 +9,11 @@ import static org.junit.Assert.assertTrue;
public class AuthTokenUtilTest { public class AuthTokenUtilTest {
@Test @Test
public void test() { public void test() {
assertFalse(AutoTokenUtil.isToken(null)); assertFalse(AuthTokenUtil.isToken(null));
assertTrue(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 ")); assertTrue(AuthTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
assertFalse(AutoTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 ")); assertFalse(AuthTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));
assertFalse(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-11112222333 ")); assertFalse(AuthTokenUtil.isToken(" 11112222-5719-4444-84d4-11112222333 "));
} }
} }

View File

@ -1,6 +1,6 @@
package com.rusefi.ui; package com.rusefi.ui;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.ui.storage.PersistentConfiguration; import com.rusefi.ui.storage.PersistentConfiguration;
import com.rusefi.ui.util.URLLabel; import com.rusefi.ui.util.URLLabel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -56,7 +56,7 @@ public class AuthTokenPanel {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor); String data = (String) clipboard.getData(DataFlavor.stringFlavor);
if (AutoTokenUtil.isToken(data)) { if (AuthTokenUtil.isToken(data)) {
authTokenTestField.setText(data); authTokenTestField.setText(data);
} }
} catch (IOException | UnsupportedFlavorException ex) { } catch (IOException | UnsupportedFlavorException ex) {
@ -85,25 +85,25 @@ public class AuthTokenPanel {
*/ */
content.add(top); content.add(top);
if (authToken.trim().isEmpty()) { if (authToken.trim().isEmpty()) {
authToken = AutoTokenUtil.TOKEN_WARNING; authToken = AuthTokenUtil.TOKEN_WARNING;
} }
content.add(new URLLabel("Manage authentication token at your forum profile", AutoTokenUtil.TOKEN_PROFILE_URL), BorderLayout.SOUTH); content.add(new URLLabel("Manage authentication token at your forum profile", AuthTokenUtil.TOKEN_PROFILE_URL), BorderLayout.SOUTH);
authTokenTestField.setText(authToken); authTokenTestField.setText(authToken);
} }
public static void setAuthToken(String value) { public static void setAuthToken(String value) {
getConfig().getRoot().setProperty(AutoTokenUtil.AUTH_TOKEN, value); getConfig().getRoot().setProperty(AuthTokenUtil.AUTH_TOKEN, value);
} }
@NotNull @NotNull
public static String getAuthToken() { public static String getAuthToken() {
return getConfig().getRoot().getProperty(AutoTokenUtil.AUTH_TOKEN); return getConfig().getRoot().getProperty(AuthTokenUtil.AUTH_TOKEN);
} }
private void setPasteButtonEnabledBasedOnClipboardContent(Clipboard clipboard, JButton paste) { private void setPasteButtonEnabledBasedOnClipboardContent(Clipboard clipboard, JButton paste) {
try { try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor); String data = (String) clipboard.getData(DataFlavor.stringFlavor);
paste.setEnabled(AutoTokenUtil.isToken(data)); paste.setEnabled(AuthTokenUtil.isToken(data));
} catch (IOException | IllegalStateException | UnsupportedFlavorException ex) { } catch (IOException | IllegalStateException | UnsupportedFlavorException ex) {
// ignoring this exception // ignoring this exception
} }
@ -115,7 +115,7 @@ public class AuthTokenPanel {
} }
private void onTextChange() { private void onTextChange() {
if (AutoTokenUtil.isToken(authTokenTestField.getText())) { if (AuthTokenUtil.isToken(authTokenTestField.getText())) {
grabText(); grabText();
} }
} }
@ -125,7 +125,7 @@ public class AuthTokenPanel {
} }
public static boolean hasToken() { public static boolean hasToken() {
return AutoTokenUtil.isToken(getAuthToken()); return AuthTokenUtil.isToken(getAuthToken());
} }
public String getToken() { public String getToken() {

View File

@ -1,7 +1,7 @@
package com.rusefi.tools; package com.rusefi.tools;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.autodetect.PortDetector; import com.rusefi.autodetect.PortDetector;
import com.rusefi.proxy.NetworkConnector; import com.rusefi.proxy.NetworkConnector;
import com.rusefi.proxy.NetworkConnectorContext; import com.rusefi.proxy.NetworkConnectorContext;
@ -11,7 +11,7 @@ public class NetworkConnectorStartup {
private final static Logging log = Logging.getLogging(NetworkConnectorStartup.class); private final static Logging log = Logging.getLogging(NetworkConnectorStartup.class);
public static void start() { public static void start() {
String authToken = AuthTokenPanel.getAuthToken(); String authToken = AuthTokenPanel.getAuthToken();
if (!AutoTokenUtil.isToken(authToken)) { if (!AuthTokenUtil.isToken(authToken)) {
System.err.println("Please configure authentication token using 'set_auth_token' command"); System.err.println("Please configure authentication token using 'set_auth_token' command");
return; return;
} }

View File

@ -1,7 +1,7 @@
package com.rusefi.server; package com.rusefi.server;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.core.SensorsHolder; import com.rusefi.core.SensorsHolder;
@ -93,7 +93,7 @@ public class ControllerConnectionState {
if (jsonString == null) if (jsonString == null)
return; return;
sessionDetails = SessionDetails.valueOf(jsonString); sessionDetails = SessionDetails.valueOf(jsonString);
if (!AutoTokenUtil.isToken(sessionDetails.getAuthToken())) if (!AuthTokenUtil.isToken(sessionDetails.getAuthToken()))
throw new IOException("Invalid token in " + jsonString); throw new IOException("Invalid token in " + jsonString);
log.info(sessionDetails.getAuthToken() + " New client: " + sessionDetails.getControllerInfo()); log.info(sessionDetails.getAuthToken() + " New client: " + sessionDetails.getControllerInfo());

View File

@ -1,6 +1,6 @@
package com.rusefi.ts_plugin; package com.rusefi.ts_plugin;
import com.rusefi.auth.AutoTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.autodetect.PortDetector; import com.rusefi.autodetect.PortDetector;
import com.rusefi.autoupdate.AutoupdateUtil; import com.rusefi.autoupdate.AutoupdateUtil;
import com.rusefi.proxy.NetworkConnector; import com.rusefi.proxy.NetworkConnector;
@ -33,7 +33,7 @@ public class BroadcastTab {
broadcast.addActionListener(e -> { broadcast.addActionListener(e -> {
String authToken = AuthTokenPanel.getAuthToken(); String authToken = AuthTokenPanel.getAuthToken();
if (!AutoTokenUtil.isToken(authToken)) { if (!AuthTokenUtil.isToken(authToken)) {
status.setText("Auth token is required to broadcast ECU"); status.setText("Auth token is required to broadcast ECU");
return; return;
} }

View File

@ -3,7 +3,6 @@ package com.rusefi.ts_plugin;
import com.rusefi.NamedThreadFactory; import com.rusefi.NamedThreadFactory;
import com.rusefi.SignatureHelper; import com.rusefi.SignatureHelper;
import com.rusefi.Timeouts; import com.rusefi.Timeouts;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.autoupdate.AutoupdateUtil; import com.rusefi.autoupdate.AutoupdateUtil;
import com.rusefi.io.serial.StreamStatistics; import com.rusefi.io.serial.StreamStatistics;
import com.rusefi.io.tcp.ServerSocketReference; import com.rusefi.io.tcp.ServerSocketReference;